amqp_connection_state_t amqp_new_connection(void) { amqp_connection_state_t state = (amqp_connection_state_t) calloc(1, sizeof(struct amqp_connection_state_t_)); if (state == NULL) { return NULL; } init_amqp_pool(&state->frame_pool, INITIAL_FRAME_POOL_PAGE_SIZE); init_amqp_pool(&state->decoding_pool, INITIAL_DECODING_POOL_PAGE_SIZE); state->state = CONNECTION_STATE_IDLE; state->inbound_buffer.bytes = NULL; state->outbound_buffer.bytes = NULL; if (amqp_tune_connection(state, 0, INITIAL_FRAME_POOL_PAGE_SIZE, 0) != 0) { empty_amqp_pool(&state->frame_pool); empty_amqp_pool(&state->decoding_pool); free(state); return NULL; } state->inbound_offset = 0; state->target_size = HEADER_SIZE; state->sockfd = -1; state->sock_inbound_buffer.len = INITIAL_INBOUND_SOCK_BUFFER_SIZE; state->sock_inbound_buffer.bytes = malloc(INITIAL_INBOUND_SOCK_BUFFER_SIZE); if (state->sock_inbound_buffer.bytes == NULL) { amqp_destroy_connection(state); return NULL; } state->sock_inbound_offset = 0; state->sock_inbound_limit = 0; state->first_queued_frame = NULL; state->last_queued_frame = NULL; return state; }
amqp_connection_state_t amqp_new_connection(void) { int res; amqp_connection_state_t state = (amqp_connection_state_t) calloc(1, sizeof(struct amqp_connection_state_t_)); if (state == NULL) { return NULL; } res = amqp_tune_connection(state, 0, AMQP_INITIAL_FRAME_POOL_PAGE_SIZE, 0); if (0 != res) { goto out_nomem; } state->inbound_buffer.bytes = state->header_buffer; state->inbound_buffer.len = sizeof(state->header_buffer); state->state = CONNECTION_STATE_INITIAL; /* the server protocol version response is 8 bytes, which conveniently is also the minimum frame size */ state->target_size = 8; state->sock_inbound_buffer.len = AMQP_INITIAL_INBOUND_SOCK_BUFFER_SIZE; state->sock_inbound_buffer.bytes = malloc(AMQP_INITIAL_INBOUND_SOCK_BUFFER_SIZE); if (state->sock_inbound_buffer.bytes == NULL) { goto out_nomem; } init_amqp_pool(&state->properties_pool, 512); return state; out_nomem: free(state->sock_inbound_buffer.bytes); free(state); return NULL; }
amqp_connection_state_t amqp_new_connection(void) { amqp_connection_state_t state = (amqp_connection_state_t) calloc(1, sizeof(struct amqp_connection_state_t_)); if (state == NULL) return NULL; init_amqp_pool(&state->frame_pool, INITIAL_FRAME_POOL_PAGE_SIZE); init_amqp_pool(&state->decoding_pool, INITIAL_DECODING_POOL_PAGE_SIZE); if (amqp_tune_connection(state, 0, INITIAL_FRAME_POOL_PAGE_SIZE, 0) != 0) goto out_nomem; state->inbound_buffer.bytes = amqp_pool_alloc(&state->frame_pool, state->inbound_buffer.len); if (state->inbound_buffer.bytes == NULL) goto out_nomem; state->state = CONNECTION_STATE_INITIAL; /* the server protocol version response is 8 bytes, which conveniently is also the minimum frame size */ state->target_size = 8; state->sockfd = -1; state->sock_inbound_buffer.len = INITIAL_INBOUND_SOCK_BUFFER_SIZE; state->sock_inbound_buffer.bytes = malloc(INITIAL_INBOUND_SOCK_BUFFER_SIZE); if (state->sock_inbound_buffer.bytes == NULL) goto out_nomem; return state; out_nomem: free(state->sock_inbound_buffer.bytes); empty_amqp_pool(&state->frame_pool); empty_amqp_pool(&state->decoding_pool); free(state); return NULL; }
static int amqp_login_inner(amqp_connection_state_t state, int channel_max, int frame_max, int heartbeat, amqp_sasl_method_enum sasl_method, va_list vl) { int res; amqp_method_t method; int server_frame_max; uint16_t server_channel_max; uint16_t server_heartbeat; amqp_send_header(state); res = amqp_simple_wait_method(state, 0, AMQP_CONNECTION_START_METHOD, &method); if (res < 0) return res; { amqp_connection_start_t *s = (amqp_connection_start_t *) method.decoded; if ((s->version_major != AMQP_PROTOCOL_VERSION_MAJOR) || (s->version_minor != AMQP_PROTOCOL_VERSION_MINOR)) { return -ERROR_INCOMPATIBLE_AMQP_VERSION; } /* TODO: check that our chosen SASL mechanism is in the list of acceptable mechanisms. Or even let the application choose from the list! */ } { amqp_table_entry_t properties[2]; amqp_connection_start_ok_t s; amqp_bytes_t response_bytes = sasl_response(&state->decoding_pool, sasl_method, vl); if (response_bytes.bytes == NULL) return -ERROR_NO_MEMORY; properties[0].key = amqp_cstring_bytes("product"); properties[0].value.kind = AMQP_FIELD_KIND_UTF8; properties[0].value.value.bytes = amqp_cstring_bytes("rabbitmq-c"); properties[1].key = amqp_cstring_bytes("information"); properties[1].value.kind = AMQP_FIELD_KIND_UTF8; properties[1].value.value.bytes = amqp_cstring_bytes("See http://hg.rabbitmq.com/rabbitmq-c/"); s.client_properties.num_entries = 2; s.client_properties.entries = properties; s.mechanism = sasl_method_name(sasl_method); s.response = response_bytes; s.locale.bytes = "en_US"; s.locale.len = 5; res = amqp_send_method(state, 0, AMQP_CONNECTION_START_OK_METHOD, &s); if (res < 0) return res; } amqp_release_buffers(state); res = amqp_simple_wait_method(state, 0, AMQP_CONNECTION_TUNE_METHOD, &method); if (res < 0) return res; { amqp_connection_tune_t *s = (amqp_connection_tune_t *) method.decoded; server_channel_max = s->channel_max; server_frame_max = s->frame_max; server_heartbeat = s->heartbeat; } if (server_channel_max != 0 && server_channel_max < channel_max) channel_max = server_channel_max; if (server_frame_max != 0 && server_frame_max < frame_max) frame_max = server_frame_max; if (server_heartbeat != 0 && server_heartbeat < heartbeat) heartbeat = server_heartbeat; res = amqp_tune_connection(state, channel_max, frame_max, heartbeat); if (res < 0) return res; { amqp_connection_tune_ok_t s; s.frame_max = frame_max; s.channel_max = channel_max; s.heartbeat = heartbeat; res = amqp_send_method(state, 0, AMQP_CONNECTION_TUNE_OK_METHOD, &s); if (res < 0) return res; } amqp_release_buffers(state); return 0; }