amqp_api.c

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