-
Notifications
You must be signed in to change notification settings - Fork 18
/
amqp_api.c
364 lines (326 loc) · 10.7 KB
/
amqp_api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <config.h> /* Perl's CORE config */
#ifndef USE_ITHREADS
#define DISABLE_THREADS
#endif
#ifndef DISABLE_THREADS
#include <pthread.h>
#endif
#include "amqp.h"
#include "amqp_framing.h"
#include "amqp_private.h"
#include <assert.h>
#define RPC_REPLY(replytype) \
(amqp_rpc_reply->reply_type == AMQP_RESPONSE_NORMAL \
? (replytype *) amqp_rpc_reply->reply.decoded \
: NULL)
amqp_channel_open_ok_t *amqp_channel_open(amqp_connection_state_t state,
amqp_channel_t channel)
{
amqp_rpc_reply_t *amqp_rpc_reply;
amqp_rpc_reply = amqp_get_rpc_reply();
*amqp_rpc_reply =
AMQP_SIMPLE_RPC(state, channel, CHANNEL, OPEN, OPEN_OK,
amqp_channel_open_t,
AMQP_EMPTY_BYTES);
return RPC_REPLY(amqp_channel_open_ok_t);
}
int amqp_basic_publish(amqp_connection_state_t state,
amqp_channel_t channel,
amqp_bytes_t exchange,
amqp_bytes_t routing_key,
amqp_boolean_t mandatory,
amqp_boolean_t immediate,
amqp_basic_properties_t const *properties,
amqp_bytes_t body)
{
amqp_frame_t f;
size_t body_offset;
size_t usable_body_payload_size = state->frame_max - (HEADER_SIZE + FOOTER_SIZE);
amqp_basic_publish_t m =
(amqp_basic_publish_t) {
.exchange = exchange,
.routing_key = routing_key,
.mandatory = mandatory,
.immediate = immediate
};
amqp_basic_properties_t default_properties;
AMQP_CHECK_RESULT(amqp_send_method(state, channel, AMQP_BASIC_PUBLISH_METHOD, &m));
if (properties == NULL) {
memset(&default_properties, 0, sizeof(default_properties));
properties = &default_properties;
}
f.frame_type = AMQP_FRAME_HEADER;
f.channel = channel;
f.payload.properties.class_id = AMQP_BASIC_CLASS;
f.payload.properties.body_size = body.len;
f.payload.properties.decoded = (void *) properties;
AMQP_CHECK_RESULT(amqp_send_frame(state, &f));
body_offset = 0;
while (1) {
int remaining = body.len - body_offset;
assert(remaining >= 0);
if (remaining == 0)
break;
f.frame_type = AMQP_FRAME_BODY;
f.channel = channel;
f.payload.body_fragment.bytes = BUF_AT(body, body_offset);
if (remaining >= usable_body_payload_size) {
f.payload.body_fragment.len = usable_body_payload_size;
} else {
f.payload.body_fragment.len = remaining;
}
body_offset += f.payload.body_fragment.len;
AMQP_CHECK_RESULT(amqp_send_frame(state, &f));
}
return 0;
}
amqp_rpc_reply_t amqp_channel_close(amqp_connection_state_t state,
amqp_channel_t channel,
int code)
{
char codestr[13];
snprintf(codestr, sizeof(codestr), "%d", code);
return AMQP_SIMPLE_RPC(state, channel, CHANNEL, CLOSE, CLOSE_OK,
amqp_channel_close_t,
code, amqp_cstring_bytes(codestr), 0, 0);
}
amqp_rpc_reply_t amqp_connection_close(amqp_connection_state_t state,
int code)
{
char codestr[13];
snprintf(codestr, sizeof(codestr), "%d", code);
return AMQP_SIMPLE_RPC(state, 0, CONNECTION, CLOSE, CLOSE_OK,
amqp_connection_close_t,
code, amqp_cstring_bytes(codestr), 0, 0);
}
amqp_exchange_declare_ok_t *amqp_exchange_declare(amqp_connection_state_t state,
amqp_channel_t channel,
amqp_bytes_t exchange,
amqp_bytes_t type,
amqp_boolean_t passive,
amqp_boolean_t durable,
amqp_boolean_t auto_delete,
amqp_table_t arguments)
{
amqp_rpc_reply_t *amqp_rpc_reply;
amqp_rpc_reply = amqp_get_rpc_reply();
*amqp_rpc_reply =
AMQP_SIMPLE_RPC(state, channel, EXCHANGE, DECLARE, DECLARE_OK,
amqp_exchange_declare_t,
0, exchange, type, passive, durable, auto_delete, 0, 0, arguments);
return RPC_REPLY(amqp_exchange_declare_ok_t);
}
amqp_exchange_delete_ok_t *amqp_exchange_delete(amqp_connection_state_t state,
amqp_channel_t channel,
amqp_bytes_t exchange,
amqp_boolean_t if_unused,
amqp_boolean_t nowait)
{
amqp_rpc_reply_t *amqp_rpc_reply;
amqp_rpc_reply = amqp_get_rpc_reply();
*amqp_rpc_reply =
AMQP_SIMPLE_RPC(state, channel, EXCHANGE, DELETE, DELETE_OK,
amqp_exchange_delete_t,
0, exchange, if_unused, nowait);
return RPC_REPLY(amqp_exchange_delete_ok_t);
}
amqp_queue_declare_ok_t *amqp_queue_declare(amqp_connection_state_t state,
amqp_channel_t channel,
amqp_bytes_t queue,
amqp_boolean_t passive,
amqp_boolean_t durable,
amqp_boolean_t exclusive,
amqp_boolean_t auto_delete,
amqp_table_t arguments)
{
amqp_rpc_reply_t *amqp_rpc_reply;
amqp_rpc_reply = amqp_get_rpc_reply();
*amqp_rpc_reply =
AMQP_SIMPLE_RPC(state, channel, QUEUE, DECLARE, DECLARE_OK,
amqp_queue_declare_t,
0, queue, passive, durable, exclusive, auto_delete, 0, arguments);
return RPC_REPLY(amqp_queue_declare_ok_t);
}
amqp_queue_bind_ok_t *amqp_queue_bind(amqp_connection_state_t state,
amqp_channel_t channel,
amqp_bytes_t queue,
amqp_bytes_t exchange,
amqp_bytes_t routing_key,
amqp_table_t arguments)
{
amqp_rpc_reply_t *amqp_rpc_reply;
amqp_rpc_reply = amqp_get_rpc_reply();
*amqp_rpc_reply =
AMQP_SIMPLE_RPC(state, channel, QUEUE, BIND, BIND_OK,
amqp_queue_bind_t,
0, queue, exchange, routing_key, 0, arguments);
return RPC_REPLY(amqp_queue_bind_ok_t);
}
amqp_queue_unbind_ok_t *amqp_queue_unbind(amqp_connection_state_t state,
amqp_channel_t channel,
amqp_bytes_t queue,
amqp_bytes_t exchange,
amqp_bytes_t binding_key,
amqp_table_t arguments)
{
amqp_rpc_reply_t *amqp_rpc_reply;
amqp_rpc_reply = amqp_get_rpc_reply();
*amqp_rpc_reply =
AMQP_SIMPLE_RPC(state, channel, QUEUE, UNBIND, UNBIND_OK,
amqp_queue_unbind_t,
0, queue, exchange, binding_key, arguments);
return RPC_REPLY(amqp_queue_unbind_ok_t);
}
amqp_basic_consume_ok_t *amqp_basic_consume(amqp_connection_state_t state,
amqp_channel_t channel,
amqp_bytes_t queue,
amqp_bytes_t consumer_tag,
amqp_boolean_t no_local,
amqp_boolean_t no_ack,
amqp_boolean_t exclusive)
{
amqp_rpc_reply_t *amqp_rpc_reply;
amqp_rpc_reply = amqp_get_rpc_reply();
*amqp_rpc_reply =
AMQP_SIMPLE_RPC(state, channel, BASIC, CONSUME, CONSUME_OK,
amqp_basic_consume_t,
0, queue, consumer_tag, no_local, no_ack, exclusive, 0);
return RPC_REPLY(amqp_basic_consume_ok_t);
}
int amqp_basic_ack(amqp_connection_state_t state,
amqp_channel_t channel,
uint64_t delivery_tag,
amqp_boolean_t multiple)
{
amqp_basic_ack_t m =
(amqp_basic_ack_t) {
.delivery_tag = delivery_tag,
.multiple = multiple
};
AMQP_CHECK_RESULT(amqp_send_method(state, channel, AMQP_BASIC_ACK_METHOD, &m));
return 0;
}
int amqp_basic_reject(amqp_connection_state_t state,
amqp_channel_t channel,
uint64_t delivery_tag,
amqp_boolean_t requeue)
{
amqp_basic_reject_t m =
(amqp_basic_reject_t) {
.delivery_tag = delivery_tag,
.requeue = requeue
};
AMQP_CHECK_RESULT(amqp_send_method(state, channel, AMQP_BASIC_REJECT_METHOD, &m));
return 0;
}
amqp_queue_purge_ok_t *amqp_queue_purge(amqp_connection_state_t state,
amqp_channel_t channel,
amqp_bytes_t queue,
amqp_boolean_t no_wait)
{
amqp_rpc_reply_t *amqp_rpc_reply;
amqp_rpc_reply = amqp_get_rpc_reply();
*amqp_rpc_reply = AMQP_SIMPLE_RPC(state, channel, QUEUE, PURGE, PURGE_OK,
amqp_queue_purge_t, channel, queue, no_wait);
return RPC_REPLY(amqp_queue_purge_ok_t);
}
amqp_rpc_reply_t amqp_basic_get(amqp_connection_state_t state,
amqp_channel_t channel,
amqp_bytes_t queue,
amqp_boolean_t no_ack)
{
amqp_method_number_t replies[] = { AMQP_BASIC_GET_OK_METHOD,
AMQP_BASIC_GET_EMPTY_METHOD,
0 };
amqp_rpc_reply_t *amqp_rpc_reply;
amqp_rpc_reply = amqp_get_rpc_reply();
*amqp_rpc_reply =
AMQP_MULTIPLE_RESPONSE_RPC(state, channel, BASIC, GET, replies,
amqp_basic_get_t,
channel, queue, no_ack);
return *amqp_rpc_reply;
}
/*
* Expose amqp_rpc_reply to dynamically linked libraries
*/
amqp_rpc_reply_t *amqp_get_rpc_reply(void)
{
#ifndef DISABLE_THREADS
static int initialized = 0;
static pthread_key_t reply_key;
static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
amqp_rpc_reply_t *amqp_rpc_reply;
if(!initialized) {
pthread_mutex_lock(&init_mutex);
if(!initialized) {
pthread_key_create(&reply_key, free);
initialized = 1;
}
pthread_mutex_unlock(&init_mutex);
}
amqp_rpc_reply = (amqp_rpc_reply_t *)pthread_getspecific(reply_key);
if(!amqp_rpc_reply) {
amqp_rpc_reply = calloc(1, sizeof(*amqp_rpc_reply));
pthread_setspecific(reply_key, (void *)amqp_rpc_reply);
}
return amqp_rpc_reply;
#else
static amqp_rpc_reply_t amqp_rpc_reply;
return &amqp_rpc_reply;
#endif
}
/*
* Expose tx-style transactions
*/
amqp_tx_select_ok_t *amqp_tx_select(amqp_connection_state_t state,
amqp_channel_t channel,
amqp_table_t arguments)
{
amqp_rpc_reply_t *amqp_rpc_reply;
amqp_rpc_reply = amqp_get_rpc_reply();
*amqp_rpc_reply =
AMQP_SIMPLE_RPC(state, channel, TX, SELECT, SELECT_OK,
amqp_tx_select_t,
channel);
return RPC_REPLY(amqp_tx_select_ok_t);
}
amqp_tx_commit_ok_t *amqp_tx_commit(amqp_connection_state_t state,
amqp_channel_t channel,
amqp_table_t arguments)
{
amqp_rpc_reply_t *amqp_rpc_reply;
amqp_rpc_reply = amqp_get_rpc_reply();
*amqp_rpc_reply =
AMQP_SIMPLE_RPC(state, channel, TX, COMMIT, COMMIT_OK,
amqp_tx_commit_t, channel);
return RPC_REPLY(amqp_tx_commit_ok_t);
}
amqp_tx_rollback_ok_t *amqp_tx_rollback(amqp_connection_state_t state,
amqp_channel_t channel,
amqp_table_t arguments)
{
amqp_rpc_reply_t *amqp_rpc_reply;
amqp_rpc_reply = amqp_get_rpc_reply();
*amqp_rpc_reply =
AMQP_SIMPLE_RPC(state, channel, TX, ROLLBACK, ROLLBACK_OK,
amqp_tx_rollback_t, channel);
return RPC_REPLY(amqp_tx_rollback_ok_t);
}
amqp_basic_qos_ok_t *amqp_basic_qos(amqp_connection_state_t state,
amqp_channel_t channel,
uint32_t prefetch_size,
uint16_t prefetch_count,
amqp_boolean_t global)
{
amqp_rpc_reply_t *amqp_rpc_reply;
amqp_rpc_reply = amqp_get_rpc_reply();
*amqp_rpc_reply =
AMQP_SIMPLE_RPC(state, channel, BASIC, QOS, QOS_OK,
amqp_basic_qos_t, prefetch_size, prefetch_count, global);
return RPC_REPLY(amqp_basic_qos_ok_t);
}