amqp_connection.c

filter: Statementstmt 58.2% 152 / 261 Branchbran 48.7% 40 / 82 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 55.9% 192 / 343 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« amqp_socket.c ↑ Summary amqp_mem.c »
1 /* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */
2 /*
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MIT
5 *
6 * Portions created by Alan Antonuk are Copyright (c) 2012-2014
7 * Alan Antonuk. All Rights Reserved.
8 *
9 * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
10 * All Rights Reserved.
11 *
12 * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
13 * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
14 *
15 * Permission is hereby granted, free of charge, to any person
16 * obtaining a copy of this software and associated documentation
17 * files (the "Software"), to deal in the Software without
18 * restriction, including without limitation the rights to use, copy,
19 * modify, merge, publish, distribute, sublicense, and/or sell copies
20 * of the Software, and to permit persons to whom the Software is
21 * furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be
24 * included in all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
34 * ***** END LICENSE BLOCK *****
35 */
36  
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40  
41 #ifdef _MSC_VER
42 # define _CRT_SECURE_NO_WARNINGS
43 #endif
44  
45 #include "amqp_tcp_socket.h"
46 #include "amqp_private.h"
47 #include "amqp_time.h"
48 #include <assert.h>
49 #include <errno.h>
50 #include <stdint.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54  
55 #ifndef AMQP_INITIAL_FRAME_POOL_PAGE_SIZE
56 #define AMQP_INITIAL_FRAME_POOL_PAGE_SIZE 65536
57 #endif
58  
59 #ifndef AMQP_INITIAL_INBOUND_SOCK_BUFFER_SIZE
60 #define AMQP_INITIAL_INBOUND_SOCK_BUFFER_SIZE 131072
61 #endif
62  
63  
64 #define ENFORCE_STATE(statevec, statenum) \
65 { \
66 amqp_connection_state_t _check_state = (statevec); \
67 size_t _wanted_state = (statenum); \
68 if (_check_state->state != _wanted_state) \
69 amqp_abort("Programming error: invalid AMQP connection state: expected %d, got %d", \
70 _wanted_state, \
71 _check_state->state); \
72 }
73  
74 27 amqp_connection_state_t amqp_new_connection(void)
75 {
76 int res;
77 amqp_connection_state_t state =
78 27 (amqp_connection_state_t) calloc(1, sizeof(struct amqp_connection_state_t_));
79  
80 27 ▶ if (state == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state == NULL) { 0 27 27
81 0 return NULL;
82 }
83  
84 27 res = amqp_tune_connection(state, 0, AMQP_INITIAL_FRAME_POOL_PAGE_SIZE, 0);
85 27 ▶ if (0 != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 != res) { 0 27 27
86 0 goto out_nomem;
87 }
88  
89 27 state->inbound_buffer.bytes = state->header_buffer;
90 27 state->inbound_buffer.len = sizeof(state->header_buffer);
91  
92 27 state->state = CONNECTION_STATE_INITIAL;
93 /* the server protocol version response is 8 bytes, which conveniently
94 is also the minimum frame size */
95 27 state->target_size = 8;
96  
97 27 state->sock_inbound_buffer.len = AMQP_INITIAL_INBOUND_SOCK_BUFFER_SIZE;
98 27 state->sock_inbound_buffer.bytes = malloc(AMQP_INITIAL_INBOUND_SOCK_BUFFER_SIZE);
99 27 ▶ if (state->sock_inbound_buffer.bytes == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->sock_inbound_buffer.bytes == NULL) { 0 27 27
100 0 goto out_nomem;
101 }
102  
103 27 init_amqp_pool(&state->properties_pool, 512);
104  
105 27 return state;
106  
107 0 out_nomem:
108 0 free(state->sock_inbound_buffer.bytes);
109 0 free(state);
110 0 return NULL;
111 }
112  
113 67 int amqp_get_sockfd(amqp_connection_state_t state)
114 {
115 67 ▶ return state->socket ? amqp_socket_get_sockfd(state->socket) : -1;
Branch: 1 branch
BranchTrueFalseTotal
return state->socket ? amqp_socket_get_sockfd(state->socket) : -1; 67 0 67
116 }
117  
118 0 void amqp_set_sockfd(amqp_connection_state_t state,
119 int sockfd)
120 {
121 0 amqp_socket_t *socket = amqp_tcp_socket_new(state);
122 0 ▶ if (!socket) {
Branch: 1 branch
BranchTrueFalseTotal
if (!socket) { 0 0 0
123 0 amqp_abort("%s", strerror(errno));
124 }
125 0 amqp_tcp_socket_set_sockfd(socket, sockfd);
126 0 }
127  
128 58 void amqp_set_socket(amqp_connection_state_t state, amqp_socket_t *socket)
129 {
130 58 amqp_socket_delete(state->socket);
131 58 state->socket = socket;
132 58 }
133  
134 amqp_socket_t *
135 289 amqp_get_socket(amqp_connection_state_t state)
136 {
137 289 return state->socket;
138 }
139  
140 27 int amqp_tune_connection(amqp_connection_state_t state,
141 int channel_max,
142 int frame_max,
143 int heartbeat)
144 {
145 void *newbuf;
146 int res;
147  
148 27 ▶ ENFORCE_STATE(state, CONNECTION_STATE_IDLE);
Branch: 1 branch
BranchTrueFalseTotal
ENFORCE_STATE(state, CONNECTION_STATE_IDLE); 0 27 27
149  
150 27 state->channel_max = channel_max;
151 27 state->frame_max = frame_max;
152  
153 27 state->heartbeat = heartbeat;
154 27 ▶ if (0 > state->heartbeat) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 > state->heartbeat) { 0 27 27
155 0 state->heartbeat = 0;
156 }
157  
158 27 res = amqp_time_s_from_now(&state->next_send_heartbeat,
159 amqp_heartbeat_send(state));
160 27 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 27 27
161 0 return res;
162 }
163 27 res = amqp_time_s_from_now(&state->next_recv_heartbeat,
164 amqp_heartbeat_recv(state));
165 27 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 27 27
166 0 return res;
167 }
168  
169 27 state->outbound_buffer.len = frame_max;
170 27 newbuf = realloc(state->outbound_buffer.bytes, frame_max);
171 27 ▶ if (newbuf == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (newbuf == NULL) { 0 27 27
172 0 return AMQP_STATUS_NO_MEMORY;
173 }
174 27 state->outbound_buffer.bytes = newbuf;
175  
176 27 return AMQP_STATUS_OK;
177 }
178  
179 1 int amqp_get_channel_max(amqp_connection_state_t state)
180 {
181 1 return state->channel_max;
182 }
183  
184 0 int amqp_get_frame_max(amqp_connection_state_t state)
185 {
186 0 return state->frame_max;
187 }
188  
189 0 int amqp_get_heartbeat(amqp_connection_state_t state)
190 {
191 0 return state->heartbeat;
192 }
193  
194 27 int amqp_destroy_connection(amqp_connection_state_t state)
195 {
196 27 int status = AMQP_STATUS_OK;
197 27 ▶ if (state) {
Branch: 1 branch
BranchTrueFalseTotal
if (state) { 27 0 27
198 int i;
199 459 ▶ for (i = 0; i < POOL_TABLE_SIZE; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < POOL_TABLE_SIZE; ++i) { 432 27 459
200 432 amqp_pool_table_entry_t *entry = state->pool_table[i];
201 455 ▶ while (NULL != entry) {
Branch: 1 branch
BranchTrueFalseTotal
while (NULL != entry) { 23 432 455
202 23 amqp_pool_table_entry_t *todelete = entry;
203 23 empty_amqp_pool(&entry->pool);
204 23 entry = entry->next;
205 23 free(todelete);
206 }
207 }
208  
209 27 free(state->outbound_buffer.bytes);
210 27 free(state->sock_inbound_buffer.bytes);
211 27 amqp_socket_delete(state->socket);
212 27 empty_amqp_pool(&state->properties_pool);
213 27 free(state);
214 }
215 27 return status;
216 }
217  
218 56 static void return_to_idle(amqp_connection_state_t state)
219 {
220 56 state->inbound_buffer.len = sizeof(state->header_buffer);
221 56 state->inbound_buffer.bytes = state->header_buffer;
222 56 state->inbound_offset = 0;
223 56 state->target_size = HEADER_SIZE;
224 56 state->state = CONNECTION_STATE_IDLE;
225 56 }
226  
227 112 static size_t consume_data(amqp_connection_state_t state,
228 amqp_bytes_t *received_data)
229 {
230 /* how much data is available and will fit? */
231 112 size_t bytes_consumed = state->target_size - state->inbound_offset;
232 112 ▶ if (received_data->len < bytes_consumed) {
Branch: 1 branch
BranchTrueFalseTotal
if (received_data->len < bytes_consumed) { 0 112 112
233 0 bytes_consumed = received_data->len;
234 }
235  
236 224 memcpy(amqp_offset(state->inbound_buffer.bytes, state->inbound_offset),
237 112 received_data->bytes, bytes_consumed);
238 112 state->inbound_offset += bytes_consumed;
239 112 received_data->bytes = amqp_offset(received_data->bytes, bytes_consumed);
240 112 received_data->len -= bytes_consumed;
241  
242 112 return bytes_consumed;
243 }
244  
245 56 int amqp_handle_input(amqp_connection_state_t state,
246 amqp_bytes_t received_data,
247 amqp_frame_t *decoded_frame)
248 {
249 size_t bytes_consumed;
250 void *raw_frame;
251  
252 /* Returning frame_type of zero indicates either insufficient input,
253 or a complete, ignored frame was read. */
254 56 decoded_frame->frame_type = 0;
255  
256 56 ▶ if (received_data.len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (received_data.len == 0) { 0 56 56
257 0 return AMQP_STATUS_OK;
258 }
259  
260 56 ▶ if (state->state == CONNECTION_STATE_IDLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->state == CONNECTION_STATE_IDLE) { 33 23 56
261 33 state->state = CONNECTION_STATE_HEADER;
262 }
263  
264 56 bytes_consumed = consume_data(state, &received_data);
265  
266 /* do we have target_size data yet? if not, return with the
267 expectation that more will arrive */
268 56 ▶ if (state->inbound_offset < state->target_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->inbound_offset < state->target_size) { 0 56 56
269 0 return (int)bytes_consumed;
270 }
271  
272 56 raw_frame = state->inbound_buffer.bytes;
273  
274 56 switch (state->state) {
275 23 case CONNECTION_STATE_INITIAL:
276 /* check for a protocol header from the server */
277 23 ▶ if (memcmp(raw_frame, "AMQP", 4) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(raw_frame, "AMQP", 4) == 0) { 0 23 23
278 0 decoded_frame->frame_type = AMQP_PSEUDOFRAME_PROTOCOL_HEADER;
279 0 decoded_frame->channel = 0;
280  
281 decoded_frame->payload.protocol_header.transport_high
282 0 = amqp_d8(raw_frame, 4);
283 decoded_frame->payload.protocol_header.transport_low
284 0 = amqp_d8(raw_frame, 5);
285 decoded_frame->payload.protocol_header.protocol_version_major
286 0 = amqp_d8(raw_frame, 6);
287 decoded_frame->payload.protocol_header.protocol_version_minor
288 0 = amqp_d8(raw_frame, 7);
289  
290 0 return_to_idle(state);
291 0 return (int)bytes_consumed;
292 }
293  
294 /* it's not a protocol header; fall through to process it as a
295 regular frame header */
296  
297 case CONNECTION_STATE_HEADER: {
298 amqp_channel_t channel;
299 amqp_pool_t *channel_pool;
300 /* frame length is 3 bytes in */
301 56 channel = amqp_d16(raw_frame, 1);
302  
303 state->target_size
304 56 = amqp_d32(raw_frame, 3) + HEADER_SIZE + FOOTER_SIZE;
305  
306 56 ▶ if ((size_t)state->frame_max < state->target_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)state->frame_max < state->target_size) { 0 56 56
307 0 return AMQP_STATUS_BAD_AMQP_DATA;
308 }
309  
310 56 channel_pool = amqp_get_or_create_channel_pool(state, channel);
311 56 ▶ if (NULL == channel_pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == channel_pool) { 0 56 56
312 0 return AMQP_STATUS_NO_MEMORY;
313 }
314  
315 56 amqp_pool_alloc_bytes(channel_pool, state->target_size, &state->inbound_buffer);
316 56 ▶ if (NULL == state->inbound_buffer.bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == state->inbound_buffer.bytes) { 0 56 56
317 0 return AMQP_STATUS_NO_MEMORY;
318 }
319 56 memcpy(state->inbound_buffer.bytes, state->header_buffer, HEADER_SIZE);
320 56 raw_frame = state->inbound_buffer.bytes;
321  
322 56 state->state = CONNECTION_STATE_BODY;
323  
324 56 bytes_consumed += consume_data(state, &received_data);
325  
326 /* do we have target_size data yet? if not, return with the
327 expectation that more will arrive */
328 56 ▶ if (state->inbound_offset < state->target_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->inbound_offset < state->target_size) { 0 56 56
329 0 return (int)bytes_consumed;
330 }
331  
332 }
333 /* fall through to process body */
334  
335 case CONNECTION_STATE_BODY: {
336 amqp_bytes_t encoded;
337 int res;
338 amqp_pool_t *channel_pool;
339  
340 /* Check frame end marker (footer) */
341 56 ▶ if (amqp_d8(raw_frame, state->target_size - 1) != AMQP_FRAME_END) {
Branch: 1 branch
BranchTrueFalseTotal
if (amqp_d8(raw_frame, state->target_size - 1) != AMQP_FRAME_END) { 0 56 56
342 0 return AMQP_STATUS_BAD_AMQP_DATA;
343 }
344  
345 56 decoded_frame->frame_type = amqp_d8(raw_frame, 0);
346 56 decoded_frame->channel = amqp_d16(raw_frame, 1);
347  
348 56 channel_pool = amqp_get_or_create_channel_pool(state, decoded_frame->channel);
349 56 ▶ if (NULL == channel_pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == channel_pool) { 0 56 56
350 0 return AMQP_STATUS_NO_MEMORY;
351 }
352  
353 56 switch (decoded_frame->frame_type) {
354 56 case AMQP_FRAME_METHOD:
355 56 decoded_frame->payload.method.id = amqp_d32(raw_frame, HEADER_SIZE);
356 56 encoded.bytes = amqp_offset(raw_frame, HEADER_SIZE + 4);
357 56 encoded.len = state->target_size - HEADER_SIZE - 4 - FOOTER_SIZE;
358  
359 56 res = amqp_decode_method(decoded_frame->payload.method.id,
360 channel_pool, encoded,
361 &decoded_frame->payload.method.decoded);
362 56 ▶ if (res < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (res < 0) { 0 56 56
363 0 return res;
364 }
365  
366 56 break;
367  
368 0 case AMQP_FRAME_HEADER:
369 decoded_frame->payload.properties.class_id
370 0 = amqp_d16(raw_frame, HEADER_SIZE);
371 /* unused 2-byte weight field goes here */
372 decoded_frame->payload.properties.body_size
373 0 = amqp_d64(raw_frame, HEADER_SIZE + 4);
374 0 encoded.bytes = amqp_offset(raw_frame, HEADER_SIZE + 12);
375 0 encoded.len = state->target_size - HEADER_SIZE - 12 - FOOTER_SIZE;
376 0 decoded_frame->payload.properties.raw = encoded;
377  
378 0 res = amqp_decode_properties(decoded_frame->payload.properties.class_id,
379 channel_pool, encoded,
380 &decoded_frame->payload.properties.decoded);
381 0 ▶ if (res < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (res < 0) { 0 0 0
382 0 return res;
383 }
384  
385 0 break;
386  
387 0 case AMQP_FRAME_BODY:
388 decoded_frame->payload.body_fragment.len
389 0 = state->target_size - HEADER_SIZE - FOOTER_SIZE;
390 decoded_frame->payload.body_fragment.bytes
391 0 = amqp_offset(raw_frame, HEADER_SIZE);
392 0 break;
393  
394 0 case AMQP_FRAME_HEARTBEAT:
395 0 break;
396  
397 0 default:
398 /* Ignore the frame */
399 0 decoded_frame->frame_type = 0;
400 0 break;
401 }
402  
403 56 return_to_idle(state);
404 56 return (int)bytes_consumed;
405 }
406  
407 0 default:
408 0 amqp_abort("Internal error: invalid amqp_connection_state_t->state %d", state->state);
409 return (int)bytes_consumed;
410 }
411 }
412  
413 0 amqp_boolean_t amqp_release_buffers_ok(amqp_connection_state_t state)
414 {
415 0 return (state->state == CONNECTION_STATE_IDLE);
416 }
417  
418 28 void amqp_release_buffers(amqp_connection_state_t state)
419 {
420 int i;
421 28 ▶ ENFORCE_STATE(state, CONNECTION_STATE_IDLE);
Branch: 1 branch
BranchTrueFalseTotal
ENFORCE_STATE(state, CONNECTION_STATE_IDLE); 0 28 28
422  
423 476 ▶ for (i = 0; i < POOL_TABLE_SIZE; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < POOL_TABLE_SIZE; ++i) { 448 28 476
424 448 amqp_pool_table_entry_t *entry = state->pool_table[i];
425  
426 476 ▶ for ( ;NULL != entry; entry = entry->next) {
Branch: 1 branch
BranchTrueFalseTotal
for ( ;NULL != entry; entry = entry->next) { 28 448 476
427 28 amqp_maybe_release_buffers_on_channel(state, entry->channel);
428 }
429 }
430 28 }
431  
432 0 void amqp_maybe_release_buffers(amqp_connection_state_t state)
433 {
434 0 ▶ if (amqp_release_buffers_ok(state)) {
Branch: 1 branch
BranchTrueFalseTotal
if (amqp_release_buffers_ok(state)) { 0 0 0
435 0 amqp_release_buffers(state);
436 }
437 0 }
438  
439 28 void amqp_maybe_release_buffers_on_channel(amqp_connection_state_t state, amqp_channel_t channel)
440 {
441 amqp_link_t *queued_link;
442 amqp_pool_t *pool;
443 28 ▶ if (CONNECTION_STATE_IDLE != state->state) {
Branch: 1 branch
BranchTrueFalseTotal
if (CONNECTION_STATE_IDLE != state->state) { 0 28 28
444 0 return;
445 }
446  
447 28 queued_link = state->first_queued_frame;
448  
449 28 ▶ while (NULL != queued_link) {
Branch: 1 branch
BranchTrueFalseTotal
while (NULL != queued_link) { 0 28 28
450 0 amqp_frame_t *frame = queued_link->data;
451 0 ▶ if (channel == frame->channel) {
Branch: 1 branch
BranchTrueFalseTotal
if (channel == frame->channel) { 0 0 0
452 0 return;
453 }
454  
455 0 queued_link = queued_link->next;
456 }
457  
458 28 pool = amqp_get_channel_pool(state, channel);
459  
460 28 ▶ if (pool != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (pool != NULL) { 28 0 28
461 28 recycle_amqp_pool(pool);
462 }
463 }
464  
465 58 static int amqp_frame_to_bytes(const amqp_frame_t *frame, amqp_bytes_t buffer,
466 amqp_bytes_t *encoded) {
467 58 void *out_frame = buffer.bytes;
468 size_t out_frame_len;
469 int res;
470  
471 58 amqp_e8(out_frame, 0, frame->frame_type);
472 58 amqp_e16(out_frame, 1, frame->channel);
473  
474 58 switch (frame->frame_type) {
475 0 case AMQP_FRAME_BODY: {
476 0 const amqp_bytes_t *body = &frame->payload.body_fragment;
477  
478 0 memcpy(amqp_offset(out_frame, HEADER_SIZE), body->bytes, body->len);
479  
480 0 out_frame_len = body->len;
481 0 break;
482 }
483 58 case AMQP_FRAME_METHOD: {
484 amqp_bytes_t method_encoded;
485  
486 58 amqp_e32(out_frame, HEADER_SIZE, frame->payload.method.id);
487  
488 58 method_encoded.bytes = amqp_offset(out_frame, HEADER_SIZE + 4);
489 58 method_encoded.len = buffer.len - HEADER_SIZE - 4 - FOOTER_SIZE;
490  
491 58 res = amqp_encode_method(frame->payload.method.id,
492 58 frame->payload.method.decoded, method_encoded);
493 58 ▶ if (res < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (res < 0) { 0 58 58
494 0 return res;
495 }
496  
497 58 out_frame_len = res + 4;
498 58 break;
499 }
500  
501 0 case AMQP_FRAME_HEADER:
502 0 amqp_e16(out_frame, HEADER_SIZE, frame->payload.properties.class_id);
503 0 amqp_e16(out_frame, HEADER_SIZE + 2, 0); /* "weight" */
504 0 amqp_e64(out_frame, HEADER_SIZE + 4, frame->payload.properties.body_size);
505  
506 0 encoded->bytes = amqp_offset(out_frame, HEADER_SIZE + 12);
507 0 encoded->len = buffer.len - HEADER_SIZE - 12 - FOOTER_SIZE;
508  
509 0 res = amqp_encode_properties(frame->payload.properties.class_id,
510 0 frame->payload.properties.decoded, *encoded);
511 0 ▶ if (res < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (res < 0) { 0 0 0
512 0 return res;
513 }
514  
515 0 out_frame_len = res + 12;
516 0 break;
517  
518 0 case AMQP_FRAME_HEARTBEAT:
519 0 out_frame_len = 0;
520 0 break;
521  
522 0 default:
523 0 return AMQP_STATUS_INVALID_PARAMETER;
524 }
525  
526 58 amqp_e32(out_frame, 3, (uint32_t)out_frame_len);
527 58 amqp_e8(out_frame, HEADER_SIZE + out_frame_len, AMQP_FRAME_END);
528  
529 58 encoded->bytes = out_frame;
530 58 encoded->len = out_frame_len + HEADER_SIZE + FOOTER_SIZE;
531  
532 58 return AMQP_STATUS_OK;
533 }
534  
535 0 int amqp_send_frame(amqp_connection_state_t state,
536 const amqp_frame_t *frame) {
537 0 return amqp_send_frame_inner(state, frame, AMQP_SF_NONE);
538 }
539  
540 58 int amqp_send_frame_inner(amqp_connection_state_t state,
541 const amqp_frame_t *frame, int flags) {
542 int res;
543 ssize_t sent;
544 amqp_bytes_t encoded;
545  
546 /* TODO: if the AMQP_SF_MORE socket optimization can be shown to work
547 * correctly, then this could be un-done so that body-frames are sent as 3
548 * send calls, getting rid of the copy of the body content, some testing
549 * would need to be done to see if this would actually a win for performance.
550 * */
551 58 res = amqp_frame_to_bytes(frame, state->outbound_buffer, &encoded);
552 58 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 58 0 58
553 0 return res;
554 }
555  
556 58 start_send:
557 58 sent = amqp_try_send(state, encoded.bytes, encoded.len,
558 state->next_recv_heartbeat, flags);
559 58 ▶ if (0 > sent) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 > sent) { 2 56 58
560 2 return (int)sent;
561 }
562  
563 /* A partial send has occurred, because of a heartbeat timeout, try and recv
564 * something */
565 56 ▶ if ((ssize_t)encoded.len != sent) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ssize_t)encoded.len != sent) { 0 56 56
566 0 res = amqp_try_recv(state);
567  
568 0 ▶ if (AMQP_STATUS_TIMEOUT == res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_TIMEOUT == res) { 0 0 0
569 0 return AMQP_STATUS_HEARTBEAT_TIMEOUT;
570 0 ▶ } else if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (AMQP_STATUS_OK != res) { 0 0 0
571 0 return res;
572 }
573  
574 0 encoded.bytes = (uint8_t*)encoded.bytes + sent;
575 0 encoded.len -= sent;
576 0 goto start_send;
577 }
578  
579 56 res = amqp_time_s_from_now(&state->next_send_heartbeat,
580 amqp_heartbeat_send(state));
581 56 return res;
582 }
583  
584 amqp_table_t *
585 0 amqp_get_server_properties(amqp_connection_state_t state)
586 {
587 0 return &state->server_properties;
588 }
589  
590 amqp_table_t *
591 0 amqp_get_client_properties(amqp_connection_state_t state)
592 {
593 0 return &state->client_properties;
594 }
« amqp_socket.c ↑ Summary amqp_mem.c »