A small memory footprint AMQP implimentation
Dependents: iothub_client_sample_amqp remote_monitoring simplesample_amqp
session.c@28:add19eb7defa, 2017-06-30 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Jun 30 10:41:22 2017 -0700
- Revision:
- 28:add19eb7defa
- Parent:
- 27:d74f1cea23e1
- Child:
- 29:4a11413cf217
1.1.18
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> |
AzureIoTClient | 21:f9c433d8e6ca | 6 | #include "azure_c_shared_utility/optimize_size.h" |
AzureIoTClient | 21:f9c433d8e6ca | 7 | #include "azure_c_shared_utility/gballoc.h" |
AzureIoTClient | 27:d74f1cea23e1 | 8 | #include "azure_c_shared_utility/xlogging.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 9 | #include "azure_uamqp_c/session.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 10 | #include "azure_uamqp_c/connection.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 11 | |
Azure.IoT Build | 0:6ae2f7bca550 | 12 | typedef struct LINK_ENDPOINT_INSTANCE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 13 | { |
AzureIoTClient | 28:add19eb7defa | 14 | char* name; |
AzureIoTClient | 28:add19eb7defa | 15 | handle input_handle; |
AzureIoTClient | 28:add19eb7defa | 16 | handle output_handle; |
AzureIoTClient | 28:add19eb7defa | 17 | ON_ENDPOINT_FRAME_RECEIVED frame_received_callback; |
AzureIoTClient | 28:add19eb7defa | 18 | ON_SESSION_STATE_CHANGED on_session_state_changed; |
AzureIoTClient | 28:add19eb7defa | 19 | ON_SESSION_FLOW_ON on_session_flow_on; |
AzureIoTClient | 28:add19eb7defa | 20 | void* callback_context; |
AzureIoTClient | 28:add19eb7defa | 21 | SESSION_HANDLE session; |
Azure.IoT Build | 0:6ae2f7bca550 | 22 | } LINK_ENDPOINT_INSTANCE; |
Azure.IoT Build | 0:6ae2f7bca550 | 23 | |
Azure.IoT Build | 0:6ae2f7bca550 | 24 | typedef struct SESSION_INSTANCE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 25 | { |
AzureIoTClient | 28:add19eb7defa | 26 | ON_ENDPOINT_FRAME_RECEIVED frame_received_callback; |
AzureIoTClient | 28:add19eb7defa | 27 | void* frame_received_callback_context; |
AzureIoTClient | 28:add19eb7defa | 28 | SESSION_STATE session_state; |
AzureIoTClient | 28:add19eb7defa | 29 | SESSION_STATE previous_session_state; |
AzureIoTClient | 28:add19eb7defa | 30 | CONNECTION_HANDLE connection; |
AzureIoTClient | 28:add19eb7defa | 31 | ENDPOINT_HANDLE endpoint; |
AzureIoTClient | 28:add19eb7defa | 32 | LINK_ENDPOINT_INSTANCE** link_endpoints; |
AzureIoTClient | 28:add19eb7defa | 33 | uint32_t link_endpoint_count; |
Azure.IoT Build | 0:6ae2f7bca550 | 34 | |
AzureIoTClient | 28:add19eb7defa | 35 | ON_LINK_ATTACHED on_link_attached; |
AzureIoTClient | 28:add19eb7defa | 36 | void* on_link_attached_callback_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 37 | |
AzureIoTClient | 28:add19eb7defa | 38 | /* Codes_SRS_SESSION_01_016: [next-outgoing-id The next-outgoing-id is the transfer-id to assign to the next transfer frame.] */ |
AzureIoTClient | 28:add19eb7defa | 39 | transfer_number next_outgoing_id; |
AzureIoTClient | 28:add19eb7defa | 40 | transfer_number next_incoming_id; |
Azure.IoT Build | 0:6ae2f7bca550 | 41 | uint32_t desired_incoming_window; |
AzureIoTClient | 28:add19eb7defa | 42 | uint32_t incoming_window; |
AzureIoTClient | 28:add19eb7defa | 43 | uint32_t outgoing_window; |
AzureIoTClient | 28:add19eb7defa | 44 | handle handle_max; |
AzureIoTClient | 28:add19eb7defa | 45 | uint32_t remote_incoming_window; |
AzureIoTClient | 28:add19eb7defa | 46 | uint32_t remote_outgoing_window; |
AzureIoTClient | 28:add19eb7defa | 47 | int is_underlying_connection_open : 1; |
Azure.IoT Build | 0:6ae2f7bca550 | 48 | } SESSION_INSTANCE; |
Azure.IoT Build | 0:6ae2f7bca550 | 49 | |
AzureIoTClient | 6:641a9672db08 | 50 | #define UNDERLYING_CONNECTION_NOT_OPEN 0 |
AzureIoTClient | 6:641a9672db08 | 51 | #define UNDERLYING_CONNECTION_OPEN -1 |
AzureIoTClient | 6:641a9672db08 | 52 | |
Azure.IoT Build | 0:6ae2f7bca550 | 53 | static void session_set_state(SESSION_INSTANCE* session_instance, SESSION_STATE session_state) |
Azure.IoT Build | 0:6ae2f7bca550 | 54 | { |
AzureIoTClient | 28:add19eb7defa | 55 | uint64_t i; |
Azure.IoT Build | 0:6ae2f7bca550 | 56 | |
AzureIoTClient | 28:add19eb7defa | 57 | session_instance->previous_session_state = session_instance->session_state; |
AzureIoTClient | 28:add19eb7defa | 58 | session_instance->session_state = session_state; |
Azure.IoT Build | 0:6ae2f7bca550 | 59 | |
AzureIoTClient | 28:add19eb7defa | 60 | for (i = 0; i < session_instance->link_endpoint_count; i++) |
AzureIoTClient | 28:add19eb7defa | 61 | { |
AzureIoTClient | 28:add19eb7defa | 62 | if (session_instance->link_endpoints[i]->on_session_state_changed != NULL) |
AzureIoTClient | 28:add19eb7defa | 63 | { |
AzureIoTClient | 28:add19eb7defa | 64 | session_instance->link_endpoints[i]->on_session_state_changed(session_instance->link_endpoints[i]->callback_context, session_state, session_instance->previous_session_state); |
AzureIoTClient | 28:add19eb7defa | 65 | } |
AzureIoTClient | 28:add19eb7defa | 66 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 67 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 68 | |
Azure.IoT Build | 0:6ae2f7bca550 | 69 | static int send_end_frame(SESSION_INSTANCE* session_instance, ERROR_HANDLE error_handle) |
Azure.IoT Build | 0:6ae2f7bca550 | 70 | { |
AzureIoTClient | 28:add19eb7defa | 71 | int result; |
AzureIoTClient | 28:add19eb7defa | 72 | END_HANDLE end_performative; |
Azure.IoT Build | 0:6ae2f7bca550 | 73 | |
AzureIoTClient | 28:add19eb7defa | 74 | end_performative = end_create(); |
AzureIoTClient | 28:add19eb7defa | 75 | if (end_performative == NULL) |
AzureIoTClient | 28:add19eb7defa | 76 | { |
AzureIoTClient | 28:add19eb7defa | 77 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 78 | } |
AzureIoTClient | 28:add19eb7defa | 79 | else |
AzureIoTClient | 28:add19eb7defa | 80 | { |
AzureIoTClient | 28:add19eb7defa | 81 | if ((error_handle != NULL) && |
AzureIoTClient | 28:add19eb7defa | 82 | (end_set_error(end_performative, error_handle) != 0)) |
AzureIoTClient | 28:add19eb7defa | 83 | { |
AzureIoTClient | 28:add19eb7defa | 84 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 85 | } |
AzureIoTClient | 28:add19eb7defa | 86 | else |
AzureIoTClient | 28:add19eb7defa | 87 | { |
AzureIoTClient | 28:add19eb7defa | 88 | AMQP_VALUE end_performative_value = amqpvalue_create_end(end_performative); |
AzureIoTClient | 28:add19eb7defa | 89 | if (end_performative_value == NULL) |
AzureIoTClient | 28:add19eb7defa | 90 | { |
AzureIoTClient | 28:add19eb7defa | 91 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 92 | } |
AzureIoTClient | 28:add19eb7defa | 93 | else |
AzureIoTClient | 28:add19eb7defa | 94 | { |
AzureIoTClient | 28:add19eb7defa | 95 | if (connection_encode_frame(session_instance->endpoint, end_performative_value, NULL, 0, NULL, NULL) != 0) |
AzureIoTClient | 28:add19eb7defa | 96 | { |
AzureIoTClient | 28:add19eb7defa | 97 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 98 | } |
AzureIoTClient | 28:add19eb7defa | 99 | else |
AzureIoTClient | 28:add19eb7defa | 100 | { |
AzureIoTClient | 28:add19eb7defa | 101 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 102 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 103 | |
AzureIoTClient | 28:add19eb7defa | 104 | amqpvalue_destroy(end_performative_value); |
AzureIoTClient | 28:add19eb7defa | 105 | } |
AzureIoTClient | 28:add19eb7defa | 106 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 107 | |
AzureIoTClient | 28:add19eb7defa | 108 | end_destroy(end_performative); |
AzureIoTClient | 28:add19eb7defa | 109 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 110 | |
AzureIoTClient | 28:add19eb7defa | 111 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 112 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 113 | |
Azure.IoT Build | 0:6ae2f7bca550 | 114 | static void end_session_with_error(SESSION_INSTANCE* session_instance, const char* condition_value, const char* description) |
Azure.IoT Build | 0:6ae2f7bca550 | 115 | { |
AzureIoTClient | 28:add19eb7defa | 116 | ERROR_HANDLE error_handle = error_create(condition_value); |
AzureIoTClient | 28:add19eb7defa | 117 | if (error_handle == NULL) |
AzureIoTClient | 28:add19eb7defa | 118 | { |
AzureIoTClient | 28:add19eb7defa | 119 | /* fatal error */ |
AzureIoTClient | 28:add19eb7defa | 120 | session_set_state(session_instance, SESSION_STATE_DISCARDING); |
AzureIoTClient | 28:add19eb7defa | 121 | (void)connection_close(session_instance->connection, "amqp:internal-error", "Cannot allocate error handle to end session"); |
AzureIoTClient | 28:add19eb7defa | 122 | } |
AzureIoTClient | 28:add19eb7defa | 123 | else |
AzureIoTClient | 28:add19eb7defa | 124 | { |
AzureIoTClient | 28:add19eb7defa | 125 | if ((error_set_description(error_handle, description) != 0) || |
AzureIoTClient | 28:add19eb7defa | 126 | (send_end_frame(session_instance, error_handle) != 0)) |
AzureIoTClient | 28:add19eb7defa | 127 | { |
AzureIoTClient | 28:add19eb7defa | 128 | /* fatal error */ |
AzureIoTClient | 28:add19eb7defa | 129 | session_set_state(session_instance, SESSION_STATE_DISCARDING); |
AzureIoTClient | 28:add19eb7defa | 130 | (void)connection_close(session_instance->connection, "amqp:internal-error", "Cannot allocate error handle to end session"); |
AzureIoTClient | 28:add19eb7defa | 131 | } |
AzureIoTClient | 28:add19eb7defa | 132 | else |
AzureIoTClient | 28:add19eb7defa | 133 | { |
AzureIoTClient | 28:add19eb7defa | 134 | session_set_state(session_instance, SESSION_STATE_DISCARDING); |
AzureIoTClient | 28:add19eb7defa | 135 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 136 | |
AzureIoTClient | 28:add19eb7defa | 137 | error_destroy(error_handle); |
AzureIoTClient | 28:add19eb7defa | 138 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 139 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 140 | |
Azure.IoT Build | 0:6ae2f7bca550 | 141 | static int send_begin(SESSION_INSTANCE* session_instance) |
Azure.IoT Build | 0:6ae2f7bca550 | 142 | { |
AzureIoTClient | 28:add19eb7defa | 143 | int result; |
AzureIoTClient | 28:add19eb7defa | 144 | BEGIN_HANDLE begin = begin_create(session_instance->next_outgoing_id, session_instance->incoming_window, session_instance->outgoing_window); |
Azure.IoT Build | 0:6ae2f7bca550 | 145 | |
AzureIoTClient | 28:add19eb7defa | 146 | if (begin == NULL) |
AzureIoTClient | 28:add19eb7defa | 147 | { |
AzureIoTClient | 28:add19eb7defa | 148 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 149 | } |
AzureIoTClient | 28:add19eb7defa | 150 | else |
AzureIoTClient | 28:add19eb7defa | 151 | { |
AzureIoTClient | 28:add19eb7defa | 152 | uint16_t remote_channel; |
AzureIoTClient | 28:add19eb7defa | 153 | if (begin_set_handle_max(begin, session_instance->handle_max) != 0) |
AzureIoTClient | 28:add19eb7defa | 154 | { |
AzureIoTClient | 28:add19eb7defa | 155 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 156 | } |
AzureIoTClient | 28:add19eb7defa | 157 | else if ((session_instance->session_state == SESSION_STATE_BEGIN_RCVD) && |
AzureIoTClient | 28:add19eb7defa | 158 | ((connection_endpoint_get_incoming_channel(session_instance->endpoint, &remote_channel) != 0) || |
AzureIoTClient | 28:add19eb7defa | 159 | (begin_set_remote_channel(begin, remote_channel) != 0))) |
AzureIoTClient | 28:add19eb7defa | 160 | { |
AzureIoTClient | 28:add19eb7defa | 161 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 162 | } |
AzureIoTClient | 28:add19eb7defa | 163 | else |
AzureIoTClient | 28:add19eb7defa | 164 | { |
AzureIoTClient | 28:add19eb7defa | 165 | AMQP_VALUE begin_performative_value = amqpvalue_create_begin(begin); |
AzureIoTClient | 28:add19eb7defa | 166 | if (begin_performative_value == NULL) |
AzureIoTClient | 28:add19eb7defa | 167 | { |
AzureIoTClient | 28:add19eb7defa | 168 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 169 | } |
AzureIoTClient | 28:add19eb7defa | 170 | else |
AzureIoTClient | 28:add19eb7defa | 171 | { |
AzureIoTClient | 28:add19eb7defa | 172 | if (connection_encode_frame(session_instance->endpoint, begin_performative_value, NULL, 0, NULL, NULL) != 0) |
AzureIoTClient | 28:add19eb7defa | 173 | { |
AzureIoTClient | 28:add19eb7defa | 174 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 175 | } |
AzureIoTClient | 28:add19eb7defa | 176 | else |
AzureIoTClient | 28:add19eb7defa | 177 | { |
AzureIoTClient | 28:add19eb7defa | 178 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 179 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 180 | |
AzureIoTClient | 28:add19eb7defa | 181 | amqpvalue_destroy(begin_performative_value); |
AzureIoTClient | 28:add19eb7defa | 182 | } |
AzureIoTClient | 28:add19eb7defa | 183 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 184 | |
AzureIoTClient | 28:add19eb7defa | 185 | begin_destroy(begin); |
AzureIoTClient | 28:add19eb7defa | 186 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 187 | |
AzureIoTClient | 28:add19eb7defa | 188 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 189 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 190 | |
Azure.IoT Build | 0:6ae2f7bca550 | 191 | static int send_flow(SESSION_INSTANCE* session) |
Azure.IoT Build | 0:6ae2f7bca550 | 192 | { |
AzureIoTClient | 28:add19eb7defa | 193 | int result; |
AzureIoTClient | 28:add19eb7defa | 194 | if (session == NULL) |
AzureIoTClient | 28:add19eb7defa | 195 | { |
AzureIoTClient | 28:add19eb7defa | 196 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 197 | } |
AzureIoTClient | 28:add19eb7defa | 198 | else |
AzureIoTClient | 28:add19eb7defa | 199 | { |
AzureIoTClient | 28:add19eb7defa | 200 | FLOW_HANDLE flow = flow_create(session->incoming_window, session->next_outgoing_id, session->outgoing_window); |
Azure.IoT Build | 0:6ae2f7bca550 | 201 | |
AzureIoTClient | 28:add19eb7defa | 202 | if (flow == NULL) |
AzureIoTClient | 28:add19eb7defa | 203 | { |
AzureIoTClient | 28:add19eb7defa | 204 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 205 | } |
AzureIoTClient | 28:add19eb7defa | 206 | else |
AzureIoTClient | 28:add19eb7defa | 207 | { |
AzureIoTClient | 28:add19eb7defa | 208 | if (flow_set_next_incoming_id(flow, session->next_incoming_id) != 0) |
AzureIoTClient | 28:add19eb7defa | 209 | { |
AzureIoTClient | 28:add19eb7defa | 210 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 211 | } |
AzureIoTClient | 28:add19eb7defa | 212 | else |
AzureIoTClient | 28:add19eb7defa | 213 | { |
AzureIoTClient | 28:add19eb7defa | 214 | AMQP_VALUE flow_performative_value = amqpvalue_create_flow(flow); |
AzureIoTClient | 28:add19eb7defa | 215 | if (flow_performative_value == NULL) |
AzureIoTClient | 28:add19eb7defa | 216 | { |
AzureIoTClient | 28:add19eb7defa | 217 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 218 | } |
AzureIoTClient | 28:add19eb7defa | 219 | else |
AzureIoTClient | 28:add19eb7defa | 220 | { |
AzureIoTClient | 28:add19eb7defa | 221 | if (connection_encode_frame(session->endpoint, flow_performative_value, NULL, 0, NULL, NULL) != 0) |
AzureIoTClient | 28:add19eb7defa | 222 | { |
AzureIoTClient | 28:add19eb7defa | 223 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 224 | } |
AzureIoTClient | 28:add19eb7defa | 225 | else |
AzureIoTClient | 28:add19eb7defa | 226 | { |
AzureIoTClient | 28:add19eb7defa | 227 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 228 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 229 | |
AzureIoTClient | 28:add19eb7defa | 230 | amqpvalue_destroy(flow_performative_value); |
AzureIoTClient | 28:add19eb7defa | 231 | } |
AzureIoTClient | 28:add19eb7defa | 232 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 233 | |
AzureIoTClient | 28:add19eb7defa | 234 | flow_destroy(flow); |
AzureIoTClient | 28:add19eb7defa | 235 | } |
AzureIoTClient | 28:add19eb7defa | 236 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 237 | |
AzureIoTClient | 28:add19eb7defa | 238 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 239 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 240 | |
Azure.IoT Build | 0:6ae2f7bca550 | 241 | static LINK_ENDPOINT_INSTANCE* find_link_endpoint_by_name(SESSION_INSTANCE* session, const char* name) |
Azure.IoT Build | 0:6ae2f7bca550 | 242 | { |
AzureIoTClient | 28:add19eb7defa | 243 | uint32_t i; |
AzureIoTClient | 28:add19eb7defa | 244 | LINK_ENDPOINT_INSTANCE* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 245 | |
AzureIoTClient | 28:add19eb7defa | 246 | for (i = 0; i < session->link_endpoint_count; i++) |
AzureIoTClient | 28:add19eb7defa | 247 | { |
AzureIoTClient | 28:add19eb7defa | 248 | if (strcmp(session->link_endpoints[i]->name, name) == 0) |
AzureIoTClient | 28:add19eb7defa | 249 | { |
AzureIoTClient | 28:add19eb7defa | 250 | break; |
AzureIoTClient | 28:add19eb7defa | 251 | } |
AzureIoTClient | 28:add19eb7defa | 252 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 253 | |
AzureIoTClient | 28:add19eb7defa | 254 | if (i == session->link_endpoint_count) |
AzureIoTClient | 28:add19eb7defa | 255 | { |
AzureIoTClient | 28:add19eb7defa | 256 | result = NULL; |
AzureIoTClient | 28:add19eb7defa | 257 | } |
AzureIoTClient | 28:add19eb7defa | 258 | else |
AzureIoTClient | 28:add19eb7defa | 259 | { |
AzureIoTClient | 28:add19eb7defa | 260 | result = session->link_endpoints[i]; |
AzureIoTClient | 28:add19eb7defa | 261 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 262 | |
AzureIoTClient | 28:add19eb7defa | 263 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 264 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 265 | |
Azure.IoT Build | 0:6ae2f7bca550 | 266 | static LINK_ENDPOINT_INSTANCE* find_link_endpoint_by_input_handle(SESSION_INSTANCE* session, handle input_handle) |
Azure.IoT Build | 0:6ae2f7bca550 | 267 | { |
AzureIoTClient | 28:add19eb7defa | 268 | uint32_t i; |
AzureIoTClient | 28:add19eb7defa | 269 | LINK_ENDPOINT_INSTANCE* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 270 | |
AzureIoTClient | 28:add19eb7defa | 271 | for (i = 0; i < session->link_endpoint_count; i++) |
AzureIoTClient | 28:add19eb7defa | 272 | { |
AzureIoTClient | 28:add19eb7defa | 273 | if (session->link_endpoints[i]->input_handle == input_handle) |
AzureIoTClient | 28:add19eb7defa | 274 | { |
AzureIoTClient | 28:add19eb7defa | 275 | break; |
AzureIoTClient | 28:add19eb7defa | 276 | } |
AzureIoTClient | 28:add19eb7defa | 277 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 278 | |
AzureIoTClient | 28:add19eb7defa | 279 | if (i == session->link_endpoint_count) |
AzureIoTClient | 28:add19eb7defa | 280 | { |
AzureIoTClient | 28:add19eb7defa | 281 | result = NULL; |
AzureIoTClient | 28:add19eb7defa | 282 | } |
AzureIoTClient | 28:add19eb7defa | 283 | else |
AzureIoTClient | 28:add19eb7defa | 284 | { |
AzureIoTClient | 28:add19eb7defa | 285 | result = session->link_endpoints[i]; |
AzureIoTClient | 28:add19eb7defa | 286 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 287 | |
AzureIoTClient | 28:add19eb7defa | 288 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 289 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 290 | |
Azure.IoT Build | 0:6ae2f7bca550 | 291 | static void on_connection_state_changed(void* context, CONNECTION_STATE new_connection_state, CONNECTION_STATE previous_connection_state) |
Azure.IoT Build | 0:6ae2f7bca550 | 292 | { |
AzureIoTClient | 28:add19eb7defa | 293 | SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 294 | |
AzureIoTClient | 28:add19eb7defa | 295 | /* Codes_SRS_SESSION_01_060: [If the previous connection state is not OPENED and the new connection state is OPENED, the BEGIN frame shall be sent out and the state shall be switched to BEGIN_SENT.] */ |
AzureIoTClient | 28:add19eb7defa | 296 | if ((new_connection_state == CONNECTION_STATE_OPENED) && (previous_connection_state != CONNECTION_STATE_OPENED) && (session_instance->session_state == SESSION_STATE_UNMAPPED)) |
AzureIoTClient | 28:add19eb7defa | 297 | { |
AzureIoTClient | 28:add19eb7defa | 298 | if (send_begin(session_instance) == 0) |
AzureIoTClient | 28:add19eb7defa | 299 | { |
AzureIoTClient | 28:add19eb7defa | 300 | session_set_state(session_instance, SESSION_STATE_BEGIN_SENT); |
AzureIoTClient | 28:add19eb7defa | 301 | } |
AzureIoTClient | 28:add19eb7defa | 302 | } |
AzureIoTClient | 28:add19eb7defa | 303 | /* Codes_SRS_SESSION_01_061: [If the previous connection state is OPENED and the new connection state is not OPENED anymore, the state shall be switched to DISCARDING.] */ |
AzureIoTClient | 28:add19eb7defa | 304 | else if ((new_connection_state == CONNECTION_STATE_CLOSE_RCVD) || (new_connection_state == CONNECTION_STATE_END)) |
AzureIoTClient | 28:add19eb7defa | 305 | { |
AzureIoTClient | 28:add19eb7defa | 306 | session_set_state(session_instance, SESSION_STATE_DISCARDING); |
AzureIoTClient | 28:add19eb7defa | 307 | } |
AzureIoTClient | 28:add19eb7defa | 308 | /* Codes_SRS_SESSION_09_001: [If the new connection state is ERROR, the state shall be switched to ERROR.] */ |
AzureIoTClient | 28:add19eb7defa | 309 | else if (new_connection_state == CONNECTION_STATE_ERROR) |
AzureIoTClient | 28:add19eb7defa | 310 | { |
AzureIoTClient | 28:add19eb7defa | 311 | session_set_state(session_instance, SESSION_STATE_ERROR); |
AzureIoTClient | 28:add19eb7defa | 312 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 313 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 314 | |
Azure.IoT Build | 0:6ae2f7bca550 | 315 | static void on_frame_received(void* context, AMQP_VALUE performative, uint32_t payload_size, const unsigned char* payload_bytes) |
Azure.IoT Build | 0:6ae2f7bca550 | 316 | { |
AzureIoTClient | 28:add19eb7defa | 317 | SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)context; |
AzureIoTClient | 28:add19eb7defa | 318 | AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(performative); |
Azure.IoT Build | 0:6ae2f7bca550 | 319 | |
AzureIoTClient | 28:add19eb7defa | 320 | if (is_begin_type_by_descriptor(descriptor)) |
AzureIoTClient | 28:add19eb7defa | 321 | { |
AzureIoTClient | 28:add19eb7defa | 322 | BEGIN_HANDLE begin_handle; |
Azure.IoT Build | 0:6ae2f7bca550 | 323 | |
AzureIoTClient | 28:add19eb7defa | 324 | if (amqpvalue_get_begin(performative, &begin_handle) != 0) |
AzureIoTClient | 28:add19eb7defa | 325 | { |
AzureIoTClient | 28:add19eb7defa | 326 | connection_close(session_instance->connection, "amqp:decode-error", "Cannot decode BEGIN frame"); |
AzureIoTClient | 28:add19eb7defa | 327 | } |
AzureIoTClient | 28:add19eb7defa | 328 | else |
AzureIoTClient | 28:add19eb7defa | 329 | { |
AzureIoTClient | 28:add19eb7defa | 330 | if ((begin_get_incoming_window(begin_handle, &session_instance->remote_incoming_window) != 0) || |
AzureIoTClient | 28:add19eb7defa | 331 | (begin_get_next_outgoing_id(begin_handle, &session_instance->next_incoming_id) != 0)) |
AzureIoTClient | 28:add19eb7defa | 332 | { |
AzureIoTClient | 28:add19eb7defa | 333 | /* error */ |
AzureIoTClient | 28:add19eb7defa | 334 | begin_destroy(begin_handle); |
AzureIoTClient | 28:add19eb7defa | 335 | session_set_state(session_instance, SESSION_STATE_DISCARDING); |
AzureIoTClient | 28:add19eb7defa | 336 | connection_close(session_instance->connection, "amqp:decode-error", "Cannot get incoming windows and next outgoing id"); |
AzureIoTClient | 28:add19eb7defa | 337 | } |
AzureIoTClient | 28:add19eb7defa | 338 | else |
AzureIoTClient | 28:add19eb7defa | 339 | { |
AzureIoTClient | 28:add19eb7defa | 340 | begin_destroy(begin_handle); |
Azure.IoT Build | 0:6ae2f7bca550 | 341 | |
AzureIoTClient | 28:add19eb7defa | 342 | if (session_instance->session_state == SESSION_STATE_BEGIN_SENT) |
AzureIoTClient | 28:add19eb7defa | 343 | { |
AzureIoTClient | 28:add19eb7defa | 344 | session_set_state(session_instance, SESSION_STATE_MAPPED); |
AzureIoTClient | 28:add19eb7defa | 345 | } |
AzureIoTClient | 28:add19eb7defa | 346 | else if(session_instance->session_state == SESSION_STATE_UNMAPPED) |
AzureIoTClient | 28:add19eb7defa | 347 | { |
AzureIoTClient | 28:add19eb7defa | 348 | session_set_state(session_instance, SESSION_STATE_BEGIN_RCVD); |
AzureIoTClient | 28:add19eb7defa | 349 | if (send_begin(session_instance) != 0) |
AzureIoTClient | 28:add19eb7defa | 350 | { |
AzureIoTClient | 28:add19eb7defa | 351 | connection_close(session_instance->connection, "amqp:internal-error", "Failed sending BEGIN frame"); |
AzureIoTClient | 28:add19eb7defa | 352 | session_set_state(session_instance, SESSION_STATE_DISCARDING); |
AzureIoTClient | 28:add19eb7defa | 353 | } |
AzureIoTClient | 28:add19eb7defa | 354 | else |
AzureIoTClient | 28:add19eb7defa | 355 | { |
AzureIoTClient | 28:add19eb7defa | 356 | session_set_state(session_instance, SESSION_STATE_MAPPED); |
AzureIoTClient | 28:add19eb7defa | 357 | } |
AzureIoTClient | 28:add19eb7defa | 358 | } |
AzureIoTClient | 28:add19eb7defa | 359 | } |
AzureIoTClient | 28:add19eb7defa | 360 | } |
AzureIoTClient | 28:add19eb7defa | 361 | } |
AzureIoTClient | 28:add19eb7defa | 362 | else if (is_attach_type_by_descriptor(descriptor)) |
AzureIoTClient | 28:add19eb7defa | 363 | { |
AzureIoTClient | 28:add19eb7defa | 364 | const char* name = NULL; |
AzureIoTClient | 28:add19eb7defa | 365 | ATTACH_HANDLE attach_handle; |
Azure.IoT Build | 0:6ae2f7bca550 | 366 | |
AzureIoTClient | 28:add19eb7defa | 367 | if (amqpvalue_get_attach(performative, &attach_handle) != 0) |
AzureIoTClient | 28:add19eb7defa | 368 | { |
AzureIoTClient | 28:add19eb7defa | 369 | end_session_with_error(session_instance, "amqp:decode-error", "Cannot decode ATTACH frame"); |
AzureIoTClient | 28:add19eb7defa | 370 | } |
AzureIoTClient | 28:add19eb7defa | 371 | else |
AzureIoTClient | 28:add19eb7defa | 372 | { |
AzureIoTClient | 28:add19eb7defa | 373 | role role; |
AzureIoTClient | 28:add19eb7defa | 374 | AMQP_VALUE source; |
AzureIoTClient | 28:add19eb7defa | 375 | AMQP_VALUE target; |
Azure.IoT Build | 0:6ae2f7bca550 | 376 | |
AzureIoTClient | 28:add19eb7defa | 377 | if ((attach_get_name(attach_handle, &name) != 0) || |
AzureIoTClient | 28:add19eb7defa | 378 | (attach_get_role(attach_handle, &role) != 0) || |
AzureIoTClient | 28:add19eb7defa | 379 | (attach_get_source(attach_handle, &source) != 0) || |
AzureIoTClient | 28:add19eb7defa | 380 | (attach_get_target(attach_handle, &target) != 0)) |
AzureIoTClient | 28:add19eb7defa | 381 | { |
AzureIoTClient | 28:add19eb7defa | 382 | end_session_with_error(session_instance, "amqp:decode-error", "Cannot get link name from ATTACH frame"); |
AzureIoTClient | 28:add19eb7defa | 383 | } |
AzureIoTClient | 28:add19eb7defa | 384 | else |
AzureIoTClient | 28:add19eb7defa | 385 | { |
AzureIoTClient | 28:add19eb7defa | 386 | LINK_ENDPOINT_INSTANCE* link_endpoint = find_link_endpoint_by_name(session_instance, name); |
AzureIoTClient | 28:add19eb7defa | 387 | if (link_endpoint == NULL) |
AzureIoTClient | 28:add19eb7defa | 388 | { |
AzureIoTClient | 28:add19eb7defa | 389 | /* new link attach */ |
AzureIoTClient | 28:add19eb7defa | 390 | if (session_instance->on_link_attached != NULL) |
AzureIoTClient | 28:add19eb7defa | 391 | { |
AzureIoTClient | 28:add19eb7defa | 392 | LINK_ENDPOINT_HANDLE new_link_endpoint = session_create_link_endpoint(session_instance, name); |
AzureIoTClient | 28:add19eb7defa | 393 | if (new_link_endpoint == NULL) |
AzureIoTClient | 28:add19eb7defa | 394 | { |
AzureIoTClient | 28:add19eb7defa | 395 | end_session_with_error(session_instance, "amqp:internal-error", "Cannot create link endpoint"); |
AzureIoTClient | 28:add19eb7defa | 396 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 397 | else if (attach_get_handle(attach_handle, &new_link_endpoint->input_handle) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 398 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 399 | end_session_with_error(session_instance, "amqp:decode-error", "Cannot get input handle from ATTACH frame"); |
Azure.IoT Build | 0:6ae2f7bca550 | 400 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 401 | else |
AzureIoTClient | 28:add19eb7defa | 402 | { |
AzureIoTClient | 28:add19eb7defa | 403 | if (!session_instance->on_link_attached(session_instance->on_link_attached_callback_context, new_link_endpoint, name, role, source, target)) |
AzureIoTClient | 28:add19eb7defa | 404 | { |
AzureIoTClient | 28:add19eb7defa | 405 | session_destroy_link_endpoint(new_link_endpoint); |
AzureIoTClient | 28:add19eb7defa | 406 | new_link_endpoint = NULL; |
AzureIoTClient | 28:add19eb7defa | 407 | } |
AzureIoTClient | 28:add19eb7defa | 408 | else |
AzureIoTClient | 28:add19eb7defa | 409 | { |
AzureIoTClient | 28:add19eb7defa | 410 | if (new_link_endpoint->frame_received_callback != NULL) |
AzureIoTClient | 28:add19eb7defa | 411 | { |
AzureIoTClient | 28:add19eb7defa | 412 | new_link_endpoint->frame_received_callback(new_link_endpoint->callback_context, performative, payload_size, payload_bytes); |
AzureIoTClient | 28:add19eb7defa | 413 | } |
AzureIoTClient | 28:add19eb7defa | 414 | } |
AzureIoTClient | 28:add19eb7defa | 415 | } |
AzureIoTClient | 28:add19eb7defa | 416 | } |
AzureIoTClient | 28:add19eb7defa | 417 | } |
AzureIoTClient | 28:add19eb7defa | 418 | else |
AzureIoTClient | 28:add19eb7defa | 419 | { |
AzureIoTClient | 28:add19eb7defa | 420 | if (attach_get_handle(attach_handle, &link_endpoint->input_handle) != 0) |
AzureIoTClient | 28:add19eb7defa | 421 | { |
AzureIoTClient | 28:add19eb7defa | 422 | end_session_with_error(session_instance, "amqp:decode-error", "Cannot get input handle from ATTACH frame"); |
AzureIoTClient | 28:add19eb7defa | 423 | } |
AzureIoTClient | 28:add19eb7defa | 424 | else |
AzureIoTClient | 28:add19eb7defa | 425 | { |
AzureIoTClient | 28:add19eb7defa | 426 | link_endpoint->frame_received_callback(link_endpoint->callback_context, performative, payload_size, payload_bytes); |
AzureIoTClient | 28:add19eb7defa | 427 | } |
AzureIoTClient | 28:add19eb7defa | 428 | } |
AzureIoTClient | 28:add19eb7defa | 429 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 430 | |
AzureIoTClient | 28:add19eb7defa | 431 | attach_destroy(attach_handle); |
AzureIoTClient | 28:add19eb7defa | 432 | } |
AzureIoTClient | 28:add19eb7defa | 433 | } |
AzureIoTClient | 28:add19eb7defa | 434 | else if (is_detach_type_by_descriptor(descriptor)) |
AzureIoTClient | 28:add19eb7defa | 435 | { |
AzureIoTClient | 28:add19eb7defa | 436 | DETACH_HANDLE detach_handle; |
Azure.IoT Build | 0:6ae2f7bca550 | 437 | |
AzureIoTClient | 28:add19eb7defa | 438 | if (amqpvalue_get_detach(performative, &detach_handle) != 0) |
AzureIoTClient | 28:add19eb7defa | 439 | { |
AzureIoTClient | 28:add19eb7defa | 440 | end_session_with_error(session_instance, "amqp:decode-error", "Cannot decode DETACH frame"); |
AzureIoTClient | 28:add19eb7defa | 441 | } |
AzureIoTClient | 28:add19eb7defa | 442 | else |
AzureIoTClient | 28:add19eb7defa | 443 | { |
AzureIoTClient | 28:add19eb7defa | 444 | uint32_t remote_handle; |
AzureIoTClient | 28:add19eb7defa | 445 | if (detach_get_handle(detach_handle, &remote_handle) != 0) |
AzureIoTClient | 28:add19eb7defa | 446 | { |
AzureIoTClient | 28:add19eb7defa | 447 | end_session_with_error(session_instance, "amqp:decode-error", "Cannot get handle from DETACH frame"); |
Azure.IoT Build | 0:6ae2f7bca550 | 448 | |
AzureIoTClient | 28:add19eb7defa | 449 | detach_destroy(detach_handle); |
AzureIoTClient | 28:add19eb7defa | 450 | } |
AzureIoTClient | 28:add19eb7defa | 451 | else |
AzureIoTClient | 28:add19eb7defa | 452 | { |
AzureIoTClient | 28:add19eb7defa | 453 | LINK_ENDPOINT_INSTANCE* link_endpoint; |
AzureIoTClient | 28:add19eb7defa | 454 | detach_destroy(detach_handle); |
Azure.IoT Build | 0:6ae2f7bca550 | 455 | |
AzureIoTClient | 28:add19eb7defa | 456 | link_endpoint = find_link_endpoint_by_input_handle(session_instance, remote_handle); |
AzureIoTClient | 28:add19eb7defa | 457 | if (link_endpoint == NULL) |
AzureIoTClient | 28:add19eb7defa | 458 | { |
AzureIoTClient | 28:add19eb7defa | 459 | end_session_with_error(session_instance, "amqp:session:unattached-handle", ""); |
AzureIoTClient | 28:add19eb7defa | 460 | } |
AzureIoTClient | 28:add19eb7defa | 461 | else |
AzureIoTClient | 28:add19eb7defa | 462 | { |
AzureIoTClient | 28:add19eb7defa | 463 | link_endpoint->frame_received_callback(link_endpoint->callback_context, performative, payload_size, payload_bytes); |
AzureIoTClient | 28:add19eb7defa | 464 | } |
AzureIoTClient | 28:add19eb7defa | 465 | } |
AzureIoTClient | 28:add19eb7defa | 466 | } |
AzureIoTClient | 28:add19eb7defa | 467 | } |
AzureIoTClient | 28:add19eb7defa | 468 | else if (is_flow_type_by_descriptor(descriptor)) |
AzureIoTClient | 28:add19eb7defa | 469 | { |
AzureIoTClient | 28:add19eb7defa | 470 | FLOW_HANDLE flow_handle; |
Azure.IoT Build | 0:6ae2f7bca550 | 471 | |
AzureIoTClient | 28:add19eb7defa | 472 | if (amqpvalue_get_flow(performative, &flow_handle) != 0) |
AzureIoTClient | 28:add19eb7defa | 473 | { |
AzureIoTClient | 28:add19eb7defa | 474 | end_session_with_error(session_instance, "amqp:decode-error", "Cannot decode FLOW frame"); |
AzureIoTClient | 28:add19eb7defa | 475 | } |
AzureIoTClient | 28:add19eb7defa | 476 | else |
AzureIoTClient | 28:add19eb7defa | 477 | { |
AzureIoTClient | 28:add19eb7defa | 478 | uint32_t remote_handle; |
AzureIoTClient | 28:add19eb7defa | 479 | transfer_number flow_next_incoming_id; |
AzureIoTClient | 28:add19eb7defa | 480 | uint32_t flow_incoming_window; |
Azure.IoT Build | 0:6ae2f7bca550 | 481 | |
Azure.IoT Build | 0:6ae2f7bca550 | 482 | if (flow_get_next_incoming_id(flow_handle, &flow_next_incoming_id) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 483 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 484 | /* |
Azure.IoT Build | 0:6ae2f7bca550 | 485 | If the next-incoming-id field of the flow frame is not set, |
Azure.IoT Build | 0:6ae2f7bca550 | 486 | then remote-incomingwindow is computed as follows: |
Azure.IoT Build | 0:6ae2f7bca550 | 487 | initial-outgoing-id(endpoint) + incoming-window(flow) - next-outgoing-id(endpoint) |
Azure.IoT Build | 0:6ae2f7bca550 | 488 | */ |
Azure.IoT Build | 0:6ae2f7bca550 | 489 | flow_next_incoming_id = session_instance->next_outgoing_id; |
Azure.IoT Build | 0:6ae2f7bca550 | 490 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 491 | |
AzureIoTClient | 28:add19eb7defa | 492 | if ((flow_get_next_outgoing_id(flow_handle, &session_instance->next_incoming_id) != 0) || |
AzureIoTClient | 28:add19eb7defa | 493 | (flow_get_incoming_window(flow_handle, &flow_incoming_window) != 0)) |
AzureIoTClient | 28:add19eb7defa | 494 | { |
AzureIoTClient | 28:add19eb7defa | 495 | flow_destroy(flow_handle); |
Azure.IoT Build | 0:6ae2f7bca550 | 496 | |
AzureIoTClient | 28:add19eb7defa | 497 | end_session_with_error(session_instance, "amqp:decode-error", "Cannot decode FLOW frame"); |
AzureIoTClient | 28:add19eb7defa | 498 | } |
AzureIoTClient | 28:add19eb7defa | 499 | else |
AzureIoTClient | 28:add19eb7defa | 500 | { |
AzureIoTClient | 28:add19eb7defa | 501 | LINK_ENDPOINT_INSTANCE* link_endpoint_instance = NULL; |
AzureIoTClient | 28:add19eb7defa | 502 | size_t i; |
Azure.IoT Build | 0:6ae2f7bca550 | 503 | |
AzureIoTClient | 28:add19eb7defa | 504 | session_instance->remote_incoming_window = flow_next_incoming_id + flow_incoming_window - session_instance->next_outgoing_id; |
Azure.IoT Build | 0:6ae2f7bca550 | 505 | |
AzureIoTClient | 28:add19eb7defa | 506 | if (flow_get_handle(flow_handle, &remote_handle) == 0) |
AzureIoTClient | 28:add19eb7defa | 507 | { |
AzureIoTClient | 28:add19eb7defa | 508 | link_endpoint_instance = find_link_endpoint_by_input_handle(session_instance, remote_handle); |
AzureIoTClient | 28:add19eb7defa | 509 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 510 | |
AzureIoTClient | 28:add19eb7defa | 511 | flow_destroy(flow_handle); |
Azure.IoT Build | 0:6ae2f7bca550 | 512 | |
AzureIoTClient | 28:add19eb7defa | 513 | if (link_endpoint_instance != NULL) |
AzureIoTClient | 28:add19eb7defa | 514 | { |
AzureIoTClient | 28:add19eb7defa | 515 | link_endpoint_instance->frame_received_callback(link_endpoint_instance->callback_context, performative, payload_size, payload_bytes); |
AzureIoTClient | 28:add19eb7defa | 516 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 517 | |
AzureIoTClient | 28:add19eb7defa | 518 | i = 0; |
AzureIoTClient | 28:add19eb7defa | 519 | while ((session_instance->remote_incoming_window > 0) && (i < session_instance->link_endpoint_count)) |
AzureIoTClient | 28:add19eb7defa | 520 | { |
AzureIoTClient | 28:add19eb7defa | 521 | /* notify the caller that it can send here */ |
AzureIoTClient | 28:add19eb7defa | 522 | if (session_instance->link_endpoints[i]->on_session_flow_on != NULL) |
AzureIoTClient | 28:add19eb7defa | 523 | { |
AzureIoTClient | 28:add19eb7defa | 524 | session_instance->link_endpoints[i]->on_session_flow_on(session_instance->link_endpoints[i]->callback_context); |
AzureIoTClient | 28:add19eb7defa | 525 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 526 | |
AzureIoTClient | 28:add19eb7defa | 527 | i++; |
AzureIoTClient | 28:add19eb7defa | 528 | } |
AzureIoTClient | 28:add19eb7defa | 529 | } |
AzureIoTClient | 28:add19eb7defa | 530 | } |
AzureIoTClient | 28:add19eb7defa | 531 | } |
AzureIoTClient | 28:add19eb7defa | 532 | else if (is_transfer_type_by_descriptor(descriptor)) |
AzureIoTClient | 28:add19eb7defa | 533 | { |
AzureIoTClient | 28:add19eb7defa | 534 | TRANSFER_HANDLE transfer_handle; |
Azure.IoT Build | 0:6ae2f7bca550 | 535 | |
AzureIoTClient | 28:add19eb7defa | 536 | if (amqpvalue_get_transfer(performative, &transfer_handle) != 0) |
AzureIoTClient | 28:add19eb7defa | 537 | { |
AzureIoTClient | 28:add19eb7defa | 538 | end_session_with_error(session_instance, "amqp:decode-error", "Cannot decode TRANSFER frame"); |
AzureIoTClient | 28:add19eb7defa | 539 | } |
AzureIoTClient | 28:add19eb7defa | 540 | else |
AzureIoTClient | 28:add19eb7defa | 541 | { |
AzureIoTClient | 28:add19eb7defa | 542 | uint32_t remote_handle; |
AzureIoTClient | 28:add19eb7defa | 543 | delivery_number delivery_id; |
Azure.IoT Build | 0:6ae2f7bca550 | 544 | |
AzureIoTClient | 28:add19eb7defa | 545 | transfer_get_delivery_id(transfer_handle, &delivery_id); |
AzureIoTClient | 28:add19eb7defa | 546 | if (transfer_get_handle(transfer_handle, &remote_handle) != 0) |
AzureIoTClient | 28:add19eb7defa | 547 | { |
AzureIoTClient | 28:add19eb7defa | 548 | transfer_destroy(transfer_handle); |
AzureIoTClient | 28:add19eb7defa | 549 | end_session_with_error(session_instance, "amqp:decode-error", "Cannot get handle from TRANSFER frame"); |
AzureIoTClient | 28:add19eb7defa | 550 | } |
AzureIoTClient | 28:add19eb7defa | 551 | else |
AzureIoTClient | 28:add19eb7defa | 552 | { |
AzureIoTClient | 28:add19eb7defa | 553 | LINK_ENDPOINT_INSTANCE* link_endpoint; |
AzureIoTClient | 28:add19eb7defa | 554 | transfer_destroy(transfer_handle); |
Azure.IoT Build | 0:6ae2f7bca550 | 555 | |
AzureIoTClient | 28:add19eb7defa | 556 | session_instance->next_incoming_id++; |
AzureIoTClient | 28:add19eb7defa | 557 | session_instance->remote_outgoing_window--; |
AzureIoTClient | 28:add19eb7defa | 558 | session_instance->incoming_window--; |
Azure.IoT Build | 0:6ae2f7bca550 | 559 | |
AzureIoTClient | 28:add19eb7defa | 560 | link_endpoint = find_link_endpoint_by_input_handle(session_instance, remote_handle); |
AzureIoTClient | 28:add19eb7defa | 561 | if (link_endpoint == NULL) |
AzureIoTClient | 28:add19eb7defa | 562 | { |
AzureIoTClient | 28:add19eb7defa | 563 | end_session_with_error(session_instance, "amqp:session:unattached-handle", ""); |
AzureIoTClient | 28:add19eb7defa | 564 | } |
AzureIoTClient | 28:add19eb7defa | 565 | else |
AzureIoTClient | 28:add19eb7defa | 566 | { |
AzureIoTClient | 28:add19eb7defa | 567 | link_endpoint->frame_received_callback(link_endpoint->callback_context, performative, payload_size, payload_bytes); |
AzureIoTClient | 28:add19eb7defa | 568 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 569 | |
AzureIoTClient | 28:add19eb7defa | 570 | if (session_instance->incoming_window == 0) |
AzureIoTClient | 28:add19eb7defa | 571 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 572 | session_instance->incoming_window = session_instance->desired_incoming_window; |
AzureIoTClient | 28:add19eb7defa | 573 | send_flow(session_instance); |
AzureIoTClient | 28:add19eb7defa | 574 | } |
AzureIoTClient | 28:add19eb7defa | 575 | } |
AzureIoTClient | 28:add19eb7defa | 576 | } |
AzureIoTClient | 28:add19eb7defa | 577 | } |
AzureIoTClient | 28:add19eb7defa | 578 | else if (is_disposition_type_by_descriptor(descriptor)) |
AzureIoTClient | 28:add19eb7defa | 579 | { |
AzureIoTClient | 28:add19eb7defa | 580 | uint32_t i; |
Azure.IoT Build | 0:6ae2f7bca550 | 581 | |
AzureIoTClient | 28:add19eb7defa | 582 | for (i = 0; i < session_instance->link_endpoint_count; i++) |
AzureIoTClient | 28:add19eb7defa | 583 | { |
AzureIoTClient | 28:add19eb7defa | 584 | LINK_ENDPOINT_INSTANCE* link_endpoint = session_instance->link_endpoints[i]; |
AzureIoTClient | 28:add19eb7defa | 585 | link_endpoint->frame_received_callback(link_endpoint->callback_context, performative, payload_size, payload_bytes); |
AzureIoTClient | 28:add19eb7defa | 586 | } |
AzureIoTClient | 28:add19eb7defa | 587 | } |
AzureIoTClient | 28:add19eb7defa | 588 | else if (is_end_type_by_descriptor(descriptor)) |
AzureIoTClient | 28:add19eb7defa | 589 | { |
AzureIoTClient | 28:add19eb7defa | 590 | END_HANDLE end_handle; |
Azure.IoT Build | 0:6ae2f7bca550 | 591 | |
AzureIoTClient | 28:add19eb7defa | 592 | if (amqpvalue_get_end(performative, &end_handle) != 0) |
AzureIoTClient | 28:add19eb7defa | 593 | { |
AzureIoTClient | 28:add19eb7defa | 594 | end_session_with_error(session_instance, "amqp:decode-error", "Cannot decode END frame"); |
AzureIoTClient | 28:add19eb7defa | 595 | } |
AzureIoTClient | 28:add19eb7defa | 596 | else |
AzureIoTClient | 28:add19eb7defa | 597 | { |
AzureIoTClient | 25:1101516ee67d | 598 | end_destroy(end_handle); |
AzureIoTClient | 28:add19eb7defa | 599 | if ((session_instance->session_state != SESSION_STATE_END_RCVD) && |
AzureIoTClient | 28:add19eb7defa | 600 | (session_instance->session_state != SESSION_STATE_DISCARDING)) |
AzureIoTClient | 28:add19eb7defa | 601 | { |
AzureIoTClient | 28:add19eb7defa | 602 | session_set_state(session_instance, SESSION_STATE_END_RCVD); |
AzureIoTClient | 28:add19eb7defa | 603 | if (send_end_frame(session_instance, NULL) != 0) |
AzureIoTClient | 28:add19eb7defa | 604 | { |
AzureIoTClient | 28:add19eb7defa | 605 | /* fatal error */ |
AzureIoTClient | 28:add19eb7defa | 606 | (void)connection_close(session_instance->connection, "amqp:internal-error", "Cannot send END frame."); |
AzureIoTClient | 28:add19eb7defa | 607 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 608 | |
AzureIoTClient | 28:add19eb7defa | 609 | session_set_state(session_instance, SESSION_STATE_DISCARDING); |
AzureIoTClient | 28:add19eb7defa | 610 | } |
AzureIoTClient | 28:add19eb7defa | 611 | } |
AzureIoTClient | 28:add19eb7defa | 612 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 613 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 614 | |
Azure.IoT Build | 0:6ae2f7bca550 | 615 | SESSION_HANDLE session_create(CONNECTION_HANDLE connection, ON_LINK_ATTACHED on_link_attached, void* callback_context) |
Azure.IoT Build | 0:6ae2f7bca550 | 616 | { |
AzureIoTClient | 28:add19eb7defa | 617 | SESSION_INSTANCE* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 618 | |
AzureIoTClient | 28:add19eb7defa | 619 | if (connection == NULL) |
AzureIoTClient | 28:add19eb7defa | 620 | { |
AzureIoTClient | 28:add19eb7defa | 621 | /* Codes_SRS_SESSION_01_031: [If connection is NULL, session_create shall fail and return NULL.] */ |
AzureIoTClient | 28:add19eb7defa | 622 | result = NULL; |
AzureIoTClient | 28:add19eb7defa | 623 | } |
AzureIoTClient | 28:add19eb7defa | 624 | else |
AzureIoTClient | 28:add19eb7defa | 625 | { |
AzureIoTClient | 28:add19eb7defa | 626 | /* Codes_SRS_SESSION_01_030: [session_create shall create a new session instance and return a non-NULL handle to it.] */ |
AzureIoTClient | 28:add19eb7defa | 627 | result = (SESSION_INSTANCE*)malloc(sizeof(SESSION_INSTANCE)); |
AzureIoTClient | 28:add19eb7defa | 628 | /* Codes_SRS_SESSION_01_042: [If allocating memory for the session fails, session_create shall fail and return NULL.] */ |
AzureIoTClient | 28:add19eb7defa | 629 | if (result != NULL) |
AzureIoTClient | 28:add19eb7defa | 630 | { |
AzureIoTClient | 28:add19eb7defa | 631 | result->connection = connection; |
AzureIoTClient | 28:add19eb7defa | 632 | result->link_endpoints = NULL; |
AzureIoTClient | 28:add19eb7defa | 633 | result->link_endpoint_count = 0; |
AzureIoTClient | 28:add19eb7defa | 634 | result->handle_max = 4294967295u; |
Azure.IoT Build | 0:6ae2f7bca550 | 635 | |
AzureIoTClient | 28:add19eb7defa | 636 | /* Codes_SRS_SESSION_01_057: [The delivery ids shall be assigned starting at 0.] */ |
AzureIoTClient | 28:add19eb7defa | 637 | /* Codes_SRS_SESSION_01_017: [The nextoutgoing-id MAY be initialized to an arbitrary value ] */ |
AzureIoTClient | 28:add19eb7defa | 638 | result->next_outgoing_id = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 639 | |
Azure.IoT Build | 0:6ae2f7bca550 | 640 | result->desired_incoming_window = 1; |
Azure.IoT Build | 0:6ae2f7bca550 | 641 | result->incoming_window = 1; |
AzureIoTClient | 28:add19eb7defa | 642 | result->outgoing_window = 1; |
AzureIoTClient | 28:add19eb7defa | 643 | result->handle_max = 4294967295u; |
AzureIoTClient | 28:add19eb7defa | 644 | result->remote_incoming_window = 0; |
AzureIoTClient | 28:add19eb7defa | 645 | result->remote_outgoing_window = 0; |
AzureIoTClient | 28:add19eb7defa | 646 | result->previous_session_state = SESSION_STATE_UNMAPPED; |
AzureIoTClient | 28:add19eb7defa | 647 | result->is_underlying_connection_open = UNDERLYING_CONNECTION_NOT_OPEN; |
AzureIoTClient | 28:add19eb7defa | 648 | result->session_state = SESSION_STATE_UNMAPPED; |
AzureIoTClient | 28:add19eb7defa | 649 | result->on_link_attached = on_link_attached; |
AzureIoTClient | 28:add19eb7defa | 650 | result->on_link_attached_callback_context = callback_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 651 | |
AzureIoTClient | 28:add19eb7defa | 652 | /* Codes_SRS_SESSION_01_032: [session_create shall create a new session endpoint by calling connection_create_endpoint.] */ |
AzureIoTClient | 28:add19eb7defa | 653 | result->endpoint = connection_create_endpoint(connection); |
AzureIoTClient | 28:add19eb7defa | 654 | if (result->endpoint == NULL) |
AzureIoTClient | 28:add19eb7defa | 655 | { |
AzureIoTClient | 28:add19eb7defa | 656 | /* Codes_SRS_SESSION_01_033: [If connection_create_endpoint fails, session_create shall fail and return NULL.] */ |
AzureIoTClient | 28:add19eb7defa | 657 | free(result); |
AzureIoTClient | 28:add19eb7defa | 658 | result = NULL; |
AzureIoTClient | 28:add19eb7defa | 659 | } |
AzureIoTClient | 28:add19eb7defa | 660 | else |
AzureIoTClient | 28:add19eb7defa | 661 | { |
AzureIoTClient | 28:add19eb7defa | 662 | session_set_state(result, SESSION_STATE_UNMAPPED); |
AzureIoTClient | 28:add19eb7defa | 663 | } |
AzureIoTClient | 28:add19eb7defa | 664 | } |
AzureIoTClient | 28:add19eb7defa | 665 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 666 | |
AzureIoTClient | 28:add19eb7defa | 667 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 668 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 669 | |
Azure.IoT Build | 0:6ae2f7bca550 | 670 | SESSION_HANDLE session_create_from_endpoint(CONNECTION_HANDLE connection, ENDPOINT_HANDLE endpoint, ON_LINK_ATTACHED on_link_attached, void* callback_context) |
Azure.IoT Build | 0:6ae2f7bca550 | 671 | { |
AzureIoTClient | 28:add19eb7defa | 672 | SESSION_INSTANCE* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 673 | |
AzureIoTClient | 28:add19eb7defa | 674 | if (endpoint == NULL) |
AzureIoTClient | 28:add19eb7defa | 675 | { |
AzureIoTClient | 28:add19eb7defa | 676 | result = NULL; |
AzureIoTClient | 28:add19eb7defa | 677 | } |
AzureIoTClient | 28:add19eb7defa | 678 | else |
AzureIoTClient | 28:add19eb7defa | 679 | { |
AzureIoTClient | 28:add19eb7defa | 680 | result = (SESSION_INSTANCE*)malloc(sizeof(SESSION_INSTANCE)); |
AzureIoTClient | 28:add19eb7defa | 681 | if (result != NULL) |
AzureIoTClient | 28:add19eb7defa | 682 | { |
AzureIoTClient | 28:add19eb7defa | 683 | result->connection = connection; |
AzureIoTClient | 28:add19eb7defa | 684 | result->link_endpoints = NULL; |
AzureIoTClient | 28:add19eb7defa | 685 | result->link_endpoint_count = 0; |
AzureIoTClient | 28:add19eb7defa | 686 | result->handle_max = 4294967295u; |
Azure.IoT Build | 0:6ae2f7bca550 | 687 | |
AzureIoTClient | 28:add19eb7defa | 688 | result->next_outgoing_id = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 689 | |
AzureIoTClient | 22:524bded3f7a8 | 690 | result->desired_incoming_window = 1; |
AzureIoTClient | 22:524bded3f7a8 | 691 | result->incoming_window = 1; |
AzureIoTClient | 28:add19eb7defa | 692 | result->outgoing_window = 1; |
AzureIoTClient | 28:add19eb7defa | 693 | result->handle_max = 4294967295u; |
AzureIoTClient | 28:add19eb7defa | 694 | result->remote_incoming_window = 0; |
AzureIoTClient | 28:add19eb7defa | 695 | result->remote_outgoing_window = 0; |
AzureIoTClient | 28:add19eb7defa | 696 | result->previous_session_state = SESSION_STATE_UNMAPPED; |
AzureIoTClient | 28:add19eb7defa | 697 | result->is_underlying_connection_open = UNDERLYING_CONNECTION_NOT_OPEN; |
AzureIoTClient | 28:add19eb7defa | 698 | result->session_state = SESSION_STATE_UNMAPPED; |
AzureIoTClient | 28:add19eb7defa | 699 | result->on_link_attached = on_link_attached; |
AzureIoTClient | 28:add19eb7defa | 700 | result->on_link_attached_callback_context = callback_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 701 | |
AzureIoTClient | 28:add19eb7defa | 702 | result->endpoint = endpoint; |
AzureIoTClient | 28:add19eb7defa | 703 | session_set_state(result, SESSION_STATE_UNMAPPED); |
AzureIoTClient | 28:add19eb7defa | 704 | } |
AzureIoTClient | 28:add19eb7defa | 705 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 706 | |
AzureIoTClient | 28:add19eb7defa | 707 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 708 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 709 | |
Azure.IoT Build | 0:6ae2f7bca550 | 710 | void session_destroy(SESSION_HANDLE session) |
Azure.IoT Build | 0:6ae2f7bca550 | 711 | { |
AzureIoTClient | 28:add19eb7defa | 712 | /* Codes_SRS_SESSION_01_036: [If session is NULL, session_destroy shall do nothing.] */ |
AzureIoTClient | 28:add19eb7defa | 713 | if (session != NULL) |
AzureIoTClient | 28:add19eb7defa | 714 | { |
AzureIoTClient | 28:add19eb7defa | 715 | SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session; |
Azure.IoT Build | 0:6ae2f7bca550 | 716 | |
AzureIoTClient | 28:add19eb7defa | 717 | session_end(session, NULL, NULL); |
Azure.IoT Build | 0:6ae2f7bca550 | 718 | |
AzureIoTClient | 28:add19eb7defa | 719 | /* Codes_SRS_SESSION_01_034: [session_destroy shall free all resources allocated by session_create.] */ |
AzureIoTClient | 28:add19eb7defa | 720 | /* Codes_SRS_SESSION_01_035: [The endpoint created in session_create shall be freed by calling connection_destroy_endpoint.] */ |
AzureIoTClient | 28:add19eb7defa | 721 | connection_destroy_endpoint(session_instance->endpoint); |
AzureIoTClient | 28:add19eb7defa | 722 | if (session_instance->link_endpoints != NULL) |
AzureIoTClient | 28:add19eb7defa | 723 | { |
AzureIoTClient | 28:add19eb7defa | 724 | free(session_instance->link_endpoints); |
AzureIoTClient | 28:add19eb7defa | 725 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 726 | |
AzureIoTClient | 28:add19eb7defa | 727 | free(session); |
AzureIoTClient | 28:add19eb7defa | 728 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 729 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 730 | |
Azure.IoT Build | 0:6ae2f7bca550 | 731 | int session_begin(SESSION_HANDLE session) |
Azure.IoT Build | 0:6ae2f7bca550 | 732 | { |
AzureIoTClient | 28:add19eb7defa | 733 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 734 | |
AzureIoTClient | 28:add19eb7defa | 735 | if (session == NULL) |
AzureIoTClient | 28:add19eb7defa | 736 | { |
AzureIoTClient | 28:add19eb7defa | 737 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 738 | } |
AzureIoTClient | 28:add19eb7defa | 739 | else |
AzureIoTClient | 28:add19eb7defa | 740 | { |
AzureIoTClient | 28:add19eb7defa | 741 | SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session; |
Azure.IoT Build | 0:6ae2f7bca550 | 742 | |
AzureIoTClient | 28:add19eb7defa | 743 | if (connection_start_endpoint(session_instance->endpoint, on_frame_received, on_connection_state_changed, session_instance) != 0) |
AzureIoTClient | 28:add19eb7defa | 744 | { |
AzureIoTClient | 28:add19eb7defa | 745 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 746 | } |
AzureIoTClient | 28:add19eb7defa | 747 | else |
AzureIoTClient | 28:add19eb7defa | 748 | { |
AzureIoTClient | 28:add19eb7defa | 749 | if (!session_instance->is_underlying_connection_open) |
AzureIoTClient | 28:add19eb7defa | 750 | { |
AzureIoTClient | 28:add19eb7defa | 751 | if (connection_open(session_instance->connection) != 0) |
AzureIoTClient | 28:add19eb7defa | 752 | { |
AzureIoTClient | 28:add19eb7defa | 753 | session_instance->is_underlying_connection_open = UNDERLYING_CONNECTION_NOT_OPEN; |
AzureIoTClient | 28:add19eb7defa | 754 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 755 | } |
AzureIoTClient | 28:add19eb7defa | 756 | else |
AzureIoTClient | 28:add19eb7defa | 757 | { |
AzureIoTClient | 28:add19eb7defa | 758 | session_instance->is_underlying_connection_open = UNDERLYING_CONNECTION_OPEN; |
AzureIoTClient | 28:add19eb7defa | 759 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 760 | } |
AzureIoTClient | 28:add19eb7defa | 761 | } |
AzureIoTClient | 28:add19eb7defa | 762 | else |
AzureIoTClient | 28:add19eb7defa | 763 | { |
AzureIoTClient | 28:add19eb7defa | 764 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 765 | } |
AzureIoTClient | 28:add19eb7defa | 766 | } |
AzureIoTClient | 28:add19eb7defa | 767 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 768 | |
AzureIoTClient | 28:add19eb7defa | 769 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 770 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 771 | |
Azure.IoT Build | 0:6ae2f7bca550 | 772 | int session_end(SESSION_HANDLE session, const char* condition_value, const char* description) |
Azure.IoT Build | 0:6ae2f7bca550 | 773 | { |
AzureIoTClient | 28:add19eb7defa | 774 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 775 | |
AzureIoTClient | 28:add19eb7defa | 776 | if (session == NULL) |
AzureIoTClient | 28:add19eb7defa | 777 | { |
AzureIoTClient | 28:add19eb7defa | 778 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 779 | } |
AzureIoTClient | 28:add19eb7defa | 780 | else |
AzureIoTClient | 28:add19eb7defa | 781 | { |
AzureIoTClient | 28:add19eb7defa | 782 | SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session; |
Azure.IoT Build | 0:6ae2f7bca550 | 783 | |
AzureIoTClient | 28:add19eb7defa | 784 | if ((session_instance->session_state != SESSION_STATE_UNMAPPED) && |
AzureIoTClient | 28:add19eb7defa | 785 | (session_instance->session_state != SESSION_STATE_DISCARDING)) |
AzureIoTClient | 28:add19eb7defa | 786 | { |
AzureIoTClient | 28:add19eb7defa | 787 | ERROR_HANDLE error_handle = NULL; |
AzureIoTClient | 28:add19eb7defa | 788 | result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 789 | |
AzureIoTClient | 28:add19eb7defa | 790 | if (condition_value != NULL) |
AzureIoTClient | 28:add19eb7defa | 791 | { |
AzureIoTClient | 28:add19eb7defa | 792 | error_handle = error_create(condition_value); |
AzureIoTClient | 28:add19eb7defa | 793 | if (error_handle == NULL) |
AzureIoTClient | 28:add19eb7defa | 794 | { |
AzureIoTClient | 28:add19eb7defa | 795 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 796 | } |
AzureIoTClient | 28:add19eb7defa | 797 | else |
AzureIoTClient | 28:add19eb7defa | 798 | { |
AzureIoTClient | 28:add19eb7defa | 799 | if (error_set_description(error_handle, description) != 0) |
AzureIoTClient | 28:add19eb7defa | 800 | { |
AzureIoTClient | 28:add19eb7defa | 801 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 802 | } |
AzureIoTClient | 28:add19eb7defa | 803 | } |
AzureIoTClient | 28:add19eb7defa | 804 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 805 | |
AzureIoTClient | 28:add19eb7defa | 806 | if (result == 0) |
AzureIoTClient | 28:add19eb7defa | 807 | { |
AzureIoTClient | 28:add19eb7defa | 808 | if (send_end_frame(session_instance, error_handle) != 0) |
AzureIoTClient | 28:add19eb7defa | 809 | { |
AzureIoTClient | 28:add19eb7defa | 810 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 811 | } |
AzureIoTClient | 28:add19eb7defa | 812 | else |
AzureIoTClient | 28:add19eb7defa | 813 | { |
AzureIoTClient | 28:add19eb7defa | 814 | session_set_state(session_instance, SESSION_STATE_DISCARDING); |
AzureIoTClient | 28:add19eb7defa | 815 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 816 | } |
AzureIoTClient | 28:add19eb7defa | 817 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 818 | |
AzureIoTClient | 28:add19eb7defa | 819 | if (error_handle != NULL) |
AzureIoTClient | 28:add19eb7defa | 820 | { |
AzureIoTClient | 28:add19eb7defa | 821 | error_destroy(error_handle); |
AzureIoTClient | 28:add19eb7defa | 822 | } |
AzureIoTClient | 28:add19eb7defa | 823 | } |
AzureIoTClient | 28:add19eb7defa | 824 | else |
AzureIoTClient | 28:add19eb7defa | 825 | { |
AzureIoTClient | 28:add19eb7defa | 826 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 827 | } |
AzureIoTClient | 28:add19eb7defa | 828 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 829 | |
AzureIoTClient | 28:add19eb7defa | 830 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 831 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 832 | |
Azure.IoT Build | 0:6ae2f7bca550 | 833 | int session_set_incoming_window(SESSION_HANDLE session, uint32_t incoming_window) |
Azure.IoT Build | 0:6ae2f7bca550 | 834 | { |
AzureIoTClient | 28:add19eb7defa | 835 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 836 | |
AzureIoTClient | 28:add19eb7defa | 837 | if (session == NULL) |
AzureIoTClient | 28:add19eb7defa | 838 | { |
AzureIoTClient | 28:add19eb7defa | 839 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 840 | } |
AzureIoTClient | 28:add19eb7defa | 841 | else |
AzureIoTClient | 28:add19eb7defa | 842 | { |
AzureIoTClient | 28:add19eb7defa | 843 | SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session; |
Azure.IoT Build | 0:6ae2f7bca550 | 844 | |
AzureIoTClient | 28:add19eb7defa | 845 | session_instance->desired_incoming_window = incoming_window; |
Azure.IoT Build | 0:6ae2f7bca550 | 846 | session_instance->incoming_window = incoming_window; |
Azure.IoT Build | 0:6ae2f7bca550 | 847 | |
AzureIoTClient | 28:add19eb7defa | 848 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 849 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 850 | |
AzureIoTClient | 28:add19eb7defa | 851 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 852 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 853 | |
Azure.IoT Build | 0:6ae2f7bca550 | 854 | int session_get_incoming_window(SESSION_HANDLE session, uint32_t* incoming_window) |
Azure.IoT Build | 0:6ae2f7bca550 | 855 | { |
AzureIoTClient | 28:add19eb7defa | 856 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 857 | |
AzureIoTClient | 28:add19eb7defa | 858 | if ((session == NULL) || |
AzureIoTClient | 28:add19eb7defa | 859 | (incoming_window == NULL)) |
AzureIoTClient | 28:add19eb7defa | 860 | { |
AzureIoTClient | 28:add19eb7defa | 861 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 862 | } |
AzureIoTClient | 28:add19eb7defa | 863 | else |
AzureIoTClient | 28:add19eb7defa | 864 | { |
AzureIoTClient | 28:add19eb7defa | 865 | SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session; |
Azure.IoT Build | 0:6ae2f7bca550 | 866 | |
AzureIoTClient | 28:add19eb7defa | 867 | *incoming_window = session_instance->incoming_window; |
Azure.IoT Build | 0:6ae2f7bca550 | 868 | |
AzureIoTClient | 28:add19eb7defa | 869 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 870 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 871 | |
AzureIoTClient | 28:add19eb7defa | 872 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 873 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 874 | |
Azure.IoT Build | 0:6ae2f7bca550 | 875 | int session_set_outgoing_window(SESSION_HANDLE session, uint32_t outgoing_window) |
Azure.IoT Build | 0:6ae2f7bca550 | 876 | { |
AzureIoTClient | 28:add19eb7defa | 877 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 878 | |
AzureIoTClient | 28:add19eb7defa | 879 | if (session == NULL) |
AzureIoTClient | 28:add19eb7defa | 880 | { |
AzureIoTClient | 28:add19eb7defa | 881 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 882 | } |
AzureIoTClient | 28:add19eb7defa | 883 | else |
AzureIoTClient | 28:add19eb7defa | 884 | { |
AzureIoTClient | 28:add19eb7defa | 885 | SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session; |
Azure.IoT Build | 0:6ae2f7bca550 | 886 | |
AzureIoTClient | 28:add19eb7defa | 887 | session_instance->outgoing_window = outgoing_window; |
Azure.IoT Build | 0:6ae2f7bca550 | 888 | |
AzureIoTClient | 28:add19eb7defa | 889 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 890 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 891 | |
AzureIoTClient | 28:add19eb7defa | 892 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 893 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 894 | |
Azure.IoT Build | 0:6ae2f7bca550 | 895 | int session_get_outgoing_window(SESSION_HANDLE session, uint32_t* outgoing_window) |
Azure.IoT Build | 0:6ae2f7bca550 | 896 | { |
AzureIoTClient | 28:add19eb7defa | 897 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 898 | |
AzureIoTClient | 28:add19eb7defa | 899 | if ((session == NULL) || |
AzureIoTClient | 28:add19eb7defa | 900 | (outgoing_window == NULL)) |
AzureIoTClient | 28:add19eb7defa | 901 | { |
AzureIoTClient | 28:add19eb7defa | 902 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 903 | } |
AzureIoTClient | 28:add19eb7defa | 904 | else |
AzureIoTClient | 28:add19eb7defa | 905 | { |
AzureIoTClient | 28:add19eb7defa | 906 | SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session; |
Azure.IoT Build | 0:6ae2f7bca550 | 907 | |
AzureIoTClient | 28:add19eb7defa | 908 | *outgoing_window = session_instance->outgoing_window; |
Azure.IoT Build | 0:6ae2f7bca550 | 909 | |
AzureIoTClient | 28:add19eb7defa | 910 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 911 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 912 | |
AzureIoTClient | 28:add19eb7defa | 913 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 914 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 915 | |
Azure.IoT Build | 0:6ae2f7bca550 | 916 | int session_set_handle_max(SESSION_HANDLE session, handle handle_max) |
Azure.IoT Build | 0:6ae2f7bca550 | 917 | { |
AzureIoTClient | 28:add19eb7defa | 918 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 919 | |
AzureIoTClient | 28:add19eb7defa | 920 | if (session == NULL) |
AzureIoTClient | 28:add19eb7defa | 921 | { |
AzureIoTClient | 28:add19eb7defa | 922 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 923 | } |
AzureIoTClient | 28:add19eb7defa | 924 | else |
AzureIoTClient | 28:add19eb7defa | 925 | { |
AzureIoTClient | 28:add19eb7defa | 926 | SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session; |
Azure.IoT Build | 0:6ae2f7bca550 | 927 | |
AzureIoTClient | 28:add19eb7defa | 928 | session_instance->handle_max = handle_max; |
Azure.IoT Build | 0:6ae2f7bca550 | 929 | |
AzureIoTClient | 28:add19eb7defa | 930 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 931 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 932 | |
AzureIoTClient | 28:add19eb7defa | 933 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 934 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 935 | |
Azure.IoT Build | 0:6ae2f7bca550 | 936 | int session_get_handle_max(SESSION_HANDLE session, handle* handle_max) |
Azure.IoT Build | 0:6ae2f7bca550 | 937 | { |
AzureIoTClient | 28:add19eb7defa | 938 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 939 | |
AzureIoTClient | 28:add19eb7defa | 940 | if ((session == NULL) || |
AzureIoTClient | 28:add19eb7defa | 941 | (handle_max == NULL)) |
AzureIoTClient | 28:add19eb7defa | 942 | { |
AzureIoTClient | 28:add19eb7defa | 943 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 944 | } |
AzureIoTClient | 28:add19eb7defa | 945 | else |
AzureIoTClient | 28:add19eb7defa | 946 | { |
AzureIoTClient | 28:add19eb7defa | 947 | SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session; |
Azure.IoT Build | 0:6ae2f7bca550 | 948 | |
AzureIoTClient | 28:add19eb7defa | 949 | *handle_max = session_instance->handle_max; |
Azure.IoT Build | 0:6ae2f7bca550 | 950 | |
AzureIoTClient | 28:add19eb7defa | 951 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 952 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 953 | |
AzureIoTClient | 28:add19eb7defa | 954 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 955 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 956 | |
Azure.IoT Build | 0:6ae2f7bca550 | 957 | LINK_ENDPOINT_HANDLE session_create_link_endpoint(SESSION_HANDLE session, const char* name) |
Azure.IoT Build | 0:6ae2f7bca550 | 958 | { |
AzureIoTClient | 28:add19eb7defa | 959 | LINK_ENDPOINT_INSTANCE* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 960 | |
AzureIoTClient | 28:add19eb7defa | 961 | /* Codes_SRS_SESSION_01_044: [If session, name or frame_received_callback is NULL, session_create_link_endpoint shall fail and return NULL.] */ |
AzureIoTClient | 28:add19eb7defa | 962 | if ((session == NULL) || |
AzureIoTClient | 28:add19eb7defa | 963 | (name == NULL)) |
AzureIoTClient | 28:add19eb7defa | 964 | { |
AzureIoTClient | 28:add19eb7defa | 965 | result = NULL; |
AzureIoTClient | 28:add19eb7defa | 966 | } |
AzureIoTClient | 28:add19eb7defa | 967 | else |
AzureIoTClient | 28:add19eb7defa | 968 | { |
AzureIoTClient | 28:add19eb7defa | 969 | /* Codes_SRS_SESSION_01_043: [session_create_link_endpoint shall create a link endpoint associated with a given session and return a non-NULL handle to it.] */ |
AzureIoTClient | 28:add19eb7defa | 970 | SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session; |
Azure.IoT Build | 0:6ae2f7bca550 | 971 | |
AzureIoTClient | 28:add19eb7defa | 972 | result = (LINK_ENDPOINT_INSTANCE*)malloc(sizeof(LINK_ENDPOINT_INSTANCE)); |
AzureIoTClient | 28:add19eb7defa | 973 | /* Codes_SRS_SESSION_01_045: [If allocating memory for the link endpoint fails, session_create_link_endpoint shall fail and return NULL.] */ |
AzureIoTClient | 28:add19eb7defa | 974 | if (result != NULL) |
AzureIoTClient | 28:add19eb7defa | 975 | { |
AzureIoTClient | 28:add19eb7defa | 976 | /* Codes_SRS_SESSION_01_046: [An unused handle shall be assigned to the link endpoint.] */ |
AzureIoTClient | 28:add19eb7defa | 977 | handle selected_handle = 0; |
AzureIoTClient | 28:add19eb7defa | 978 | size_t i; |
AzureIoTClient | 23:1111ee8bcba4 | 979 | size_t name_length; |
Azure.IoT Build | 0:6ae2f7bca550 | 980 | |
AzureIoTClient | 28:add19eb7defa | 981 | for (i = 0; i < session_instance->link_endpoint_count; i++) |
AzureIoTClient | 28:add19eb7defa | 982 | { |
AzureIoTClient | 28:add19eb7defa | 983 | if (session_instance->link_endpoints[i]->output_handle > selected_handle) |
AzureIoTClient | 28:add19eb7defa | 984 | { |
AzureIoTClient | 28:add19eb7defa | 985 | break; |
AzureIoTClient | 28:add19eb7defa | 986 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 987 | |
AzureIoTClient | 28:add19eb7defa | 988 | selected_handle++; |
AzureIoTClient | 28:add19eb7defa | 989 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 990 | |
AzureIoTClient | 28:add19eb7defa | 991 | result->on_session_state_changed = NULL; |
AzureIoTClient | 28:add19eb7defa | 992 | result->on_session_flow_on = NULL; |
AzureIoTClient | 28:add19eb7defa | 993 | result->frame_received_callback = NULL; |
AzureIoTClient | 28:add19eb7defa | 994 | result->callback_context = NULL; |
AzureIoTClient | 28:add19eb7defa | 995 | result->output_handle = selected_handle; |
AzureIoTClient | 28:add19eb7defa | 996 | result->input_handle = 0xFFFFFFFF; |
AzureIoTClient | 23:1111ee8bcba4 | 997 | name_length = strlen(name); |
AzureIoTClient | 28:add19eb7defa | 998 | result->name = (char*)malloc(name_length + 1); |
AzureIoTClient | 28:add19eb7defa | 999 | if (result->name == NULL) |
AzureIoTClient | 28:add19eb7defa | 1000 | { |
AzureIoTClient | 28:add19eb7defa | 1001 | /* Codes_SRS_SESSION_01_045: [If allocating memory for the link endpoint fails, session_create_link_endpoint shall fail and return NULL.] */ |
AzureIoTClient | 28:add19eb7defa | 1002 | free(result); |
AzureIoTClient | 28:add19eb7defa | 1003 | result = NULL; |
AzureIoTClient | 28:add19eb7defa | 1004 | } |
AzureIoTClient | 28:add19eb7defa | 1005 | else |
AzureIoTClient | 28:add19eb7defa | 1006 | { |
AzureIoTClient | 28:add19eb7defa | 1007 | LINK_ENDPOINT_INSTANCE** new_link_endpoints; |
AzureIoTClient | 28:add19eb7defa | 1008 | (void)memcpy(result->name, name, name_length + 1); |
AzureIoTClient | 28:add19eb7defa | 1009 | result->session = session; |
Azure.IoT Build | 0:6ae2f7bca550 | 1010 | |
AzureIoTClient | 28:add19eb7defa | 1011 | new_link_endpoints = (LINK_ENDPOINT_INSTANCE**)realloc(session_instance->link_endpoints, sizeof(LINK_ENDPOINT_INSTANCE*) * (session_instance->link_endpoint_count + 1)); |
AzureIoTClient | 28:add19eb7defa | 1012 | if (new_link_endpoints == NULL) |
AzureIoTClient | 28:add19eb7defa | 1013 | { |
AzureIoTClient | 28:add19eb7defa | 1014 | /* Codes_SRS_SESSION_01_045: [If allocating memory for the link endpoint fails, session_create_link_endpoint shall fail and return NULL.] */ |
AzureIoTClient | 21:f9c433d8e6ca | 1015 | free(result->name); |
AzureIoTClient | 28:add19eb7defa | 1016 | free(result); |
AzureIoTClient | 28:add19eb7defa | 1017 | result = NULL; |
AzureIoTClient | 28:add19eb7defa | 1018 | } |
AzureIoTClient | 28:add19eb7defa | 1019 | else |
AzureIoTClient | 28:add19eb7defa | 1020 | { |
AzureIoTClient | 28:add19eb7defa | 1021 | session_instance->link_endpoints = new_link_endpoints; |
Azure.IoT Build | 0:6ae2f7bca550 | 1022 | |
AzureIoTClient | 28:add19eb7defa | 1023 | if (session_instance->link_endpoint_count - selected_handle > 0) |
AzureIoTClient | 28:add19eb7defa | 1024 | { |
AzureIoTClient | 28:add19eb7defa | 1025 | (void)memmove(&session_instance->link_endpoints[selected_handle + 1], &session_instance->link_endpoints[selected_handle], (session_instance->link_endpoint_count - selected_handle) * sizeof(LINK_ENDPOINT_INSTANCE*)); |
AzureIoTClient | 28:add19eb7defa | 1026 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1027 | |
AzureIoTClient | 28:add19eb7defa | 1028 | session_instance->link_endpoints[selected_handle] = result; |
AzureIoTClient | 28:add19eb7defa | 1029 | session_instance->link_endpoint_count++; |
AzureIoTClient | 28:add19eb7defa | 1030 | } |
AzureIoTClient | 28:add19eb7defa | 1031 | } |
AzureIoTClient | 28:add19eb7defa | 1032 | } |
AzureIoTClient | 28:add19eb7defa | 1033 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1034 | |
AzureIoTClient | 28:add19eb7defa | 1035 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1036 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1037 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1038 | void session_destroy_link_endpoint(LINK_ENDPOINT_HANDLE link_endpoint) |
Azure.IoT Build | 0:6ae2f7bca550 | 1039 | { |
AzureIoTClient | 28:add19eb7defa | 1040 | /* Codes_SRS_SESSION_01_050: [If link_endpoint is NULL, session_destroy_link_endpoint shall do nothing.] */ |
AzureIoTClient | 28:add19eb7defa | 1041 | if (link_endpoint != NULL) |
AzureIoTClient | 28:add19eb7defa | 1042 | { |
AzureIoTClient | 28:add19eb7defa | 1043 | LINK_ENDPOINT_INSTANCE* endpoint_instance = (LINK_ENDPOINT_INSTANCE*)link_endpoint; |
AzureIoTClient | 28:add19eb7defa | 1044 | SESSION_INSTANCE* session_instance = endpoint_instance->session; |
AzureIoTClient | 28:add19eb7defa | 1045 | uint64_t i; |
Azure.IoT Build | 0:6ae2f7bca550 | 1046 | |
AzureIoTClient | 28:add19eb7defa | 1047 | /* Codes_SRS_SESSION_01_049: [session_destroy_link_endpoint shall free all resources associated with the endpoint.] */ |
AzureIoTClient | 28:add19eb7defa | 1048 | for (i = 0; i < session_instance->link_endpoint_count; i++) |
AzureIoTClient | 28:add19eb7defa | 1049 | { |
AzureIoTClient | 28:add19eb7defa | 1050 | if (session_instance->link_endpoints[i] == link_endpoint) |
AzureIoTClient | 28:add19eb7defa | 1051 | { |
AzureIoTClient | 28:add19eb7defa | 1052 | break; |
AzureIoTClient | 28:add19eb7defa | 1053 | } |
AzureIoTClient | 28:add19eb7defa | 1054 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1055 | |
AzureIoTClient | 28:add19eb7defa | 1056 | if (i < session_instance->link_endpoint_count) |
AzureIoTClient | 28:add19eb7defa | 1057 | { |
AzureIoTClient | 28:add19eb7defa | 1058 | LINK_ENDPOINT_INSTANCE** new_endpoints; |
Azure.IoT Build | 0:6ae2f7bca550 | 1059 | |
AzureIoTClient | 28:add19eb7defa | 1060 | if (i < (session_instance->link_endpoint_count - 1)) |
AzureIoTClient | 28:add19eb7defa | 1061 | { |
AzureIoTClient | 28:add19eb7defa | 1062 | (void)memmove(&session_instance->link_endpoints[i], &session_instance->link_endpoints[i + 1], (session_instance->link_endpoint_count - (uint32_t)i - 1) * sizeof(LINK_ENDPOINT_INSTANCE*)); |
AzureIoTClient | 28:add19eb7defa | 1063 | } |
AzureIoTClient | 7:9e9ab3b0efef | 1064 | |
AzureIoTClient | 28:add19eb7defa | 1065 | session_instance->link_endpoint_count--; |
Azure.IoT Build | 0:6ae2f7bca550 | 1066 | |
AzureIoTClient | 28:add19eb7defa | 1067 | if (session_instance->link_endpoint_count == 0) |
AzureIoTClient | 28:add19eb7defa | 1068 | { |
AzureIoTClient | 28:add19eb7defa | 1069 | free(session_instance->link_endpoints); |
AzureIoTClient | 28:add19eb7defa | 1070 | session_instance->link_endpoints = NULL; |
AzureIoTClient | 28:add19eb7defa | 1071 | } |
AzureIoTClient | 28:add19eb7defa | 1072 | else |
AzureIoTClient | 28:add19eb7defa | 1073 | { |
AzureIoTClient | 28:add19eb7defa | 1074 | new_endpoints = (LINK_ENDPOINT_INSTANCE**)realloc(session_instance->link_endpoints, sizeof(LINK_ENDPOINT_INSTANCE*) * session_instance->link_endpoint_count); |
AzureIoTClient | 28:add19eb7defa | 1075 | if (new_endpoints != NULL) |
AzureIoTClient | 28:add19eb7defa | 1076 | { |
AzureIoTClient | 28:add19eb7defa | 1077 | session_instance->link_endpoints = new_endpoints; |
AzureIoTClient | 28:add19eb7defa | 1078 | } |
AzureIoTClient | 28:add19eb7defa | 1079 | } |
AzureIoTClient | 28:add19eb7defa | 1080 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1081 | |
AzureIoTClient | 28:add19eb7defa | 1082 | if (endpoint_instance->name != NULL) |
AzureIoTClient | 28:add19eb7defa | 1083 | { |
AzureIoTClient | 28:add19eb7defa | 1084 | free(endpoint_instance->name); |
AzureIoTClient | 28:add19eb7defa | 1085 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1086 | |
AzureIoTClient | 28:add19eb7defa | 1087 | free(endpoint_instance); |
AzureIoTClient | 28:add19eb7defa | 1088 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1089 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1090 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1091 | int session_start_link_endpoint(LINK_ENDPOINT_HANDLE link_endpoint, ON_ENDPOINT_FRAME_RECEIVED frame_received_callback, ON_SESSION_STATE_CHANGED on_session_state_changed, ON_SESSION_FLOW_ON on_session_flow_on, void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 1092 | { |
AzureIoTClient | 28:add19eb7defa | 1093 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1094 | |
AzureIoTClient | 28:add19eb7defa | 1095 | if ((link_endpoint == NULL) || |
AzureIoTClient | 28:add19eb7defa | 1096 | (frame_received_callback == NULL)) |
AzureIoTClient | 28:add19eb7defa | 1097 | { |
AzureIoTClient | 28:add19eb7defa | 1098 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1099 | } |
AzureIoTClient | 28:add19eb7defa | 1100 | else |
AzureIoTClient | 28:add19eb7defa | 1101 | { |
AzureIoTClient | 28:add19eb7defa | 1102 | link_endpoint->frame_received_callback = frame_received_callback; |
AzureIoTClient | 28:add19eb7defa | 1103 | link_endpoint->on_session_state_changed = on_session_state_changed; |
AzureIoTClient | 28:add19eb7defa | 1104 | link_endpoint->on_session_flow_on = on_session_flow_on; |
AzureIoTClient | 28:add19eb7defa | 1105 | link_endpoint->callback_context = context; |
Azure.IoT Build | 0:6ae2f7bca550 | 1106 | |
AzureIoTClient | 28:add19eb7defa | 1107 | if (link_endpoint->on_session_state_changed != NULL) |
AzureIoTClient | 28:add19eb7defa | 1108 | { |
AzureIoTClient | 28:add19eb7defa | 1109 | link_endpoint->on_session_state_changed(link_endpoint->callback_context, link_endpoint->session->session_state, link_endpoint->session->previous_session_state); |
AzureIoTClient | 28:add19eb7defa | 1110 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1111 | |
AzureIoTClient | 28:add19eb7defa | 1112 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 1113 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1114 | |
AzureIoTClient | 28:add19eb7defa | 1115 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1116 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1117 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1118 | static int encode_frame(LINK_ENDPOINT_HANDLE link_endpoint, const AMQP_VALUE performative, PAYLOAD* payloads, size_t payload_count) |
Azure.IoT Build | 0:6ae2f7bca550 | 1119 | { |
AzureIoTClient | 28:add19eb7defa | 1120 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1121 | |
AzureIoTClient | 28:add19eb7defa | 1122 | if ((link_endpoint == NULL) || |
AzureIoTClient | 28:add19eb7defa | 1123 | (performative == NULL)) |
AzureIoTClient | 28:add19eb7defa | 1124 | { |
AzureIoTClient | 28:add19eb7defa | 1125 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1126 | } |
AzureIoTClient | 28:add19eb7defa | 1127 | else |
AzureIoTClient | 28:add19eb7defa | 1128 | { |
AzureIoTClient | 28:add19eb7defa | 1129 | LINK_ENDPOINT_INSTANCE* link_endpoint_instance = (LINK_ENDPOINT_INSTANCE*)link_endpoint; |
AzureIoTClient | 28:add19eb7defa | 1130 | SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)link_endpoint_instance->session; |
Azure.IoT Build | 0:6ae2f7bca550 | 1131 | |
AzureIoTClient | 28:add19eb7defa | 1132 | if (connection_encode_frame(session_instance->endpoint, performative, payloads, payload_count, NULL, NULL) != 0) |
AzureIoTClient | 28:add19eb7defa | 1133 | { |
AzureIoTClient | 28:add19eb7defa | 1134 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1135 | } |
AzureIoTClient | 28:add19eb7defa | 1136 | else |
AzureIoTClient | 28:add19eb7defa | 1137 | { |
AzureIoTClient | 28:add19eb7defa | 1138 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 1139 | } |
AzureIoTClient | 28:add19eb7defa | 1140 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1141 | |
AzureIoTClient | 28:add19eb7defa | 1142 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1143 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1144 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1145 | int session_send_flow(LINK_ENDPOINT_HANDLE link_endpoint, FLOW_HANDLE flow) |
Azure.IoT Build | 0:6ae2f7bca550 | 1146 | { |
AzureIoTClient | 28:add19eb7defa | 1147 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1148 | |
AzureIoTClient | 28:add19eb7defa | 1149 | if ((link_endpoint == NULL) || |
AzureIoTClient | 28:add19eb7defa | 1150 | (flow == NULL)) |
AzureIoTClient | 28:add19eb7defa | 1151 | { |
AzureIoTClient | 28:add19eb7defa | 1152 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1153 | } |
AzureIoTClient | 28:add19eb7defa | 1154 | else |
AzureIoTClient | 28:add19eb7defa | 1155 | { |
AzureIoTClient | 28:add19eb7defa | 1156 | LINK_ENDPOINT_INSTANCE* link_endpoint_instance = (LINK_ENDPOINT_INSTANCE*)link_endpoint; |
AzureIoTClient | 28:add19eb7defa | 1157 | SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)link_endpoint_instance->session; |
Azure.IoT Build | 0:6ae2f7bca550 | 1158 | |
AzureIoTClient | 28:add19eb7defa | 1159 | result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 1160 | |
AzureIoTClient | 28:add19eb7defa | 1161 | if (session_instance->session_state == SESSION_STATE_BEGIN_RCVD) |
AzureIoTClient | 28:add19eb7defa | 1162 | { |
AzureIoTClient | 28:add19eb7defa | 1163 | if (flow_set_next_incoming_id(flow, session_instance->next_incoming_id) != 0) |
AzureIoTClient | 28:add19eb7defa | 1164 | { |
AzureIoTClient | 28:add19eb7defa | 1165 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1166 | } |
AzureIoTClient | 28:add19eb7defa | 1167 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1168 | |
AzureIoTClient | 28:add19eb7defa | 1169 | if (result == 0) |
AzureIoTClient | 28:add19eb7defa | 1170 | { |
AzureIoTClient | 12:b30dacf113f2 | 1171 | if ((flow_set_next_incoming_id(flow, session_instance->next_incoming_id) != 0) || |
AzureIoTClient | 12:b30dacf113f2 | 1172 | (flow_set_incoming_window(flow, session_instance->incoming_window) != 0) || |
AzureIoTClient | 12:b30dacf113f2 | 1173 | (flow_set_next_outgoing_id(flow, session_instance->next_outgoing_id) != 0) || |
AzureIoTClient | 28:add19eb7defa | 1174 | (flow_set_outgoing_window(flow, session_instance->outgoing_window) != 0) || |
AzureIoTClient | 28:add19eb7defa | 1175 | (flow_set_handle(flow, link_endpoint_instance->output_handle) != 0)) |
AzureIoTClient | 28:add19eb7defa | 1176 | { |
AzureIoTClient | 28:add19eb7defa | 1177 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1178 | } |
AzureIoTClient | 28:add19eb7defa | 1179 | else |
AzureIoTClient | 28:add19eb7defa | 1180 | { |
AzureIoTClient | 28:add19eb7defa | 1181 | AMQP_VALUE flow_performative_value = amqpvalue_create_flow(flow); |
AzureIoTClient | 28:add19eb7defa | 1182 | if (flow_performative_value == NULL) |
AzureIoTClient | 28:add19eb7defa | 1183 | { |
AzureIoTClient | 28:add19eb7defa | 1184 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1185 | } |
AzureIoTClient | 28:add19eb7defa | 1186 | else |
AzureIoTClient | 28:add19eb7defa | 1187 | { |
AzureIoTClient | 28:add19eb7defa | 1188 | if (encode_frame(link_endpoint, flow_performative_value, NULL, 0) != 0) |
AzureIoTClient | 28:add19eb7defa | 1189 | { |
AzureIoTClient | 28:add19eb7defa | 1190 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1191 | } |
AzureIoTClient | 28:add19eb7defa | 1192 | else |
AzureIoTClient | 28:add19eb7defa | 1193 | { |
AzureIoTClient | 28:add19eb7defa | 1194 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 1195 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1196 | |
AzureIoTClient | 28:add19eb7defa | 1197 | amqpvalue_destroy(flow_performative_value); |
AzureIoTClient | 28:add19eb7defa | 1198 | } |
AzureIoTClient | 28:add19eb7defa | 1199 | } |
AzureIoTClient | 28:add19eb7defa | 1200 | } |
AzureIoTClient | 28:add19eb7defa | 1201 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1202 | |
AzureIoTClient | 28:add19eb7defa | 1203 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1204 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1205 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1206 | int session_send_attach(LINK_ENDPOINT_HANDLE link_endpoint, ATTACH_HANDLE attach) |
Azure.IoT Build | 0:6ae2f7bca550 | 1207 | { |
AzureIoTClient | 28:add19eb7defa | 1208 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1209 | |
AzureIoTClient | 28:add19eb7defa | 1210 | if ((link_endpoint == NULL) || |
AzureIoTClient | 28:add19eb7defa | 1211 | (attach == NULL)) |
AzureIoTClient | 28:add19eb7defa | 1212 | { |
AzureIoTClient | 28:add19eb7defa | 1213 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1214 | } |
AzureIoTClient | 28:add19eb7defa | 1215 | else |
AzureIoTClient | 28:add19eb7defa | 1216 | { |
AzureIoTClient | 28:add19eb7defa | 1217 | LINK_ENDPOINT_INSTANCE* link_endpoint_instance = (LINK_ENDPOINT_INSTANCE*)link_endpoint; |
Azure.IoT Build | 0:6ae2f7bca550 | 1218 | |
AzureIoTClient | 28:add19eb7defa | 1219 | if (attach_set_handle(attach, link_endpoint_instance->output_handle) != 0) |
AzureIoTClient | 28:add19eb7defa | 1220 | { |
AzureIoTClient | 28:add19eb7defa | 1221 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1222 | } |
AzureIoTClient | 28:add19eb7defa | 1223 | else |
AzureIoTClient | 28:add19eb7defa | 1224 | { |
AzureIoTClient | 28:add19eb7defa | 1225 | AMQP_VALUE attach_performative_value = amqpvalue_create_attach(attach); |
AzureIoTClient | 28:add19eb7defa | 1226 | if (attach_performative_value == NULL) |
AzureIoTClient | 28:add19eb7defa | 1227 | { |
AzureIoTClient | 28:add19eb7defa | 1228 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1229 | } |
AzureIoTClient | 28:add19eb7defa | 1230 | else |
AzureIoTClient | 28:add19eb7defa | 1231 | { |
AzureIoTClient | 28:add19eb7defa | 1232 | if (encode_frame(link_endpoint, attach_performative_value, NULL, 0) != 0) |
AzureIoTClient | 28:add19eb7defa | 1233 | { |
AzureIoTClient | 28:add19eb7defa | 1234 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1235 | } |
AzureIoTClient | 28:add19eb7defa | 1236 | else |
AzureIoTClient | 28:add19eb7defa | 1237 | { |
AzureIoTClient | 28:add19eb7defa | 1238 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 1239 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1240 | |
AzureIoTClient | 28:add19eb7defa | 1241 | amqpvalue_destroy(attach_performative_value); |
AzureIoTClient | 28:add19eb7defa | 1242 | } |
AzureIoTClient | 28:add19eb7defa | 1243 | } |
AzureIoTClient | 28:add19eb7defa | 1244 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1245 | |
AzureIoTClient | 28:add19eb7defa | 1246 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1247 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1248 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1249 | int session_send_disposition(LINK_ENDPOINT_HANDLE link_endpoint, DISPOSITION_HANDLE disposition) |
Azure.IoT Build | 0:6ae2f7bca550 | 1250 | { |
AzureIoTClient | 28:add19eb7defa | 1251 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1252 | |
AzureIoTClient | 28:add19eb7defa | 1253 | if ((link_endpoint == NULL) || |
AzureIoTClient | 28:add19eb7defa | 1254 | (disposition == NULL)) |
AzureIoTClient | 28:add19eb7defa | 1255 | { |
AzureIoTClient | 28:add19eb7defa | 1256 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1257 | } |
AzureIoTClient | 28:add19eb7defa | 1258 | else |
AzureIoTClient | 28:add19eb7defa | 1259 | { |
AzureIoTClient | 28:add19eb7defa | 1260 | AMQP_VALUE disposition_performative_value = amqpvalue_create_disposition(disposition); |
AzureIoTClient | 28:add19eb7defa | 1261 | if (disposition_performative_value == NULL) |
AzureIoTClient | 28:add19eb7defa | 1262 | { |
AzureIoTClient | 28:add19eb7defa | 1263 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1264 | } |
AzureIoTClient | 28:add19eb7defa | 1265 | else |
AzureIoTClient | 28:add19eb7defa | 1266 | { |
AzureIoTClient | 28:add19eb7defa | 1267 | if (encode_frame(link_endpoint, disposition_performative_value, NULL, 0) != 0) |
AzureIoTClient | 28:add19eb7defa | 1268 | { |
AzureIoTClient | 28:add19eb7defa | 1269 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1270 | } |
AzureIoTClient | 28:add19eb7defa | 1271 | else |
AzureIoTClient | 28:add19eb7defa | 1272 | { |
AzureIoTClient | 28:add19eb7defa | 1273 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 1274 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1275 | |
AzureIoTClient | 28:add19eb7defa | 1276 | amqpvalue_destroy(disposition_performative_value); |
AzureIoTClient | 28:add19eb7defa | 1277 | } |
AzureIoTClient | 28:add19eb7defa | 1278 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1279 | |
AzureIoTClient | 28:add19eb7defa | 1280 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1281 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1282 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1283 | int session_send_detach(LINK_ENDPOINT_HANDLE link_endpoint, DETACH_HANDLE detach) |
Azure.IoT Build | 0:6ae2f7bca550 | 1284 | { |
AzureIoTClient | 28:add19eb7defa | 1285 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1286 | |
AzureIoTClient | 28:add19eb7defa | 1287 | if ((link_endpoint == NULL) || |
AzureIoTClient | 28:add19eb7defa | 1288 | (detach == NULL)) |
AzureIoTClient | 28:add19eb7defa | 1289 | { |
AzureIoTClient | 28:add19eb7defa | 1290 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1291 | } |
AzureIoTClient | 28:add19eb7defa | 1292 | else |
AzureIoTClient | 28:add19eb7defa | 1293 | { |
AzureIoTClient | 28:add19eb7defa | 1294 | LINK_ENDPOINT_INSTANCE* link_endpoint_instance = (LINK_ENDPOINT_INSTANCE*)link_endpoint; |
Azure.IoT Build | 0:6ae2f7bca550 | 1295 | |
AzureIoTClient | 28:add19eb7defa | 1296 | if (detach_set_handle(detach, link_endpoint_instance->output_handle) != 0) |
AzureIoTClient | 28:add19eb7defa | 1297 | { |
AzureIoTClient | 28:add19eb7defa | 1298 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1299 | } |
AzureIoTClient | 28:add19eb7defa | 1300 | else |
AzureIoTClient | 28:add19eb7defa | 1301 | { |
AzureIoTClient | 28:add19eb7defa | 1302 | AMQP_VALUE detach_performative_value = amqpvalue_create_detach(detach); |
AzureIoTClient | 28:add19eb7defa | 1303 | if (detach_performative_value == NULL) |
AzureIoTClient | 28:add19eb7defa | 1304 | { |
AzureIoTClient | 28:add19eb7defa | 1305 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1306 | } |
AzureIoTClient | 28:add19eb7defa | 1307 | else |
AzureIoTClient | 28:add19eb7defa | 1308 | { |
AzureIoTClient | 28:add19eb7defa | 1309 | if (encode_frame(link_endpoint, detach_performative_value, NULL, 0) != 0) |
AzureIoTClient | 28:add19eb7defa | 1310 | { |
AzureIoTClient | 28:add19eb7defa | 1311 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 1312 | } |
AzureIoTClient | 28:add19eb7defa | 1313 | else |
AzureIoTClient | 28:add19eb7defa | 1314 | { |
AzureIoTClient | 28:add19eb7defa | 1315 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 1316 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1317 | |
AzureIoTClient | 28:add19eb7defa | 1318 | amqpvalue_destroy(detach_performative_value); |
AzureIoTClient | 28:add19eb7defa | 1319 | } |
AzureIoTClient | 28:add19eb7defa | 1320 | } |
AzureIoTClient | 28:add19eb7defa | 1321 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1322 | |
AzureIoTClient | 28:add19eb7defa | 1323 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1324 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1325 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1326 | /* Codes_SRS_SESSION_01_051: [session_send_transfer shall send a transfer frame with the performative indicated in the transfer argument.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 1327 | SESSION_SEND_TRANSFER_RESULT session_send_transfer(LINK_ENDPOINT_HANDLE link_endpoint, TRANSFER_HANDLE transfer, PAYLOAD* payloads, size_t payload_count, delivery_number* delivery_id, ON_SEND_COMPLETE on_send_complete, void* callback_context) |
Azure.IoT Build | 0:6ae2f7bca550 | 1328 | { |
AzureIoTClient | 28:add19eb7defa | 1329 | SESSION_SEND_TRANSFER_RESULT result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1330 | |
AzureIoTClient | 28:add19eb7defa | 1331 | /* Codes_SRS_SESSION_01_054: [If link_endpoint or transfer is NULL, session_send_transfer shall fail and return a non-zero value.] */ |
AzureIoTClient | 28:add19eb7defa | 1332 | if ((link_endpoint == NULL) || |
AzureIoTClient | 28:add19eb7defa | 1333 | (transfer == NULL)) |
AzureIoTClient | 28:add19eb7defa | 1334 | { |
AzureIoTClient | 28:add19eb7defa | 1335 | result = SESSION_SEND_TRANSFER_ERROR; |
AzureIoTClient | 28:add19eb7defa | 1336 | } |
AzureIoTClient | 28:add19eb7defa | 1337 | else |
AzureIoTClient | 28:add19eb7defa | 1338 | { |
AzureIoTClient | 28:add19eb7defa | 1339 | LINK_ENDPOINT_INSTANCE* link_endpoint_instance = (LINK_ENDPOINT_INSTANCE*)link_endpoint; |
AzureIoTClient | 28:add19eb7defa | 1340 | SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)link_endpoint_instance->session; |
Azure.IoT Build | 0:6ae2f7bca550 | 1341 | |
AzureIoTClient | 28:add19eb7defa | 1342 | /* Codes_SRS_SESSION_01_059: [When session_send_transfer is called while the session is not in the MAPPED state, session_send_transfer shall fail and return a non-zero value.] */ |
AzureIoTClient | 28:add19eb7defa | 1343 | if (session_instance->session_state != SESSION_STATE_MAPPED) |
AzureIoTClient | 28:add19eb7defa | 1344 | { |
AzureIoTClient | 28:add19eb7defa | 1345 | result = SESSION_SEND_TRANSFER_ERROR; |
AzureIoTClient | 28:add19eb7defa | 1346 | } |
AzureIoTClient | 28:add19eb7defa | 1347 | else |
AzureIoTClient | 28:add19eb7defa | 1348 | { |
AzureIoTClient | 28:add19eb7defa | 1349 | size_t payload_size = 0; |
AzureIoTClient | 28:add19eb7defa | 1350 | size_t i; |
Azure.IoT Build | 0:6ae2f7bca550 | 1351 | |
AzureIoTClient | 28:add19eb7defa | 1352 | for (i = 0; i < payload_count; i++) |
AzureIoTClient | 28:add19eb7defa | 1353 | { |
AzureIoTClient | 6:641a9672db08 | 1354 | if ((payloads[i].length > UINT32_MAX) || |
AzureIoTClient | 6:641a9672db08 | 1355 | (payload_size + payloads[i].length < payload_size)) |
AzureIoTClient | 6:641a9672db08 | 1356 | { |
AzureIoTClient | 6:641a9672db08 | 1357 | break; |
AzureIoTClient | 6:641a9672db08 | 1358 | } |
AzureIoTClient | 6:641a9672db08 | 1359 | |
AzureIoTClient | 6:641a9672db08 | 1360 | payload_size += payloads[i].length; |
AzureIoTClient | 6:641a9672db08 | 1361 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1362 | |
AzureIoTClient | 6:641a9672db08 | 1363 | if ((i < payload_count) || |
AzureIoTClient | 6:641a9672db08 | 1364 | (payload_size > UINT32_MAX)) |
AzureIoTClient | 6:641a9672db08 | 1365 | { |
AzureIoTClient | 6:641a9672db08 | 1366 | result = SESSION_SEND_TRANSFER_ERROR; |
AzureIoTClient | 6:641a9672db08 | 1367 | } |
AzureIoTClient | 6:641a9672db08 | 1368 | else |
AzureIoTClient | 6:641a9672db08 | 1369 | { |
AzureIoTClient | 6:641a9672db08 | 1370 | if (session_instance->remote_incoming_window == 0) |
AzureIoTClient | 6:641a9672db08 | 1371 | { |
AzureIoTClient | 6:641a9672db08 | 1372 | result = SESSION_SEND_TRANSFER_BUSY; |
AzureIoTClient | 6:641a9672db08 | 1373 | } |
AzureIoTClient | 6:641a9672db08 | 1374 | else |
AzureIoTClient | 6:641a9672db08 | 1375 | { |
AzureIoTClient | 6:641a9672db08 | 1376 | /* Codes_SRS_SESSION_01_012: [The session endpoint assigns each outgoing transfer frame an implicit transfer-id from a session scoped sequence.] */ |
AzureIoTClient | 6:641a9672db08 | 1377 | /* Codes_SRS_SESSION_01_027: [sending a transfer Upon sending a transfer, the sending endpoint will increment its next-outgoing-id] */ |
AzureIoTClient | 6:641a9672db08 | 1378 | *delivery_id = session_instance->next_outgoing_id; |
AzureIoTClient | 6:641a9672db08 | 1379 | if ((transfer_set_handle(transfer, link_endpoint_instance->output_handle) != 0) || |
AzureIoTClient | 6:641a9672db08 | 1380 | (transfer_set_delivery_id(transfer, *delivery_id) != 0) || |
AzureIoTClient | 6:641a9672db08 | 1381 | (transfer_set_more(transfer, false) != 0)) |
AzureIoTClient | 6:641a9672db08 | 1382 | { |
AzureIoTClient | 6:641a9672db08 | 1383 | /* Codes_SRS_SESSION_01_058: [When any other error occurs, session_send_transfer shall fail and return a non-zero value.] */ |
AzureIoTClient | 6:641a9672db08 | 1384 | result = SESSION_SEND_TRANSFER_ERROR; |
AzureIoTClient | 6:641a9672db08 | 1385 | } |
AzureIoTClient | 6:641a9672db08 | 1386 | else |
AzureIoTClient | 6:641a9672db08 | 1387 | { |
AzureIoTClient | 6:641a9672db08 | 1388 | AMQP_VALUE transfer_value; |
Azure.IoT Build | 0:6ae2f7bca550 | 1389 | |
AzureIoTClient | 6:641a9672db08 | 1390 | transfer_value = amqpvalue_create_transfer(transfer); |
AzureIoTClient | 6:641a9672db08 | 1391 | if (transfer_value == NULL) |
AzureIoTClient | 6:641a9672db08 | 1392 | { |
AzureIoTClient | 6:641a9672db08 | 1393 | /* Codes_SRS_SESSION_01_058: [When any other error occurs, session_send_transfer shall fail and return a non-zero value.] */ |
AzureIoTClient | 6:641a9672db08 | 1394 | result = SESSION_SEND_TRANSFER_ERROR; |
AzureIoTClient | 6:641a9672db08 | 1395 | } |
AzureIoTClient | 6:641a9672db08 | 1396 | else |
AzureIoTClient | 6:641a9672db08 | 1397 | { |
AzureIoTClient | 6:641a9672db08 | 1398 | uint32_t available_frame_size; |
AzureIoTClient | 6:641a9672db08 | 1399 | size_t encoded_size; |
Azure.IoT Build | 0:6ae2f7bca550 | 1400 | |
AzureIoTClient | 6:641a9672db08 | 1401 | if ((connection_get_remote_max_frame_size(session_instance->connection, &available_frame_size) != 0) || |
AzureIoTClient | 6:641a9672db08 | 1402 | (amqpvalue_get_encoded_size(transfer_value, &encoded_size) != 0)) |
AzureIoTClient | 6:641a9672db08 | 1403 | { |
AzureIoTClient | 6:641a9672db08 | 1404 | result = SESSION_SEND_TRANSFER_ERROR; |
AzureIoTClient | 6:641a9672db08 | 1405 | } |
AzureIoTClient | 6:641a9672db08 | 1406 | else |
AzureIoTClient | 6:641a9672db08 | 1407 | { |
AzureIoTClient | 6:641a9672db08 | 1408 | payload_size = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 1409 | |
AzureIoTClient | 6:641a9672db08 | 1410 | for (i = 0; i < payload_count; i++) |
AzureIoTClient | 6:641a9672db08 | 1411 | { |
AzureIoTClient | 6:641a9672db08 | 1412 | payload_size += payloads[i].length; |
AzureIoTClient | 6:641a9672db08 | 1413 | } |
AzureIoTClient | 6:641a9672db08 | 1414 | |
AzureIoTClient | 6:641a9672db08 | 1415 | available_frame_size -= (uint32_t)encoded_size; |
AzureIoTClient | 6:641a9672db08 | 1416 | available_frame_size -= 8; |
Azure.IoT Build | 0:6ae2f7bca550 | 1417 | |
AzureIoTClient | 6:641a9672db08 | 1418 | if (available_frame_size >= payload_size) |
AzureIoTClient | 6:641a9672db08 | 1419 | { |
AzureIoTClient | 6:641a9672db08 | 1420 | /* Codes_SRS_SESSION_01_055: [The encoding of the frame shall be done by calling connection_encode_frame and passing as arguments: the connection handle associated with the session, the transfer performative and the payload chunks passed to session_send_transfer.] */ |
AzureIoTClient | 6:641a9672db08 | 1421 | if (connection_encode_frame(session_instance->endpoint, transfer_value, payloads, payload_count, on_send_complete, callback_context) != 0) |
AzureIoTClient | 6:641a9672db08 | 1422 | { |
AzureIoTClient | 6:641a9672db08 | 1423 | /* Codes_SRS_SESSION_01_056: [If connection_encode_frame fails then session_send_transfer shall fail and return a non-zero value.] */ |
AzureIoTClient | 6:641a9672db08 | 1424 | result = SESSION_SEND_TRANSFER_ERROR; |
AzureIoTClient | 6:641a9672db08 | 1425 | } |
AzureIoTClient | 6:641a9672db08 | 1426 | else |
AzureIoTClient | 6:641a9672db08 | 1427 | { |
AzureIoTClient | 6:641a9672db08 | 1428 | /* Codes_SRS_SESSION_01_018: [is incremented after each successive transfer according to RFC-1982 [RFC1982] serial number arithmetic.] */ |
AzureIoTClient | 6:641a9672db08 | 1429 | session_instance->next_outgoing_id++; |
AzureIoTClient | 6:641a9672db08 | 1430 | session_instance->remote_incoming_window--; |
AzureIoTClient | 6:641a9672db08 | 1431 | session_instance->outgoing_window--; |
Azure.IoT Build | 0:6ae2f7bca550 | 1432 | |
AzureIoTClient | 6:641a9672db08 | 1433 | /* Codes_SRS_SESSION_01_053: [On success, session_send_transfer shall return 0.] */ |
AzureIoTClient | 6:641a9672db08 | 1434 | result = SESSION_SEND_TRANSFER_OK; |
AzureIoTClient | 6:641a9672db08 | 1435 | } |
AzureIoTClient | 6:641a9672db08 | 1436 | } |
AzureIoTClient | 6:641a9672db08 | 1437 | else |
AzureIoTClient | 6:641a9672db08 | 1438 | { |
AzureIoTClient | 6:641a9672db08 | 1439 | size_t current_payload_index = 0; |
AzureIoTClient | 6:641a9672db08 | 1440 | uint32_t current_payload_pos = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 1441 | |
AzureIoTClient | 6:641a9672db08 | 1442 | /* break it down into different deliveries */ |
AzureIoTClient | 6:641a9672db08 | 1443 | while (payload_size > 0) |
AzureIoTClient | 6:641a9672db08 | 1444 | { |
AzureIoTClient | 6:641a9672db08 | 1445 | uint32_t transfer_frame_payload_count = 0; |
AzureIoTClient | 6:641a9672db08 | 1446 | uint32_t current_transfer_frame_payload_size = (uint32_t)payload_size; |
AzureIoTClient | 6:641a9672db08 | 1447 | uint32_t byte_counter; |
AzureIoTClient | 6:641a9672db08 | 1448 | size_t temp_current_payload_index = current_payload_index; |
AzureIoTClient | 6:641a9672db08 | 1449 | uint32_t temp_current_payload_pos = current_payload_pos; |
AzureIoTClient | 6:641a9672db08 | 1450 | AMQP_VALUE multi_transfer_amqp_value; |
AzureIoTClient | 28:add19eb7defa | 1451 | PAYLOAD* transfer_frame_payloads; |
AzureIoTClient | 6:641a9672db08 | 1452 | bool more; |
AzureIoTClient | 6:641a9672db08 | 1453 | |
AzureIoTClient | 6:641a9672db08 | 1454 | if (current_transfer_frame_payload_size > available_frame_size) |
AzureIoTClient | 6:641a9672db08 | 1455 | { |
AzureIoTClient | 6:641a9672db08 | 1456 | current_transfer_frame_payload_size = available_frame_size; |
AzureIoTClient | 6:641a9672db08 | 1457 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1458 | |
AzureIoTClient | 6:641a9672db08 | 1459 | if (available_frame_size >= payload_size) |
AzureIoTClient | 6:641a9672db08 | 1460 | { |
AzureIoTClient | 6:641a9672db08 | 1461 | more = false; |
AzureIoTClient | 6:641a9672db08 | 1462 | } |
AzureIoTClient | 6:641a9672db08 | 1463 | else |
AzureIoTClient | 6:641a9672db08 | 1464 | { |
AzureIoTClient | 6:641a9672db08 | 1465 | more = true; |
AzureIoTClient | 6:641a9672db08 | 1466 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1467 | |
AzureIoTClient | 6:641a9672db08 | 1468 | if (transfer_set_more(transfer, more) != 0) |
AzureIoTClient | 6:641a9672db08 | 1469 | { |
AzureIoTClient | 6:641a9672db08 | 1470 | break; |
AzureIoTClient | 6:641a9672db08 | 1471 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1472 | |
AzureIoTClient | 6:641a9672db08 | 1473 | multi_transfer_amqp_value = amqpvalue_create_transfer(transfer); |
AzureIoTClient | 6:641a9672db08 | 1474 | if (multi_transfer_amqp_value == NULL) |
AzureIoTClient | 6:641a9672db08 | 1475 | { |
AzureIoTClient | 6:641a9672db08 | 1476 | break; |
AzureIoTClient | 6:641a9672db08 | 1477 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1478 | |
AzureIoTClient | 6:641a9672db08 | 1479 | byte_counter = current_transfer_frame_payload_size; |
AzureIoTClient | 6:641a9672db08 | 1480 | while (byte_counter > 0) |
AzureIoTClient | 6:641a9672db08 | 1481 | { |
AzureIoTClient | 6:641a9672db08 | 1482 | if (payloads[temp_current_payload_index].length - temp_current_payload_pos >= byte_counter) |
AzureIoTClient | 6:641a9672db08 | 1483 | { |
AzureIoTClient | 6:641a9672db08 | 1484 | /* more data than we need */ |
AzureIoTClient | 6:641a9672db08 | 1485 | temp_current_payload_pos += byte_counter; |
AzureIoTClient | 6:641a9672db08 | 1486 | byte_counter = 0; |
AzureIoTClient | 6:641a9672db08 | 1487 | } |
AzureIoTClient | 6:641a9672db08 | 1488 | else |
AzureIoTClient | 6:641a9672db08 | 1489 | { |
AzureIoTClient | 6:641a9672db08 | 1490 | byte_counter -= (uint32_t)payloads[temp_current_payload_index].length - temp_current_payload_pos; |
AzureIoTClient | 6:641a9672db08 | 1491 | temp_current_payload_index++; |
AzureIoTClient | 6:641a9672db08 | 1492 | temp_current_payload_pos = 0; |
AzureIoTClient | 6:641a9672db08 | 1493 | } |
AzureIoTClient | 6:641a9672db08 | 1494 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1495 | |
AzureIoTClient | 6:641a9672db08 | 1496 | transfer_frame_payload_count = (uint32_t)(temp_current_payload_index - current_payload_index + 1); |
AzureIoTClient | 25:1101516ee67d | 1497 | transfer_frame_payloads = (PAYLOAD*)malloc(transfer_frame_payload_count * sizeof(PAYLOAD)); |
AzureIoTClient | 6:641a9672db08 | 1498 | if (transfer_frame_payloads == NULL) |
AzureIoTClient | 6:641a9672db08 | 1499 | { |
AzureIoTClient | 6:641a9672db08 | 1500 | amqpvalue_destroy(multi_transfer_amqp_value); |
AzureIoTClient | 6:641a9672db08 | 1501 | break; |
AzureIoTClient | 6:641a9672db08 | 1502 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1503 | |
AzureIoTClient | 6:641a9672db08 | 1504 | /* copy data */ |
AzureIoTClient | 6:641a9672db08 | 1505 | byte_counter = current_transfer_frame_payload_size; |
AzureIoTClient | 6:641a9672db08 | 1506 | transfer_frame_payload_count = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 1507 | |
AzureIoTClient | 6:641a9672db08 | 1508 | while (byte_counter > 0) |
AzureIoTClient | 6:641a9672db08 | 1509 | { |
AzureIoTClient | 6:641a9672db08 | 1510 | if (payloads[current_payload_index].length - current_payload_pos > byte_counter) |
AzureIoTClient | 6:641a9672db08 | 1511 | { |
AzureIoTClient | 6:641a9672db08 | 1512 | /* more data than we need */ |
AzureIoTClient | 6:641a9672db08 | 1513 | transfer_frame_payloads[transfer_frame_payload_count].bytes = payloads[current_payload_index].bytes + current_payload_pos; |
AzureIoTClient | 6:641a9672db08 | 1514 | transfer_frame_payloads[transfer_frame_payload_count].length = byte_counter; |
AzureIoTClient | 6:641a9672db08 | 1515 | current_payload_pos += byte_counter; |
AzureIoTClient | 6:641a9672db08 | 1516 | byte_counter = 0; |
AzureIoTClient | 6:641a9672db08 | 1517 | } |
AzureIoTClient | 6:641a9672db08 | 1518 | else |
AzureIoTClient | 6:641a9672db08 | 1519 | { |
AzureIoTClient | 6:641a9672db08 | 1520 | /* copy entire payload and move to the next */ |
AzureIoTClient | 6:641a9672db08 | 1521 | transfer_frame_payloads[transfer_frame_payload_count].bytes = payloads[current_payload_index].bytes + current_payload_pos; |
AzureIoTClient | 6:641a9672db08 | 1522 | transfer_frame_payloads[transfer_frame_payload_count].length = payloads[current_payload_index].length - current_payload_pos; |
AzureIoTClient | 6:641a9672db08 | 1523 | byte_counter -= (uint32_t)payloads[current_payload_index].length - current_payload_pos; |
AzureIoTClient | 6:641a9672db08 | 1524 | current_payload_index++; |
AzureIoTClient | 6:641a9672db08 | 1525 | current_payload_pos = 0; |
AzureIoTClient | 6:641a9672db08 | 1526 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1527 | |
AzureIoTClient | 6:641a9672db08 | 1528 | transfer_frame_payload_count++; |
AzureIoTClient | 6:641a9672db08 | 1529 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1530 | |
AzureIoTClient | 6:641a9672db08 | 1531 | if (connection_encode_frame(session_instance->endpoint, multi_transfer_amqp_value, transfer_frame_payloads, transfer_frame_payload_count, on_send_complete, callback_context) != 0) |
AzureIoTClient | 6:641a9672db08 | 1532 | { |
AzureIoTClient | 21:f9c433d8e6ca | 1533 | free(transfer_frame_payloads); |
AzureIoTClient | 6:641a9672db08 | 1534 | amqpvalue_destroy(multi_transfer_amqp_value); |
AzureIoTClient | 6:641a9672db08 | 1535 | break; |
AzureIoTClient | 6:641a9672db08 | 1536 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1537 | |
AzureIoTClient | 21:f9c433d8e6ca | 1538 | free(transfer_frame_payloads); |
AzureIoTClient | 6:641a9672db08 | 1539 | amqpvalue_destroy(multi_transfer_amqp_value); |
AzureIoTClient | 6:641a9672db08 | 1540 | payload_size -= current_transfer_frame_payload_size; |
AzureIoTClient | 6:641a9672db08 | 1541 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1542 | |
AzureIoTClient | 6:641a9672db08 | 1543 | if (payload_size > 0) |
AzureIoTClient | 6:641a9672db08 | 1544 | { |
AzureIoTClient | 6:641a9672db08 | 1545 | result = SESSION_SEND_TRANSFER_ERROR; |
AzureIoTClient | 6:641a9672db08 | 1546 | } |
AzureIoTClient | 6:641a9672db08 | 1547 | else |
AzureIoTClient | 6:641a9672db08 | 1548 | { |
AzureIoTClient | 6:641a9672db08 | 1549 | /* Codes_SRS_SESSION_01_018: [is incremented after each successive transfer according to RFC-1982 [RFC1982] serial number arithmetic.] */ |
AzureIoTClient | 6:641a9672db08 | 1550 | session_instance->next_outgoing_id++; |
AzureIoTClient | 6:641a9672db08 | 1551 | session_instance->remote_incoming_window--; |
AzureIoTClient | 6:641a9672db08 | 1552 | session_instance->outgoing_window--; |
Azure.IoT Build | 0:6ae2f7bca550 | 1553 | |
AzureIoTClient | 6:641a9672db08 | 1554 | result = SESSION_SEND_TRANSFER_OK; |
AzureIoTClient | 6:641a9672db08 | 1555 | } |
AzureIoTClient | 6:641a9672db08 | 1556 | } |
AzureIoTClient | 6:641a9672db08 | 1557 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1558 | |
AzureIoTClient | 6:641a9672db08 | 1559 | amqpvalue_destroy(transfer_value); |
AzureIoTClient | 6:641a9672db08 | 1560 | } |
AzureIoTClient | 6:641a9672db08 | 1561 | } |
AzureIoTClient | 6:641a9672db08 | 1562 | } |
AzureIoTClient | 6:641a9672db08 | 1563 | } |
AzureIoTClient | 28:add19eb7defa | 1564 | } |
AzureIoTClient | 28:add19eb7defa | 1565 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1566 | |
AzureIoTClient | 28:add19eb7defa | 1567 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1568 | } |