A small memory footprint AMQP implimentation
Dependents: iothub_client_sample_amqp remote_monitoring simplesample_amqp
connection.c@17:923575db8b2d, 2017-01-24 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Tue Jan 24 15:23:52 2017 -0800
- Revision:
- 17:923575db8b2d
- Parent:
- 16:22a72cf8e416
- Child:
- 19:000ab4e6a2c1
1.1.5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Azure.IoT Build | 0:6ae2f7bca550 | 1 | // Copyright (c) Microsoft. All rights reserved. |
Azure.IoT Build | 0:6ae2f7bca550 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
Azure.IoT Build | 0:6ae2f7bca550 | 3 | |
Azure.IoT Build | 0:6ae2f7bca550 | 4 | #include <stdlib.h> |
Azure.IoT Build | 0:6ae2f7bca550 | 5 | #include <string.h> |
Azure.IoT Build | 0:6ae2f7bca550 | 6 | |
Azure.IoT Build | 0:6ae2f7bca550 | 7 | #include "azure_c_shared_utility/xio.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 8 | #include "azure_c_shared_utility/xlogging.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 9 | #include "azure_c_shared_utility/tickcounter.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 10 | |
Azure.IoT Build | 0:6ae2f7bca550 | 11 | #include "azure_uamqp_c/connection.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 12 | #include "azure_uamqp_c/frame_codec.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 13 | #include "azure_uamqp_c/amqp_frame_codec.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 14 | #include "azure_uamqp_c/amqp_definitions.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 15 | #include "azure_uamqp_c/amqpalloc.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 16 | #include "azure_uamqp_c/amqpvalue_to_string.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 17 | |
Azure.IoT Build | 0:6ae2f7bca550 | 18 | /* Requirements satisfied by the virtue of implementing the ISO:*/ |
Azure.IoT Build | 0:6ae2f7bca550 | 19 | /* Codes_SRS_CONNECTION_01_088: [Any data appearing beyond the protocol header MUST match the version indicated by the protocol header.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 20 | /* Codes_SRS_CONNECTION_01_015: [Implementations SHOULD NOT expect to be able to reuse open TCP sockets after close performatives have been exchanged.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 21 | |
AzureIoTClient | 16:22a72cf8e416 | 22 | /* Codes_SRS_CONNECTION_01_087: [The protocol header consists of the upper case ASCII letters "AMQP" followed by a protocol id of zero, followed by three unsigned bytes representing the major, minor, and revision of the protocol version (currently 1 (MAJOR), 0 (MINOR), 0 (REVISION)). In total this is an 8-octet sequence] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 23 | static const unsigned char amqp_header[] = { 'A', 'M', 'Q', 'P', 0, 1, 0, 0 }; |
Azure.IoT Build | 0:6ae2f7bca550 | 24 | |
Azure.IoT Build | 0:6ae2f7bca550 | 25 | typedef enum RECEIVE_FRAME_STATE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 26 | { |
AzureIoTClient | 4:98007eb79fa8 | 27 | RECEIVE_FRAME_STATE_FRAME_SIZE, |
AzureIoTClient | 4:98007eb79fa8 | 28 | RECEIVE_FRAME_STATE_FRAME_DATA |
Azure.IoT Build | 0:6ae2f7bca550 | 29 | } RECEIVE_FRAME_STATE; |
Azure.IoT Build | 0:6ae2f7bca550 | 30 | |
Azure.IoT Build | 0:6ae2f7bca550 | 31 | typedef struct ENDPOINT_INSTANCE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 32 | { |
AzureIoTClient | 4:98007eb79fa8 | 33 | uint16_t incoming_channel; |
AzureIoTClient | 4:98007eb79fa8 | 34 | uint16_t outgoing_channel; |
AzureIoTClient | 4:98007eb79fa8 | 35 | ON_ENDPOINT_FRAME_RECEIVED on_endpoint_frame_received; |
AzureIoTClient | 4:98007eb79fa8 | 36 | ON_CONNECTION_STATE_CHANGED on_connection_state_changed; |
AzureIoTClient | 4:98007eb79fa8 | 37 | void* callback_context; |
AzureIoTClient | 4:98007eb79fa8 | 38 | CONNECTION_HANDLE connection; |
Azure.IoT Build | 0:6ae2f7bca550 | 39 | } ENDPOINT_INSTANCE; |
Azure.IoT Build | 0:6ae2f7bca550 | 40 | |
Azure.IoT Build | 0:6ae2f7bca550 | 41 | typedef struct CONNECTION_INSTANCE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 42 | { |
AzureIoTClient | 4:98007eb79fa8 | 43 | XIO_HANDLE io; |
AzureIoTClient | 4:98007eb79fa8 | 44 | size_t header_bytes_received; |
AzureIoTClient | 4:98007eb79fa8 | 45 | CONNECTION_STATE connection_state; |
AzureIoTClient | 4:98007eb79fa8 | 46 | FRAME_CODEC_HANDLE frame_codec; |
AzureIoTClient | 4:98007eb79fa8 | 47 | AMQP_FRAME_CODEC_HANDLE amqp_frame_codec; |
AzureIoTClient | 4:98007eb79fa8 | 48 | ENDPOINT_INSTANCE** endpoints; |
AzureIoTClient | 4:98007eb79fa8 | 49 | uint32_t endpoint_count; |
AzureIoTClient | 4:98007eb79fa8 | 50 | char* host_name; |
AzureIoTClient | 4:98007eb79fa8 | 51 | char* container_id; |
AzureIoTClient | 4:98007eb79fa8 | 52 | TICK_COUNTER_HANDLE tick_counter; |
AzureIoTClient | 4:98007eb79fa8 | 53 | uint32_t remote_max_frame_size; |
Azure.IoT Build | 0:6ae2f7bca550 | 54 | |
AzureIoTClient | 4:98007eb79fa8 | 55 | ON_SEND_COMPLETE on_send_complete; |
AzureIoTClient | 4:98007eb79fa8 | 56 | void* on_send_complete_callback_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 57 | |
AzureIoTClient | 4:98007eb79fa8 | 58 | ON_NEW_ENDPOINT on_new_endpoint; |
AzureIoTClient | 4:98007eb79fa8 | 59 | void* on_new_endpoint_callback_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 60 | |
Azure.IoT Build | 0:6ae2f7bca550 | 61 | ON_CONNECTION_STATE_CHANGED on_connection_state_changed; |
Azure.IoT Build | 0:6ae2f7bca550 | 62 | void* on_connection_state_changed_callback_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 63 | ON_IO_ERROR on_io_error; |
Azure.IoT Build | 0:6ae2f7bca550 | 64 | void* on_io_error_callback_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 65 | |
AzureIoTClient | 4:98007eb79fa8 | 66 | /* options */ |
AzureIoTClient | 4:98007eb79fa8 | 67 | uint32_t max_frame_size; |
AzureIoTClient | 4:98007eb79fa8 | 68 | uint16_t channel_max; |
AzureIoTClient | 4:98007eb79fa8 | 69 | milliseconds idle_timeout; |
AzureIoTClient | 4:98007eb79fa8 | 70 | milliseconds remote_idle_timeout; |
Azure.IoT.Build | 14:0b0e28c75ded | 71 | tickcounter_ms_t last_frame_received_time; |
Azure.IoT.Build | 14:0b0e28c75ded | 72 | tickcounter_ms_t last_frame_sent_time; |
Azure.IoT Build | 0:6ae2f7bca550 | 73 | |
AzureIoTClient | 4:98007eb79fa8 | 74 | unsigned int is_underlying_io_open : 1; |
AzureIoTClient | 4:98007eb79fa8 | 75 | unsigned int idle_timeout_specified : 1; |
AzureIoTClient | 4:98007eb79fa8 | 76 | unsigned int is_remote_frame_received : 1; |
AzureIoTClient | 4:98007eb79fa8 | 77 | unsigned int is_trace_on : 1; |
Azure.IoT Build | 0:6ae2f7bca550 | 78 | } CONNECTION_INSTANCE; |
Azure.IoT Build | 0:6ae2f7bca550 | 79 | |
Azure.IoT Build | 0:6ae2f7bca550 | 80 | /* Codes_SRS_CONNECTION_01_258: [on_connection_state_changed shall be invoked whenever the connection state changes.]*/ |
Azure.IoT Build | 0:6ae2f7bca550 | 81 | static void connection_set_state(CONNECTION_INSTANCE* connection_instance, CONNECTION_STATE connection_state) |
Azure.IoT Build | 0:6ae2f7bca550 | 82 | { |
AzureIoTClient | 4:98007eb79fa8 | 83 | uint64_t i; |
Azure.IoT Build | 0:6ae2f7bca550 | 84 | |
AzureIoTClient | 4:98007eb79fa8 | 85 | CONNECTION_STATE previous_state = connection_instance->connection_state; |
AzureIoTClient | 4:98007eb79fa8 | 86 | connection_instance->connection_state = connection_state; |
Azure.IoT Build | 0:6ae2f7bca550 | 87 | |
Azure.IoT Build | 0:6ae2f7bca550 | 88 | /* Codes_SRS_CONNECTION_22_001: [If a connection state changed occurs and a callback is registered the callback shall be called.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 89 | if (connection_instance->on_connection_state_changed) |
Azure.IoT Build | 0:6ae2f7bca550 | 90 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 91 | connection_instance->on_connection_state_changed(connection_instance->on_connection_state_changed_callback_context, connection_state, previous_state); |
Azure.IoT Build | 0:6ae2f7bca550 | 92 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 93 | |
AzureIoTClient | 16:22a72cf8e416 | 94 | /* Codes_SRS_CONNECTION_01_260: [Each endpoint's on_connection_state_changed shall be called.] */ |
AzureIoTClient | 4:98007eb79fa8 | 95 | for (i = 0; i < connection_instance->endpoint_count; i++) |
AzureIoTClient | 4:98007eb79fa8 | 96 | { |
AzureIoTClient | 4:98007eb79fa8 | 97 | /* Codes_SRS_CONNECTION_01_259: [The callback_context passed in connection_create_endpoint.] */ |
AzureIoTClient | 4:98007eb79fa8 | 98 | connection_instance->endpoints[i]->on_connection_state_changed(connection_instance->endpoints[i]->callback_context, connection_state, previous_state); |
AzureIoTClient | 4:98007eb79fa8 | 99 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 100 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 101 | |
Azure.IoT Build | 0:6ae2f7bca550 | 102 | static int send_header(CONNECTION_INSTANCE* connection_instance) |
Azure.IoT Build | 0:6ae2f7bca550 | 103 | { |
AzureIoTClient | 4:98007eb79fa8 | 104 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 105 | |
AzureIoTClient | 16:22a72cf8e416 | 106 | /* Codes_SRS_CONNECTION_01_093: [_ When the client opens a new socket connection to a server, it MUST send a protocol header with the client's preferred protocol version.] */ |
AzureIoTClient | 4:98007eb79fa8 | 107 | /* Codes_SRS_CONNECTION_01_104: [Sending the protocol header shall be done by using xio_send.] */ |
AzureIoTClient | 4:98007eb79fa8 | 108 | if (xio_send(connection_instance->io, amqp_header, sizeof(amqp_header), NULL, NULL) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 109 | { |
AzureIoTClient | 4:98007eb79fa8 | 110 | /* Codes_SRS_CONNECTION_01_106: [When sending the protocol header fails, the connection shall be immediately closed.] */ |
AzureIoTClient | 4:98007eb79fa8 | 111 | xio_close(connection_instance->io, NULL, NULL); |
Azure.IoT Build | 0:6ae2f7bca550 | 112 | |
AzureIoTClient | 4:98007eb79fa8 | 113 | /* Codes_SRS_CONNECTION_01_057: [END In this state it is illegal for either endpoint to write anything more onto the connection. The connection can be safely closed and discarded.] */ |
AzureIoTClient | 4:98007eb79fa8 | 114 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
Azure.IoT Build | 0:6ae2f7bca550 | 115 | |
AzureIoTClient | 4:98007eb79fa8 | 116 | /* Codes_SRS_CONNECTION_01_105: [When xio_send fails, connection_dowork shall return a non-zero value.] */ |
AzureIoTClient | 4:98007eb79fa8 | 117 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 118 | } |
AzureIoTClient | 4:98007eb79fa8 | 119 | else |
AzureIoTClient | 4:98007eb79fa8 | 120 | { |
AzureIoTClient | 4:98007eb79fa8 | 121 | if (connection_instance->is_trace_on == 1) |
AzureIoTClient | 4:98007eb79fa8 | 122 | { |
AzureIoTClient | 16:22a72cf8e416 | 123 | LOG(AZ_LOG_TRACE, LOG_LINE, "-> Header (AMQP 0.1.0.0)"); |
AzureIoTClient | 4:98007eb79fa8 | 124 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 125 | |
AzureIoTClient | 4:98007eb79fa8 | 126 | /* Codes_SRS_CONNECTION_01_041: [HDR SENT In this state the connection header has been sent to the peer but no connection header has been received.] */ |
AzureIoTClient | 4:98007eb79fa8 | 127 | connection_set_state(connection_instance, CONNECTION_STATE_HDR_SENT); |
AzureIoTClient | 4:98007eb79fa8 | 128 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 129 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 130 | |
AzureIoTClient | 4:98007eb79fa8 | 131 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 132 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 133 | |
Azure.IoT Build | 0:6ae2f7bca550 | 134 | static const char* get_frame_type_as_string(AMQP_VALUE descriptor) |
Azure.IoT Build | 0:6ae2f7bca550 | 135 | { |
AzureIoTClient | 4:98007eb79fa8 | 136 | const char* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 137 | |
AzureIoTClient | 4:98007eb79fa8 | 138 | if (is_open_type_by_descriptor(descriptor)) |
AzureIoTClient | 4:98007eb79fa8 | 139 | { |
AzureIoTClient | 4:98007eb79fa8 | 140 | result = "[OPEN]"; |
AzureIoTClient | 4:98007eb79fa8 | 141 | } |
AzureIoTClient | 4:98007eb79fa8 | 142 | else if (is_begin_type_by_descriptor(descriptor)) |
AzureIoTClient | 4:98007eb79fa8 | 143 | { |
AzureIoTClient | 4:98007eb79fa8 | 144 | result = "[BEGIN]"; |
AzureIoTClient | 4:98007eb79fa8 | 145 | } |
AzureIoTClient | 4:98007eb79fa8 | 146 | else if (is_attach_type_by_descriptor(descriptor)) |
AzureIoTClient | 4:98007eb79fa8 | 147 | { |
AzureIoTClient | 4:98007eb79fa8 | 148 | result = "[ATTACH]"; |
AzureIoTClient | 4:98007eb79fa8 | 149 | } |
AzureIoTClient | 4:98007eb79fa8 | 150 | else if (is_flow_type_by_descriptor(descriptor)) |
AzureIoTClient | 4:98007eb79fa8 | 151 | { |
AzureIoTClient | 4:98007eb79fa8 | 152 | result = "[FLOW]"; |
AzureIoTClient | 4:98007eb79fa8 | 153 | } |
AzureIoTClient | 4:98007eb79fa8 | 154 | else if (is_disposition_type_by_descriptor(descriptor)) |
AzureIoTClient | 4:98007eb79fa8 | 155 | { |
AzureIoTClient | 4:98007eb79fa8 | 156 | result = "[DISPOSITION]"; |
AzureIoTClient | 4:98007eb79fa8 | 157 | } |
AzureIoTClient | 4:98007eb79fa8 | 158 | else if (is_transfer_type_by_descriptor(descriptor)) |
AzureIoTClient | 4:98007eb79fa8 | 159 | { |
AzureIoTClient | 4:98007eb79fa8 | 160 | result = "[TRANSFER]"; |
AzureIoTClient | 4:98007eb79fa8 | 161 | } |
AzureIoTClient | 4:98007eb79fa8 | 162 | else if (is_detach_type_by_descriptor(descriptor)) |
AzureIoTClient | 4:98007eb79fa8 | 163 | { |
AzureIoTClient | 4:98007eb79fa8 | 164 | result = "[DETACH]"; |
AzureIoTClient | 4:98007eb79fa8 | 165 | } |
AzureIoTClient | 4:98007eb79fa8 | 166 | else if (is_end_type_by_descriptor(descriptor)) |
AzureIoTClient | 4:98007eb79fa8 | 167 | { |
AzureIoTClient | 4:98007eb79fa8 | 168 | result = "[END]"; |
AzureIoTClient | 4:98007eb79fa8 | 169 | } |
AzureIoTClient | 4:98007eb79fa8 | 170 | else if (is_close_type_by_descriptor(descriptor)) |
AzureIoTClient | 4:98007eb79fa8 | 171 | { |
AzureIoTClient | 4:98007eb79fa8 | 172 | result = "[CLOSE]"; |
AzureIoTClient | 4:98007eb79fa8 | 173 | } |
AzureIoTClient | 4:98007eb79fa8 | 174 | else |
AzureIoTClient | 4:98007eb79fa8 | 175 | { |
AzureIoTClient | 4:98007eb79fa8 | 176 | result = "[Unknown]"; |
AzureIoTClient | 4:98007eb79fa8 | 177 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 178 | |
AzureIoTClient | 4:98007eb79fa8 | 179 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 180 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 181 | |
Azure.IoT Build | 5:ae49385aff34 | 182 | static void log_incoming_frame(AMQP_VALUE performative) |
Azure.IoT Build | 0:6ae2f7bca550 | 183 | { |
AzureIoTClient | 9:c22db038556c | 184 | #ifdef NO_LOGGING |
AzureIoTClient | 9:c22db038556c | 185 | UNUSED(performative); |
AzureIoTClient | 9:c22db038556c | 186 | #else |
AzureIoTClient | 4:98007eb79fa8 | 187 | AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(performative); |
AzureIoTClient | 4:98007eb79fa8 | 188 | if (descriptor != NULL) |
AzureIoTClient | 4:98007eb79fa8 | 189 | { |
AzureIoTClient | 16:22a72cf8e416 | 190 | LOG(AZ_LOG_TRACE, 0, "<- "); |
AzureIoTClient | 16:22a72cf8e416 | 191 | LOG(AZ_LOG_TRACE, 0, (char*)get_frame_type_as_string(descriptor)); |
AzureIoTClient | 4:98007eb79fa8 | 192 | char* performative_as_string = NULL; |
AzureIoTClient | 16:22a72cf8e416 | 193 | LOG(AZ_LOG_TRACE, LOG_LINE, (performative_as_string = amqpvalue_to_string(performative))); |
AzureIoTClient | 4:98007eb79fa8 | 194 | if (performative_as_string != NULL) |
AzureIoTClient | 4:98007eb79fa8 | 195 | { |
AzureIoTClient | 4:98007eb79fa8 | 196 | amqpalloc_free(performative_as_string); |
AzureIoTClient | 4:98007eb79fa8 | 197 | } |
AzureIoTClient | 4:98007eb79fa8 | 198 | } |
AzureIoTClient | 9:c22db038556c | 199 | #endif |
Azure.IoT Build | 0:6ae2f7bca550 | 200 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 201 | |
Azure.IoT Build | 5:ae49385aff34 | 202 | static void log_outgoing_frame(AMQP_VALUE performative) |
Azure.IoT Build | 0:6ae2f7bca550 | 203 | { |
AzureIoTClient | 9:c22db038556c | 204 | #ifdef NO_LOGGING |
AzureIoTClient | 9:c22db038556c | 205 | UNUSED(performative); |
AzureIoTClient | 9:c22db038556c | 206 | #else |
AzureIoTClient | 4:98007eb79fa8 | 207 | AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(performative); |
AzureIoTClient | 4:98007eb79fa8 | 208 | if (descriptor != NULL) |
AzureIoTClient | 4:98007eb79fa8 | 209 | { |
AzureIoTClient | 16:22a72cf8e416 | 210 | LOG(AZ_LOG_TRACE, 0, "-> "); |
AzureIoTClient | 16:22a72cf8e416 | 211 | LOG(AZ_LOG_TRACE, 0, (char*)get_frame_type_as_string(descriptor)); |
AzureIoTClient | 4:98007eb79fa8 | 212 | char* performative_as_string = NULL; |
AzureIoTClient | 16:22a72cf8e416 | 213 | LOG(AZ_LOG_TRACE, LOG_LINE, (performative_as_string = amqpvalue_to_string(performative))); |
AzureIoTClient | 4:98007eb79fa8 | 214 | if (performative_as_string != NULL) |
AzureIoTClient | 4:98007eb79fa8 | 215 | { |
AzureIoTClient | 4:98007eb79fa8 | 216 | amqpalloc_free(performative_as_string); |
AzureIoTClient | 4:98007eb79fa8 | 217 | } |
AzureIoTClient | 4:98007eb79fa8 | 218 | } |
AzureIoTClient | 9:c22db038556c | 219 | #endif |
Azure.IoT Build | 0:6ae2f7bca550 | 220 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 221 | |
Azure.IoT Build | 0:6ae2f7bca550 | 222 | static void on_bytes_encoded(void* context, const unsigned char* bytes, size_t length, bool encode_complete) |
Azure.IoT Build | 0:6ae2f7bca550 | 223 | { |
AzureIoTClient | 4:98007eb79fa8 | 224 | CONNECTION_INSTANCE* connection_instance = (CONNECTION_INSTANCE*)context; |
AzureIoTClient | 4:98007eb79fa8 | 225 | if (xio_send(connection_instance->io, bytes, length, encode_complete ? connection_instance->on_send_complete : NULL, connection_instance->on_send_complete_callback_context) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 226 | { |
AzureIoTClient | 4:98007eb79fa8 | 227 | xio_close(connection_instance->io, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 228 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 229 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 230 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 231 | |
Azure.IoT Build | 0:6ae2f7bca550 | 232 | static int send_open_frame(CONNECTION_INSTANCE* connection_instance) |
Azure.IoT Build | 0:6ae2f7bca550 | 233 | { |
AzureIoTClient | 4:98007eb79fa8 | 234 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 235 | |
AzureIoTClient | 4:98007eb79fa8 | 236 | /* Codes_SRS_CONNECTION_01_151: [The connection max_frame_size setting shall be passed down to the frame_codec when the Open frame is sent.] */ |
AzureIoTClient | 4:98007eb79fa8 | 237 | if (frame_codec_set_max_frame_size(connection_instance->frame_codec, connection_instance->max_frame_size) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 238 | { |
AzureIoTClient | 4:98007eb79fa8 | 239 | /* Codes_SRS_CONNECTION_01_207: [If frame_codec_set_max_frame_size fails the connection shall be closed and the state set to END.] */ |
AzureIoTClient | 4:98007eb79fa8 | 240 | xio_close(connection_instance->io, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 241 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 242 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 243 | } |
AzureIoTClient | 4:98007eb79fa8 | 244 | else |
AzureIoTClient | 4:98007eb79fa8 | 245 | { |
AzureIoTClient | 4:98007eb79fa8 | 246 | /* Codes_SRS_CONNECTION_01_134: [The container id field shall be filled with the container id specified in connection_create.] */ |
AzureIoTClient | 4:98007eb79fa8 | 247 | OPEN_HANDLE open_performative = open_create(connection_instance->container_id); |
AzureIoTClient | 4:98007eb79fa8 | 248 | if (open_performative == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 249 | { |
AzureIoTClient | 4:98007eb79fa8 | 250 | /* Codes_SRS_CONNECTION_01_208: [If the open frame cannot be constructed, the connection shall be closed and set to the END state.] */ |
AzureIoTClient | 4:98007eb79fa8 | 251 | xio_close(connection_instance->io, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 252 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 253 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 254 | } |
AzureIoTClient | 4:98007eb79fa8 | 255 | else |
AzureIoTClient | 4:98007eb79fa8 | 256 | { |
AzureIoTClient | 4:98007eb79fa8 | 257 | /* Codes_SRS_CONNECTION_01_137: [The max_frame_size connection setting shall be set in the open frame by using open_set_max_frame_size.] */ |
AzureIoTClient | 4:98007eb79fa8 | 258 | if (open_set_max_frame_size(open_performative, connection_instance->max_frame_size) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 259 | { |
AzureIoTClient | 4:98007eb79fa8 | 260 | /* Codes_SRS_CONNECTION_01_208: [If the open frame cannot be constructed, the connection shall be closed and set to the END state.] */ |
AzureIoTClient | 4:98007eb79fa8 | 261 | xio_close(connection_instance->io, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 262 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 263 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 264 | } |
AzureIoTClient | 4:98007eb79fa8 | 265 | /* Codes_SRS_CONNECTION_01_139: [The channel_max connection setting shall be set in the open frame by using open_set_channel_max.] */ |
AzureIoTClient | 4:98007eb79fa8 | 266 | else if (open_set_channel_max(open_performative, connection_instance->channel_max) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 267 | { |
AzureIoTClient | 4:98007eb79fa8 | 268 | /* Codes_SRS_CONNECTION_01_208: [If the open frame cannot be constructed, the connection shall be closed and set to the END state.] */ |
AzureIoTClient | 4:98007eb79fa8 | 269 | xio_close(connection_instance->io, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 270 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 271 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 272 | } |
AzureIoTClient | 4:98007eb79fa8 | 273 | /* Codes_SRS_CONNECTION_01_142: [If no idle_timeout value has been specified, no value shall be stamped in the open frame (no call to open_set_idle_time_out shall be made).] */ |
AzureIoTClient | 4:98007eb79fa8 | 274 | else if ((connection_instance->idle_timeout_specified) && |
AzureIoTClient | 4:98007eb79fa8 | 275 | /* Codes_SRS_CONNECTION_01_141: [If idle_timeout has been specified by a call to connection_set_idle_timeout, then that value shall be stamped in the open frame.] */ |
AzureIoTClient | 4:98007eb79fa8 | 276 | (open_set_idle_time_out(open_performative, connection_instance->idle_timeout) != 0)) |
AzureIoTClient | 4:98007eb79fa8 | 277 | { |
AzureIoTClient | 4:98007eb79fa8 | 278 | /* Codes_SRS_CONNECTION_01_208: [If the open frame cannot be constructed, the connection shall be closed and set to the END state.] */ |
AzureIoTClient | 4:98007eb79fa8 | 279 | xio_close(connection_instance->io, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 280 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 281 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 282 | } |
AzureIoTClient | 4:98007eb79fa8 | 283 | /* Codes_SRS_CONNECTION_01_136: [If no hostname value has been specified, no value shall be stamped in the open frame (no call to open_set_hostname shall be made).] */ |
AzureIoTClient | 4:98007eb79fa8 | 284 | else if ((connection_instance->host_name != NULL) && |
AzureIoTClient | 4:98007eb79fa8 | 285 | /* Codes_SRS_CONNECTION_01_135: [If hostname has been specified by a call to connection_set_hostname, then that value shall be stamped in the open frame.] */ |
AzureIoTClient | 4:98007eb79fa8 | 286 | (open_set_hostname(open_performative, connection_instance->host_name) != 0)) |
AzureIoTClient | 4:98007eb79fa8 | 287 | { |
AzureIoTClient | 4:98007eb79fa8 | 288 | /* Codes_SRS_CONNECTION_01_208: [If the open frame cannot be constructed, the connection shall be closed and set to the END state.] */ |
AzureIoTClient | 4:98007eb79fa8 | 289 | xio_close(connection_instance->io, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 290 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 291 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 292 | } |
AzureIoTClient | 4:98007eb79fa8 | 293 | else |
AzureIoTClient | 4:98007eb79fa8 | 294 | { |
AzureIoTClient | 4:98007eb79fa8 | 295 | AMQP_VALUE open_performative_value = amqpvalue_create_open(open_performative); |
AzureIoTClient | 4:98007eb79fa8 | 296 | if (open_performative_value == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 297 | { |
AzureIoTClient | 4:98007eb79fa8 | 298 | /* Codes_SRS_CONNECTION_01_208: [If the open frame cannot be constructed, the connection shall be closed and set to the END state.] */ |
AzureIoTClient | 4:98007eb79fa8 | 299 | xio_close(connection_instance->io, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 300 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 301 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 302 | } |
AzureIoTClient | 4:98007eb79fa8 | 303 | else |
AzureIoTClient | 4:98007eb79fa8 | 304 | { |
AzureIoTClient | 4:98007eb79fa8 | 305 | /* Codes_SRS_CONNECTION_01_002: [Each AMQP connection begins with an exchange of capabilities and limitations, including the maximum frame size.] */ |
AzureIoTClient | 4:98007eb79fa8 | 306 | /* Codes_SRS_CONNECTION_01_004: [After establishing or accepting a TCP connection and sending the protocol header, each peer MUST send an open frame before sending any other frames.] */ |
AzureIoTClient | 4:98007eb79fa8 | 307 | /* Codes_SRS_CONNECTION_01_005: [The open frame describes the capabilities and limits of that peer.] */ |
AzureIoTClient | 4:98007eb79fa8 | 308 | /* Codes_SRS_CONNECTION_01_205: [Sending the AMQP OPEN frame shall be done by calling amqp_frame_codec_begin_encode_frame with channel number 0, the actual performative payload and 0 as payload_size.] */ |
AzureIoTClient | 4:98007eb79fa8 | 309 | /* Codes_SRS_CONNECTION_01_006: [The open frame can only be sent on channel 0.] */ |
AzureIoTClient | 4:98007eb79fa8 | 310 | connection_instance->on_send_complete = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 311 | connection_instance->on_send_complete_callback_context = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 312 | if (amqp_frame_codec_encode_frame(connection_instance->amqp_frame_codec, 0, open_performative_value, NULL, 0, on_bytes_encoded, connection_instance) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 313 | { |
AzureIoTClient | 4:98007eb79fa8 | 314 | /* Codes_SRS_CONNECTION_01_206: [If sending the frame fails, the connection shall be closed and state set to END.] */ |
AzureIoTClient | 4:98007eb79fa8 | 315 | xio_close(connection_instance->io, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 316 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 317 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 318 | } |
AzureIoTClient | 4:98007eb79fa8 | 319 | else |
AzureIoTClient | 4:98007eb79fa8 | 320 | { |
AzureIoTClient | 4:98007eb79fa8 | 321 | if (connection_instance->is_trace_on == 1) |
AzureIoTClient | 4:98007eb79fa8 | 322 | { |
Azure.IoT Build | 5:ae49385aff34 | 323 | log_outgoing_frame(open_performative_value); |
AzureIoTClient | 4:98007eb79fa8 | 324 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 325 | |
AzureIoTClient | 4:98007eb79fa8 | 326 | /* Codes_SRS_CONNECTION_01_046: [OPEN SENT In this state the connection headers have been exchanged. An open frame has been sent to the peer but no open frame has yet been received.] */ |
AzureIoTClient | 4:98007eb79fa8 | 327 | connection_set_state(connection_instance, CONNECTION_STATE_OPEN_SENT); |
AzureIoTClient | 4:98007eb79fa8 | 328 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 329 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 330 | |
AzureIoTClient | 4:98007eb79fa8 | 331 | amqpvalue_destroy(open_performative_value); |
AzureIoTClient | 4:98007eb79fa8 | 332 | } |
AzureIoTClient | 4:98007eb79fa8 | 333 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 334 | |
AzureIoTClient | 4:98007eb79fa8 | 335 | open_destroy(open_performative); |
AzureIoTClient | 4:98007eb79fa8 | 336 | } |
AzureIoTClient | 4:98007eb79fa8 | 337 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 338 | |
AzureIoTClient | 4:98007eb79fa8 | 339 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 340 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 341 | |
Azure.IoT Build | 0:6ae2f7bca550 | 342 | static int send_close_frame(CONNECTION_INSTANCE* connection_instance, ERROR_HANDLE error_handle) |
Azure.IoT Build | 0:6ae2f7bca550 | 343 | { |
AzureIoTClient | 4:98007eb79fa8 | 344 | int result; |
AzureIoTClient | 4:98007eb79fa8 | 345 | CLOSE_HANDLE close_performative; |
Azure.IoT Build | 0:6ae2f7bca550 | 346 | |
AzureIoTClient | 4:98007eb79fa8 | 347 | /* Codes_SRS_CONNECTION_01_217: [The CLOSE frame shall be constructed by using close_create.] */ |
AzureIoTClient | 4:98007eb79fa8 | 348 | close_performative = close_create(); |
AzureIoTClient | 4:98007eb79fa8 | 349 | if (close_performative == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 350 | { |
AzureIoTClient | 4:98007eb79fa8 | 351 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 352 | } |
AzureIoTClient | 4:98007eb79fa8 | 353 | else |
AzureIoTClient | 4:98007eb79fa8 | 354 | { |
AzureIoTClient | 4:98007eb79fa8 | 355 | if ((error_handle != NULL) && |
AzureIoTClient | 4:98007eb79fa8 | 356 | /* Codes_SRS_CONNECTION_01_238: [If set, this field indicates that the connection is being closed due to an error condition.] */ |
AzureIoTClient | 4:98007eb79fa8 | 357 | (close_set_error(close_performative, error_handle) != 0)) |
AzureIoTClient | 4:98007eb79fa8 | 358 | { |
AzureIoTClient | 4:98007eb79fa8 | 359 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 360 | } |
AzureIoTClient | 4:98007eb79fa8 | 361 | else |
AzureIoTClient | 4:98007eb79fa8 | 362 | { |
AzureIoTClient | 4:98007eb79fa8 | 363 | AMQP_VALUE close_performative_value = amqpvalue_create_close(close_performative); |
AzureIoTClient | 4:98007eb79fa8 | 364 | if (close_performative_value == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 365 | { |
AzureIoTClient | 4:98007eb79fa8 | 366 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 367 | } |
AzureIoTClient | 4:98007eb79fa8 | 368 | else |
AzureIoTClient | 4:98007eb79fa8 | 369 | { |
AzureIoTClient | 4:98007eb79fa8 | 370 | /* Codes_SRS_CONNECTION_01_215: [Sending the AMQP CLOSE frame shall be done by calling amqp_frame_codec_begin_encode_frame with channel number 0, the actual performative payload and 0 as payload_size.] */ |
AzureIoTClient | 4:98007eb79fa8 | 371 | /* Codes_SRS_CONNECTION_01_013: [However, implementations SHOULD send it on channel 0] */ |
AzureIoTClient | 4:98007eb79fa8 | 372 | connection_instance->on_send_complete = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 373 | connection_instance->on_send_complete_callback_context = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 374 | if (amqp_frame_codec_encode_frame(connection_instance->amqp_frame_codec, 0, close_performative_value, NULL, 0, on_bytes_encoded, connection_instance) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 375 | { |
AzureIoTClient | 4:98007eb79fa8 | 376 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 377 | } |
AzureIoTClient | 4:98007eb79fa8 | 378 | else |
AzureIoTClient | 4:98007eb79fa8 | 379 | { |
AzureIoTClient | 4:98007eb79fa8 | 380 | if (connection_instance->is_trace_on == 1) |
AzureIoTClient | 4:98007eb79fa8 | 381 | { |
Azure.IoT Build | 5:ae49385aff34 | 382 | log_outgoing_frame(close_performative_value); |
AzureIoTClient | 4:98007eb79fa8 | 383 | } |
Azure.IoT Build | 5:ae49385aff34 | 384 | |
AzureIoTClient | 4:98007eb79fa8 | 385 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 386 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 387 | |
AzureIoTClient | 4:98007eb79fa8 | 388 | amqpvalue_destroy(close_performative_value); |
AzureIoTClient | 4:98007eb79fa8 | 389 | } |
AzureIoTClient | 4:98007eb79fa8 | 390 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 391 | |
AzureIoTClient | 4:98007eb79fa8 | 392 | close_destroy(close_performative); |
AzureIoTClient | 4:98007eb79fa8 | 393 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 394 | |
AzureIoTClient | 4:98007eb79fa8 | 395 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 396 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 397 | |
Azure.IoT Build | 0:6ae2f7bca550 | 398 | static void close_connection_with_error(CONNECTION_INSTANCE* connection_instance, const char* condition_value, const char* description) |
Azure.IoT Build | 0:6ae2f7bca550 | 399 | { |
AzureIoTClient | 4:98007eb79fa8 | 400 | ERROR_HANDLE error_handle = error_create(condition_value); |
AzureIoTClient | 4:98007eb79fa8 | 401 | if (error_handle == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 402 | { |
AzureIoTClient | 4:98007eb79fa8 | 403 | /* Codes_SRS_CONNECTION_01_214: [If the close frame cannot be constructed or sent, the connection shall be closed and set to the END state.] */ |
AzureIoTClient | 4:98007eb79fa8 | 404 | (void)xio_close(connection_instance->io, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 405 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 406 | } |
AzureIoTClient | 4:98007eb79fa8 | 407 | else |
AzureIoTClient | 4:98007eb79fa8 | 408 | { |
AzureIoTClient | 4:98007eb79fa8 | 409 | /* Codes_SRS_CONNECTION_01_219: [The error description shall be set to an implementation defined string.] */ |
AzureIoTClient | 4:98007eb79fa8 | 410 | if ((error_set_description(error_handle, description) != 0) || |
AzureIoTClient | 4:98007eb79fa8 | 411 | (send_close_frame(connection_instance, error_handle) != 0)) |
AzureIoTClient | 4:98007eb79fa8 | 412 | { |
AzureIoTClient | 4:98007eb79fa8 | 413 | /* Codes_SRS_CONNECTION_01_214: [If the close frame cannot be constructed or sent, the connection shall be closed and set to the END state.] */ |
AzureIoTClient | 4:98007eb79fa8 | 414 | (void)xio_close(connection_instance->io, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 415 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 416 | } |
AzureIoTClient | 4:98007eb79fa8 | 417 | else |
AzureIoTClient | 4:98007eb79fa8 | 418 | { |
AzureIoTClient | 4:98007eb79fa8 | 419 | /* Codes_SRS_CONNECTION_01_213: [When passing the bytes to frame_codec fails, a CLOSE frame shall be sent and the state shall be set to DISCARDING.] */ |
AzureIoTClient | 4:98007eb79fa8 | 420 | /* Codes_SRS_CONNECTION_01_055: [DISCARDING The DISCARDING state is a variant of the CLOSE SENT state where the close is triggered by an error.] */ |
AzureIoTClient | 16:22a72cf8e416 | 421 | /* Codes_SRS_CONNECTION_01_010: [After writing this frame the peer SHOULD continue to read from the connection until it receives the partner's close frame ] */ |
AzureIoTClient | 4:98007eb79fa8 | 422 | connection_set_state(connection_instance, CONNECTION_STATE_DISCARDING); |
AzureIoTClient | 4:98007eb79fa8 | 423 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 424 | |
AzureIoTClient | 4:98007eb79fa8 | 425 | error_destroy(error_handle); |
AzureIoTClient | 4:98007eb79fa8 | 426 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 427 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 428 | |
Azure.IoT Build | 0:6ae2f7bca550 | 429 | static ENDPOINT_INSTANCE* find_session_endpoint_by_outgoing_channel(CONNECTION_INSTANCE* connection, uint16_t outgoing_channel) |
Azure.IoT Build | 0:6ae2f7bca550 | 430 | { |
AzureIoTClient | 4:98007eb79fa8 | 431 | uint32_t i; |
AzureIoTClient | 4:98007eb79fa8 | 432 | ENDPOINT_INSTANCE* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 433 | |
AzureIoTClient | 4:98007eb79fa8 | 434 | for (i = 0; i < connection->endpoint_count; i++) |
AzureIoTClient | 4:98007eb79fa8 | 435 | { |
AzureIoTClient | 4:98007eb79fa8 | 436 | if (connection->endpoints[i]->outgoing_channel == outgoing_channel) |
AzureIoTClient | 4:98007eb79fa8 | 437 | { |
AzureIoTClient | 4:98007eb79fa8 | 438 | break; |
AzureIoTClient | 4:98007eb79fa8 | 439 | } |
AzureIoTClient | 4:98007eb79fa8 | 440 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 441 | |
AzureIoTClient | 4:98007eb79fa8 | 442 | if (i == connection->endpoint_count) |
AzureIoTClient | 4:98007eb79fa8 | 443 | { |
AzureIoTClient | 4:98007eb79fa8 | 444 | result = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 445 | } |
AzureIoTClient | 4:98007eb79fa8 | 446 | else |
AzureIoTClient | 4:98007eb79fa8 | 447 | { |
AzureIoTClient | 4:98007eb79fa8 | 448 | result = connection->endpoints[i]; |
AzureIoTClient | 4:98007eb79fa8 | 449 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 450 | |
AzureIoTClient | 4:98007eb79fa8 | 451 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 452 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 453 | |
Azure.IoT Build | 0:6ae2f7bca550 | 454 | static ENDPOINT_INSTANCE* find_session_endpoint_by_incoming_channel(CONNECTION_INSTANCE* connection, uint16_t incoming_channel) |
Azure.IoT Build | 0:6ae2f7bca550 | 455 | { |
AzureIoTClient | 4:98007eb79fa8 | 456 | uint32_t i; |
AzureIoTClient | 4:98007eb79fa8 | 457 | ENDPOINT_INSTANCE* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 458 | |
AzureIoTClient | 4:98007eb79fa8 | 459 | for (i = 0; i < connection->endpoint_count; i++) |
AzureIoTClient | 4:98007eb79fa8 | 460 | { |
AzureIoTClient | 4:98007eb79fa8 | 461 | if (connection->endpoints[i]->incoming_channel == incoming_channel) |
AzureIoTClient | 4:98007eb79fa8 | 462 | { |
AzureIoTClient | 4:98007eb79fa8 | 463 | break; |
AzureIoTClient | 4:98007eb79fa8 | 464 | } |
AzureIoTClient | 4:98007eb79fa8 | 465 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 466 | |
AzureIoTClient | 4:98007eb79fa8 | 467 | if (i == connection->endpoint_count) |
AzureIoTClient | 4:98007eb79fa8 | 468 | { |
AzureIoTClient | 4:98007eb79fa8 | 469 | result = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 470 | } |
AzureIoTClient | 4:98007eb79fa8 | 471 | else |
AzureIoTClient | 4:98007eb79fa8 | 472 | { |
AzureIoTClient | 4:98007eb79fa8 | 473 | result = connection->endpoints[i]; |
AzureIoTClient | 4:98007eb79fa8 | 474 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 475 | |
AzureIoTClient | 4:98007eb79fa8 | 476 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 477 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 478 | |
Azure.IoT Build | 0:6ae2f7bca550 | 479 | static int connection_byte_received(CONNECTION_INSTANCE* connection_instance, unsigned char b) |
Azure.IoT Build | 0:6ae2f7bca550 | 480 | { |
AzureIoTClient | 4:98007eb79fa8 | 481 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 482 | |
AzureIoTClient | 4:98007eb79fa8 | 483 | switch (connection_instance->connection_state) |
AzureIoTClient | 4:98007eb79fa8 | 484 | { |
AzureIoTClient | 4:98007eb79fa8 | 485 | default: |
AzureIoTClient | 4:98007eb79fa8 | 486 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 487 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 488 | |
AzureIoTClient | 4:98007eb79fa8 | 489 | /* Codes_SRS_CONNECTION_01_039: [START In this state a connection exists, but nothing has been sent or received. This is the state an implementation would be in immediately after performing a socket connect or socket accept.] */ |
AzureIoTClient | 4:98007eb79fa8 | 490 | case CONNECTION_STATE_START: |
Azure.IoT Build | 0:6ae2f7bca550 | 491 | |
AzureIoTClient | 4:98007eb79fa8 | 492 | /* Codes_SRS_CONNECTION_01_041: [HDR SENT In this state the connection header has been sent to the peer but no connection header has been received.] */ |
AzureIoTClient | 4:98007eb79fa8 | 493 | case CONNECTION_STATE_HDR_SENT: |
AzureIoTClient | 4:98007eb79fa8 | 494 | if (b != amqp_header[connection_instance->header_bytes_received]) |
AzureIoTClient | 4:98007eb79fa8 | 495 | { |
AzureIoTClient | 4:98007eb79fa8 | 496 | /* Codes_SRS_CONNECTION_01_089: [If the incoming and outgoing protocol headers do not match, both peers MUST close their outgoing stream] */ |
AzureIoTClient | 4:98007eb79fa8 | 497 | xio_close(connection_instance->io, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 498 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 499 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 500 | } |
AzureIoTClient | 4:98007eb79fa8 | 501 | else |
AzureIoTClient | 4:98007eb79fa8 | 502 | { |
AzureIoTClient | 4:98007eb79fa8 | 503 | connection_instance->header_bytes_received++; |
AzureIoTClient | 4:98007eb79fa8 | 504 | if (connection_instance->header_bytes_received == sizeof(amqp_header)) |
AzureIoTClient | 4:98007eb79fa8 | 505 | { |
AzureIoTClient | 4:98007eb79fa8 | 506 | if (connection_instance->is_trace_on == 1) |
AzureIoTClient | 4:98007eb79fa8 | 507 | { |
AzureIoTClient | 16:22a72cf8e416 | 508 | LOG(AZ_LOG_TRACE, LOG_LINE, "<- Header (AMQP 0.1.0.0)"); |
AzureIoTClient | 4:98007eb79fa8 | 509 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 510 | |
AzureIoTClient | 4:98007eb79fa8 | 511 | connection_set_state(connection_instance, CONNECTION_STATE_HDR_EXCH); |
Azure.IoT Build | 0:6ae2f7bca550 | 512 | |
AzureIoTClient | 4:98007eb79fa8 | 513 | if (send_open_frame(connection_instance) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 514 | { |
AzureIoTClient | 4:98007eb79fa8 | 515 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 516 | } |
AzureIoTClient | 4:98007eb79fa8 | 517 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 518 | |
AzureIoTClient | 4:98007eb79fa8 | 519 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 520 | } |
AzureIoTClient | 4:98007eb79fa8 | 521 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 522 | |
AzureIoTClient | 4:98007eb79fa8 | 523 | /* Codes_SRS_CONNECTION_01_040: [HDR RCVD In this state the connection header has been received from the peer but a connection header has not been sent.] */ |
AzureIoTClient | 4:98007eb79fa8 | 524 | case CONNECTION_STATE_HDR_RCVD: |
Azure.IoT Build | 0:6ae2f7bca550 | 525 | |
AzureIoTClient | 4:98007eb79fa8 | 526 | /* Codes_SRS_CONNECTION_01_042: [HDR EXCH In this state the connection header has been sent to the peer and a connection header has been received from the peer.] */ |
AzureIoTClient | 4:98007eb79fa8 | 527 | /* we should not really get into this state, but just in case, we would treat that in the same way as HDR_RCVD */ |
AzureIoTClient | 4:98007eb79fa8 | 528 | case CONNECTION_STATE_HDR_EXCH: |
Azure.IoT Build | 0:6ae2f7bca550 | 529 | |
AzureIoTClient | 4:98007eb79fa8 | 530 | /* Codes_SRS_CONNECTION_01_045: [OPEN RCVD In this state the connection headers have been exchanged. An open frame has been received from the peer but an open frame has not been sent.] */ |
AzureIoTClient | 4:98007eb79fa8 | 531 | case CONNECTION_STATE_OPEN_RCVD: |
Azure.IoT Build | 0:6ae2f7bca550 | 532 | |
AzureIoTClient | 4:98007eb79fa8 | 533 | /* Codes_SRS_CONNECTION_01_046: [OPEN SENT In this state the connection headers have been exchanged. An open frame has been sent to the peer but no open frame has yet been received.] */ |
AzureIoTClient | 4:98007eb79fa8 | 534 | case CONNECTION_STATE_OPEN_SENT: |
Azure.IoT Build | 0:6ae2f7bca550 | 535 | |
AzureIoTClient | 4:98007eb79fa8 | 536 | /* Codes_SRS_CONNECTION_01_048: [OPENED In this state the connection header and the open frame have been both sent and received.] */ |
AzureIoTClient | 4:98007eb79fa8 | 537 | case CONNECTION_STATE_OPENED: |
AzureIoTClient | 4:98007eb79fa8 | 538 | /* Codes_SRS_CONNECTION_01_212: [After the initial handshake has been done all bytes received from the io instance shall be passed to the frame_codec for decoding by calling frame_codec_receive_bytes.] */ |
AzureIoTClient | 4:98007eb79fa8 | 539 | if (frame_codec_receive_bytes(connection_instance->frame_codec, &b, 1) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 540 | { |
AzureIoTClient | 4:98007eb79fa8 | 541 | /* Codes_SRS_CONNECTION_01_218: [The error amqp:internal-error shall be set in the error.condition field of the CLOSE frame.] */ |
AzureIoTClient | 4:98007eb79fa8 | 542 | /* Codes_SRS_CONNECTION_01_219: [The error description shall be set to an implementation defined string.] */ |
AzureIoTClient | 4:98007eb79fa8 | 543 | close_connection_with_error(connection_instance, "amqp:internal-error", "connection_byte_received::frame_codec_receive_bytes failed"); |
AzureIoTClient | 4:98007eb79fa8 | 544 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 545 | } |
AzureIoTClient | 4:98007eb79fa8 | 546 | else |
AzureIoTClient | 4:98007eb79fa8 | 547 | { |
AzureIoTClient | 4:98007eb79fa8 | 548 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 549 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 550 | |
AzureIoTClient | 4:98007eb79fa8 | 551 | break; |
AzureIoTClient | 4:98007eb79fa8 | 552 | } |
AzureIoTClient | 4:98007eb79fa8 | 553 | |
AzureIoTClient | 4:98007eb79fa8 | 554 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 555 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 556 | |
Azure.IoT Build | 0:6ae2f7bca550 | 557 | static void connection_on_bytes_received(void* context, const unsigned char* buffer, size_t size) |
Azure.IoT Build | 0:6ae2f7bca550 | 558 | { |
AzureIoTClient | 4:98007eb79fa8 | 559 | size_t i; |
Azure.IoT Build | 0:6ae2f7bca550 | 560 | |
AzureIoTClient | 4:98007eb79fa8 | 561 | for (i = 0; i < size; i++) |
AzureIoTClient | 4:98007eb79fa8 | 562 | { |
AzureIoTClient | 4:98007eb79fa8 | 563 | if (connection_byte_received((CONNECTION_INSTANCE*)context, buffer[i]) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 564 | { |
AzureIoTClient | 4:98007eb79fa8 | 565 | break; |
AzureIoTClient | 4:98007eb79fa8 | 566 | } |
AzureIoTClient | 4:98007eb79fa8 | 567 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 568 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 569 | |
Azure.IoT Build | 0:6ae2f7bca550 | 570 | static void connection_on_io_open_complete(void* context, IO_OPEN_RESULT io_open_result) |
Azure.IoT Build | 0:6ae2f7bca550 | 571 | { |
AzureIoTClient | 4:98007eb79fa8 | 572 | CONNECTION_INSTANCE* connection_instance = (CONNECTION_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 573 | |
AzureIoTClient | 4:98007eb79fa8 | 574 | if (io_open_result == IO_OPEN_OK) |
AzureIoTClient | 4:98007eb79fa8 | 575 | { |
AzureIoTClient | 4:98007eb79fa8 | 576 | /* Codes_SRS_CONNECTION_01_084: [The connection_instance state machine implementing the protocol requirements shall be run as part of connection_dowork.] */ |
AzureIoTClient | 4:98007eb79fa8 | 577 | switch (connection_instance->connection_state) |
AzureIoTClient | 4:98007eb79fa8 | 578 | { |
AzureIoTClient | 4:98007eb79fa8 | 579 | default: |
AzureIoTClient | 4:98007eb79fa8 | 580 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 581 | |
AzureIoTClient | 4:98007eb79fa8 | 582 | case CONNECTION_STATE_START: |
AzureIoTClient | 4:98007eb79fa8 | 583 | /* Codes_SRS_CONNECTION_01_086: [Prior to sending any frames on a connection_instance, each peer MUST start by sending a protocol header that indicates the protocol version used on the connection_instance.] */ |
AzureIoTClient | 4:98007eb79fa8 | 584 | /* Codes_SRS_CONNECTION_01_091: [The AMQP peer which acted in the role of the TCP client (i.e. the peer that actively opened the connection_instance) MUST immediately send its outgoing protocol header on establishment of the TCP connection_instance.] */ |
AzureIoTClient | 4:98007eb79fa8 | 585 | (void)send_header(connection_instance); |
AzureIoTClient | 4:98007eb79fa8 | 586 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 587 | |
AzureIoTClient | 4:98007eb79fa8 | 588 | case CONNECTION_STATE_HDR_SENT: |
AzureIoTClient | 4:98007eb79fa8 | 589 | case CONNECTION_STATE_OPEN_SENT: |
AzureIoTClient | 4:98007eb79fa8 | 590 | case CONNECTION_STATE_OPENED: |
AzureIoTClient | 4:98007eb79fa8 | 591 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 592 | |
AzureIoTClient | 4:98007eb79fa8 | 593 | case CONNECTION_STATE_HDR_EXCH: |
AzureIoTClient | 4:98007eb79fa8 | 594 | /* Codes_SRS_CONNECTION_01_002: [Each AMQP connection_instance begins with an exchange of capabilities and limitations, including the maximum frame size.] */ |
AzureIoTClient | 4:98007eb79fa8 | 595 | /* Codes_SRS_CONNECTION_01_004: [After establishing or accepting a TCP connection_instance and sending the protocol header, each peer MUST send an open frame before sending any other frames.] */ |
AzureIoTClient | 4:98007eb79fa8 | 596 | /* Codes_SRS_CONNECTION_01_005: [The open frame describes the capabilities and limits of that peer.] */ |
AzureIoTClient | 4:98007eb79fa8 | 597 | if (send_open_frame(connection_instance) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 598 | { |
AzureIoTClient | 4:98007eb79fa8 | 599 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 600 | } |
AzureIoTClient | 4:98007eb79fa8 | 601 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 602 | |
AzureIoTClient | 4:98007eb79fa8 | 603 | case CONNECTION_STATE_OPEN_RCVD: |
AzureIoTClient | 4:98007eb79fa8 | 604 | break; |
AzureIoTClient | 4:98007eb79fa8 | 605 | } |
AzureIoTClient | 4:98007eb79fa8 | 606 | } |
AzureIoTClient | 4:98007eb79fa8 | 607 | else |
AzureIoTClient | 4:98007eb79fa8 | 608 | { |
AzureIoTClient | 4:98007eb79fa8 | 609 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 610 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 611 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 612 | |
Azure.IoT Build | 0:6ae2f7bca550 | 613 | static void connection_on_io_error(void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 614 | { |
AzureIoTClient | 4:98007eb79fa8 | 615 | CONNECTION_INSTANCE* connection_instance = (CONNECTION_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 616 | |
Azure.IoT Build | 0:6ae2f7bca550 | 617 | /* Codes_SRS_CONNECTION_22_005: [If the io notifies the connection instance of an IO_STATE_ERROR state and an io error callback is registered, the connection shall call the registered callback.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 618 | if (connection_instance->on_io_error) |
Azure.IoT Build | 0:6ae2f7bca550 | 619 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 620 | connection_instance->on_io_error(connection_instance->on_io_error_callback_context); |
Azure.IoT Build | 0:6ae2f7bca550 | 621 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 622 | |
AzureIoTClient | 4:98007eb79fa8 | 623 | if (connection_instance->connection_state != CONNECTION_STATE_END) |
AzureIoTClient | 4:98007eb79fa8 | 624 | { |
AzureIoTClient | 4:98007eb79fa8 | 625 | /* Codes_SRS_CONNECTION_01_202: [If the io notifies the connection instance of an IO_STATE_ERROR state the connection shall be closed and the state set to END.] */ |
AzureIoTClient | 4:98007eb79fa8 | 626 | connection_set_state(connection_instance, CONNECTION_STATE_ERROR); |
AzureIoTClient | 4:98007eb79fa8 | 627 | (void)xio_close(connection_instance->io, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 628 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 629 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 630 | |
Azure.IoT Build | 0:6ae2f7bca550 | 631 | static void on_empty_amqp_frame_received(void* context, uint16_t channel) |
Azure.IoT Build | 0:6ae2f7bca550 | 632 | { |
AzureIoTClient | 6:641a9672db08 | 633 | (void)channel; |
AzureIoTClient | 6:641a9672db08 | 634 | /* It does not matter on which channel we received the frame */ |
AzureIoTClient | 4:98007eb79fa8 | 635 | CONNECTION_INSTANCE* connection_instance = (CONNECTION_INSTANCE*)context; |
AzureIoTClient | 6:641a9672db08 | 636 | if (connection_instance->is_trace_on == 1) |
AzureIoTClient | 6:641a9672db08 | 637 | { |
AzureIoTClient | 16:22a72cf8e416 | 638 | LOG(AZ_LOG_TRACE, LOG_LINE, "<- Empty frame"); |
AzureIoTClient | 6:641a9672db08 | 639 | } |
AzureIoTClient | 4:98007eb79fa8 | 640 | if (tickcounter_get_current_ms(connection_instance->tick_counter, &connection_instance->last_frame_received_time) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 641 | { |
AzureIoTClient | 4:98007eb79fa8 | 642 | /* error */ |
AzureIoTClient | 4:98007eb79fa8 | 643 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 644 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 645 | |
Azure.IoT Build | 0:6ae2f7bca550 | 646 | static void on_amqp_frame_received(void* context, uint16_t channel, AMQP_VALUE performative, const unsigned char* payload_bytes, uint32_t payload_size) |
Azure.IoT Build | 0:6ae2f7bca550 | 647 | { |
AzureIoTClient | 6:641a9672db08 | 648 | (void)channel; |
AzureIoTClient | 4:98007eb79fa8 | 649 | CONNECTION_INSTANCE* connection_instance = (CONNECTION_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 650 | |
AzureIoTClient | 4:98007eb79fa8 | 651 | if (tickcounter_get_current_ms(connection_instance->tick_counter, &connection_instance->last_frame_received_time) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 652 | { |
AzureIoTClient | 4:98007eb79fa8 | 653 | close_connection_with_error(connection_instance, "amqp:internal-error", "cannot get current tick count"); |
AzureIoTClient | 4:98007eb79fa8 | 654 | } |
AzureIoTClient | 4:98007eb79fa8 | 655 | else |
AzureIoTClient | 4:98007eb79fa8 | 656 | { |
AzureIoTClient | 4:98007eb79fa8 | 657 | if (connection_instance->is_underlying_io_open) |
AzureIoTClient | 4:98007eb79fa8 | 658 | { |
AzureIoTClient | 4:98007eb79fa8 | 659 | switch (connection_instance->connection_state) |
AzureIoTClient | 4:98007eb79fa8 | 660 | { |
AzureIoTClient | 4:98007eb79fa8 | 661 | default: |
AzureIoTClient | 4:98007eb79fa8 | 662 | if (performative == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 663 | { |
AzureIoTClient | 4:98007eb79fa8 | 664 | /* Codes_SRS_CONNECTION_01_223: [If the on_endpoint_frame_received is called with a NULL performative then the connection shall be closed with the error condition amqp:internal-error and an implementation defined error description.] */ |
AzureIoTClient | 4:98007eb79fa8 | 665 | close_connection_with_error(connection_instance, "amqp:internal-error", "connection_endpoint_frame_received::NULL performative"); |
AzureIoTClient | 4:98007eb79fa8 | 666 | } |
AzureIoTClient | 4:98007eb79fa8 | 667 | else |
AzureIoTClient | 4:98007eb79fa8 | 668 | { |
AzureIoTClient | 4:98007eb79fa8 | 669 | AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(performative); |
AzureIoTClient | 4:98007eb79fa8 | 670 | uint64_t performative_ulong; |
Azure.IoT Build | 0:6ae2f7bca550 | 671 | |
AzureIoTClient | 4:98007eb79fa8 | 672 | if (connection_instance->is_trace_on == 1) |
AzureIoTClient | 4:98007eb79fa8 | 673 | { |
Azure.IoT Build | 5:ae49385aff34 | 674 | log_incoming_frame(performative); |
AzureIoTClient | 4:98007eb79fa8 | 675 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 676 | |
AzureIoTClient | 4:98007eb79fa8 | 677 | if (is_open_type_by_descriptor(descriptor)) |
AzureIoTClient | 4:98007eb79fa8 | 678 | { |
AzureIoTClient | 4:98007eb79fa8 | 679 | if (channel != 0) |
AzureIoTClient | 4:98007eb79fa8 | 680 | { |
AzureIoTClient | 4:98007eb79fa8 | 681 | /* Codes_SRS_CONNECTION_01_006: [The open frame can only be sent on channel 0.] */ |
AzureIoTClient | 4:98007eb79fa8 | 682 | /* Codes_SRS_CONNECTION_01_222: [If an Open frame is received in a manner violating the ISO specification, the connection shall be closed with condition amqp:not-allowed and description being an implementation defined string.] */ |
AzureIoTClient | 4:98007eb79fa8 | 683 | close_connection_with_error(connection_instance, "amqp:not-allowed", "OPEN frame received on a channel that is not 0"); |
AzureIoTClient | 4:98007eb79fa8 | 684 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 685 | |
AzureIoTClient | 4:98007eb79fa8 | 686 | if (connection_instance->connection_state == CONNECTION_STATE_OPENED) |
AzureIoTClient | 4:98007eb79fa8 | 687 | { |
AzureIoTClient | 4:98007eb79fa8 | 688 | /* Codes_SRS_CONNECTION_01_239: [If an Open frame is received in the Opened state the connection shall be closed with condition amqp:illegal-state and description being an implementation defined string.] */ |
AzureIoTClient | 4:98007eb79fa8 | 689 | close_connection_with_error(connection_instance, "amqp:illegal-state", "OPEN frame received in the OPENED state"); |
AzureIoTClient | 4:98007eb79fa8 | 690 | } |
AzureIoTClient | 4:98007eb79fa8 | 691 | else if ((connection_instance->connection_state == CONNECTION_STATE_OPEN_SENT) || |
AzureIoTClient | 4:98007eb79fa8 | 692 | (connection_instance->connection_state == CONNECTION_STATE_HDR_EXCH)) |
AzureIoTClient | 4:98007eb79fa8 | 693 | { |
AzureIoTClient | 4:98007eb79fa8 | 694 | OPEN_HANDLE open_handle; |
AzureIoTClient | 4:98007eb79fa8 | 695 | if (amqpvalue_get_open(performative, &open_handle) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 696 | { |
AzureIoTClient | 4:98007eb79fa8 | 697 | /* Codes_SRS_CONNECTION_01_143: [If any of the values in the received open frame are invalid then the connection shall be closed.] */ |
AzureIoTClient | 4:98007eb79fa8 | 698 | /* Codes_SRS_CONNECTION_01_220: [The error amqp:invalid-field shall be set in the error.condition field of the CLOSE frame.] */ |
AzureIoTClient | 4:98007eb79fa8 | 699 | close_connection_with_error(connection_instance, "amqp:invalid-field", "connection_endpoint_frame_received::failed parsing OPEN frame"); |
AzureIoTClient | 4:98007eb79fa8 | 700 | } |
AzureIoTClient | 4:98007eb79fa8 | 701 | else |
AzureIoTClient | 4:98007eb79fa8 | 702 | { |
AzureIoTClient | 4:98007eb79fa8 | 703 | (void)open_get_idle_time_out(open_handle, &connection_instance->remote_idle_timeout); |
AzureIoTClient | 4:98007eb79fa8 | 704 | if ((open_get_max_frame_size(open_handle, &connection_instance->remote_max_frame_size) != 0) || |
AzureIoTClient | 4:98007eb79fa8 | 705 | /* Codes_SRS_CONNECTION_01_167: [Both peers MUST accept frames of up to 512 (MIN-MAX-FRAME-SIZE) octets.] */ |
AzureIoTClient | 4:98007eb79fa8 | 706 | (connection_instance->remote_max_frame_size < 512)) |
AzureIoTClient | 4:98007eb79fa8 | 707 | { |
AzureIoTClient | 4:98007eb79fa8 | 708 | /* Codes_SRS_CONNECTION_01_143: [If any of the values in the received open frame are invalid then the connection shall be closed.] */ |
AzureIoTClient | 4:98007eb79fa8 | 709 | /* Codes_SRS_CONNECTION_01_220: [The error amqp:invalid-field shall be set in the error.condition field of the CLOSE frame.] */ |
AzureIoTClient | 4:98007eb79fa8 | 710 | close_connection_with_error(connection_instance, "amqp:invalid-field", "connection_endpoint_frame_received::failed parsing OPEN frame"); |
AzureIoTClient | 4:98007eb79fa8 | 711 | } |
AzureIoTClient | 4:98007eb79fa8 | 712 | else |
AzureIoTClient | 4:98007eb79fa8 | 713 | { |
AzureIoTClient | 4:98007eb79fa8 | 714 | if (connection_instance->connection_state == CONNECTION_STATE_OPEN_SENT) |
AzureIoTClient | 4:98007eb79fa8 | 715 | { |
AzureIoTClient | 4:98007eb79fa8 | 716 | connection_set_state(connection_instance, CONNECTION_STATE_OPENED); |
AzureIoTClient | 4:98007eb79fa8 | 717 | } |
AzureIoTClient | 4:98007eb79fa8 | 718 | else |
AzureIoTClient | 4:98007eb79fa8 | 719 | { |
AzureIoTClient | 4:98007eb79fa8 | 720 | if (send_open_frame(connection_instance) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 721 | { |
AzureIoTClient | 4:98007eb79fa8 | 722 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 723 | } |
AzureIoTClient | 4:98007eb79fa8 | 724 | else |
AzureIoTClient | 4:98007eb79fa8 | 725 | { |
AzureIoTClient | 4:98007eb79fa8 | 726 | connection_set_state(connection_instance, CONNECTION_STATE_OPENED); |
AzureIoTClient | 4:98007eb79fa8 | 727 | } |
AzureIoTClient | 4:98007eb79fa8 | 728 | } |
AzureIoTClient | 4:98007eb79fa8 | 729 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 730 | |
AzureIoTClient | 4:98007eb79fa8 | 731 | open_destroy(open_handle); |
AzureIoTClient | 4:98007eb79fa8 | 732 | } |
AzureIoTClient | 4:98007eb79fa8 | 733 | } |
AzureIoTClient | 4:98007eb79fa8 | 734 | else |
AzureIoTClient | 4:98007eb79fa8 | 735 | { |
AzureIoTClient | 4:98007eb79fa8 | 736 | /* do nothing for now ... */ |
AzureIoTClient | 4:98007eb79fa8 | 737 | } |
AzureIoTClient | 4:98007eb79fa8 | 738 | } |
AzureIoTClient | 4:98007eb79fa8 | 739 | else if (is_close_type_by_descriptor(descriptor)) |
AzureIoTClient | 4:98007eb79fa8 | 740 | { |
AzureIoTClient | 4:98007eb79fa8 | 741 | /* Codes_SRS_CONNECTION_01_012: [A close frame MAY be received on any channel up to the maximum channel number negotiated in open.] */ |
AzureIoTClient | 4:98007eb79fa8 | 742 | /* Codes_SRS_CONNECTION_01_242: [The connection module shall accept CLOSE frames even if they have extra payload bytes besides the Close performative.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 743 | |
AzureIoTClient | 4:98007eb79fa8 | 744 | /* Codes_SRS_CONNECTION_01_225: [HDR_RCVD HDR OPEN] */ |
AzureIoTClient | 4:98007eb79fa8 | 745 | if ((connection_instance->connection_state == CONNECTION_STATE_HDR_RCVD) || |
AzureIoTClient | 4:98007eb79fa8 | 746 | /* Codes_SRS_CONNECTION_01_227: [HDR_EXCH OPEN OPEN] */ |
AzureIoTClient | 4:98007eb79fa8 | 747 | (connection_instance->connection_state == CONNECTION_STATE_HDR_EXCH) || |
AzureIoTClient | 4:98007eb79fa8 | 748 | /* Codes_SRS_CONNECTION_01_228: [OPEN_RCVD OPEN *] */ |
AzureIoTClient | 4:98007eb79fa8 | 749 | (connection_instance->connection_state == CONNECTION_STATE_OPEN_RCVD) || |
AzureIoTClient | 4:98007eb79fa8 | 750 | /* Codes_SRS_CONNECTION_01_235: [CLOSE_SENT - * TCP Close for Write] */ |
AzureIoTClient | 4:98007eb79fa8 | 751 | (connection_instance->connection_state == CONNECTION_STATE_CLOSE_SENT) || |
AzureIoTClient | 4:98007eb79fa8 | 752 | /* Codes_SRS_CONNECTION_01_236: [DISCARDING - * TCP Close for Write] */ |
AzureIoTClient | 4:98007eb79fa8 | 753 | (connection_instance->connection_state == CONNECTION_STATE_DISCARDING)) |
AzureIoTClient | 4:98007eb79fa8 | 754 | { |
AzureIoTClient | 4:98007eb79fa8 | 755 | xio_close(connection_instance->io, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 756 | } |
AzureIoTClient | 4:98007eb79fa8 | 757 | else |
AzureIoTClient | 4:98007eb79fa8 | 758 | { |
AzureIoTClient | 4:98007eb79fa8 | 759 | CLOSE_HANDLE close_handle; |
Azure.IoT Build | 0:6ae2f7bca550 | 760 | |
AzureIoTClient | 4:98007eb79fa8 | 761 | /* Codes_SRS_CONNECTION_01_012: [A close frame MAY be received on any channel up to the maximum channel number negotiated in open.] */ |
AzureIoTClient | 4:98007eb79fa8 | 762 | if (channel > connection_instance->channel_max) |
AzureIoTClient | 4:98007eb79fa8 | 763 | { |
AzureIoTClient | 4:98007eb79fa8 | 764 | close_connection_with_error(connection_instance, "amqp:invalid-field", "connection_endpoint_frame_received::failed parsing CLOSE frame"); |
AzureIoTClient | 4:98007eb79fa8 | 765 | } |
AzureIoTClient | 4:98007eb79fa8 | 766 | else |
AzureIoTClient | 4:98007eb79fa8 | 767 | { |
AzureIoTClient | 4:98007eb79fa8 | 768 | if (amqpvalue_get_close(performative, &close_handle) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 769 | { |
AzureIoTClient | 4:98007eb79fa8 | 770 | close_connection_with_error(connection_instance, "amqp:invalid-field", "connection_endpoint_frame_received::failed parsing CLOSE frame"); |
AzureIoTClient | 4:98007eb79fa8 | 771 | } |
AzureIoTClient | 4:98007eb79fa8 | 772 | else |
AzureIoTClient | 4:98007eb79fa8 | 773 | { |
AzureIoTClient | 4:98007eb79fa8 | 774 | close_destroy(close_handle); |
Azure.IoT Build | 0:6ae2f7bca550 | 775 | |
AzureIoTClient | 4:98007eb79fa8 | 776 | connection_set_state(connection_instance, CONNECTION_STATE_CLOSE_RCVD); |
Azure.IoT Build | 0:6ae2f7bca550 | 777 | |
AzureIoTClient | 4:98007eb79fa8 | 778 | (void)send_close_frame(connection_instance, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 779 | /* Codes_SRS_CONNECTION_01_214: [If the close frame cannot be constructed or sent, the connection shall be closed and set to the END state.] */ |
AzureIoTClient | 4:98007eb79fa8 | 780 | (void)xio_close(connection_instance->io, NULL, NULL); |
Azure.IoT Build | 0:6ae2f7bca550 | 781 | |
AzureIoTClient | 4:98007eb79fa8 | 782 | connection_set_state(connection_instance, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 783 | } |
AzureIoTClient | 4:98007eb79fa8 | 784 | } |
AzureIoTClient | 4:98007eb79fa8 | 785 | } |
AzureIoTClient | 4:98007eb79fa8 | 786 | } |
AzureIoTClient | 4:98007eb79fa8 | 787 | else |
AzureIoTClient | 4:98007eb79fa8 | 788 | { |
AzureIoTClient | 4:98007eb79fa8 | 789 | amqpvalue_get_ulong(descriptor, &performative_ulong); |
Azure.IoT Build | 0:6ae2f7bca550 | 790 | |
AzureIoTClient | 4:98007eb79fa8 | 791 | switch (performative_ulong) |
AzureIoTClient | 4:98007eb79fa8 | 792 | { |
AzureIoTClient | 4:98007eb79fa8 | 793 | default: |
AzureIoTClient | 16:22a72cf8e416 | 794 | LOG(AZ_LOG_ERROR, LOG_LINE, "Bad performative: %02x", performative); |
AzureIoTClient | 4:98007eb79fa8 | 795 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 796 | |
AzureIoTClient | 4:98007eb79fa8 | 797 | case AMQP_BEGIN: |
AzureIoTClient | 4:98007eb79fa8 | 798 | { |
AzureIoTClient | 4:98007eb79fa8 | 799 | BEGIN_HANDLE begin; |
AzureIoTClient | 4:98007eb79fa8 | 800 | amqpvalue_get_begin(performative, &begin); |
Azure.IoT Build | 0:6ae2f7bca550 | 801 | |
AzureIoTClient | 4:98007eb79fa8 | 802 | if (begin == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 803 | { |
AzureIoTClient | 4:98007eb79fa8 | 804 | /* error */ |
AzureIoTClient | 4:98007eb79fa8 | 805 | } |
AzureIoTClient | 4:98007eb79fa8 | 806 | else |
AzureIoTClient | 4:98007eb79fa8 | 807 | { |
AzureIoTClient | 4:98007eb79fa8 | 808 | uint16_t remote_channel; |
AzureIoTClient | 4:98007eb79fa8 | 809 | ENDPOINT_HANDLE new_endpoint = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 810 | bool remote_begin = false; |
Azure.IoT Build | 0:6ae2f7bca550 | 811 | |
AzureIoTClient | 4:98007eb79fa8 | 812 | if (begin_get_remote_channel(begin, &remote_channel) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 813 | { |
AzureIoTClient | 4:98007eb79fa8 | 814 | remote_begin = true; |
AzureIoTClient | 4:98007eb79fa8 | 815 | if (connection_instance->on_new_endpoint != NULL) |
AzureIoTClient | 4:98007eb79fa8 | 816 | { |
AzureIoTClient | 4:98007eb79fa8 | 817 | new_endpoint = connection_create_endpoint(connection_instance); |
AzureIoTClient | 4:98007eb79fa8 | 818 | if (!connection_instance->on_new_endpoint(connection_instance->on_new_endpoint_callback_context, new_endpoint)) |
AzureIoTClient | 4:98007eb79fa8 | 819 | { |
AzureIoTClient | 4:98007eb79fa8 | 820 | connection_destroy_endpoint(new_endpoint); |
AzureIoTClient | 4:98007eb79fa8 | 821 | new_endpoint = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 822 | } |
AzureIoTClient | 4:98007eb79fa8 | 823 | } |
AzureIoTClient | 4:98007eb79fa8 | 824 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 825 | |
AzureIoTClient | 4:98007eb79fa8 | 826 | if (!remote_begin) |
AzureIoTClient | 4:98007eb79fa8 | 827 | { |
AzureIoTClient | 4:98007eb79fa8 | 828 | ENDPOINT_INSTANCE* session_endpoint = find_session_endpoint_by_outgoing_channel(connection_instance, remote_channel); |
AzureIoTClient | 4:98007eb79fa8 | 829 | if (session_endpoint == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 830 | { |
AzureIoTClient | 4:98007eb79fa8 | 831 | /* error */ |
AzureIoTClient | 4:98007eb79fa8 | 832 | } |
AzureIoTClient | 4:98007eb79fa8 | 833 | else |
AzureIoTClient | 4:98007eb79fa8 | 834 | { |
AzureIoTClient | 4:98007eb79fa8 | 835 | session_endpoint->incoming_channel = channel; |
AzureIoTClient | 4:98007eb79fa8 | 836 | session_endpoint->on_endpoint_frame_received(session_endpoint->callback_context, performative, payload_size, payload_bytes); |
AzureIoTClient | 4:98007eb79fa8 | 837 | } |
AzureIoTClient | 4:98007eb79fa8 | 838 | } |
AzureIoTClient | 4:98007eb79fa8 | 839 | else |
AzureIoTClient | 4:98007eb79fa8 | 840 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 841 | if (new_endpoint != NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 842 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 843 | new_endpoint->incoming_channel = channel; |
Azure.IoT Build | 0:6ae2f7bca550 | 844 | new_endpoint->on_endpoint_frame_received(new_endpoint->callback_context, performative, payload_size, payload_bytes); |
Azure.IoT Build | 0:6ae2f7bca550 | 845 | } |
AzureIoTClient | 4:98007eb79fa8 | 846 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 847 | |
AzureIoTClient | 4:98007eb79fa8 | 848 | begin_destroy(begin); |
AzureIoTClient | 4:98007eb79fa8 | 849 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 850 | |
AzureIoTClient | 4:98007eb79fa8 | 851 | break; |
AzureIoTClient | 4:98007eb79fa8 | 852 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 853 | |
AzureIoTClient | 4:98007eb79fa8 | 854 | case AMQP_FLOW: |
AzureIoTClient | 4:98007eb79fa8 | 855 | case AMQP_TRANSFER: |
AzureIoTClient | 4:98007eb79fa8 | 856 | case AMQP_DISPOSITION: |
AzureIoTClient | 4:98007eb79fa8 | 857 | case AMQP_END: |
AzureIoTClient | 4:98007eb79fa8 | 858 | case AMQP_ATTACH: |
AzureIoTClient | 4:98007eb79fa8 | 859 | case AMQP_DETACH: |
AzureIoTClient | 4:98007eb79fa8 | 860 | { |
AzureIoTClient | 4:98007eb79fa8 | 861 | ENDPOINT_INSTANCE* session_endpoint = find_session_endpoint_by_incoming_channel(connection_instance, channel); |
AzureIoTClient | 4:98007eb79fa8 | 862 | if (session_endpoint == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 863 | { |
AzureIoTClient | 4:98007eb79fa8 | 864 | /* error */ |
AzureIoTClient | 4:98007eb79fa8 | 865 | } |
AzureIoTClient | 4:98007eb79fa8 | 866 | else |
AzureIoTClient | 4:98007eb79fa8 | 867 | { |
AzureIoTClient | 4:98007eb79fa8 | 868 | session_endpoint->on_endpoint_frame_received(session_endpoint->callback_context, performative, payload_size, payload_bytes); |
AzureIoTClient | 4:98007eb79fa8 | 869 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 870 | |
AzureIoTClient | 4:98007eb79fa8 | 871 | break; |
AzureIoTClient | 4:98007eb79fa8 | 872 | } |
AzureIoTClient | 4:98007eb79fa8 | 873 | } |
AzureIoTClient | 4:98007eb79fa8 | 874 | } |
AzureIoTClient | 4:98007eb79fa8 | 875 | } |
AzureIoTClient | 4:98007eb79fa8 | 876 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 877 | |
AzureIoTClient | 4:98007eb79fa8 | 878 | case CONNECTION_STATE_START: |
AzureIoTClient | 4:98007eb79fa8 | 879 | /* Codes_SRS_CONNECTION_01_224: [START HDR HDR] */ |
AzureIoTClient | 4:98007eb79fa8 | 880 | case CONNECTION_STATE_HDR_SENT: |
AzureIoTClient | 4:98007eb79fa8 | 881 | /* Codes_SRS_CONNECTION_01_226: [HDR_SENT OPEN HDR] */ |
AzureIoTClient | 4:98007eb79fa8 | 882 | case CONNECTION_STATE_OPEN_PIPE: |
AzureIoTClient | 4:98007eb79fa8 | 883 | /* Codes_SRS_CONNECTION_01_230: [OPEN_PIPE ** HDR] */ |
AzureIoTClient | 4:98007eb79fa8 | 884 | case CONNECTION_STATE_OC_PIPE: |
AzureIoTClient | 4:98007eb79fa8 | 885 | /* Codes_SRS_CONNECTION_01_232: [OC_PIPE - HDR TCP Close for Write] */ |
AzureIoTClient | 4:98007eb79fa8 | 886 | case CONNECTION_STATE_CLOSE_RCVD: |
AzureIoTClient | 4:98007eb79fa8 | 887 | /* Codes_SRS_CONNECTION_01_234: [CLOSE_RCVD * - TCP Close for Read] */ |
AzureIoTClient | 4:98007eb79fa8 | 888 | case CONNECTION_STATE_END: |
AzureIoTClient | 4:98007eb79fa8 | 889 | /* Codes_SRS_CONNECTION_01_237: [END - - TCP Close] */ |
AzureIoTClient | 4:98007eb79fa8 | 890 | xio_close(connection_instance->io, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 891 | break; |
AzureIoTClient | 4:98007eb79fa8 | 892 | } |
AzureIoTClient | 4:98007eb79fa8 | 893 | } |
AzureIoTClient | 4:98007eb79fa8 | 894 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 895 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 896 | |
Azure.IoT Build | 0:6ae2f7bca550 | 897 | static void frame_codec_error(void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 898 | { |
AzureIoTClient | 6:641a9672db08 | 899 | /* Bug: some error handling should happen here |
AzureIoTClient | 6:641a9672db08 | 900 | Filed: uAMQP: frame_codec error and amqp_frame_codec_error should handle the errors */ |
AzureIoTClient | 6:641a9672db08 | 901 | (void)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 902 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 903 | |
Azure.IoT Build | 0:6ae2f7bca550 | 904 | static void amqp_frame_codec_error(void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 905 | { |
AzureIoTClient | 6:641a9672db08 | 906 | /* Bug: some error handling should happen here |
AzureIoTClient | 6:641a9672db08 | 907 | Filed: uAMQP: frame_codec error and amqp_frame_codec_error should handle the errors */ |
AzureIoTClient | 6:641a9672db08 | 908 | (void)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 909 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 910 | |
Azure.IoT Build | 0:6ae2f7bca550 | 911 | /* Codes_SRS_CONNECTION_01_001: [connection_create shall open a new connection to a specified host/port.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 912 | CONNECTION_HANDLE connection_create(XIO_HANDLE xio, const char* hostname, const char* container_id, ON_NEW_ENDPOINT on_new_endpoint, void* callback_context) |
Azure.IoT Build | 0:6ae2f7bca550 | 913 | { |
Azure.IoT Build | 5:ae49385aff34 | 914 | return connection_create2(xio, hostname, container_id, on_new_endpoint, callback_context, NULL, NULL, NULL, NULL); |
Azure.IoT Build | 0:6ae2f7bca550 | 915 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 916 | |
Azure.IoT Build | 0:6ae2f7bca550 | 917 | /* Codes_SRS_CONNECTION_01_001: [connection_create shall open a new connection to a specified host/port.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 918 | /* Codes_SRS_CONNECTION_22_002: [connection_create shall allow registering connections state and io error callbacks.] */ |
Azure.IoT Build | 5:ae49385aff34 | 919 | CONNECTION_HANDLE connection_create2(XIO_HANDLE xio, const char* hostname, const char* container_id, ON_NEW_ENDPOINT on_new_endpoint, void* callback_context, ON_CONNECTION_STATE_CHANGED on_connection_state_changed, void* on_connection_state_changed_context, ON_IO_ERROR on_io_error, void* on_io_error_context) |
Azure.IoT Build | 0:6ae2f7bca550 | 920 | { |
AzureIoTClient | 4:98007eb79fa8 | 921 | CONNECTION_INSTANCE* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 922 | |
AzureIoTClient | 4:98007eb79fa8 | 923 | if ((xio == NULL) || |
AzureIoTClient | 4:98007eb79fa8 | 924 | (container_id == NULL)) |
AzureIoTClient | 4:98007eb79fa8 | 925 | { |
AzureIoTClient | 4:98007eb79fa8 | 926 | /* Codes_SRS_CONNECTION_01_071: [If xio or container_id is NULL, connection_create shall return NULL.] */ |
AzureIoTClient | 4:98007eb79fa8 | 927 | result = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 928 | } |
AzureIoTClient | 4:98007eb79fa8 | 929 | else |
AzureIoTClient | 4:98007eb79fa8 | 930 | { |
AzureIoTClient | 4:98007eb79fa8 | 931 | result = (CONNECTION_INSTANCE*)amqpalloc_malloc(sizeof(CONNECTION_INSTANCE)); |
AzureIoTClient | 4:98007eb79fa8 | 932 | /* Codes_SRS_CONNECTION_01_081: [If allocating the memory for the connection fails then connection_create shall return NULL.] */ |
AzureIoTClient | 4:98007eb79fa8 | 933 | if (result != NULL) |
AzureIoTClient | 4:98007eb79fa8 | 934 | { |
AzureIoTClient | 4:98007eb79fa8 | 935 | result->io = xio; |
Azure.IoT Build | 0:6ae2f7bca550 | 936 | |
AzureIoTClient | 4:98007eb79fa8 | 937 | /* Codes_SRS_CONNECTION_01_082: [connection_create shall allocate a new frame_codec instance to be used for frame encoding/decoding.] */ |
Azure.IoT Build | 5:ae49385aff34 | 938 | result->frame_codec = frame_codec_create(frame_codec_error, result); |
AzureIoTClient | 4:98007eb79fa8 | 939 | if (result->frame_codec == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 940 | { |
AzureIoTClient | 4:98007eb79fa8 | 941 | /* Codes_SRS_CONNECTION_01_083: [If frame_codec_create fails then connection_create shall return NULL.] */ |
AzureIoTClient | 4:98007eb79fa8 | 942 | amqpalloc_free(result); |
AzureIoTClient | 4:98007eb79fa8 | 943 | result = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 944 | } |
AzureIoTClient | 4:98007eb79fa8 | 945 | else |
AzureIoTClient | 4:98007eb79fa8 | 946 | { |
AzureIoTClient | 4:98007eb79fa8 | 947 | result->amqp_frame_codec = amqp_frame_codec_create(result->frame_codec, on_amqp_frame_received, on_empty_amqp_frame_received, amqp_frame_codec_error, result); |
AzureIoTClient | 4:98007eb79fa8 | 948 | if (result->amqp_frame_codec == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 949 | { |
AzureIoTClient | 4:98007eb79fa8 | 950 | /* Codes_SRS_CONNECTION_01_108: [If amqp_frame_codec_create fails, connection_create shall return NULL.] */ |
AzureIoTClient | 4:98007eb79fa8 | 951 | frame_codec_destroy(result->frame_codec); |
AzureIoTClient | 4:98007eb79fa8 | 952 | amqpalloc_free(result); |
AzureIoTClient | 4:98007eb79fa8 | 953 | result = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 954 | } |
AzureIoTClient | 4:98007eb79fa8 | 955 | else |
AzureIoTClient | 4:98007eb79fa8 | 956 | { |
AzureIoTClient | 4:98007eb79fa8 | 957 | if (hostname != NULL) |
AzureIoTClient | 4:98007eb79fa8 | 958 | { |
AzureIoTClient | 4:98007eb79fa8 | 959 | result->host_name = (char*)amqpalloc_malloc(strlen(hostname) + 1); |
AzureIoTClient | 4:98007eb79fa8 | 960 | if (result->host_name == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 961 | { |
AzureIoTClient | 4:98007eb79fa8 | 962 | /* Codes_SRS_CONNECTION_01_081: [If allocating the memory for the connection fails then connection_create shall return NULL.] */ |
AzureIoTClient | 4:98007eb79fa8 | 963 | amqp_frame_codec_destroy(result->amqp_frame_codec); |
AzureIoTClient | 4:98007eb79fa8 | 964 | frame_codec_destroy(result->frame_codec); |
AzureIoTClient | 4:98007eb79fa8 | 965 | amqpalloc_free(result); |
AzureIoTClient | 4:98007eb79fa8 | 966 | result = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 967 | } |
AzureIoTClient | 4:98007eb79fa8 | 968 | else |
AzureIoTClient | 4:98007eb79fa8 | 969 | { |
AzureIoTClient | 4:98007eb79fa8 | 970 | strcpy(result->host_name, hostname); |
AzureIoTClient | 4:98007eb79fa8 | 971 | } |
AzureIoTClient | 4:98007eb79fa8 | 972 | } |
AzureIoTClient | 4:98007eb79fa8 | 973 | else |
AzureIoTClient | 4:98007eb79fa8 | 974 | { |
AzureIoTClient | 4:98007eb79fa8 | 975 | result->host_name = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 976 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 977 | |
AzureIoTClient | 4:98007eb79fa8 | 978 | if (result != NULL) |
AzureIoTClient | 4:98007eb79fa8 | 979 | { |
AzureIoTClient | 4:98007eb79fa8 | 980 | result->container_id = (char*)amqpalloc_malloc(strlen(container_id) + 1); |
AzureIoTClient | 4:98007eb79fa8 | 981 | if (result->container_id == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 982 | { |
AzureIoTClient | 4:98007eb79fa8 | 983 | /* Codes_SRS_CONNECTION_01_081: [If allocating the memory for the connection fails then connection_create shall return NULL.] */ |
AzureIoTClient | 4:98007eb79fa8 | 984 | amqpalloc_free(result->host_name); |
AzureIoTClient | 4:98007eb79fa8 | 985 | amqp_frame_codec_destroy(result->amqp_frame_codec); |
AzureIoTClient | 4:98007eb79fa8 | 986 | frame_codec_destroy(result->frame_codec); |
AzureIoTClient | 4:98007eb79fa8 | 987 | amqpalloc_free(result); |
AzureIoTClient | 4:98007eb79fa8 | 988 | result = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 989 | } |
AzureIoTClient | 4:98007eb79fa8 | 990 | else |
AzureIoTClient | 4:98007eb79fa8 | 991 | { |
AzureIoTClient | 4:98007eb79fa8 | 992 | result->tick_counter = tickcounter_create(); |
AzureIoTClient | 4:98007eb79fa8 | 993 | if (result->tick_counter == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 994 | { |
AzureIoTClient | 4:98007eb79fa8 | 995 | amqpalloc_free(result->container_id); |
AzureIoTClient | 4:98007eb79fa8 | 996 | amqpalloc_free(result->host_name); |
AzureIoTClient | 4:98007eb79fa8 | 997 | amqp_frame_codec_destroy(result->amqp_frame_codec); |
AzureIoTClient | 4:98007eb79fa8 | 998 | frame_codec_destroy(result->frame_codec); |
AzureIoTClient | 4:98007eb79fa8 | 999 | amqpalloc_free(result); |
AzureIoTClient | 4:98007eb79fa8 | 1000 | result = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 1001 | } |
AzureIoTClient | 4:98007eb79fa8 | 1002 | else |
AzureIoTClient | 4:98007eb79fa8 | 1003 | { |
AzureIoTClient | 4:98007eb79fa8 | 1004 | strcpy(result->container_id, container_id); |
Azure.IoT Build | 0:6ae2f7bca550 | 1005 | |
AzureIoTClient | 4:98007eb79fa8 | 1006 | /* Codes_SRS_CONNECTION_01_173: [<field name="max-frame-size" type="uint" default="4294967295"/>] */ |
AzureIoTClient | 4:98007eb79fa8 | 1007 | result->max_frame_size = 4294967295u; |
AzureIoTClient | 4:98007eb79fa8 | 1008 | /* Codes: [<field name="channel-max" type="ushort" default="65535"/>] */ |
AzureIoTClient | 4:98007eb79fa8 | 1009 | result->channel_max = 65535; |
Azure.IoT Build | 0:6ae2f7bca550 | 1010 | |
AzureIoTClient | 4:98007eb79fa8 | 1011 | /* Codes_SRS_CONNECTION_01_175: [<field name="idle-time-out" type="milliseconds"/>] */ |
AzureIoTClient | 4:98007eb79fa8 | 1012 | /* Codes_SRS_CONNECTION_01_192: [A value of zero is the same as if it was not set (null).] */ |
AzureIoTClient | 4:98007eb79fa8 | 1013 | result->idle_timeout = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1014 | result->remote_idle_timeout = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 1015 | |
AzureIoTClient | 4:98007eb79fa8 | 1016 | result->endpoint_count = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1017 | result->endpoints = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 1018 | result->header_bytes_received = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1019 | result->is_remote_frame_received = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 1020 | |
AzureIoTClient | 4:98007eb79fa8 | 1021 | result->is_underlying_io_open = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1022 | result->remote_max_frame_size = 512; |
AzureIoTClient | 4:98007eb79fa8 | 1023 | result->is_trace_on = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 1024 | |
AzureIoTClient | 4:98007eb79fa8 | 1025 | /* Mark that settings have not yet been set by the user */ |
AzureIoTClient | 4:98007eb79fa8 | 1026 | result->idle_timeout_specified = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 1027 | |
AzureIoTClient | 4:98007eb79fa8 | 1028 | result->on_new_endpoint = on_new_endpoint; |
AzureIoTClient | 4:98007eb79fa8 | 1029 | result->on_new_endpoint_callback_context = callback_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 1030 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1031 | result->on_io_error = on_io_error; |
Azure.IoT Build | 0:6ae2f7bca550 | 1032 | result->on_io_error_callback_context = on_io_error_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 1033 | result->on_connection_state_changed = on_connection_state_changed; |
Azure.IoT Build | 0:6ae2f7bca550 | 1034 | result->on_connection_state_changed_callback_context = on_connection_state_changed_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 1035 | |
AzureIoTClient | 4:98007eb79fa8 | 1036 | /* Codes_SRS_CONNECTION_01_072: [When connection_create succeeds, the state of the connection shall be CONNECTION_STATE_START.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1037 | connection_set_state(result, CONNECTION_STATE_START); |
AzureIoTClient | 4:98007eb79fa8 | 1038 | } |
AzureIoTClient | 4:98007eb79fa8 | 1039 | } |
AzureIoTClient | 4:98007eb79fa8 | 1040 | } |
AzureIoTClient | 4:98007eb79fa8 | 1041 | } |
AzureIoTClient | 4:98007eb79fa8 | 1042 | } |
AzureIoTClient | 4:98007eb79fa8 | 1043 | } |
AzureIoTClient | 4:98007eb79fa8 | 1044 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1045 | |
AzureIoTClient | 4:98007eb79fa8 | 1046 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1047 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1048 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1049 | void connection_destroy(CONNECTION_HANDLE connection) |
Azure.IoT Build | 0:6ae2f7bca550 | 1050 | { |
AzureIoTClient | 4:98007eb79fa8 | 1051 | /* Codes_SRS_CONNECTION_01_079: [If handle is NULL, connection_destroy shall do nothing.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1052 | if (connection != NULL) |
AzureIoTClient | 4:98007eb79fa8 | 1053 | { |
AzureIoTClient | 4:98007eb79fa8 | 1054 | /* Codes_SRS_CONNECTION_01_073: [connection_destroy shall free all resources associated with a connection.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1055 | if (connection->is_underlying_io_open) |
AzureIoTClient | 4:98007eb79fa8 | 1056 | { |
AzureIoTClient | 4:98007eb79fa8 | 1057 | connection_close(connection, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 1058 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1059 | |
AzureIoTClient | 4:98007eb79fa8 | 1060 | amqp_frame_codec_destroy(connection->amqp_frame_codec); |
AzureIoTClient | 4:98007eb79fa8 | 1061 | frame_codec_destroy(connection->frame_codec); |
AzureIoTClient | 4:98007eb79fa8 | 1062 | tickcounter_destroy(connection->tick_counter); |
Azure.IoT Build | 0:6ae2f7bca550 | 1063 | |
AzureIoTClient | 4:98007eb79fa8 | 1064 | amqpalloc_free(connection->host_name); |
AzureIoTClient | 4:98007eb79fa8 | 1065 | amqpalloc_free(connection->container_id); |
Azure.IoT Build | 0:6ae2f7bca550 | 1066 | |
AzureIoTClient | 4:98007eb79fa8 | 1067 | /* Codes_SRS_CONNECTION_01_074: [connection_destroy shall close the socket connection.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1068 | amqpalloc_free(connection); |
AzureIoTClient | 4:98007eb79fa8 | 1069 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1070 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1071 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1072 | int connection_open(CONNECTION_HANDLE connection) |
Azure.IoT Build | 0:6ae2f7bca550 | 1073 | { |
AzureIoTClient | 4:98007eb79fa8 | 1074 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1075 | |
AzureIoTClient | 4:98007eb79fa8 | 1076 | if (connection == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 1077 | { |
AzureIoTClient | 4:98007eb79fa8 | 1078 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1079 | } |
AzureIoTClient | 4:98007eb79fa8 | 1080 | else |
AzureIoTClient | 4:98007eb79fa8 | 1081 | { |
AzureIoTClient | 4:98007eb79fa8 | 1082 | if (!connection->is_underlying_io_open) |
AzureIoTClient | 4:98007eb79fa8 | 1083 | { |
AzureIoTClient | 4:98007eb79fa8 | 1084 | if (xio_open(connection->io, connection_on_io_open_complete, connection, connection_on_bytes_received, connection, connection_on_io_error, connection) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 1085 | { |
AzureIoTClient | 4:98007eb79fa8 | 1086 | connection_set_state(connection, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 1087 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1088 | } |
AzureIoTClient | 4:98007eb79fa8 | 1089 | else |
AzureIoTClient | 4:98007eb79fa8 | 1090 | { |
AzureIoTClient | 4:98007eb79fa8 | 1091 | connection->is_underlying_io_open = 1; |
Azure.IoT Build | 0:6ae2f7bca550 | 1092 | |
AzureIoTClient | 4:98007eb79fa8 | 1093 | connection_set_state(connection, CONNECTION_STATE_START); |
Azure.IoT Build | 0:6ae2f7bca550 | 1094 | |
AzureIoTClient | 4:98007eb79fa8 | 1095 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1096 | } |
AzureIoTClient | 4:98007eb79fa8 | 1097 | } |
AzureIoTClient | 4:98007eb79fa8 | 1098 | else |
AzureIoTClient | 4:98007eb79fa8 | 1099 | { |
AzureIoTClient | 4:98007eb79fa8 | 1100 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1101 | } |
AzureIoTClient | 4:98007eb79fa8 | 1102 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1103 | |
AzureIoTClient | 4:98007eb79fa8 | 1104 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1105 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1106 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1107 | int connection_listen(CONNECTION_HANDLE connection) |
Azure.IoT Build | 0:6ae2f7bca550 | 1108 | { |
AzureIoTClient | 4:98007eb79fa8 | 1109 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1110 | |
AzureIoTClient | 4:98007eb79fa8 | 1111 | if (connection == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 1112 | { |
AzureIoTClient | 4:98007eb79fa8 | 1113 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1114 | } |
AzureIoTClient | 4:98007eb79fa8 | 1115 | else |
AzureIoTClient | 4:98007eb79fa8 | 1116 | { |
AzureIoTClient | 4:98007eb79fa8 | 1117 | if (!connection->is_underlying_io_open) |
AzureIoTClient | 4:98007eb79fa8 | 1118 | { |
AzureIoTClient | 4:98007eb79fa8 | 1119 | if (xio_open(connection->io, connection_on_io_open_complete, connection, connection_on_bytes_received, connection, connection_on_io_error, connection) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 1120 | { |
AzureIoTClient | 4:98007eb79fa8 | 1121 | connection_set_state(connection, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 1122 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1123 | } |
AzureIoTClient | 4:98007eb79fa8 | 1124 | else |
AzureIoTClient | 4:98007eb79fa8 | 1125 | { |
AzureIoTClient | 4:98007eb79fa8 | 1126 | connection->is_underlying_io_open = 1; |
Azure.IoT Build | 0:6ae2f7bca550 | 1127 | |
AzureIoTClient | 4:98007eb79fa8 | 1128 | connection_set_state(connection, CONNECTION_STATE_HDR_EXCH); |
Azure.IoT Build | 0:6ae2f7bca550 | 1129 | |
AzureIoTClient | 4:98007eb79fa8 | 1130 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1131 | } |
AzureIoTClient | 4:98007eb79fa8 | 1132 | } |
AzureIoTClient | 4:98007eb79fa8 | 1133 | else |
AzureIoTClient | 4:98007eb79fa8 | 1134 | { |
AzureIoTClient | 4:98007eb79fa8 | 1135 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1136 | } |
AzureIoTClient | 4:98007eb79fa8 | 1137 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1138 | |
AzureIoTClient | 4:98007eb79fa8 | 1139 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1140 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1141 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1142 | int connection_close(CONNECTION_HANDLE connection, const char* condition_value, const char* description) |
Azure.IoT Build | 0:6ae2f7bca550 | 1143 | { |
AzureIoTClient | 4:98007eb79fa8 | 1144 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1145 | |
AzureIoTClient | 4:98007eb79fa8 | 1146 | if (connection == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 1147 | { |
AzureIoTClient | 4:98007eb79fa8 | 1148 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1149 | } |
AzureIoTClient | 4:98007eb79fa8 | 1150 | else |
AzureIoTClient | 4:98007eb79fa8 | 1151 | { |
AzureIoTClient | 4:98007eb79fa8 | 1152 | if (condition_value != NULL) |
AzureIoTClient | 4:98007eb79fa8 | 1153 | { |
AzureIoTClient | 4:98007eb79fa8 | 1154 | close_connection_with_error(connection, condition_value, description); |
AzureIoTClient | 4:98007eb79fa8 | 1155 | } |
AzureIoTClient | 4:98007eb79fa8 | 1156 | else |
AzureIoTClient | 4:98007eb79fa8 | 1157 | { |
AzureIoTClient | 4:98007eb79fa8 | 1158 | (void)send_close_frame(connection, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 1159 | connection_set_state(connection, CONNECTION_STATE_END); |
AzureIoTClient | 4:98007eb79fa8 | 1160 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1161 | |
AzureIoTClient | 4:98007eb79fa8 | 1162 | (void)xio_close(connection->io, NULL, NULL); |
AzureIoTClient | 4:98007eb79fa8 | 1163 | connection->is_underlying_io_open = 1; |
Azure.IoT Build | 0:6ae2f7bca550 | 1164 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1165 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1166 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1167 | |
AzureIoTClient | 4:98007eb79fa8 | 1168 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1169 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1170 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1171 | int connection_set_max_frame_size(CONNECTION_HANDLE connection, uint32_t max_frame_size) |
Azure.IoT Build | 0:6ae2f7bca550 | 1172 | { |
AzureIoTClient | 4:98007eb79fa8 | 1173 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1174 | |
AzureIoTClient | 4:98007eb79fa8 | 1175 | /* Codes_SRS_CONNECTION_01_163: [If connection is NULL, connection_set_max_frame_size shall fail and return a non-zero value.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1176 | if ((connection == NULL) || |
AzureIoTClient | 4:98007eb79fa8 | 1177 | /* Codes_SRS_CONNECTION_01_150: [If the max_frame_size is invalid then connection_set_max_frame_size shall fail and return a non-zero value.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1178 | /* Codes_SRS_CONNECTION_01_167: [Both peers MUST accept frames of up to 512 (MIN-MAX-FRAME-SIZE) octets.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1179 | (max_frame_size < 512)) |
AzureIoTClient | 4:98007eb79fa8 | 1180 | { |
AzureIoTClient | 4:98007eb79fa8 | 1181 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1182 | } |
AzureIoTClient | 4:98007eb79fa8 | 1183 | else |
AzureIoTClient | 4:98007eb79fa8 | 1184 | { |
AzureIoTClient | 4:98007eb79fa8 | 1185 | /* Codes_SRS_CONNECTION_01_157: [If connection_set_max_frame_size is called after the initial Open frame has been sent, it shall fail and return a non-zero value.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1186 | if (connection->connection_state != CONNECTION_STATE_START) |
AzureIoTClient | 4:98007eb79fa8 | 1187 | { |
AzureIoTClient | 4:98007eb79fa8 | 1188 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1189 | } |
AzureIoTClient | 4:98007eb79fa8 | 1190 | else |
AzureIoTClient | 4:98007eb79fa8 | 1191 | { |
AzureIoTClient | 4:98007eb79fa8 | 1192 | /* Codes_SRS_CONNECTION_01_148: [connection_set_max_frame_size shall set the max_frame_size associated with a connection.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1193 | /* Codes_SRS_CONNECTION_01_164: [If connection_set_max_frame_size fails, the previous max_frame_size setting shall be retained.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1194 | connection->max_frame_size = max_frame_size; |
Azure.IoT Build | 0:6ae2f7bca550 | 1195 | |
AzureIoTClient | 4:98007eb79fa8 | 1196 | /* Codes_SRS_CONNECTION_01_149: [On success connection_set_max_frame_size shall return 0.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1197 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1198 | } |
AzureIoTClient | 4:98007eb79fa8 | 1199 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1200 | |
AzureIoTClient | 4:98007eb79fa8 | 1201 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1202 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1203 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1204 | int connection_get_max_frame_size(CONNECTION_HANDLE connection, uint32_t* max_frame_size) |
Azure.IoT Build | 0:6ae2f7bca550 | 1205 | { |
AzureIoTClient | 4:98007eb79fa8 | 1206 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1207 | |
AzureIoTClient | 4:98007eb79fa8 | 1208 | /* Codes_SRS_CONNECTION_01_170: [If connection or max_frame_size is NULL, connection_get_max_frame_size shall fail and return a non-zero value.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1209 | if ((connection == NULL) || |
AzureIoTClient | 4:98007eb79fa8 | 1210 | (max_frame_size == NULL)) |
AzureIoTClient | 4:98007eb79fa8 | 1211 | { |
AzureIoTClient | 4:98007eb79fa8 | 1212 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1213 | } |
AzureIoTClient | 4:98007eb79fa8 | 1214 | else |
AzureIoTClient | 4:98007eb79fa8 | 1215 | { |
AzureIoTClient | 4:98007eb79fa8 | 1216 | /* Codes_SRS_CONNECTION_01_168: [connection_get_max_frame_size shall return in the max_frame_size argument the current max frame size setting.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1217 | *max_frame_size = connection->max_frame_size; |
Azure.IoT Build | 0:6ae2f7bca550 | 1218 | |
AzureIoTClient | 4:98007eb79fa8 | 1219 | /* Codes_SRS_CONNECTION_01_169: [On success, connection_get_max_frame_size shall return 0.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1220 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1221 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1222 | |
AzureIoTClient | 4:98007eb79fa8 | 1223 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1224 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1225 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1226 | int connection_set_channel_max(CONNECTION_HANDLE connection, uint16_t channel_max) |
Azure.IoT Build | 0:6ae2f7bca550 | 1227 | { |
AzureIoTClient | 4:98007eb79fa8 | 1228 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1229 | |
AzureIoTClient | 4:98007eb79fa8 | 1230 | /* Codes_SRS_CONNECTION_01_181: [If connection is NULL then connection_set_channel_max shall fail and return a non-zero value.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1231 | if (connection == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 1232 | { |
AzureIoTClient | 4:98007eb79fa8 | 1233 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1234 | } |
AzureIoTClient | 4:98007eb79fa8 | 1235 | else |
AzureIoTClient | 4:98007eb79fa8 | 1236 | { |
AzureIoTClient | 4:98007eb79fa8 | 1237 | /* Codes_SRS_CONNECTION_01_156: [If connection_set_channel_max is called after the initial Open frame has been sent, it shall fail and return a non-zero value.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1238 | if (connection->connection_state != CONNECTION_STATE_START) |
AzureIoTClient | 4:98007eb79fa8 | 1239 | { |
AzureIoTClient | 4:98007eb79fa8 | 1240 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1241 | } |
AzureIoTClient | 4:98007eb79fa8 | 1242 | else |
AzureIoTClient | 4:98007eb79fa8 | 1243 | { |
AzureIoTClient | 4:98007eb79fa8 | 1244 | /* Codes_SRS_CONNECTION_01_153: [connection_set_channel_max shall set the channel_max associated with a connection.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1245 | /* Codes_SRS_CONNECTION_01_165: [If connection_set_channel_max fails, the previous channel_max setting shall be retained.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1246 | connection->channel_max = channel_max; |
Azure.IoT Build | 0:6ae2f7bca550 | 1247 | |
AzureIoTClient | 4:98007eb79fa8 | 1248 | /* Codes_SRS_CONNECTION_01_154: [On success connection_set_channel_max shall return 0.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1249 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1250 | } |
AzureIoTClient | 4:98007eb79fa8 | 1251 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1252 | |
AzureIoTClient | 4:98007eb79fa8 | 1253 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1254 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1255 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1256 | int connection_get_channel_max(CONNECTION_HANDLE connection, uint16_t* channel_max) |
Azure.IoT Build | 0:6ae2f7bca550 | 1257 | { |
AzureIoTClient | 4:98007eb79fa8 | 1258 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1259 | |
AzureIoTClient | 4:98007eb79fa8 | 1260 | /* Codes_SRS_CONNECTION_01_184: [If connection or channel_max is NULL, connection_get_channel_max shall fail and return a non-zero value.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1261 | if ((connection == NULL) || |
AzureIoTClient | 4:98007eb79fa8 | 1262 | (channel_max == NULL)) |
AzureIoTClient | 4:98007eb79fa8 | 1263 | { |
AzureIoTClient | 4:98007eb79fa8 | 1264 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1265 | } |
AzureIoTClient | 4:98007eb79fa8 | 1266 | else |
AzureIoTClient | 4:98007eb79fa8 | 1267 | { |
AzureIoTClient | 4:98007eb79fa8 | 1268 | /* Codes_SRS_CONNECTION_01_182: [connection_get_channel_max shall return in the channel_max argument the current channel_max setting.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1269 | *channel_max = connection->channel_max; |
Azure.IoT Build | 0:6ae2f7bca550 | 1270 | |
AzureIoTClient | 4:98007eb79fa8 | 1271 | /* Codes_SRS_CONNECTION_01_183: [On success, connection_get_channel_max shall return 0.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1272 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1273 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1274 | |
AzureIoTClient | 4:98007eb79fa8 | 1275 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1276 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1277 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1278 | int connection_set_idle_timeout(CONNECTION_HANDLE connection, milliseconds idle_timeout) |
Azure.IoT Build | 0:6ae2f7bca550 | 1279 | { |
AzureIoTClient | 4:98007eb79fa8 | 1280 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1281 | |
AzureIoTClient | 4:98007eb79fa8 | 1282 | /* Codes_SRS_CONNECTION_01_191: [If connection is NULL, connection_set_idle_timeout shall fail and return a non-zero value.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1283 | if (connection == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 1284 | { |
AzureIoTClient | 4:98007eb79fa8 | 1285 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1286 | } |
AzureIoTClient | 4:98007eb79fa8 | 1287 | else |
AzureIoTClient | 4:98007eb79fa8 | 1288 | { |
AzureIoTClient | 4:98007eb79fa8 | 1289 | /* Codes_SRS_CONNECTION_01_158: [If connection_set_idle_timeout is called after the initial Open frame has been sent, it shall fail and return a non-zero value.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1290 | if (connection->connection_state != CONNECTION_STATE_START) |
AzureIoTClient | 4:98007eb79fa8 | 1291 | { |
AzureIoTClient | 4:98007eb79fa8 | 1292 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1293 | } |
AzureIoTClient | 4:98007eb79fa8 | 1294 | else |
AzureIoTClient | 4:98007eb79fa8 | 1295 | { |
AzureIoTClient | 4:98007eb79fa8 | 1296 | /* Codes_SRS_CONNECTION_01_159: [connection_set_idle_timeout shall set the idle_timeout associated with a connection.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1297 | /* Codes_SRS_CONNECTION_01_166: [If connection_set_idle_timeout fails, the previous idle_timeout setting shall be retained.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1298 | connection->idle_timeout = idle_timeout; |
AzureIoTClient | 4:98007eb79fa8 | 1299 | connection->idle_timeout_specified = true; |
Azure.IoT Build | 0:6ae2f7bca550 | 1300 | |
AzureIoTClient | 4:98007eb79fa8 | 1301 | /* Codes_SRS_CONNECTION_01_160: [On success connection_set_idle_timeout shall return 0.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1302 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1303 | } |
AzureIoTClient | 4:98007eb79fa8 | 1304 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1305 | |
AzureIoTClient | 4:98007eb79fa8 | 1306 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1307 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1308 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1309 | int connection_get_idle_timeout(CONNECTION_HANDLE connection, milliseconds* idle_timeout) |
Azure.IoT Build | 0:6ae2f7bca550 | 1310 | { |
AzureIoTClient | 4:98007eb79fa8 | 1311 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1312 | |
AzureIoTClient | 4:98007eb79fa8 | 1313 | /* Codes_SRS_CONNECTION_01_190: [If connection or idle_timeout is NULL, connection_get_idle_timeout shall fail and return a non-zero value.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1314 | if ((connection == NULL) || |
AzureIoTClient | 4:98007eb79fa8 | 1315 | (idle_timeout == NULL)) |
AzureIoTClient | 4:98007eb79fa8 | 1316 | { |
AzureIoTClient | 4:98007eb79fa8 | 1317 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1318 | } |
AzureIoTClient | 4:98007eb79fa8 | 1319 | else |
AzureIoTClient | 4:98007eb79fa8 | 1320 | { |
AzureIoTClient | 4:98007eb79fa8 | 1321 | /* Codes_SRS_CONNECTION_01_188: [connection_get_idle_timeout shall return in the idle_timeout argument the current idle_timeout setting.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1322 | *idle_timeout = connection->idle_timeout; |
Azure.IoT Build | 0:6ae2f7bca550 | 1323 | |
AzureIoTClient | 4:98007eb79fa8 | 1324 | /* Codes_SRS_CONNECTION_01_189: [On success, connection_get_idle_timeout shall return 0.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1325 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1326 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1327 | |
AzureIoTClient | 4:98007eb79fa8 | 1328 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1329 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1330 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1331 | int connection_get_remote_max_frame_size(CONNECTION_HANDLE connection, uint32_t* remote_max_frame_size) |
Azure.IoT Build | 0:6ae2f7bca550 | 1332 | { |
AzureIoTClient | 4:98007eb79fa8 | 1333 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1334 | |
AzureIoTClient | 4:98007eb79fa8 | 1335 | if ((connection == NULL) || |
AzureIoTClient | 4:98007eb79fa8 | 1336 | (remote_max_frame_size == NULL)) |
AzureIoTClient | 4:98007eb79fa8 | 1337 | { |
AzureIoTClient | 4:98007eb79fa8 | 1338 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1339 | } |
AzureIoTClient | 4:98007eb79fa8 | 1340 | else |
AzureIoTClient | 4:98007eb79fa8 | 1341 | { |
AzureIoTClient | 4:98007eb79fa8 | 1342 | *remote_max_frame_size = connection->remote_max_frame_size; |
Azure.IoT Build | 0:6ae2f7bca550 | 1343 | |
AzureIoTClient | 4:98007eb79fa8 | 1344 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1345 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1346 | |
AzureIoTClient | 4:98007eb79fa8 | 1347 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1348 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1349 | |
AzureIoTClient | 1:eab586236bfe | 1350 | uint64_t connection_handle_deadlines(CONNECTION_HANDLE connection) |
AzureIoTClient | 1:eab586236bfe | 1351 | { |
AzureIoTClient | 1:eab586236bfe | 1352 | uint64_t local_deadline = (uint64_t )-1; |
AzureIoTClient | 1:eab586236bfe | 1353 | uint64_t remote_deadline = (uint64_t)-1; |
AzureIoTClient | 1:eab586236bfe | 1354 | |
AzureIoTClient | 1:eab586236bfe | 1355 | if (connection != NULL) |
AzureIoTClient | 1:eab586236bfe | 1356 | { |
Azure.IoT.Build | 14:0b0e28c75ded | 1357 | tickcounter_ms_t current_ms; |
AzureIoTClient | 1:eab586236bfe | 1358 | |
AzureIoTClient | 1:eab586236bfe | 1359 | if (tickcounter_get_current_ms(connection->tick_counter, ¤t_ms) != 0) |
AzureIoTClient | 1:eab586236bfe | 1360 | { |
AzureIoTClient | 1:eab586236bfe | 1361 | close_connection_with_error(connection, "amqp:internal-error", "Could not get tick count"); |
AzureIoTClient | 1:eab586236bfe | 1362 | } |
AzureIoTClient | 1:eab586236bfe | 1363 | else |
AzureIoTClient | 1:eab586236bfe | 1364 | { |
AzureIoTClient | 1:eab586236bfe | 1365 | if (connection->idle_timeout_specified && (connection->idle_timeout != 0)) |
AzureIoTClient | 1:eab586236bfe | 1366 | { |
AzureIoTClient | 1:eab586236bfe | 1367 | /* Calculate time until configured idle timeout expires */ |
AzureIoTClient | 1:eab586236bfe | 1368 | |
AzureIoTClient | 1:eab586236bfe | 1369 | uint64_t time_since_last_received = current_ms - connection->last_frame_received_time; |
AzureIoTClient | 1:eab586236bfe | 1370 | if (time_since_last_received < connection->idle_timeout) |
AzureIoTClient | 1:eab586236bfe | 1371 | { |
AzureIoTClient | 1:eab586236bfe | 1372 | local_deadline = connection->idle_timeout - time_since_last_received; |
AzureIoTClient | 1:eab586236bfe | 1373 | } |
AzureIoTClient | 1:eab586236bfe | 1374 | else |
AzureIoTClient | 1:eab586236bfe | 1375 | { |
AzureIoTClient | 1:eab586236bfe | 1376 | local_deadline = 0; |
AzureIoTClient | 1:eab586236bfe | 1377 | |
AzureIoTClient | 1:eab586236bfe | 1378 | /* close connection */ |
AzureIoTClient | 1:eab586236bfe | 1379 | close_connection_with_error(connection, "amqp:internal-error", "No frame received for the idle timeout"); |
AzureIoTClient | 1:eab586236bfe | 1380 | } |
AzureIoTClient | 1:eab586236bfe | 1381 | } |
AzureIoTClient | 1:eab586236bfe | 1382 | |
AzureIoTClient | 1:eab586236bfe | 1383 | if (local_deadline != 0 && connection->remote_idle_timeout != 0) |
AzureIoTClient | 1:eab586236bfe | 1384 | { |
AzureIoTClient | 1:eab586236bfe | 1385 | /* Calculate time until remote idle timeout expires */ |
AzureIoTClient | 1:eab586236bfe | 1386 | |
AzureIoTClient | 1:eab586236bfe | 1387 | uint64_t remote_idle_timeout = (connection->remote_idle_timeout / 2); |
AzureIoTClient | 1:eab586236bfe | 1388 | uint64_t time_since_last_sent = current_ms - connection->last_frame_sent_time; |
AzureIoTClient | 1:eab586236bfe | 1389 | |
AzureIoTClient | 1:eab586236bfe | 1390 | if (time_since_last_sent < remote_idle_timeout) |
AzureIoTClient | 1:eab586236bfe | 1391 | { |
AzureIoTClient | 1:eab586236bfe | 1392 | remote_deadline = remote_idle_timeout - time_since_last_sent; |
AzureIoTClient | 1:eab586236bfe | 1393 | } |
AzureIoTClient | 1:eab586236bfe | 1394 | else |
AzureIoTClient | 1:eab586236bfe | 1395 | { |
AzureIoTClient | 1:eab586236bfe | 1396 | connection->on_send_complete = NULL; |
AzureIoTClient | 1:eab586236bfe | 1397 | if (amqp_frame_codec_encode_empty_frame(connection->amqp_frame_codec, 0, on_bytes_encoded, connection) != 0) |
AzureIoTClient | 1:eab586236bfe | 1398 | { |
AzureIoTClient | 1:eab586236bfe | 1399 | /* close connection */ |
AzureIoTClient | 1:eab586236bfe | 1400 | close_connection_with_error(connection, "amqp:internal-error", "Cannot send empty frame"); |
AzureIoTClient | 1:eab586236bfe | 1401 | } |
AzureIoTClient | 1:eab586236bfe | 1402 | else |
AzureIoTClient | 1:eab586236bfe | 1403 | { |
AzureIoTClient | 6:641a9672db08 | 1404 | if (connection->is_trace_on == 1) |
AzureIoTClient | 6:641a9672db08 | 1405 | { |
AzureIoTClient | 16:22a72cf8e416 | 1406 | LOG(AZ_LOG_TRACE, LOG_LINE, "-> Empty frame"); |
AzureIoTClient | 6:641a9672db08 | 1407 | } |
AzureIoTClient | 1:eab586236bfe | 1408 | |
AzureIoTClient | 1:eab586236bfe | 1409 | connection->last_frame_sent_time = current_ms; |
AzureIoTClient | 1:eab586236bfe | 1410 | |
AzureIoTClient | 1:eab586236bfe | 1411 | remote_deadline = remote_idle_timeout; |
AzureIoTClient | 1:eab586236bfe | 1412 | } |
AzureIoTClient | 1:eab586236bfe | 1413 | } |
AzureIoTClient | 1:eab586236bfe | 1414 | } |
AzureIoTClient | 1:eab586236bfe | 1415 | } |
AzureIoTClient | 1:eab586236bfe | 1416 | } |
AzureIoTClient | 1:eab586236bfe | 1417 | |
AzureIoTClient | 1:eab586236bfe | 1418 | /* Return the shorter of each deadline, or 0 to indicate connection closed */ |
AzureIoTClient | 1:eab586236bfe | 1419 | return local_deadline > remote_deadline ? remote_deadline : local_deadline; |
AzureIoTClient | 1:eab586236bfe | 1420 | } |
AzureIoTClient | 1:eab586236bfe | 1421 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1422 | void connection_dowork(CONNECTION_HANDLE connection) |
Azure.IoT Build | 0:6ae2f7bca550 | 1423 | { |
AzureIoTClient | 4:98007eb79fa8 | 1424 | /* Codes_SRS_CONNECTION_01_078: [If handle is NULL, connection_dowork shall do nothing.] */ |
AzureIoTClient | 1:eab586236bfe | 1425 | if (connection != NULL) |
AzureIoTClient | 1:eab586236bfe | 1426 | { |
AzureIoTClient | 1:eab586236bfe | 1427 | if (connection_handle_deadlines(connection) > 0) |
AzureIoTClient | 1:eab586236bfe | 1428 | { |
AzureIoTClient | 1:eab586236bfe | 1429 | /* Codes_SRS_CONNECTION_01_076: [connection_dowork shall schedule the underlying IO interface to do its work by calling xio_dowork.] */ |
AzureIoTClient | 1:eab586236bfe | 1430 | xio_dowork(connection->io); |
AzureIoTClient | 1:eab586236bfe | 1431 | } |
AzureIoTClient | 1:eab586236bfe | 1432 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1433 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1434 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1435 | ENDPOINT_HANDLE connection_create_endpoint(CONNECTION_HANDLE connection) |
Azure.IoT Build | 0:6ae2f7bca550 | 1436 | { |
AzureIoTClient | 4:98007eb79fa8 | 1437 | ENDPOINT_INSTANCE* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1438 | |
AzureIoTClient | 4:98007eb79fa8 | 1439 | /* Codes_SRS_CONNECTION_01_113: [If connection, on_endpoint_frame_received or on_connection_state_changed is NULL, connection_create_endpoint shall fail and return NULL.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1440 | /* Codes_SRS_CONNECTION_01_193: [The context argument shall be allowed to be NULL.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1441 | if (connection == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 1442 | { |
AzureIoTClient | 4:98007eb79fa8 | 1443 | result = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 1444 | } |
AzureIoTClient | 4:98007eb79fa8 | 1445 | else |
AzureIoTClient | 4:98007eb79fa8 | 1446 | { |
AzureIoTClient | 4:98007eb79fa8 | 1447 | /* Codes_SRS_CONNECTION_01_115: [If no more endpoints can be created due to all channels being used, connection_create_endpoint shall fail and return NULL.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1448 | if (connection->endpoint_count >= connection->channel_max) |
AzureIoTClient | 4:98007eb79fa8 | 1449 | { |
AzureIoTClient | 4:98007eb79fa8 | 1450 | result = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 1451 | } |
AzureIoTClient | 4:98007eb79fa8 | 1452 | else |
AzureIoTClient | 4:98007eb79fa8 | 1453 | { |
AzureIoTClient | 4:98007eb79fa8 | 1454 | uint32_t i = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 1455 | |
AzureIoTClient | 4:98007eb79fa8 | 1456 | /* Codes_SRS_CONNECTION_01_128: [The lowest number outgoing channel shall be associated with the newly created endpoint.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1457 | for (i = 0; i < connection->endpoint_count; i++) |
AzureIoTClient | 4:98007eb79fa8 | 1458 | { |
AzureIoTClient | 4:98007eb79fa8 | 1459 | if (connection->endpoints[i]->outgoing_channel > i) |
AzureIoTClient | 4:98007eb79fa8 | 1460 | { |
AzureIoTClient | 4:98007eb79fa8 | 1461 | /* found a gap in the sorted endpoint array */ |
AzureIoTClient | 4:98007eb79fa8 | 1462 | break; |
AzureIoTClient | 4:98007eb79fa8 | 1463 | } |
AzureIoTClient | 4:98007eb79fa8 | 1464 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1465 | |
AzureIoTClient | 4:98007eb79fa8 | 1466 | /* Codes_SRS_CONNECTION_01_127: [On success, connection_create_endpoint shall return a non-NULL handle to the newly created endpoint.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1467 | result = amqpalloc_malloc(sizeof(ENDPOINT_INSTANCE)); |
AzureIoTClient | 4:98007eb79fa8 | 1468 | /* Codes_SRS_CONNECTION_01_196: [If memory cannot be allocated for the new endpoint, connection_create_endpoint shall fail and return NULL.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1469 | if (result != NULL) |
AzureIoTClient | 4:98007eb79fa8 | 1470 | { |
AzureIoTClient | 4:98007eb79fa8 | 1471 | ENDPOINT_INSTANCE** new_endpoints; |
Azure.IoT Build | 0:6ae2f7bca550 | 1472 | |
AzureIoTClient | 4:98007eb79fa8 | 1473 | result->on_endpoint_frame_received = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 1474 | result->on_connection_state_changed = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 1475 | result->callback_context = NULL; |
AzureIoTClient | 6:641a9672db08 | 1476 | result->outgoing_channel = (uint16_t)i; |
AzureIoTClient | 4:98007eb79fa8 | 1477 | result->connection = connection; |
Azure.IoT Build | 0:6ae2f7bca550 | 1478 | |
AzureIoTClient | 4:98007eb79fa8 | 1479 | /* Codes_SRS_CONNECTION_01_197: [The newly created endpoint shall be added to the endpoints list, so that it can be tracked.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1480 | new_endpoints = (ENDPOINT_INSTANCE**)amqpalloc_realloc(connection->endpoints, sizeof(ENDPOINT_INSTANCE*) * (connection->endpoint_count + 1)); |
AzureIoTClient | 4:98007eb79fa8 | 1481 | if (new_endpoints == NULL) |
AzureIoTClient | 4:98007eb79fa8 | 1482 | { |
AzureIoTClient | 4:98007eb79fa8 | 1483 | /* Tests_SRS_CONNECTION_01_198: [If adding the endpoint to the endpoints list tracked by the connection fails, connection_create_endpoint shall fail and return NULL.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1484 | amqpalloc_free(result); |
AzureIoTClient | 4:98007eb79fa8 | 1485 | result = NULL; |
AzureIoTClient | 4:98007eb79fa8 | 1486 | } |
AzureIoTClient | 4:98007eb79fa8 | 1487 | else |
AzureIoTClient | 4:98007eb79fa8 | 1488 | { |
AzureIoTClient | 4:98007eb79fa8 | 1489 | connection->endpoints = new_endpoints; |
Azure.IoT Build | 0:6ae2f7bca550 | 1490 | |
AzureIoTClient | 4:98007eb79fa8 | 1491 | if (i < connection->endpoint_count) |
AzureIoTClient | 4:98007eb79fa8 | 1492 | { |
AzureIoTClient | 4:98007eb79fa8 | 1493 | (void)memmove(&connection->endpoints[i + 1], &connection->endpoints[i], sizeof(ENDPOINT_INSTANCE*) * (connection->endpoint_count - i)); |
AzureIoTClient | 4:98007eb79fa8 | 1494 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1495 | |
AzureIoTClient | 4:98007eb79fa8 | 1496 | connection->endpoints[i] = result; |
AzureIoTClient | 4:98007eb79fa8 | 1497 | connection->endpoint_count++; |
Azure.IoT Build | 0:6ae2f7bca550 | 1498 | |
AzureIoTClient | 4:98007eb79fa8 | 1499 | /* Codes_SRS_CONNECTION_01_112: [connection_create_endpoint shall create a new endpoint that can be used by a session.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1500 | } |
AzureIoTClient | 4:98007eb79fa8 | 1501 | } |
AzureIoTClient | 4:98007eb79fa8 | 1502 | } |
AzureIoTClient | 4:98007eb79fa8 | 1503 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1504 | |
AzureIoTClient | 4:98007eb79fa8 | 1505 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1506 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1507 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1508 | int connection_start_endpoint(ENDPOINT_HANDLE endpoint, ON_ENDPOINT_FRAME_RECEIVED on_endpoint_frame_received, ON_CONNECTION_STATE_CHANGED on_connection_state_changed, void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 1509 | { |
AzureIoTClient | 4:98007eb79fa8 | 1510 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1511 | |
AzureIoTClient | 4:98007eb79fa8 | 1512 | if ((endpoint == NULL) || |
AzureIoTClient | 4:98007eb79fa8 | 1513 | (on_endpoint_frame_received == NULL) || |
AzureIoTClient | 4:98007eb79fa8 | 1514 | (on_connection_state_changed == NULL)) |
AzureIoTClient | 4:98007eb79fa8 | 1515 | { |
AzureIoTClient | 4:98007eb79fa8 | 1516 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1517 | } |
AzureIoTClient | 4:98007eb79fa8 | 1518 | else |
AzureIoTClient | 4:98007eb79fa8 | 1519 | { |
AzureIoTClient | 4:98007eb79fa8 | 1520 | endpoint->on_endpoint_frame_received = on_endpoint_frame_received; |
AzureIoTClient | 4:98007eb79fa8 | 1521 | endpoint->on_connection_state_changed = on_connection_state_changed; |
AzureIoTClient | 4:98007eb79fa8 | 1522 | endpoint->callback_context = context; |
Azure.IoT Build | 0:6ae2f7bca550 | 1523 | |
AzureIoTClient | 4:98007eb79fa8 | 1524 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1525 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1526 | |
AzureIoTClient | 4:98007eb79fa8 | 1527 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1528 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1529 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1530 | int connection_endpoint_get_incoming_channel(ENDPOINT_HANDLE endpoint, uint16_t* incoming_channel) |
Azure.IoT Build | 0:6ae2f7bca550 | 1531 | { |
AzureIoTClient | 4:98007eb79fa8 | 1532 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1533 | |
AzureIoTClient | 4:98007eb79fa8 | 1534 | if ((endpoint == NULL) || |
AzureIoTClient | 4:98007eb79fa8 | 1535 | (incoming_channel == NULL)) |
AzureIoTClient | 4:98007eb79fa8 | 1536 | { |
AzureIoTClient | 4:98007eb79fa8 | 1537 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1538 | } |
AzureIoTClient | 4:98007eb79fa8 | 1539 | else |
AzureIoTClient | 4:98007eb79fa8 | 1540 | { |
AzureIoTClient | 4:98007eb79fa8 | 1541 | *incoming_channel = endpoint->incoming_channel; |
AzureIoTClient | 4:98007eb79fa8 | 1542 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1543 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1544 | |
AzureIoTClient | 4:98007eb79fa8 | 1545 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1546 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1547 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1548 | /* Codes_SRS_CONNECTION_01_129: [connection_destroy_endpoint shall free all resources associated with an endpoint created by connection_create_endpoint.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 1549 | void connection_destroy_endpoint(ENDPOINT_HANDLE endpoint) |
Azure.IoT Build | 0:6ae2f7bca550 | 1550 | { |
AzureIoTClient | 4:98007eb79fa8 | 1551 | if (endpoint != NULL) |
AzureIoTClient | 4:98007eb79fa8 | 1552 | { |
AzureIoTClient | 4:98007eb79fa8 | 1553 | CONNECTION_INSTANCE* connection = (CONNECTION_INSTANCE*)endpoint->connection; |
AzureIoTClient | 4:98007eb79fa8 | 1554 | size_t i; |
Azure.IoT Build | 0:6ae2f7bca550 | 1555 | |
AzureIoTClient | 4:98007eb79fa8 | 1556 | for (i = 0; i < connection->endpoint_count; i++) |
AzureIoTClient | 4:98007eb79fa8 | 1557 | { |
AzureIoTClient | 4:98007eb79fa8 | 1558 | if (connection->endpoints[i] == endpoint) |
AzureIoTClient | 4:98007eb79fa8 | 1559 | { |
AzureIoTClient | 4:98007eb79fa8 | 1560 | break; |
AzureIoTClient | 4:98007eb79fa8 | 1561 | } |
AzureIoTClient | 4:98007eb79fa8 | 1562 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1563 | |
AzureIoTClient | 4:98007eb79fa8 | 1564 | /* Codes_SRS_CONNECTION_01_130: [The outgoing channel associated with the endpoint shall be released by removing the endpoint from the endpoint list.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1565 | /* Codes_SRS_CONNECTION_01_131: [Any incoming channel number associated with the endpoint shall be released.] */ |
AzureIoTClient | 11:b9de84324501 | 1566 | if (i < connection->endpoint_count && i > 0) |
AzureIoTClient | 11:b9de84324501 | 1567 | { |
AzureIoTClient | 11:b9de84324501 | 1568 | (void)memmove(connection->endpoints + i, connection->endpoints + i + 1, sizeof(ENDPOINT_INSTANCE*) * (connection->endpoint_count - i - 1)); |
Azure.IoT Build | 0:6ae2f7bca550 | 1569 | |
AzureIoTClient | 11:b9de84324501 | 1570 | ENDPOINT_INSTANCE** new_endpoints = (ENDPOINT_INSTANCE**)amqpalloc_realloc(connection->endpoints, (connection->endpoint_count - 1) * sizeof(ENDPOINT_INSTANCE*)); |
AzureIoTClient | 11:b9de84324501 | 1571 | if (new_endpoints != NULL) |
AzureIoTClient | 11:b9de84324501 | 1572 | { |
AzureIoTClient | 11:b9de84324501 | 1573 | connection->endpoints = new_endpoints; |
AzureIoTClient | 11:b9de84324501 | 1574 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1575 | |
AzureIoTClient | 11:b9de84324501 | 1576 | connection->endpoint_count--; |
AzureIoTClient | 11:b9de84324501 | 1577 | } |
AzureIoTClient | 11:b9de84324501 | 1578 | else if (connection->endpoint_count == 1) |
AzureIoTClient | 11:b9de84324501 | 1579 | { |
AzureIoTClient | 11:b9de84324501 | 1580 | amqpalloc_free(connection->endpoints); |
AzureIoTClient | 11:b9de84324501 | 1581 | connection->endpoints = NULL; |
AzureIoTClient | 11:b9de84324501 | 1582 | connection->endpoint_count = 0; |
AzureIoTClient | 11:b9de84324501 | 1583 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1584 | |
AzureIoTClient | 4:98007eb79fa8 | 1585 | amqpalloc_free(endpoint); |
AzureIoTClient | 4:98007eb79fa8 | 1586 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1587 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1588 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1589 | /* Codes_SRS_CONNECTION_01_247: [connection_encode_frame shall send a frame for a certain endpoint.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 1590 | int connection_encode_frame(ENDPOINT_HANDLE endpoint, const AMQP_VALUE performative, PAYLOAD* payloads, size_t payload_count, ON_SEND_COMPLETE on_send_complete, void* callback_context) |
Azure.IoT Build | 0:6ae2f7bca550 | 1591 | { |
AzureIoTClient | 4:98007eb79fa8 | 1592 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1593 | |
AzureIoTClient | 4:98007eb79fa8 | 1594 | /* Codes_SRS_CONNECTION_01_249: [If endpoint or performative are NULL, connection_encode_frame shall fail and return a non-zero value.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1595 | if ((endpoint == NULL) || |
AzureIoTClient | 4:98007eb79fa8 | 1596 | (performative == NULL)) |
AzureIoTClient | 4:98007eb79fa8 | 1597 | { |
AzureIoTClient | 4:98007eb79fa8 | 1598 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1599 | } |
AzureIoTClient | 4:98007eb79fa8 | 1600 | else |
AzureIoTClient | 4:98007eb79fa8 | 1601 | { |
AzureIoTClient | 4:98007eb79fa8 | 1602 | CONNECTION_INSTANCE* connection = (CONNECTION_INSTANCE*)endpoint->connection; |
AzureIoTClient | 4:98007eb79fa8 | 1603 | AMQP_FRAME_CODEC_HANDLE amqp_frame_codec = connection->amqp_frame_codec; |
Azure.IoT Build | 0:6ae2f7bca550 | 1604 | |
AzureIoTClient | 4:98007eb79fa8 | 1605 | /* Codes_SRS_CONNECTION_01_254: [If connection_encode_frame is called before the connection is in the OPENED state, connection_encode_frame shall fail and return a non-zero value.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1606 | if (connection->connection_state != CONNECTION_STATE_OPENED) |
AzureIoTClient | 4:98007eb79fa8 | 1607 | { |
AzureIoTClient | 4:98007eb79fa8 | 1608 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1609 | } |
AzureIoTClient | 4:98007eb79fa8 | 1610 | else |
AzureIoTClient | 4:98007eb79fa8 | 1611 | { |
AzureIoTClient | 4:98007eb79fa8 | 1612 | /* Codes_SRS_CONNECTION_01_255: [The payload size shall be computed based on all the payload chunks passed as argument in payloads.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1613 | /* Codes_SRS_CONNECTION_01_250: [connection_encode_frame shall initiate the frame send by calling amqp_frame_codec_begin_encode_frame.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1614 | /* Codes_SRS_CONNECTION_01_251: [The channel number passed to amqp_frame_codec_begin_encode_frame shall be the outgoing channel number associated with the endpoint by connection_create_endpoint.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1615 | /* Codes_SRS_CONNECTION_01_252: [The performative passed to amqp_frame_codec_begin_encode_frame shall be the performative argument of connection_encode_frame.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1616 | connection->on_send_complete = on_send_complete; |
AzureIoTClient | 4:98007eb79fa8 | 1617 | connection->on_send_complete_callback_context = callback_context; |
AzureIoTClient | 4:98007eb79fa8 | 1618 | if (amqp_frame_codec_encode_frame(amqp_frame_codec, endpoint->outgoing_channel, performative, payloads, payload_count, on_bytes_encoded, connection) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 1619 | { |
AzureIoTClient | 4:98007eb79fa8 | 1620 | /* Codes_SRS_CONNECTION_01_253: [If amqp_frame_codec_begin_encode_frame or amqp_frame_codec_encode_payload_bytes fails, then connection_encode_frame shall fail and return a non-zero value.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1621 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1622 | } |
AzureIoTClient | 4:98007eb79fa8 | 1623 | else |
AzureIoTClient | 4:98007eb79fa8 | 1624 | { |
AzureIoTClient | 4:98007eb79fa8 | 1625 | if (connection->is_trace_on == 1) |
AzureIoTClient | 4:98007eb79fa8 | 1626 | { |
Azure.IoT Build | 5:ae49385aff34 | 1627 | log_outgoing_frame(performative); |
AzureIoTClient | 4:98007eb79fa8 | 1628 | } |
Azure.IoT Build | 5:ae49385aff34 | 1629 | |
AzureIoTClient | 4:98007eb79fa8 | 1630 | if (tickcounter_get_current_ms(connection->tick_counter, &connection->last_frame_sent_time) != 0) |
AzureIoTClient | 4:98007eb79fa8 | 1631 | { |
AzureIoTClient | 4:98007eb79fa8 | 1632 | result = __LINE__; |
AzureIoTClient | 4:98007eb79fa8 | 1633 | } |
AzureIoTClient | 4:98007eb79fa8 | 1634 | else |
AzureIoTClient | 4:98007eb79fa8 | 1635 | { |
AzureIoTClient | 4:98007eb79fa8 | 1636 | /* Codes_SRS_CONNECTION_01_248: [On success it shall return 0.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1637 | result = 0; |
AzureIoTClient | 4:98007eb79fa8 | 1638 | } |
AzureIoTClient | 4:98007eb79fa8 | 1639 | } |
AzureIoTClient | 4:98007eb79fa8 | 1640 | } |
AzureIoTClient | 4:98007eb79fa8 | 1641 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1642 | |
AzureIoTClient | 4:98007eb79fa8 | 1643 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1644 | } |
AzureIoTClient | 4:98007eb79fa8 | 1645 | |
AzureIoTClient | 4:98007eb79fa8 | 1646 | void connection_set_trace(CONNECTION_HANDLE connection, bool traceOn) |
AzureIoTClient | 4:98007eb79fa8 | 1647 | { |
AzureIoTClient | 4:98007eb79fa8 | 1648 | /* Codes_SRS_CONNECTION_07_002: [If connection is NULL then connection_set_trace shall do nothing.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1649 | if (connection != NULL) |
AzureIoTClient | 4:98007eb79fa8 | 1650 | { |
AzureIoTClient | 4:98007eb79fa8 | 1651 | /* Codes_SRS_CONNECTION_07_001: [connection_set_trace shall set the ability to turn on and off trace logging.] */ |
AzureIoTClient | 4:98007eb79fa8 | 1652 | connection->is_trace_on = traceOn ? 1 : 0; |
AzureIoTClient | 4:98007eb79fa8 | 1653 | } |
AzureIoTClient | 4:98007eb79fa8 | 1654 | } |