| 1 |
|
| #include <stdlib.h> |
| 2 |
|
| #include <stdio.h> |
| 3 |
|
| #include <string.h> |
| 4 |
|
| #include <stdint.h> |
| 5 |
|
| #include <errno.h> |
| 6 |
|
| |
| 7 |
|
| #include <unistd.h> |
| 8 |
|
| #include <sys/uio.h> |
| 9 |
|
| #include <sys/time.h> |
| 10 |
|
| |
| 11 |
|
| #include "amqp.h" |
| 12 |
|
| #include "amqp_framing.h" |
| 13 |
|
| #include "amqp_private.h" |
| 14 |
|
| |
| 15 |
|
| #include <assert.h> |
| 16 |
|
| |
| 17 |
|
| #define INITIAL_FRAME_POOL_PAGE_SIZE 65536 |
| 18 |
|
| #define INITIAL_DECODING_POOL_PAGE_SIZE 131072 |
| 19 |
|
| #define INITIAL_INBOUND_SOCK_BUFFER_SIZE 131072 |
| 20 |
|
| |
| 21 |
|
| #define ENFORCE_STATE(statevec, statenum) \ |
| 22 |
|
| { \ |
| 23 |
|
| amqp_connection_state_t _check_state = (statevec); \ |
| 24 |
|
| int _wanted_state = (statenum); \ |
| 25 |
|
| amqp_assert(_check_state->state == _wanted_state, \ |
| 26 |
|
| "Programming error: invalid AMQP connection state: expected %d, got %d", \ |
| 27 |
|
| _wanted_state, \ |
| 28 |
|
| _check_state->state); \ |
| 29 |
|
| } |
| 30 |
|
| |
| 31 |
16 |
| amqp_connection_state_t amqp_new_connection(void) { |
| 32 |
|
| amqp_connection_state_t state = |
| 33 |
16 |
| (amqp_connection_state_t) calloc(1, sizeof(struct amqp_connection_state_t_)); |
| 34 |
|
| |
| 35 |
16 |
▶ | if (state == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state == NULL) { |
0 |
16 |
16 |
|
| 36 |
0 |
| return NULL; |
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
16 |
| init_amqp_pool(&state->frame_pool, INITIAL_FRAME_POOL_PAGE_SIZE); |
| 40 |
16 |
| init_amqp_pool(&state->decoding_pool, INITIAL_DECODING_POOL_PAGE_SIZE); |
| 41 |
|
| |
| 42 |
16 |
| state->state = CONNECTION_STATE_IDLE; |
| 43 |
|
| |
| 44 |
16 |
| state->inbound_buffer.bytes = NULL; |
| 45 |
16 |
| state->outbound_buffer.bytes = NULL; |
| 46 |
16 |
▶ | if (amqp_tune_connection(state, 0, INITIAL_FRAME_POOL_PAGE_SIZE, 0) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (amqp_tune_connection(state, 0, INITIAL_FRAME_POOL_PAGE_SIZE, 0) != 0) { |
0 |
16 |
16 |
|
| 47 |
0 |
| empty_amqp_pool(&state->frame_pool); |
| 48 |
0 |
| empty_amqp_pool(&state->decoding_pool); |
| 49 |
0 |
| free(state); |
| 50 |
0 |
| return NULL; |
| 51 |
|
| } |
| 52 |
|
| |
| 53 |
16 |
| state->inbound_offset = 0; |
| 54 |
16 |
| state->target_size = HEADER_SIZE; |
| 55 |
|
| |
| 56 |
16 |
| state->sockfd = -1; |
| 57 |
16 |
| state->sock_inbound_buffer.len = INITIAL_INBOUND_SOCK_BUFFER_SIZE; |
| 58 |
16 |
| state->sock_inbound_buffer.bytes = malloc(INITIAL_INBOUND_SOCK_BUFFER_SIZE); |
| 59 |
16 |
▶ | if (state->sock_inbound_buffer.bytes == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state->sock_inbound_buffer.bytes == NULL) { |
0 |
16 |
16 |
|
| 60 |
0 |
| amqp_destroy_connection(state); |
| 61 |
0 |
| return NULL; |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
16 |
| state->sock_inbound_offset = 0; |
| 65 |
16 |
| state->sock_inbound_limit = 0; |
| 66 |
|
| |
| 67 |
16 |
| state->first_queued_frame = NULL; |
| 68 |
16 |
| state->last_queued_frame = NULL; |
| 69 |
|
| |
| 70 |
16 |
| return state; |
| 71 |
|
| } |
| 72 |
|
| |
| 73 |
16 |
| int amqp_get_sockfd(amqp_connection_state_t state) { |
| 74 |
16 |
| return state->sockfd; |
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
0 |
| void amqp_set_sockfd(amqp_connection_state_t state, |
| 78 |
|
| int sockfd) |
| 79 |
|
| { |
| 80 |
0 |
| state->sockfd = sockfd; |
| 81 |
0 |
| } |
| 82 |
|
| |
| 83 |
16 |
| int amqp_tune_connection(amqp_connection_state_t state, |
| 84 |
|
| int channel_max, |
| 85 |
|
| int frame_max, |
| 86 |
|
| int heartbeat) |
| 87 |
|
| { |
| 88 |
|
| void *newbuf; |
| 89 |
|
| |
| 90 |
16 |
▶ | ENFORCE_STATE(state, CONNECTION_STATE_IDLE); |
Branch: 1 branch
| Branch | True | False | Total |
| ENFORCE_STATE(state, CONNECTION_STATE_IDLE); |
0 |
16 |
16 |
|
| 91 |
|
| |
| 92 |
16 |
| state->channel_max = channel_max; |
| 93 |
16 |
| state->frame_max = frame_max; |
| 94 |
16 |
| state->heartbeat = heartbeat; |
| 95 |
|
| |
| 96 |
16 |
| empty_amqp_pool(&state->frame_pool); |
| 97 |
16 |
| init_amqp_pool(&state->frame_pool, frame_max); |
| 98 |
|
| |
| 99 |
16 |
| state->inbound_buffer.len = frame_max; |
| 100 |
16 |
| state->outbound_buffer.len = frame_max; |
| 101 |
16 |
| newbuf = realloc(state->outbound_buffer.bytes, frame_max); |
| 102 |
16 |
▶ | if (newbuf == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (newbuf == NULL) { |
0 |
16 |
16 |
|
| 103 |
0 |
| amqp_destroy_connection(state); |
| 104 |
0 |
| return -ENOMEM; |
| 105 |
|
| } |
| 106 |
16 |
| state->outbound_buffer.bytes = newbuf; |
| 107 |
|
| |
| 108 |
16 |
| return 0; |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
0 |
| int amqp_get_channel_max(amqp_connection_state_t state) { |
| 112 |
0 |
| return state->channel_max; |
| 113 |
|
| } |
| 114 |
|
| |
| 115 |
16 |
| void amqp_destroy_connection(amqp_connection_state_t state) { |
| 116 |
16 |
| empty_amqp_pool(&state->frame_pool); |
| 117 |
16 |
| empty_amqp_pool(&state->decoding_pool); |
| 118 |
16 |
| free(state->outbound_buffer.bytes); |
| 119 |
16 |
| free(state->sock_inbound_buffer.bytes); |
| 120 |
16 |
| free(state); |
| 121 |
16 |
| } |
| 122 |
|
| |
| 123 |
0 |
| static void return_to_idle(amqp_connection_state_t state) { |
| 124 |
0 |
| state->inbound_buffer.bytes = NULL; |
| 125 |
0 |
| state->inbound_offset = 0; |
| 126 |
0 |
| state->target_size = HEADER_SIZE; |
| 127 |
0 |
| state->state = CONNECTION_STATE_IDLE; |
| 128 |
0 |
| } |
| 129 |
|
| |
| 130 |
1 |
| void amqp_set_basic_return_cb(amqp_connection_state_t state, |
| 131 |
|
| amqp_basic_return_fn_t f, void *data) { |
| 132 |
1 |
| state->basic_return_callback = f; |
| 133 |
1 |
| state->basic_return_callback_data = data; |
| 134 |
1 |
| } |
| 135 |
0 |
| int amqp_handle_input(amqp_connection_state_t state, |
| 136 |
|
| amqp_bytes_t received_data, |
| 137 |
|
| amqp_frame_t *decoded_frame) |
| 138 |
|
| { |
| 139 |
0 |
| int total_bytes_consumed = 0; |
| 140 |
|
| int bytes_consumed; |
| 141 |
|
| |
| 142 |
|
| /* Returning frame_type of zero indicates either insufficient input, |
| 143 |
|
| or a complete, ignored frame was read. */ |
| 144 |
0 |
| decoded_frame->frame_type = 0; |
| 145 |
|
| |
| 146 |
0 |
| read_more: |
| 147 |
0 |
▶ | if (received_data.len == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (received_data.len == 0) { |
0 |
0 |
0 |
|
| 148 |
0 |
| return total_bytes_consumed; |
| 149 |
|
| } |
| 150 |
|
| |
| 151 |
0 |
▶ | if (state->state == CONNECTION_STATE_IDLE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state->state == CONNECTION_STATE_IDLE) { |
0 |
0 |
0 |
|
| 152 |
0 |
| state->inbound_buffer.bytes = amqp_pool_alloc(&state->frame_pool, state->inbound_buffer.len); |
| 153 |
0 |
| state->state = CONNECTION_STATE_WAITING_FOR_HEADER; |
| 154 |
|
| } |
| 155 |
|
| |
| 156 |
0 |
| bytes_consumed = state->target_size - state->inbound_offset; |
| 157 |
0 |
▶ | if (received_data.len < bytes_consumed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (received_data.len < bytes_consumed) { |
0 |
0 |
0 |
|
| 158 |
0 |
| bytes_consumed = received_data.len; |
| 159 |
|
| } |
| 160 |
|
| |
| 161 |
0 |
▶ | E_BYTES(state->inbound_buffer, state->inbound_offset, bytes_consumed, received_data.bytes); |
Branch: 1 branch
| Branch | True | False | Total |
| E_BYTES(state->inbound_buffer, state->inbound_offset, bytes_consumed, received_data.bytes); |
0 |
0 |
0 |
|
| 162 |
0 |
| state->inbound_offset += bytes_consumed; |
| 163 |
0 |
| total_bytes_consumed += bytes_consumed; |
| 164 |
|
| |
| 165 |
0 |
▶ | assert(state->inbound_offset <= state->target_size); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(state->inbound_offset <= state->target_size); |
0 |
0 |
0 |
|
| 166 |
|
| |
| 167 |
0 |
▶ | if (state->inbound_offset < state->target_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state->inbound_offset < state->target_size) { |
0 |
0 |
0 |
|
| 168 |
0 |
| return total_bytes_consumed; |
| 169 |
|
| } |
| 170 |
|
| |
| 171 |
0 |
| switch (state->state) { |
| 172 |
0 |
| case CONNECTION_STATE_WAITING_FOR_HEADER: |
| 173 |
0 |
▶ | if (D_8(state->inbound_buffer, 0) == AMQP_PSEUDOFRAME_PROTOCOL_HEADER && |
Branch: 2 branches
| Branch | True | False | Total |
| if (D_8(state->inbound_buffer, 0) == AMQP_PSEUDOFRAME_PROTOCOL_HEADER && |
0 |
0 |
0 |
| if (D_8(state->inbound_buffer, 0) == AMQP_PSEUDOFRAME_PROTOCOL_HEADER && |
0 |
0 |
0 |
|
| 174 |
0 |
▶ | D_16(state->inbound_buffer, 1) == AMQP_PSEUDOFRAME_PROTOCOL_CHANNEL) |
Branch: 2 branches
| Branch | True | False | Total |
| D_16(state->inbound_buffer, 1) == AMQP_PSEUDOFRAME_PROTOCOL_CHANNEL) |
0 |
0 |
0 |
| D_16(state->inbound_buffer, 1) == AMQP_PSEUDOFRAME_PROTOCOL_CHANNEL) |
0 |
0 |
0 |
|
| 175 |
|
| { |
| 176 |
0 |
| state->target_size = 8; |
| 177 |
0 |
| state->state = CONNECTION_STATE_WAITING_FOR_PROTOCOL_HEADER; |
| 178 |
|
| } else { |
| 179 |
0 |
▶ | state->target_size = D_32(state->inbound_buffer, 3) + HEADER_SIZE + FOOTER_SIZE; |
Branch: 1 branch
| Branch | True | False | Total |
| state->target_size = D_32(state->inbound_buffer, 3) + HEADER_SIZE + FOOTER_SIZE; |
0 |
0 |
0 |
|
| 180 |
0 |
| state->state = CONNECTION_STATE_WAITING_FOR_BODY; |
| 181 |
|
| } |
| 182 |
|
| |
| 183 |
|
| /* Wind buffer forward, and try to read some body out of it. */ |
| 184 |
0 |
| received_data.len -= bytes_consumed; |
| 185 |
0 |
| received_data.bytes = ((char *) received_data.bytes) + bytes_consumed; |
| 186 |
0 |
| goto read_more; |
| 187 |
|
| |
| 188 |
0 |
| case CONNECTION_STATE_WAITING_FOR_BODY: { |
| 189 |
0 |
▶ | int frame_type = D_8(state->inbound_buffer, 0); |
Branch: 1 branch
| Branch | True | False | Total |
| int frame_type = D_8(state->inbound_buffer, 0); |
0 |
0 |
0 |
|
| 190 |
|
| |
| 191 |
|
| #if 0 |
| 192 |
|
| printf("recving:\n"); |
| 193 |
|
| amqp_dump(state->inbound_buffer.bytes, state->target_size); |
| 194 |
|
| #endif |
| 195 |
|
| |
| 196 |
|
| /* Check frame end marker (footer) */ |
| 197 |
0 |
▶ | if (D_8(state->inbound_buffer, state->target_size - 1) != AMQP_FRAME_END) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (D_8(state->inbound_buffer, state->target_size - 1) != AMQP_FRAME_END) { |
0 |
0 |
0 |
| if (D_8(state->inbound_buffer, state->target_size - 1) != AMQP_FRAME_END) { |
0 |
0 |
0 |
|
| 198 |
0 |
| return -EINVAL; |
| 199 |
|
| } |
| 200 |
|
| |
| 201 |
0 |
▶ | decoded_frame->channel = D_16(state->inbound_buffer, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| decoded_frame->channel = D_16(state->inbound_buffer, 1); |
0 |
0 |
0 |
|
| 202 |
|
| |
| 203 |
0 |
| switch (frame_type) { |
| 204 |
0 |
| case AMQP_FRAME_METHOD: { |
| 205 |
|
| amqp_bytes_t encoded; |
| 206 |
|
| |
| 207 |
|
| /* Four bytes of method ID before the method args. */ |
| 208 |
0 |
| encoded.len = state->target_size - (HEADER_SIZE + 4 + FOOTER_SIZE); |
| 209 |
0 |
▶ | encoded.bytes = D_BYTES(state->inbound_buffer, HEADER_SIZE + 4, encoded.len); |
Branch: 1 branch
| Branch | True | False | Total |
| encoded.bytes = D_BYTES(state->inbound_buffer, HEADER_SIZE + 4, encoded.len); |
0 |
0 |
0 |
|
| 210 |
|
| |
| 211 |
0 |
| decoded_frame->frame_type = AMQP_FRAME_METHOD; |
| 212 |
0 |
▶ | decoded_frame->payload.method.id = D_32(state->inbound_buffer, HEADER_SIZE); |
Branch: 1 branch
| Branch | True | False | Total |
| decoded_frame->payload.method.id = D_32(state->inbound_buffer, HEADER_SIZE); |
0 |
0 |
0 |
|
| 213 |
0 |
▶ | AMQP_CHECK_RESULT(amqp_decode_method(decoded_frame->payload.method.id, |
Branch: 1 branch
| Branch | True | False | Total |
| AMQP_CHECK_RESULT(amqp_decode_method(decoded_frame->payload.method.id, |
0 |
0 |
0 |
|
| 214 |
|
| &state->decoding_pool, |
| 215 |
|
| encoded, |
| 216 |
|
| &decoded_frame->payload.method.decoded)); |
| 217 |
0 |
| break; |
| 218 |
|
| } |
| 219 |
|
| |
| 220 |
0 |
| case AMQP_FRAME_HEADER: { |
| 221 |
|
| amqp_bytes_t encoded; |
| 222 |
|
| |
| 223 |
|
| /* 12 bytes for properties header. */ |
| 224 |
0 |
| encoded.len = state->target_size - (HEADER_SIZE + 12 + FOOTER_SIZE); |
| 225 |
0 |
▶ | encoded.bytes = D_BYTES(state->inbound_buffer, HEADER_SIZE + 12, encoded.len); |
Branch: 1 branch
| Branch | True | False | Total |
| encoded.bytes = D_BYTES(state->inbound_buffer, HEADER_SIZE + 12, encoded.len); |
0 |
0 |
0 |
|
| 226 |
|
| |
| 227 |
0 |
| decoded_frame->frame_type = AMQP_FRAME_HEADER; |
| 228 |
0 |
▶ | decoded_frame->payload.properties.class_id = D_16(state->inbound_buffer, HEADER_SIZE); |
Branch: 1 branch
| Branch | True | False | Total |
| decoded_frame->payload.properties.class_id = D_16(state->inbound_buffer, HEADER_SIZE); |
0 |
0 |
0 |
|
| 229 |
0 |
▶ | decoded_frame->payload.properties.body_size = D_64(state->inbound_buffer, HEADER_SIZE+4); |
Branch: 2 branches
| Branch | True | False | Total |
| decoded_frame->payload.properties.body_size = D_64(state->inbound_buffer, HEADER_SIZE+4); |
0 |
0 |
0 |
| decoded_frame->payload.properties.body_size = D_64(state->inbound_buffer, HEADER_SIZE+4); |
0 |
0 |
0 |
|
| 230 |
0 |
▶ | AMQP_CHECK_RESULT(amqp_decode_properties(decoded_frame->payload.properties.class_id, |
Branch: 1 branch
| Branch | True | False | Total |
| AMQP_CHECK_RESULT(amqp_decode_properties(decoded_frame->payload.properties.class_id, |
0 |
0 |
0 |
|
| 231 |
|
| &state->decoding_pool, |
| 232 |
|
| encoded, |
| 233 |
|
| &decoded_frame->payload.properties.decoded)); |
| 234 |
0 |
| break; |
| 235 |
|
| } |
| 236 |
|
| |
| 237 |
0 |
| case AMQP_FRAME_BODY: { |
| 238 |
0 |
| size_t fragment_len = state->target_size - (HEADER_SIZE + FOOTER_SIZE); |
| 239 |
|
| |
| 240 |
0 |
| decoded_frame->frame_type = AMQP_FRAME_BODY; |
| 241 |
0 |
| decoded_frame->payload.body_fragment.len = fragment_len; |
| 242 |
0 |
| decoded_frame->payload.body_fragment.bytes = |
| 243 |
0 |
▶ | D_BYTES(state->inbound_buffer, HEADER_SIZE, fragment_len); |
Branch: 1 branch
| Branch | True | False | Total |
| D_BYTES(state->inbound_buffer, HEADER_SIZE, fragment_len); |
0 |
0 |
0 |
|
| 244 |
0 |
| break; |
| 245 |
|
| } |
| 246 |
|
| |
| 247 |
0 |
| case AMQP_FRAME_HEARTBEAT: |
| 248 |
0 |
| decoded_frame->frame_type = AMQP_FRAME_HEARTBEAT; |
| 249 |
0 |
| break; |
| 250 |
|
| |
| 251 |
0 |
| default: |
| 252 |
|
| /* Ignore the frame by not changing frame_type away from 0. */ |
| 253 |
0 |
| break; |
| 254 |
|
| } |
| 255 |
|
| |
| 256 |
0 |
| return_to_idle(state); |
| 257 |
|
| |
| 258 |
0 |
▶ | if(decoded_frame->frame_type == AMQP_FRAME_METHOD && |
Branch: 1 branch
| Branch | True | False | Total |
| if(decoded_frame->frame_type == AMQP_FRAME_METHOD && |
0 |
0 |
0 |
|
| 259 |
0 |
▶ | decoded_frame->payload.method.id == AMQP_BASIC_RETURN_METHOD) { |
Branch: 1 branch
| Branch | True | False | Total |
| decoded_frame->payload.method.id == AMQP_BASIC_RETURN_METHOD) { |
0 |
0 |
0 |
|
| 260 |
0 |
| amqp_basic_return_t *m = decoded_frame->payload.method.decoded; |
| 261 |
0 |
▶ | if(state->basic_return_callback) |
Branch: 1 branch
| Branch | True | False | Total |
| if(state->basic_return_callback) |
0 |
0 |
0 |
|
| 262 |
0 |
| state->basic_return_callback(decoded_frame->channel, m, |
| 263 |
|
| state->basic_return_callback_data); |
| 264 |
|
| } |
| 265 |
|
| |
| 266 |
0 |
| return total_bytes_consumed; |
| 267 |
|
| } |
| 268 |
|
| |
| 269 |
0 |
| case CONNECTION_STATE_WAITING_FOR_PROTOCOL_HEADER: |
| 270 |
0 |
| decoded_frame->frame_type = AMQP_PSEUDOFRAME_PROTOCOL_HEADER; |
| 271 |
0 |
| decoded_frame->channel = AMQP_PSEUDOFRAME_PROTOCOL_CHANNEL; |
| 272 |
0 |
▶ | amqp_assert(D_8(state->inbound_buffer, 3) == (uint8_t) 'P', |
Branch: 2 branches
| Branch | True | False | Total |
| amqp_assert(D_8(state->inbound_buffer, 3) == (uint8_t) 'P', |
0 |
0 |
0 |
| amqp_assert(D_8(state->inbound_buffer, 3) == (uint8_t) 'P', |
0 |
0 |
0 |
|
| 273 |
|
| "Invalid protocol header received"); |
| 274 |
0 |
▶ | decoded_frame->payload.protocol_header.transport_high = D_8(state->inbound_buffer, 4); |
Branch: 1 branch
| Branch | True | False | Total |
| decoded_frame->payload.protocol_header.transport_high = D_8(state->inbound_buffer, 4); |
0 |
0 |
0 |
|
| 275 |
0 |
▶ | decoded_frame->payload.protocol_header.transport_low = D_8(state->inbound_buffer, 5); |
Branch: 1 branch
| Branch | True | False | Total |
| decoded_frame->payload.protocol_header.transport_low = D_8(state->inbound_buffer, 5); |
0 |
0 |
0 |
|
| 276 |
0 |
▶ | decoded_frame->payload.protocol_header.protocol_version_major = D_8(state->inbound_buffer, 6); |
Branch: 1 branch
| Branch | True | False | Total |
| decoded_frame->payload.protocol_header.protocol_version_major = D_8(state->inbound_buffer, 6); |
0 |
0 |
0 |
|
| 277 |
0 |
▶ | decoded_frame->payload.protocol_header.protocol_version_minor = D_8(state->inbound_buffer, 7); |
Branch: 1 branch
| Branch | True | False | Total |
| decoded_frame->payload.protocol_header.protocol_version_minor = D_8(state->inbound_buffer, 7); |
0 |
0 |
0 |
|
| 278 |
|
| |
| 279 |
0 |
| return_to_idle(state); |
| 280 |
0 |
| return total_bytes_consumed; |
| 281 |
|
| |
| 282 |
0 |
| default: |
| 283 |
0 |
| amqp_assert(0, "Internal error: invalid amqp_connection_state_t->state %d", state->state); |
| 284 |
|
| } |
| 285 |
|
| } |
| 286 |
|
| |
| 287 |
16 |
| amqp_boolean_t amqp_release_buffers_ok(amqp_connection_state_t state) { |
| 288 |
16 |
▶ | return (state->state == CONNECTION_STATE_IDLE) && (state->first_queued_frame == NULL); |
Branch: 2 branches
| Branch | True | False | Total |
| return (state->state == CONNECTION_STATE_IDLE) && (state->first_queued_frame == NULL); |
16 |
0 |
16 |
| return (state->state == CONNECTION_STATE_IDLE) && (state->first_queued_frame == NULL); |
16 |
0 |
16 |
|
| 289 |
|
| } |
| 290 |
|
| |
| 291 |
16 |
| void amqp_release_buffers(amqp_connection_state_t state) { |
| 292 |
16 |
▶ | ENFORCE_STATE(state, CONNECTION_STATE_IDLE); |
Branch: 1 branch
| Branch | True | False | Total |
| ENFORCE_STATE(state, CONNECTION_STATE_IDLE); |
0 |
16 |
16 |
|
| 293 |
|
| |
| 294 |
16 |
▶ | amqp_assert(state->first_queued_frame == NULL, |
Branch: 1 branch
| Branch | True | False | Total |
| amqp_assert(state->first_queued_frame == NULL, |
0 |
16 |
16 |
|
| 295 |
|
| "Programming error: attempt to amqp_release_buffers while waiting events enqueued"); |
| 296 |
|
| |
| 297 |
16 |
| recycle_amqp_pool(&state->frame_pool); |
| 298 |
16 |
| recycle_amqp_pool(&state->decoding_pool); |
| 299 |
16 |
| } |
| 300 |
|
| |
| 301 |
16 |
| void amqp_maybe_release_buffers(amqp_connection_state_t state) { |
| 302 |
16 |
▶ | if (amqp_release_buffers_ok(state)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (amqp_release_buffers_ok(state)) { |
16 |
0 |
16 |
|
| 303 |
16 |
| amqp_release_buffers(state); |
| 304 |
|
| } |
| 305 |
16 |
| } |
| 306 |
|
| |
| 307 |
92 |
| static int inner_send_frame(amqp_connection_state_t state, |
| 308 |
|
| amqp_frame_t const *frame, |
| 309 |
|
| amqp_bytes_t *encoded, |
| 310 |
|
| int *payload_len) |
| 311 |
|
| { |
| 312 |
|
| int separate_body; |
| 313 |
|
| |
| 314 |
92 |
▶ | E_8(state->outbound_buffer, 0, frame->frame_type); |
Branch: 1 branch
| Branch | True | False | Total |
| E_8(state->outbound_buffer, 0, frame->frame_type); |
0 |
92 |
92 |
|
| 315 |
92 |
▶ | E_16(state->outbound_buffer, 1, frame->channel); |
Branch: 1 branch
| Branch | True | False | Total |
| E_16(state->outbound_buffer, 1, frame->channel); |
0 |
92 |
92 |
|
| 316 |
92 |
| switch (frame->frame_type) { |
| 317 |
91 |
| case AMQP_FRAME_METHOD: |
| 318 |
91 |
▶ | E_32(state->outbound_buffer, HEADER_SIZE, frame->payload.method.id); |
Branch: 1 branch
| Branch | True | False | Total |
| E_32(state->outbound_buffer, HEADER_SIZE, frame->payload.method.id); |
0 |
91 |
91 |
|
| 319 |
91 |
| encoded->len = state->outbound_buffer.len - (HEADER_SIZE + 4 + FOOTER_SIZE); |
| 320 |
91 |
▶ | encoded->bytes = D_BYTES(state->outbound_buffer, HEADER_SIZE + 4, encoded->len); |
Branch: 1 branch
| Branch | True | False | Total |
| encoded->bytes = D_BYTES(state->outbound_buffer, HEADER_SIZE + 4, encoded->len); |
0 |
91 |
91 |
|
| 321 |
91 |
▶ | *payload_len = AMQP_CHECK_RESULT(amqp_encode_method(frame->payload.method.id, |
Branch: 1 branch
| Branch | True | False | Total |
| *payload_len = AMQP_CHECK_RESULT(amqp_encode_method(frame->payload.method.id, |
0 |
91 |
91 |
|
| 322 |
|
| frame->payload.method.decoded, |
| 323 |
91 |
| *encoded)) + 4; |
| 324 |
91 |
| separate_body = 0; |
| 325 |
91 |
| break; |
| 326 |
|
| |
| 327 |
0 |
| case AMQP_FRAME_HEADER: |
| 328 |
0 |
▶ | E_16(state->outbound_buffer, HEADER_SIZE, frame->payload.properties.class_id); |
Branch: 1 branch
| Branch | True | False | Total |
| E_16(state->outbound_buffer, HEADER_SIZE, frame->payload.properties.class_id); |
0 |
0 |
0 |
|
| 329 |
0 |
▶ | E_16(state->outbound_buffer, HEADER_SIZE+2, 0); /* "weight" */ |
Branch: 1 branch
| Branch | True | False | Total |
| E_16(state->outbound_buffer, HEADER_SIZE+2, 0); /* "weight" */ |
0 |
0 |
0 |
|
| 330 |
0 |
▶ | E_64(state->outbound_buffer, HEADER_SIZE+4, frame->payload.properties.body_size); |
Branch: 2 branches
| Branch | True | False | Total |
| E_64(state->outbound_buffer, HEADER_SIZE+4, frame->payload.properties.body_size); |
0 |
0 |
0 |
| E_64(state->outbound_buffer, HEADER_SIZE+4, frame->payload.properties.body_size); |
0 |
0 |
0 |
|
| 331 |
0 |
| encoded->len = state->outbound_buffer.len - (HEADER_SIZE + 12 + FOOTER_SIZE); |
| 332 |
0 |
▶ | encoded->bytes = D_BYTES(state->outbound_buffer, HEADER_SIZE + 12, encoded->len); |
Branch: 1 branch
| Branch | True | False | Total |
| encoded->bytes = D_BYTES(state->outbound_buffer, HEADER_SIZE + 12, encoded->len); |
0 |
0 |
0 |
|
| 333 |
0 |
▶ | *payload_len = AMQP_CHECK_RESULT(amqp_encode_properties(frame->payload.properties.class_id, |
Branch: 1 branch
| Branch | True | False | Total |
| *payload_len = AMQP_CHECK_RESULT(amqp_encode_properties(frame->payload.properties.class_id, |
0 |
0 |
0 |
|
| 334 |
|
| frame->payload.properties.decoded, |
| 335 |
0 |
| *encoded)) + 12; |
| 336 |
0 |
| separate_body = 0; |
| 337 |
0 |
| break; |
| 338 |
|
| |
| 339 |
0 |
| case AMQP_FRAME_BODY: |
| 340 |
0 |
| *encoded = frame->payload.body_fragment; |
| 341 |
0 |
| *payload_len = encoded->len; |
| 342 |
0 |
| separate_body = 1; |
| 343 |
0 |
| break; |
| 344 |
|
| |
| 345 |
1 |
| case AMQP_FRAME_HEARTBEAT: |
| 346 |
1 |
| *encoded = AMQP_EMPTY_BYTES; |
| 347 |
1 |
| *payload_len = 0; |
| 348 |
1 |
| separate_body = 0; |
| 349 |
1 |
| break; |
| 350 |
|
| |
| 351 |
0 |
| default: |
| 352 |
0 |
| return -EINVAL; |
| 353 |
|
| } |
| 354 |
|
| |
| 355 |
92 |
▶ | E_32(state->outbound_buffer, 3, *payload_len); |
Branch: 1 branch
| Branch | True | False | Total |
| E_32(state->outbound_buffer, 3, *payload_len); |
0 |
92 |
92 |
|
| 356 |
92 |
▶ | if (!separate_body) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!separate_body) { |
92 |
0 |
92 |
|
| 357 |
92 |
▶ | E_8(state->outbound_buffer, *payload_len + HEADER_SIZE, AMQP_FRAME_END); |
Branch: 1 branch
| Branch | True | False | Total |
| E_8(state->outbound_buffer, *payload_len + HEADER_SIZE, AMQP_FRAME_END); |
0 |
92 |
92 |
|
| 358 |
|
| } |
| 359 |
|
| |
| 360 |
|
| #if 0 |
| 361 |
|
| if (separate_body) { |
| 362 |
|
| printf("sending body frame (header):\n"); |
| 363 |
|
| amqp_dump(state->outbound_buffer.bytes, HEADER_SIZE); |
| 364 |
|
| printf("sending body frame (payload):\n"); |
| 365 |
|
| amqp_dump(encoded->bytes, *payload_len); |
| 366 |
|
| } else { |
| 367 |
|
| printf("sending:\n"); |
| 368 |
|
| amqp_dump(state->outbound_buffer.bytes, *payload_len + HEADER_SIZE + FOOTER_SIZE); |
| 369 |
|
| } |
| 370 |
|
| #endif |
| 371 |
|
| |
| 372 |
92 |
▶ | if(state->heartbeat) gettimeofday(&state->last_send, NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| if(state->heartbeat) gettimeofday(&state->last_send, NULL); |
0 |
92 |
92 |
|
| 373 |
92 |
| return separate_body; |
| 374 |
|
| } |
| 375 |
|
| |
| 376 |
92 |
| int amqp_send_frame(amqp_connection_state_t state, |
| 377 |
|
| amqp_frame_t const *frame) |
| 378 |
|
| { |
| 379 |
|
| amqp_bytes_t encoded; |
| 380 |
|
| int payload_len; |
| 381 |
|
| int separate_body; |
| 382 |
|
| |
| 383 |
92 |
| separate_body = inner_send_frame(state, frame, &encoded, &payload_len); |
| 384 |
92 |
| switch (separate_body) { |
| 385 |
92 |
| case 0: |
| 386 |
92 |
▶ | AMQP_CHECK_RESULT(write(state->sockfd, |
Branch: 1 branch
| Branch | True | False | Total |
| AMQP_CHECK_RESULT(write(state->sockfd, |
92 |
0 |
92 |
|
| 387 |
|
| state->outbound_buffer.bytes, |
| 388 |
|
| payload_len + (HEADER_SIZE + FOOTER_SIZE))); |
| 389 |
0 |
| return 0; |
| 390 |
|
| |
| 391 |
0 |
| case 1: |
| 392 |
0 |
▶ | AMQP_CHECK_RESULT(write(state->sockfd, state->outbound_buffer.bytes, HEADER_SIZE)); |
Branch: 1 branch
| Branch | True | False | Total |
| AMQP_CHECK_RESULT(write(state->sockfd, state->outbound_buffer.bytes, HEADER_SIZE)); |
0 |
0 |
0 |
|
| 393 |
0 |
▶ | AMQP_CHECK_RESULT(write(state->sockfd, encoded.bytes, payload_len)); |
Branch: 1 branch
| Branch | True | False | Total |
| AMQP_CHECK_RESULT(write(state->sockfd, encoded.bytes, payload_len)); |
0 |
0 |
0 |
|
| 394 |
|
| { |
| 395 |
0 |
| unsigned char frame_end_byte = AMQP_FRAME_END; |
| 396 |
|
| assert(FOOTER_SIZE == 1); |
| 397 |
0 |
▶ | AMQP_CHECK_RESULT(write(state->sockfd, &frame_end_byte, FOOTER_SIZE)); |
Branch: 1 branch
| Branch | True | False | Total |
| AMQP_CHECK_RESULT(write(state->sockfd, &frame_end_byte, FOOTER_SIZE)); |
0 |
0 |
0 |
|
| 398 |
|
| } |
| 399 |
0 |
| return 0; |
| 400 |
|
| |
| 401 |
0 |
| default: |
| 402 |
0 |
| return separate_body; |
| 403 |
|
| } |
| 404 |
|
| } |
| 405 |
|
| |
| 406 |
0 |
| int amqp_send_frame_to(amqp_connection_state_t state, |
| 407 |
|
| amqp_frame_t const *frame, |
| 408 |
|
| amqp_output_fn_t fn, |
| 409 |
|
| void *context) |
| 410 |
|
| { |
| 411 |
|
| amqp_bytes_t encoded; |
| 412 |
|
| int payload_len; |
| 413 |
|
| int separate_body; |
| 414 |
|
| |
| 415 |
0 |
| separate_body = inner_send_frame(state, frame, &encoded, &payload_len); |
| 416 |
0 |
| switch (separate_body) { |
| 417 |
0 |
| case 0: |
| 418 |
0 |
▶ | AMQP_CHECK_RESULT(fn(context, |
Branch: 1 branch
| Branch | True | False | Total |
| AMQP_CHECK_RESULT(fn(context, |
0 |
0 |
0 |
|
| 419 |
|
| state->outbound_buffer.bytes, |
| 420 |
|
| payload_len + (HEADER_SIZE + FOOTER_SIZE))); |
| 421 |
0 |
| return 0; |
| 422 |
|
| |
| 423 |
0 |
| case 1: |
| 424 |
0 |
▶ | AMQP_CHECK_RESULT(fn(context, state->outbound_buffer.bytes, HEADER_SIZE)); |
Branch: 1 branch
| Branch | True | False | Total |
| AMQP_CHECK_RESULT(fn(context, state->outbound_buffer.bytes, HEADER_SIZE)); |
0 |
0 |
0 |
|
| 425 |
0 |
▶ | AMQP_CHECK_RESULT(fn(context, encoded.bytes, payload_len)); |
Branch: 1 branch
| Branch | True | False | Total |
| AMQP_CHECK_RESULT(fn(context, encoded.bytes, payload_len)); |
0 |
0 |
0 |
|
| 426 |
|
| { |
| 427 |
0 |
| unsigned char frame_end_byte = AMQP_FRAME_END; |
| 428 |
|
| assert(FOOTER_SIZE == 1); |
| 429 |
0 |
▶ | AMQP_CHECK_RESULT(fn(context, &frame_end_byte, FOOTER_SIZE)); |
Branch: 1 branch
| Branch | True | False | Total |
| AMQP_CHECK_RESULT(fn(context, &frame_end_byte, FOOTER_SIZE)); |
0 |
0 |
0 |
|
| 430 |
|
| } |
| 431 |
0 |
| return 0; |
| 432 |
|
| |
| 433 |
0 |
| default: |
| 434 |
0 |
| return separate_body; |
| 435 |
|
| } |
| 436 |
|
| } |