A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Fri Jul 14 16:38:03 2017 -0700
Revision:
29:4a11413cf217
Parent:
28:add19eb7defa
Child:
30:0407b2db334c
1.1.19

Who changed what in which revision?

UserRevisionLine numberNew 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 29:4a11413cf217 377 if (attach_get_name(attach_handle, &name) != 0)
AzureIoTClient 28:add19eb7defa 378 {
AzureIoTClient 28:add19eb7defa 379 end_session_with_error(session_instance, "amqp:decode-error", "Cannot get link name from ATTACH frame");
AzureIoTClient 28:add19eb7defa 380 }
AzureIoTClient 29:4a11413cf217 381 else if (attach_get_role(attach_handle, &role) != 0)
AzureIoTClient 29:4a11413cf217 382 {
AzureIoTClient 29:4a11413cf217 383 end_session_with_error(session_instance, "amqp:decode-error", "Cannot get link role from ATTACH frame");
AzureIoTClient 29:4a11413cf217 384 }
AzureIoTClient 29:4a11413cf217 385 else if (attach_get_source(attach_handle, &source) != 0)
AzureIoTClient 29:4a11413cf217 386 {
AzureIoTClient 29:4a11413cf217 387 end_session_with_error(session_instance, "amqp:decode-error", "Cannot get link source from ATTACH frame");
AzureIoTClient 29:4a11413cf217 388 }
AzureIoTClient 29:4a11413cf217 389 else if (attach_get_target(attach_handle, &target) != 0)
AzureIoTClient 29:4a11413cf217 390 {
AzureIoTClient 29:4a11413cf217 391 end_session_with_error(session_instance, "amqp:decode-error", "Cannot get link target from ATTACH frame");
AzureIoTClient 29:4a11413cf217 392 }
AzureIoTClient 28:add19eb7defa 393 else
AzureIoTClient 28:add19eb7defa 394 {
AzureIoTClient 28:add19eb7defa 395 LINK_ENDPOINT_INSTANCE* link_endpoint = find_link_endpoint_by_name(session_instance, name);
AzureIoTClient 28:add19eb7defa 396 if (link_endpoint == NULL)
AzureIoTClient 28:add19eb7defa 397 {
AzureIoTClient 28:add19eb7defa 398 /* new link attach */
AzureIoTClient 28:add19eb7defa 399 if (session_instance->on_link_attached != NULL)
AzureIoTClient 28:add19eb7defa 400 {
AzureIoTClient 28:add19eb7defa 401 LINK_ENDPOINT_HANDLE new_link_endpoint = session_create_link_endpoint(session_instance, name);
AzureIoTClient 28:add19eb7defa 402 if (new_link_endpoint == NULL)
AzureIoTClient 28:add19eb7defa 403 {
AzureIoTClient 28:add19eb7defa 404 end_session_with_error(session_instance, "amqp:internal-error", "Cannot create link endpoint");
AzureIoTClient 28:add19eb7defa 405 }
Azure.IoT Build 0:6ae2f7bca550 406 else if (attach_get_handle(attach_handle, &new_link_endpoint->input_handle) != 0)
Azure.IoT Build 0:6ae2f7bca550 407 {
Azure.IoT Build 0:6ae2f7bca550 408 end_session_with_error(session_instance, "amqp:decode-error", "Cannot get input handle from ATTACH frame");
Azure.IoT Build 0:6ae2f7bca550 409 }
Azure.IoT Build 0:6ae2f7bca550 410 else
AzureIoTClient 28:add19eb7defa 411 {
AzureIoTClient 28:add19eb7defa 412 if (!session_instance->on_link_attached(session_instance->on_link_attached_callback_context, new_link_endpoint, name, role, source, target))
AzureIoTClient 28:add19eb7defa 413 {
AzureIoTClient 28:add19eb7defa 414 session_destroy_link_endpoint(new_link_endpoint);
AzureIoTClient 28:add19eb7defa 415 new_link_endpoint = NULL;
AzureIoTClient 28:add19eb7defa 416 }
AzureIoTClient 28:add19eb7defa 417 else
AzureIoTClient 28:add19eb7defa 418 {
AzureIoTClient 28:add19eb7defa 419 if (new_link_endpoint->frame_received_callback != NULL)
AzureIoTClient 28:add19eb7defa 420 {
AzureIoTClient 28:add19eb7defa 421 new_link_endpoint->frame_received_callback(new_link_endpoint->callback_context, performative, payload_size, payload_bytes);
AzureIoTClient 28:add19eb7defa 422 }
AzureIoTClient 28:add19eb7defa 423 }
AzureIoTClient 28:add19eb7defa 424 }
AzureIoTClient 28:add19eb7defa 425 }
AzureIoTClient 28:add19eb7defa 426 }
AzureIoTClient 28:add19eb7defa 427 else
AzureIoTClient 28:add19eb7defa 428 {
AzureIoTClient 28:add19eb7defa 429 if (attach_get_handle(attach_handle, &link_endpoint->input_handle) != 0)
AzureIoTClient 28:add19eb7defa 430 {
AzureIoTClient 28:add19eb7defa 431 end_session_with_error(session_instance, "amqp:decode-error", "Cannot get input handle from ATTACH frame");
AzureIoTClient 28:add19eb7defa 432 }
AzureIoTClient 28:add19eb7defa 433 else
AzureIoTClient 28:add19eb7defa 434 {
AzureIoTClient 28:add19eb7defa 435 link_endpoint->frame_received_callback(link_endpoint->callback_context, performative, payload_size, payload_bytes);
AzureIoTClient 28:add19eb7defa 436 }
AzureIoTClient 28:add19eb7defa 437 }
AzureIoTClient 28:add19eb7defa 438 }
Azure.IoT Build 0:6ae2f7bca550 439
AzureIoTClient 28:add19eb7defa 440 attach_destroy(attach_handle);
AzureIoTClient 28:add19eb7defa 441 }
AzureIoTClient 28:add19eb7defa 442 }
AzureIoTClient 28:add19eb7defa 443 else if (is_detach_type_by_descriptor(descriptor))
AzureIoTClient 28:add19eb7defa 444 {
AzureIoTClient 28:add19eb7defa 445 DETACH_HANDLE detach_handle;
Azure.IoT Build 0:6ae2f7bca550 446
AzureIoTClient 28:add19eb7defa 447 if (amqpvalue_get_detach(performative, &detach_handle) != 0)
AzureIoTClient 28:add19eb7defa 448 {
AzureIoTClient 28:add19eb7defa 449 end_session_with_error(session_instance, "amqp:decode-error", "Cannot decode DETACH frame");
AzureIoTClient 28:add19eb7defa 450 }
AzureIoTClient 28:add19eb7defa 451 else
AzureIoTClient 28:add19eb7defa 452 {
AzureIoTClient 28:add19eb7defa 453 uint32_t remote_handle;
AzureIoTClient 28:add19eb7defa 454 if (detach_get_handle(detach_handle, &remote_handle) != 0)
AzureIoTClient 28:add19eb7defa 455 {
AzureIoTClient 28:add19eb7defa 456 end_session_with_error(session_instance, "amqp:decode-error", "Cannot get handle from DETACH frame");
Azure.IoT Build 0:6ae2f7bca550 457
AzureIoTClient 28:add19eb7defa 458 detach_destroy(detach_handle);
AzureIoTClient 28:add19eb7defa 459 }
AzureIoTClient 28:add19eb7defa 460 else
AzureIoTClient 28:add19eb7defa 461 {
AzureIoTClient 28:add19eb7defa 462 LINK_ENDPOINT_INSTANCE* link_endpoint;
AzureIoTClient 28:add19eb7defa 463 detach_destroy(detach_handle);
Azure.IoT Build 0:6ae2f7bca550 464
AzureIoTClient 28:add19eb7defa 465 link_endpoint = find_link_endpoint_by_input_handle(session_instance, remote_handle);
AzureIoTClient 28:add19eb7defa 466 if (link_endpoint == NULL)
AzureIoTClient 28:add19eb7defa 467 {
AzureIoTClient 28:add19eb7defa 468 end_session_with_error(session_instance, "amqp:session:unattached-handle", "");
AzureIoTClient 28:add19eb7defa 469 }
AzureIoTClient 28:add19eb7defa 470 else
AzureIoTClient 28:add19eb7defa 471 {
AzureIoTClient 28:add19eb7defa 472 link_endpoint->frame_received_callback(link_endpoint->callback_context, performative, payload_size, payload_bytes);
AzureIoTClient 28:add19eb7defa 473 }
AzureIoTClient 28:add19eb7defa 474 }
AzureIoTClient 28:add19eb7defa 475 }
AzureIoTClient 28:add19eb7defa 476 }
AzureIoTClient 28:add19eb7defa 477 else if (is_flow_type_by_descriptor(descriptor))
AzureIoTClient 28:add19eb7defa 478 {
AzureIoTClient 28:add19eb7defa 479 FLOW_HANDLE flow_handle;
Azure.IoT Build 0:6ae2f7bca550 480
AzureIoTClient 28:add19eb7defa 481 if (amqpvalue_get_flow(performative, &flow_handle) != 0)
AzureIoTClient 28:add19eb7defa 482 {
AzureIoTClient 28:add19eb7defa 483 end_session_with_error(session_instance, "amqp:decode-error", "Cannot decode FLOW frame");
AzureIoTClient 28:add19eb7defa 484 }
AzureIoTClient 28:add19eb7defa 485 else
AzureIoTClient 28:add19eb7defa 486 {
AzureIoTClient 28:add19eb7defa 487 uint32_t remote_handle;
AzureIoTClient 28:add19eb7defa 488 transfer_number flow_next_incoming_id;
AzureIoTClient 28:add19eb7defa 489 uint32_t flow_incoming_window;
Azure.IoT Build 0:6ae2f7bca550 490
Azure.IoT Build 0:6ae2f7bca550 491 if (flow_get_next_incoming_id(flow_handle, &flow_next_incoming_id) != 0)
Azure.IoT Build 0:6ae2f7bca550 492 {
Azure.IoT Build 0:6ae2f7bca550 493 /*
Azure.IoT Build 0:6ae2f7bca550 494 If the next-incoming-id field of the flow frame is not set,
Azure.IoT Build 0:6ae2f7bca550 495 then remote-incomingwindow is computed as follows:
Azure.IoT Build 0:6ae2f7bca550 496 initial-outgoing-id(endpoint) + incoming-window(flow) - next-outgoing-id(endpoint)
Azure.IoT Build 0:6ae2f7bca550 497 */
Azure.IoT Build 0:6ae2f7bca550 498 flow_next_incoming_id = session_instance->next_outgoing_id;
Azure.IoT Build 0:6ae2f7bca550 499 }
Azure.IoT Build 0:6ae2f7bca550 500
AzureIoTClient 28:add19eb7defa 501 if ((flow_get_next_outgoing_id(flow_handle, &session_instance->next_incoming_id) != 0) ||
AzureIoTClient 28:add19eb7defa 502 (flow_get_incoming_window(flow_handle, &flow_incoming_window) != 0))
AzureIoTClient 28:add19eb7defa 503 {
AzureIoTClient 28:add19eb7defa 504 flow_destroy(flow_handle);
Azure.IoT Build 0:6ae2f7bca550 505
AzureIoTClient 28:add19eb7defa 506 end_session_with_error(session_instance, "amqp:decode-error", "Cannot decode FLOW frame");
AzureIoTClient 28:add19eb7defa 507 }
AzureIoTClient 28:add19eb7defa 508 else
AzureIoTClient 28:add19eb7defa 509 {
AzureIoTClient 28:add19eb7defa 510 LINK_ENDPOINT_INSTANCE* link_endpoint_instance = NULL;
AzureIoTClient 28:add19eb7defa 511 size_t i;
Azure.IoT Build 0:6ae2f7bca550 512
AzureIoTClient 28:add19eb7defa 513 session_instance->remote_incoming_window = flow_next_incoming_id + flow_incoming_window - session_instance->next_outgoing_id;
Azure.IoT Build 0:6ae2f7bca550 514
AzureIoTClient 28:add19eb7defa 515 if (flow_get_handle(flow_handle, &remote_handle) == 0)
AzureIoTClient 28:add19eb7defa 516 {
AzureIoTClient 28:add19eb7defa 517 link_endpoint_instance = find_link_endpoint_by_input_handle(session_instance, remote_handle);
AzureIoTClient 28:add19eb7defa 518 }
Azure.IoT Build 0:6ae2f7bca550 519
AzureIoTClient 28:add19eb7defa 520 flow_destroy(flow_handle);
Azure.IoT Build 0:6ae2f7bca550 521
AzureIoTClient 28:add19eb7defa 522 if (link_endpoint_instance != NULL)
AzureIoTClient 28:add19eb7defa 523 {
AzureIoTClient 28:add19eb7defa 524 link_endpoint_instance->frame_received_callback(link_endpoint_instance->callback_context, performative, payload_size, payload_bytes);
AzureIoTClient 28:add19eb7defa 525 }
Azure.IoT Build 0:6ae2f7bca550 526
AzureIoTClient 28:add19eb7defa 527 i = 0;
AzureIoTClient 28:add19eb7defa 528 while ((session_instance->remote_incoming_window > 0) && (i < session_instance->link_endpoint_count))
AzureIoTClient 28:add19eb7defa 529 {
AzureIoTClient 28:add19eb7defa 530 /* notify the caller that it can send here */
AzureIoTClient 28:add19eb7defa 531 if (session_instance->link_endpoints[i]->on_session_flow_on != NULL)
AzureIoTClient 28:add19eb7defa 532 {
AzureIoTClient 28:add19eb7defa 533 session_instance->link_endpoints[i]->on_session_flow_on(session_instance->link_endpoints[i]->callback_context);
AzureIoTClient 28:add19eb7defa 534 }
Azure.IoT Build 0:6ae2f7bca550 535
AzureIoTClient 28:add19eb7defa 536 i++;
AzureIoTClient 28:add19eb7defa 537 }
AzureIoTClient 28:add19eb7defa 538 }
AzureIoTClient 28:add19eb7defa 539 }
AzureIoTClient 28:add19eb7defa 540 }
AzureIoTClient 28:add19eb7defa 541 else if (is_transfer_type_by_descriptor(descriptor))
AzureIoTClient 28:add19eb7defa 542 {
AzureIoTClient 28:add19eb7defa 543 TRANSFER_HANDLE transfer_handle;
Azure.IoT Build 0:6ae2f7bca550 544
AzureIoTClient 28:add19eb7defa 545 if (amqpvalue_get_transfer(performative, &transfer_handle) != 0)
AzureIoTClient 28:add19eb7defa 546 {
AzureIoTClient 28:add19eb7defa 547 end_session_with_error(session_instance, "amqp:decode-error", "Cannot decode TRANSFER frame");
AzureIoTClient 28:add19eb7defa 548 }
AzureIoTClient 28:add19eb7defa 549 else
AzureIoTClient 28:add19eb7defa 550 {
AzureIoTClient 28:add19eb7defa 551 uint32_t remote_handle;
AzureIoTClient 28:add19eb7defa 552 delivery_number delivery_id;
Azure.IoT Build 0:6ae2f7bca550 553
AzureIoTClient 28:add19eb7defa 554 transfer_get_delivery_id(transfer_handle, &delivery_id);
AzureIoTClient 28:add19eb7defa 555 if (transfer_get_handle(transfer_handle, &remote_handle) != 0)
AzureIoTClient 28:add19eb7defa 556 {
AzureIoTClient 28:add19eb7defa 557 transfer_destroy(transfer_handle);
AzureIoTClient 28:add19eb7defa 558 end_session_with_error(session_instance, "amqp:decode-error", "Cannot get handle from TRANSFER frame");
AzureIoTClient 28:add19eb7defa 559 }
AzureIoTClient 28:add19eb7defa 560 else
AzureIoTClient 28:add19eb7defa 561 {
AzureIoTClient 28:add19eb7defa 562 LINK_ENDPOINT_INSTANCE* link_endpoint;
AzureIoTClient 28:add19eb7defa 563 transfer_destroy(transfer_handle);
Azure.IoT Build 0:6ae2f7bca550 564
AzureIoTClient 28:add19eb7defa 565 session_instance->next_incoming_id++;
AzureIoTClient 28:add19eb7defa 566 session_instance->remote_outgoing_window--;
AzureIoTClient 28:add19eb7defa 567 session_instance->incoming_window--;
Azure.IoT Build 0:6ae2f7bca550 568
AzureIoTClient 28:add19eb7defa 569 link_endpoint = find_link_endpoint_by_input_handle(session_instance, remote_handle);
AzureIoTClient 28:add19eb7defa 570 if (link_endpoint == NULL)
AzureIoTClient 28:add19eb7defa 571 {
AzureIoTClient 28:add19eb7defa 572 end_session_with_error(session_instance, "amqp:session:unattached-handle", "");
AzureIoTClient 28:add19eb7defa 573 }
AzureIoTClient 28:add19eb7defa 574 else
AzureIoTClient 28:add19eb7defa 575 {
AzureIoTClient 28:add19eb7defa 576 link_endpoint->frame_received_callback(link_endpoint->callback_context, performative, payload_size, payload_bytes);
AzureIoTClient 28:add19eb7defa 577 }
Azure.IoT Build 0:6ae2f7bca550 578
AzureIoTClient 28:add19eb7defa 579 if (session_instance->incoming_window == 0)
AzureIoTClient 28:add19eb7defa 580 {
Azure.IoT Build 0:6ae2f7bca550 581 session_instance->incoming_window = session_instance->desired_incoming_window;
AzureIoTClient 28:add19eb7defa 582 send_flow(session_instance);
AzureIoTClient 28:add19eb7defa 583 }
AzureIoTClient 28:add19eb7defa 584 }
AzureIoTClient 28:add19eb7defa 585 }
AzureIoTClient 28:add19eb7defa 586 }
AzureIoTClient 28:add19eb7defa 587 else if (is_disposition_type_by_descriptor(descriptor))
AzureIoTClient 28:add19eb7defa 588 {
AzureIoTClient 28:add19eb7defa 589 uint32_t i;
Azure.IoT Build 0:6ae2f7bca550 590
AzureIoTClient 28:add19eb7defa 591 for (i = 0; i < session_instance->link_endpoint_count; i++)
AzureIoTClient 28:add19eb7defa 592 {
AzureIoTClient 28:add19eb7defa 593 LINK_ENDPOINT_INSTANCE* link_endpoint = session_instance->link_endpoints[i];
AzureIoTClient 28:add19eb7defa 594 link_endpoint->frame_received_callback(link_endpoint->callback_context, performative, payload_size, payload_bytes);
AzureIoTClient 28:add19eb7defa 595 }
AzureIoTClient 28:add19eb7defa 596 }
AzureIoTClient 28:add19eb7defa 597 else if (is_end_type_by_descriptor(descriptor))
AzureIoTClient 28:add19eb7defa 598 {
AzureIoTClient 28:add19eb7defa 599 END_HANDLE end_handle;
Azure.IoT Build 0:6ae2f7bca550 600
AzureIoTClient 28:add19eb7defa 601 if (amqpvalue_get_end(performative, &end_handle) != 0)
AzureIoTClient 28:add19eb7defa 602 {
AzureIoTClient 28:add19eb7defa 603 end_session_with_error(session_instance, "amqp:decode-error", "Cannot decode END frame");
AzureIoTClient 28:add19eb7defa 604 }
AzureIoTClient 28:add19eb7defa 605 else
AzureIoTClient 28:add19eb7defa 606 {
AzureIoTClient 25:1101516ee67d 607 end_destroy(end_handle);
AzureIoTClient 28:add19eb7defa 608 if ((session_instance->session_state != SESSION_STATE_END_RCVD) &&
AzureIoTClient 28:add19eb7defa 609 (session_instance->session_state != SESSION_STATE_DISCARDING))
AzureIoTClient 28:add19eb7defa 610 {
AzureIoTClient 28:add19eb7defa 611 session_set_state(session_instance, SESSION_STATE_END_RCVD);
AzureIoTClient 28:add19eb7defa 612 if (send_end_frame(session_instance, NULL) != 0)
AzureIoTClient 28:add19eb7defa 613 {
AzureIoTClient 28:add19eb7defa 614 /* fatal error */
AzureIoTClient 28:add19eb7defa 615 (void)connection_close(session_instance->connection, "amqp:internal-error", "Cannot send END frame.");
AzureIoTClient 28:add19eb7defa 616 }
Azure.IoT Build 0:6ae2f7bca550 617
AzureIoTClient 28:add19eb7defa 618 session_set_state(session_instance, SESSION_STATE_DISCARDING);
AzureIoTClient 28:add19eb7defa 619 }
AzureIoTClient 28:add19eb7defa 620 }
AzureIoTClient 28:add19eb7defa 621 }
Azure.IoT Build 0:6ae2f7bca550 622 }
Azure.IoT Build 0:6ae2f7bca550 623
Azure.IoT Build 0:6ae2f7bca550 624 SESSION_HANDLE session_create(CONNECTION_HANDLE connection, ON_LINK_ATTACHED on_link_attached, void* callback_context)
Azure.IoT Build 0:6ae2f7bca550 625 {
AzureIoTClient 28:add19eb7defa 626 SESSION_INSTANCE* result;
Azure.IoT Build 0:6ae2f7bca550 627
AzureIoTClient 28:add19eb7defa 628 if (connection == NULL)
AzureIoTClient 28:add19eb7defa 629 {
AzureIoTClient 28:add19eb7defa 630 /* Codes_SRS_SESSION_01_031: [If connection is NULL, session_create shall fail and return NULL.] */
AzureIoTClient 28:add19eb7defa 631 result = NULL;
AzureIoTClient 28:add19eb7defa 632 }
AzureIoTClient 28:add19eb7defa 633 else
AzureIoTClient 28:add19eb7defa 634 {
AzureIoTClient 28:add19eb7defa 635 /* Codes_SRS_SESSION_01_030: [session_create shall create a new session instance and return a non-NULL handle to it.] */
AzureIoTClient 28:add19eb7defa 636 result = (SESSION_INSTANCE*)malloc(sizeof(SESSION_INSTANCE));
AzureIoTClient 28:add19eb7defa 637 /* Codes_SRS_SESSION_01_042: [If allocating memory for the session fails, session_create shall fail and return NULL.] */
AzureIoTClient 28:add19eb7defa 638 if (result != NULL)
AzureIoTClient 28:add19eb7defa 639 {
AzureIoTClient 28:add19eb7defa 640 result->connection = connection;
AzureIoTClient 28:add19eb7defa 641 result->link_endpoints = NULL;
AzureIoTClient 28:add19eb7defa 642 result->link_endpoint_count = 0;
AzureIoTClient 28:add19eb7defa 643 result->handle_max = 4294967295u;
Azure.IoT Build 0:6ae2f7bca550 644
AzureIoTClient 28:add19eb7defa 645 /* Codes_SRS_SESSION_01_057: [The delivery ids shall be assigned starting at 0.] */
AzureIoTClient 28:add19eb7defa 646 /* Codes_SRS_SESSION_01_017: [The nextoutgoing-id MAY be initialized to an arbitrary value ] */
AzureIoTClient 28:add19eb7defa 647 result->next_outgoing_id = 0;
Azure.IoT Build 0:6ae2f7bca550 648
Azure.IoT Build 0:6ae2f7bca550 649 result->desired_incoming_window = 1;
Azure.IoT Build 0:6ae2f7bca550 650 result->incoming_window = 1;
AzureIoTClient 28:add19eb7defa 651 result->outgoing_window = 1;
AzureIoTClient 28:add19eb7defa 652 result->handle_max = 4294967295u;
AzureIoTClient 28:add19eb7defa 653 result->remote_incoming_window = 0;
AzureIoTClient 28:add19eb7defa 654 result->remote_outgoing_window = 0;
AzureIoTClient 28:add19eb7defa 655 result->previous_session_state = SESSION_STATE_UNMAPPED;
AzureIoTClient 28:add19eb7defa 656 result->is_underlying_connection_open = UNDERLYING_CONNECTION_NOT_OPEN;
AzureIoTClient 28:add19eb7defa 657 result->session_state = SESSION_STATE_UNMAPPED;
AzureIoTClient 28:add19eb7defa 658 result->on_link_attached = on_link_attached;
AzureIoTClient 28:add19eb7defa 659 result->on_link_attached_callback_context = callback_context;
Azure.IoT Build 0:6ae2f7bca550 660
AzureIoTClient 28:add19eb7defa 661 /* Codes_SRS_SESSION_01_032: [session_create shall create a new session endpoint by calling connection_create_endpoint.] */
AzureIoTClient 28:add19eb7defa 662 result->endpoint = connection_create_endpoint(connection);
AzureIoTClient 28:add19eb7defa 663 if (result->endpoint == NULL)
AzureIoTClient 28:add19eb7defa 664 {
AzureIoTClient 28:add19eb7defa 665 /* Codes_SRS_SESSION_01_033: [If connection_create_endpoint fails, session_create shall fail and return NULL.] */
AzureIoTClient 28:add19eb7defa 666 free(result);
AzureIoTClient 28:add19eb7defa 667 result = NULL;
AzureIoTClient 28:add19eb7defa 668 }
AzureIoTClient 28:add19eb7defa 669 else
AzureIoTClient 28:add19eb7defa 670 {
AzureIoTClient 28:add19eb7defa 671 session_set_state(result, SESSION_STATE_UNMAPPED);
AzureIoTClient 28:add19eb7defa 672 }
AzureIoTClient 28:add19eb7defa 673 }
AzureIoTClient 28:add19eb7defa 674 }
Azure.IoT Build 0:6ae2f7bca550 675
AzureIoTClient 28:add19eb7defa 676 return result;
Azure.IoT Build 0:6ae2f7bca550 677 }
Azure.IoT Build 0:6ae2f7bca550 678
Azure.IoT Build 0:6ae2f7bca550 679 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 680 {
AzureIoTClient 28:add19eb7defa 681 SESSION_INSTANCE* result;
Azure.IoT Build 0:6ae2f7bca550 682
AzureIoTClient 28:add19eb7defa 683 if (endpoint == NULL)
AzureIoTClient 28:add19eb7defa 684 {
AzureIoTClient 28:add19eb7defa 685 result = NULL;
AzureIoTClient 28:add19eb7defa 686 }
AzureIoTClient 28:add19eb7defa 687 else
AzureIoTClient 28:add19eb7defa 688 {
AzureIoTClient 28:add19eb7defa 689 result = (SESSION_INSTANCE*)malloc(sizeof(SESSION_INSTANCE));
AzureIoTClient 28:add19eb7defa 690 if (result != NULL)
AzureIoTClient 28:add19eb7defa 691 {
AzureIoTClient 28:add19eb7defa 692 result->connection = connection;
AzureIoTClient 28:add19eb7defa 693 result->link_endpoints = NULL;
AzureIoTClient 28:add19eb7defa 694 result->link_endpoint_count = 0;
AzureIoTClient 28:add19eb7defa 695 result->handle_max = 4294967295u;
Azure.IoT Build 0:6ae2f7bca550 696
AzureIoTClient 28:add19eb7defa 697 result->next_outgoing_id = 0;
Azure.IoT Build 0:6ae2f7bca550 698
AzureIoTClient 22:524bded3f7a8 699 result->desired_incoming_window = 1;
AzureIoTClient 22:524bded3f7a8 700 result->incoming_window = 1;
AzureIoTClient 28:add19eb7defa 701 result->outgoing_window = 1;
AzureIoTClient 28:add19eb7defa 702 result->handle_max = 4294967295u;
AzureIoTClient 28:add19eb7defa 703 result->remote_incoming_window = 0;
AzureIoTClient 28:add19eb7defa 704 result->remote_outgoing_window = 0;
AzureIoTClient 28:add19eb7defa 705 result->previous_session_state = SESSION_STATE_UNMAPPED;
AzureIoTClient 28:add19eb7defa 706 result->is_underlying_connection_open = UNDERLYING_CONNECTION_NOT_OPEN;
AzureIoTClient 28:add19eb7defa 707 result->session_state = SESSION_STATE_UNMAPPED;
AzureIoTClient 28:add19eb7defa 708 result->on_link_attached = on_link_attached;
AzureIoTClient 28:add19eb7defa 709 result->on_link_attached_callback_context = callback_context;
Azure.IoT Build 0:6ae2f7bca550 710
AzureIoTClient 28:add19eb7defa 711 result->endpoint = endpoint;
AzureIoTClient 28:add19eb7defa 712 session_set_state(result, SESSION_STATE_UNMAPPED);
AzureIoTClient 28:add19eb7defa 713 }
AzureIoTClient 28:add19eb7defa 714 }
Azure.IoT Build 0:6ae2f7bca550 715
AzureIoTClient 28:add19eb7defa 716 return result;
Azure.IoT Build 0:6ae2f7bca550 717 }
Azure.IoT Build 0:6ae2f7bca550 718
Azure.IoT Build 0:6ae2f7bca550 719 void session_destroy(SESSION_HANDLE session)
Azure.IoT Build 0:6ae2f7bca550 720 {
AzureIoTClient 28:add19eb7defa 721 /* Codes_SRS_SESSION_01_036: [If session is NULL, session_destroy shall do nothing.] */
AzureIoTClient 28:add19eb7defa 722 if (session != NULL)
AzureIoTClient 28:add19eb7defa 723 {
AzureIoTClient 28:add19eb7defa 724 SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session;
Azure.IoT Build 0:6ae2f7bca550 725
AzureIoTClient 28:add19eb7defa 726 session_end(session, NULL, NULL);
Azure.IoT Build 0:6ae2f7bca550 727
AzureIoTClient 28:add19eb7defa 728 /* Codes_SRS_SESSION_01_034: [session_destroy shall free all resources allocated by session_create.] */
AzureIoTClient 28:add19eb7defa 729 /* Codes_SRS_SESSION_01_035: [The endpoint created in session_create shall be freed by calling connection_destroy_endpoint.] */
AzureIoTClient 28:add19eb7defa 730 connection_destroy_endpoint(session_instance->endpoint);
AzureIoTClient 28:add19eb7defa 731 if (session_instance->link_endpoints != NULL)
AzureIoTClient 28:add19eb7defa 732 {
AzureIoTClient 28:add19eb7defa 733 free(session_instance->link_endpoints);
AzureIoTClient 28:add19eb7defa 734 }
Azure.IoT Build 0:6ae2f7bca550 735
AzureIoTClient 28:add19eb7defa 736 free(session);
AzureIoTClient 28:add19eb7defa 737 }
Azure.IoT Build 0:6ae2f7bca550 738 }
Azure.IoT Build 0:6ae2f7bca550 739
Azure.IoT Build 0:6ae2f7bca550 740 int session_begin(SESSION_HANDLE session)
Azure.IoT Build 0:6ae2f7bca550 741 {
AzureIoTClient 28:add19eb7defa 742 int result;
Azure.IoT Build 0:6ae2f7bca550 743
AzureIoTClient 28:add19eb7defa 744 if (session == NULL)
AzureIoTClient 28:add19eb7defa 745 {
AzureIoTClient 28:add19eb7defa 746 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 747 }
AzureIoTClient 28:add19eb7defa 748 else
AzureIoTClient 28:add19eb7defa 749 {
AzureIoTClient 28:add19eb7defa 750 SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session;
Azure.IoT Build 0:6ae2f7bca550 751
AzureIoTClient 28:add19eb7defa 752 if (connection_start_endpoint(session_instance->endpoint, on_frame_received, on_connection_state_changed, session_instance) != 0)
AzureIoTClient 28:add19eb7defa 753 {
AzureIoTClient 28:add19eb7defa 754 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 755 }
AzureIoTClient 28:add19eb7defa 756 else
AzureIoTClient 28:add19eb7defa 757 {
AzureIoTClient 28:add19eb7defa 758 if (!session_instance->is_underlying_connection_open)
AzureIoTClient 28:add19eb7defa 759 {
AzureIoTClient 28:add19eb7defa 760 if (connection_open(session_instance->connection) != 0)
AzureIoTClient 28:add19eb7defa 761 {
AzureIoTClient 28:add19eb7defa 762 session_instance->is_underlying_connection_open = UNDERLYING_CONNECTION_NOT_OPEN;
AzureIoTClient 28:add19eb7defa 763 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 764 }
AzureIoTClient 28:add19eb7defa 765 else
AzureIoTClient 28:add19eb7defa 766 {
AzureIoTClient 28:add19eb7defa 767 session_instance->is_underlying_connection_open = UNDERLYING_CONNECTION_OPEN;
AzureIoTClient 28:add19eb7defa 768 result = 0;
AzureIoTClient 28:add19eb7defa 769 }
AzureIoTClient 28:add19eb7defa 770 }
AzureIoTClient 28:add19eb7defa 771 else
AzureIoTClient 28:add19eb7defa 772 {
AzureIoTClient 28:add19eb7defa 773 result = 0;
AzureIoTClient 28:add19eb7defa 774 }
AzureIoTClient 28:add19eb7defa 775 }
AzureIoTClient 28:add19eb7defa 776 }
Azure.IoT Build 0:6ae2f7bca550 777
AzureIoTClient 28:add19eb7defa 778 return result;
Azure.IoT Build 0:6ae2f7bca550 779 }
Azure.IoT Build 0:6ae2f7bca550 780
Azure.IoT Build 0:6ae2f7bca550 781 int session_end(SESSION_HANDLE session, const char* condition_value, const char* description)
Azure.IoT Build 0:6ae2f7bca550 782 {
AzureIoTClient 28:add19eb7defa 783 int result;
Azure.IoT Build 0:6ae2f7bca550 784
AzureIoTClient 28:add19eb7defa 785 if (session == NULL)
AzureIoTClient 28:add19eb7defa 786 {
AzureIoTClient 28:add19eb7defa 787 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 788 }
AzureIoTClient 28:add19eb7defa 789 else
AzureIoTClient 28:add19eb7defa 790 {
AzureIoTClient 28:add19eb7defa 791 SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session;
Azure.IoT Build 0:6ae2f7bca550 792
AzureIoTClient 28:add19eb7defa 793 if ((session_instance->session_state != SESSION_STATE_UNMAPPED) &&
AzureIoTClient 28:add19eb7defa 794 (session_instance->session_state != SESSION_STATE_DISCARDING))
AzureIoTClient 28:add19eb7defa 795 {
AzureIoTClient 28:add19eb7defa 796 ERROR_HANDLE error_handle = NULL;
AzureIoTClient 28:add19eb7defa 797 result = 0;
Azure.IoT Build 0:6ae2f7bca550 798
AzureIoTClient 28:add19eb7defa 799 if (condition_value != NULL)
AzureIoTClient 28:add19eb7defa 800 {
AzureIoTClient 28:add19eb7defa 801 error_handle = error_create(condition_value);
AzureIoTClient 28:add19eb7defa 802 if (error_handle == NULL)
AzureIoTClient 28:add19eb7defa 803 {
AzureIoTClient 28:add19eb7defa 804 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 805 }
AzureIoTClient 28:add19eb7defa 806 else
AzureIoTClient 28:add19eb7defa 807 {
AzureIoTClient 28:add19eb7defa 808 if (error_set_description(error_handle, description) != 0)
AzureIoTClient 28:add19eb7defa 809 {
AzureIoTClient 28:add19eb7defa 810 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 811 }
AzureIoTClient 28:add19eb7defa 812 }
AzureIoTClient 28:add19eb7defa 813 }
Azure.IoT Build 0:6ae2f7bca550 814
AzureIoTClient 28:add19eb7defa 815 if (result == 0)
AzureIoTClient 28:add19eb7defa 816 {
AzureIoTClient 28:add19eb7defa 817 if (send_end_frame(session_instance, error_handle) != 0)
AzureIoTClient 28:add19eb7defa 818 {
AzureIoTClient 28:add19eb7defa 819 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 820 }
AzureIoTClient 28:add19eb7defa 821 else
AzureIoTClient 28:add19eb7defa 822 {
AzureIoTClient 28:add19eb7defa 823 session_set_state(session_instance, SESSION_STATE_DISCARDING);
AzureIoTClient 28:add19eb7defa 824 result = 0;
AzureIoTClient 28:add19eb7defa 825 }
AzureIoTClient 28:add19eb7defa 826 }
Azure.IoT Build 0:6ae2f7bca550 827
AzureIoTClient 28:add19eb7defa 828 if (error_handle != NULL)
AzureIoTClient 28:add19eb7defa 829 {
AzureIoTClient 28:add19eb7defa 830 error_destroy(error_handle);
AzureIoTClient 28:add19eb7defa 831 }
AzureIoTClient 28:add19eb7defa 832 }
AzureIoTClient 28:add19eb7defa 833 else
AzureIoTClient 28:add19eb7defa 834 {
AzureIoTClient 28:add19eb7defa 835 result = 0;
AzureIoTClient 28:add19eb7defa 836 }
AzureIoTClient 28:add19eb7defa 837 }
Azure.IoT Build 0:6ae2f7bca550 838
AzureIoTClient 28:add19eb7defa 839 return result;
Azure.IoT Build 0:6ae2f7bca550 840 }
Azure.IoT Build 0:6ae2f7bca550 841
Azure.IoT Build 0:6ae2f7bca550 842 int session_set_incoming_window(SESSION_HANDLE session, uint32_t incoming_window)
Azure.IoT Build 0:6ae2f7bca550 843 {
AzureIoTClient 28:add19eb7defa 844 int result;
Azure.IoT Build 0:6ae2f7bca550 845
AzureIoTClient 28:add19eb7defa 846 if (session == NULL)
AzureIoTClient 28:add19eb7defa 847 {
AzureIoTClient 28:add19eb7defa 848 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 849 }
AzureIoTClient 28:add19eb7defa 850 else
AzureIoTClient 28:add19eb7defa 851 {
AzureIoTClient 28:add19eb7defa 852 SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session;
Azure.IoT Build 0:6ae2f7bca550 853
AzureIoTClient 28:add19eb7defa 854 session_instance->desired_incoming_window = incoming_window;
Azure.IoT Build 0:6ae2f7bca550 855 session_instance->incoming_window = incoming_window;
Azure.IoT Build 0:6ae2f7bca550 856
AzureIoTClient 28:add19eb7defa 857 result = 0;
AzureIoTClient 28:add19eb7defa 858 }
Azure.IoT Build 0:6ae2f7bca550 859
AzureIoTClient 28:add19eb7defa 860 return result;
Azure.IoT Build 0:6ae2f7bca550 861 }
Azure.IoT Build 0:6ae2f7bca550 862
Azure.IoT Build 0:6ae2f7bca550 863 int session_get_incoming_window(SESSION_HANDLE session, uint32_t* incoming_window)
Azure.IoT Build 0:6ae2f7bca550 864 {
AzureIoTClient 28:add19eb7defa 865 int result;
Azure.IoT Build 0:6ae2f7bca550 866
AzureIoTClient 28:add19eb7defa 867 if ((session == NULL) ||
AzureIoTClient 28:add19eb7defa 868 (incoming_window == NULL))
AzureIoTClient 28:add19eb7defa 869 {
AzureIoTClient 28:add19eb7defa 870 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 871 }
AzureIoTClient 28:add19eb7defa 872 else
AzureIoTClient 28:add19eb7defa 873 {
AzureIoTClient 28:add19eb7defa 874 SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session;
Azure.IoT Build 0:6ae2f7bca550 875
AzureIoTClient 28:add19eb7defa 876 *incoming_window = session_instance->incoming_window;
Azure.IoT Build 0:6ae2f7bca550 877
AzureIoTClient 28:add19eb7defa 878 result = 0;
AzureIoTClient 28:add19eb7defa 879 }
Azure.IoT Build 0:6ae2f7bca550 880
AzureIoTClient 28:add19eb7defa 881 return result;
Azure.IoT Build 0:6ae2f7bca550 882 }
Azure.IoT Build 0:6ae2f7bca550 883
Azure.IoT Build 0:6ae2f7bca550 884 int session_set_outgoing_window(SESSION_HANDLE session, uint32_t outgoing_window)
Azure.IoT Build 0:6ae2f7bca550 885 {
AzureIoTClient 28:add19eb7defa 886 int result;
Azure.IoT Build 0:6ae2f7bca550 887
AzureIoTClient 28:add19eb7defa 888 if (session == NULL)
AzureIoTClient 28:add19eb7defa 889 {
AzureIoTClient 28:add19eb7defa 890 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 891 }
AzureIoTClient 28:add19eb7defa 892 else
AzureIoTClient 28:add19eb7defa 893 {
AzureIoTClient 28:add19eb7defa 894 SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session;
Azure.IoT Build 0:6ae2f7bca550 895
AzureIoTClient 28:add19eb7defa 896 session_instance->outgoing_window = outgoing_window;
Azure.IoT Build 0:6ae2f7bca550 897
AzureIoTClient 28:add19eb7defa 898 result = 0;
AzureIoTClient 28:add19eb7defa 899 }
Azure.IoT Build 0:6ae2f7bca550 900
AzureIoTClient 28:add19eb7defa 901 return result;
Azure.IoT Build 0:6ae2f7bca550 902 }
Azure.IoT Build 0:6ae2f7bca550 903
Azure.IoT Build 0:6ae2f7bca550 904 int session_get_outgoing_window(SESSION_HANDLE session, uint32_t* outgoing_window)
Azure.IoT Build 0:6ae2f7bca550 905 {
AzureIoTClient 28:add19eb7defa 906 int result;
Azure.IoT Build 0:6ae2f7bca550 907
AzureIoTClient 28:add19eb7defa 908 if ((session == NULL) ||
AzureIoTClient 28:add19eb7defa 909 (outgoing_window == NULL))
AzureIoTClient 28:add19eb7defa 910 {
AzureIoTClient 28:add19eb7defa 911 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 912 }
AzureIoTClient 28:add19eb7defa 913 else
AzureIoTClient 28:add19eb7defa 914 {
AzureIoTClient 28:add19eb7defa 915 SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session;
Azure.IoT Build 0:6ae2f7bca550 916
AzureIoTClient 28:add19eb7defa 917 *outgoing_window = session_instance->outgoing_window;
Azure.IoT Build 0:6ae2f7bca550 918
AzureIoTClient 28:add19eb7defa 919 result = 0;
AzureIoTClient 28:add19eb7defa 920 }
Azure.IoT Build 0:6ae2f7bca550 921
AzureIoTClient 28:add19eb7defa 922 return result;
Azure.IoT Build 0:6ae2f7bca550 923 }
Azure.IoT Build 0:6ae2f7bca550 924
Azure.IoT Build 0:6ae2f7bca550 925 int session_set_handle_max(SESSION_HANDLE session, handle handle_max)
Azure.IoT Build 0:6ae2f7bca550 926 {
AzureIoTClient 28:add19eb7defa 927 int result;
Azure.IoT Build 0:6ae2f7bca550 928
AzureIoTClient 28:add19eb7defa 929 if (session == NULL)
AzureIoTClient 28:add19eb7defa 930 {
AzureIoTClient 28:add19eb7defa 931 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 932 }
AzureIoTClient 28:add19eb7defa 933 else
AzureIoTClient 28:add19eb7defa 934 {
AzureIoTClient 28:add19eb7defa 935 SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session;
Azure.IoT Build 0:6ae2f7bca550 936
AzureIoTClient 28:add19eb7defa 937 session_instance->handle_max = handle_max;
Azure.IoT Build 0:6ae2f7bca550 938
AzureIoTClient 28:add19eb7defa 939 result = 0;
AzureIoTClient 28:add19eb7defa 940 }
Azure.IoT Build 0:6ae2f7bca550 941
AzureIoTClient 28:add19eb7defa 942 return result;
Azure.IoT Build 0:6ae2f7bca550 943 }
Azure.IoT Build 0:6ae2f7bca550 944
Azure.IoT Build 0:6ae2f7bca550 945 int session_get_handle_max(SESSION_HANDLE session, handle* handle_max)
Azure.IoT Build 0:6ae2f7bca550 946 {
AzureIoTClient 28:add19eb7defa 947 int result;
Azure.IoT Build 0:6ae2f7bca550 948
AzureIoTClient 28:add19eb7defa 949 if ((session == NULL) ||
AzureIoTClient 28:add19eb7defa 950 (handle_max == NULL))
AzureIoTClient 28:add19eb7defa 951 {
AzureIoTClient 28:add19eb7defa 952 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 953 }
AzureIoTClient 28:add19eb7defa 954 else
AzureIoTClient 28:add19eb7defa 955 {
AzureIoTClient 28:add19eb7defa 956 SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session;
Azure.IoT Build 0:6ae2f7bca550 957
AzureIoTClient 28:add19eb7defa 958 *handle_max = session_instance->handle_max;
Azure.IoT Build 0:6ae2f7bca550 959
AzureIoTClient 28:add19eb7defa 960 result = 0;
AzureIoTClient 28:add19eb7defa 961 }
Azure.IoT Build 0:6ae2f7bca550 962
AzureIoTClient 28:add19eb7defa 963 return result;
Azure.IoT Build 0:6ae2f7bca550 964 }
Azure.IoT Build 0:6ae2f7bca550 965
Azure.IoT Build 0:6ae2f7bca550 966 LINK_ENDPOINT_HANDLE session_create_link_endpoint(SESSION_HANDLE session, const char* name)
Azure.IoT Build 0:6ae2f7bca550 967 {
AzureIoTClient 28:add19eb7defa 968 LINK_ENDPOINT_INSTANCE* result;
Azure.IoT Build 0:6ae2f7bca550 969
AzureIoTClient 28:add19eb7defa 970 /* 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 971 if ((session == NULL) ||
AzureIoTClient 28:add19eb7defa 972 (name == NULL))
AzureIoTClient 28:add19eb7defa 973 {
AzureIoTClient 28:add19eb7defa 974 result = NULL;
AzureIoTClient 28:add19eb7defa 975 }
AzureIoTClient 28:add19eb7defa 976 else
AzureIoTClient 28:add19eb7defa 977 {
AzureIoTClient 28:add19eb7defa 978 /* 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 979 SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session;
Azure.IoT Build 0:6ae2f7bca550 980
AzureIoTClient 28:add19eb7defa 981 result = (LINK_ENDPOINT_INSTANCE*)malloc(sizeof(LINK_ENDPOINT_INSTANCE));
AzureIoTClient 28:add19eb7defa 982 /* 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 983 if (result != NULL)
AzureIoTClient 28:add19eb7defa 984 {
AzureIoTClient 28:add19eb7defa 985 /* Codes_SRS_SESSION_01_046: [An unused handle shall be assigned to the link endpoint.] */
AzureIoTClient 28:add19eb7defa 986 handle selected_handle = 0;
AzureIoTClient 28:add19eb7defa 987 size_t i;
AzureIoTClient 23:1111ee8bcba4 988 size_t name_length;
Azure.IoT Build 0:6ae2f7bca550 989
AzureIoTClient 28:add19eb7defa 990 for (i = 0; i < session_instance->link_endpoint_count; i++)
AzureIoTClient 28:add19eb7defa 991 {
AzureIoTClient 28:add19eb7defa 992 if (session_instance->link_endpoints[i]->output_handle > selected_handle)
AzureIoTClient 28:add19eb7defa 993 {
AzureIoTClient 28:add19eb7defa 994 break;
AzureIoTClient 28:add19eb7defa 995 }
Azure.IoT Build 0:6ae2f7bca550 996
AzureIoTClient 28:add19eb7defa 997 selected_handle++;
AzureIoTClient 28:add19eb7defa 998 }
Azure.IoT Build 0:6ae2f7bca550 999
AzureIoTClient 28:add19eb7defa 1000 result->on_session_state_changed = NULL;
AzureIoTClient 28:add19eb7defa 1001 result->on_session_flow_on = NULL;
AzureIoTClient 28:add19eb7defa 1002 result->frame_received_callback = NULL;
AzureIoTClient 28:add19eb7defa 1003 result->callback_context = NULL;
AzureIoTClient 28:add19eb7defa 1004 result->output_handle = selected_handle;
AzureIoTClient 28:add19eb7defa 1005 result->input_handle = 0xFFFFFFFF;
AzureIoTClient 23:1111ee8bcba4 1006 name_length = strlen(name);
AzureIoTClient 28:add19eb7defa 1007 result->name = (char*)malloc(name_length + 1);
AzureIoTClient 28:add19eb7defa 1008 if (result->name == NULL)
AzureIoTClient 28:add19eb7defa 1009 {
AzureIoTClient 28:add19eb7defa 1010 /* 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 1011 free(result);
AzureIoTClient 28:add19eb7defa 1012 result = NULL;
AzureIoTClient 28:add19eb7defa 1013 }
AzureIoTClient 28:add19eb7defa 1014 else
AzureIoTClient 28:add19eb7defa 1015 {
AzureIoTClient 28:add19eb7defa 1016 LINK_ENDPOINT_INSTANCE** new_link_endpoints;
AzureIoTClient 28:add19eb7defa 1017 (void)memcpy(result->name, name, name_length + 1);
AzureIoTClient 28:add19eb7defa 1018 result->session = session;
Azure.IoT Build 0:6ae2f7bca550 1019
AzureIoTClient 28:add19eb7defa 1020 new_link_endpoints = (LINK_ENDPOINT_INSTANCE**)realloc(session_instance->link_endpoints, sizeof(LINK_ENDPOINT_INSTANCE*) * (session_instance->link_endpoint_count + 1));
AzureIoTClient 28:add19eb7defa 1021 if (new_link_endpoints == NULL)
AzureIoTClient 28:add19eb7defa 1022 {
AzureIoTClient 28:add19eb7defa 1023 /* 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 1024 free(result->name);
AzureIoTClient 28:add19eb7defa 1025 free(result);
AzureIoTClient 28:add19eb7defa 1026 result = NULL;
AzureIoTClient 28:add19eb7defa 1027 }
AzureIoTClient 28:add19eb7defa 1028 else
AzureIoTClient 28:add19eb7defa 1029 {
AzureIoTClient 28:add19eb7defa 1030 session_instance->link_endpoints = new_link_endpoints;
Azure.IoT Build 0:6ae2f7bca550 1031
AzureIoTClient 28:add19eb7defa 1032 if (session_instance->link_endpoint_count - selected_handle > 0)
AzureIoTClient 28:add19eb7defa 1033 {
AzureIoTClient 28:add19eb7defa 1034 (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 1035 }
Azure.IoT Build 0:6ae2f7bca550 1036
AzureIoTClient 28:add19eb7defa 1037 session_instance->link_endpoints[selected_handle] = result;
AzureIoTClient 28:add19eb7defa 1038 session_instance->link_endpoint_count++;
AzureIoTClient 28:add19eb7defa 1039 }
AzureIoTClient 28:add19eb7defa 1040 }
AzureIoTClient 28:add19eb7defa 1041 }
AzureIoTClient 28:add19eb7defa 1042 }
Azure.IoT Build 0:6ae2f7bca550 1043
AzureIoTClient 28:add19eb7defa 1044 return result;
Azure.IoT Build 0:6ae2f7bca550 1045 }
Azure.IoT Build 0:6ae2f7bca550 1046
Azure.IoT Build 0:6ae2f7bca550 1047 void session_destroy_link_endpoint(LINK_ENDPOINT_HANDLE link_endpoint)
Azure.IoT Build 0:6ae2f7bca550 1048 {
AzureIoTClient 28:add19eb7defa 1049 /* Codes_SRS_SESSION_01_050: [If link_endpoint is NULL, session_destroy_link_endpoint shall do nothing.] */
AzureIoTClient 28:add19eb7defa 1050 if (link_endpoint != NULL)
AzureIoTClient 28:add19eb7defa 1051 {
AzureIoTClient 28:add19eb7defa 1052 LINK_ENDPOINT_INSTANCE* endpoint_instance = (LINK_ENDPOINT_INSTANCE*)link_endpoint;
AzureIoTClient 28:add19eb7defa 1053 SESSION_INSTANCE* session_instance = endpoint_instance->session;
AzureIoTClient 28:add19eb7defa 1054 uint64_t i;
Azure.IoT Build 0:6ae2f7bca550 1055
AzureIoTClient 28:add19eb7defa 1056 /* Codes_SRS_SESSION_01_049: [session_destroy_link_endpoint shall free all resources associated with the endpoint.] */
AzureIoTClient 28:add19eb7defa 1057 for (i = 0; i < session_instance->link_endpoint_count; i++)
AzureIoTClient 28:add19eb7defa 1058 {
AzureIoTClient 28:add19eb7defa 1059 if (session_instance->link_endpoints[i] == link_endpoint)
AzureIoTClient 28:add19eb7defa 1060 {
AzureIoTClient 28:add19eb7defa 1061 break;
AzureIoTClient 28:add19eb7defa 1062 }
AzureIoTClient 28:add19eb7defa 1063 }
Azure.IoT Build 0:6ae2f7bca550 1064
AzureIoTClient 28:add19eb7defa 1065 if (i < session_instance->link_endpoint_count)
AzureIoTClient 28:add19eb7defa 1066 {
AzureIoTClient 28:add19eb7defa 1067 LINK_ENDPOINT_INSTANCE** new_endpoints;
Azure.IoT Build 0:6ae2f7bca550 1068
AzureIoTClient 28:add19eb7defa 1069 if (i < (session_instance->link_endpoint_count - 1))
AzureIoTClient 28:add19eb7defa 1070 {
AzureIoTClient 28:add19eb7defa 1071 (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 1072 }
AzureIoTClient 7:9e9ab3b0efef 1073
AzureIoTClient 28:add19eb7defa 1074 session_instance->link_endpoint_count--;
Azure.IoT Build 0:6ae2f7bca550 1075
AzureIoTClient 28:add19eb7defa 1076 if (session_instance->link_endpoint_count == 0)
AzureIoTClient 28:add19eb7defa 1077 {
AzureIoTClient 28:add19eb7defa 1078 free(session_instance->link_endpoints);
AzureIoTClient 28:add19eb7defa 1079 session_instance->link_endpoints = NULL;
AzureIoTClient 28:add19eb7defa 1080 }
AzureIoTClient 28:add19eb7defa 1081 else
AzureIoTClient 28:add19eb7defa 1082 {
AzureIoTClient 28:add19eb7defa 1083 new_endpoints = (LINK_ENDPOINT_INSTANCE**)realloc(session_instance->link_endpoints, sizeof(LINK_ENDPOINT_INSTANCE*) * session_instance->link_endpoint_count);
AzureIoTClient 28:add19eb7defa 1084 if (new_endpoints != NULL)
AzureIoTClient 28:add19eb7defa 1085 {
AzureIoTClient 28:add19eb7defa 1086 session_instance->link_endpoints = new_endpoints;
AzureIoTClient 28:add19eb7defa 1087 }
AzureIoTClient 28:add19eb7defa 1088 }
AzureIoTClient 28:add19eb7defa 1089 }
Azure.IoT Build 0:6ae2f7bca550 1090
AzureIoTClient 28:add19eb7defa 1091 if (endpoint_instance->name != NULL)
AzureIoTClient 28:add19eb7defa 1092 {
AzureIoTClient 28:add19eb7defa 1093 free(endpoint_instance->name);
AzureIoTClient 28:add19eb7defa 1094 }
Azure.IoT Build 0:6ae2f7bca550 1095
AzureIoTClient 28:add19eb7defa 1096 free(endpoint_instance);
AzureIoTClient 28:add19eb7defa 1097 }
Azure.IoT Build 0:6ae2f7bca550 1098 }
Azure.IoT Build 0:6ae2f7bca550 1099
Azure.IoT Build 0:6ae2f7bca550 1100 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 1101 {
AzureIoTClient 28:add19eb7defa 1102 int result;
Azure.IoT Build 0:6ae2f7bca550 1103
AzureIoTClient 28:add19eb7defa 1104 if ((link_endpoint == NULL) ||
AzureIoTClient 28:add19eb7defa 1105 (frame_received_callback == NULL))
AzureIoTClient 28:add19eb7defa 1106 {
AzureIoTClient 28:add19eb7defa 1107 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1108 }
AzureIoTClient 28:add19eb7defa 1109 else
AzureIoTClient 28:add19eb7defa 1110 {
AzureIoTClient 28:add19eb7defa 1111 link_endpoint->frame_received_callback = frame_received_callback;
AzureIoTClient 28:add19eb7defa 1112 link_endpoint->on_session_state_changed = on_session_state_changed;
AzureIoTClient 28:add19eb7defa 1113 link_endpoint->on_session_flow_on = on_session_flow_on;
AzureIoTClient 28:add19eb7defa 1114 link_endpoint->callback_context = context;
Azure.IoT Build 0:6ae2f7bca550 1115
AzureIoTClient 28:add19eb7defa 1116 if (link_endpoint->on_session_state_changed != NULL)
AzureIoTClient 28:add19eb7defa 1117 {
AzureIoTClient 28:add19eb7defa 1118 link_endpoint->on_session_state_changed(link_endpoint->callback_context, link_endpoint->session->session_state, link_endpoint->session->previous_session_state);
AzureIoTClient 28:add19eb7defa 1119 }
Azure.IoT Build 0:6ae2f7bca550 1120
AzureIoTClient 28:add19eb7defa 1121 result = 0;
AzureIoTClient 28:add19eb7defa 1122 }
Azure.IoT Build 0:6ae2f7bca550 1123
AzureIoTClient 28:add19eb7defa 1124 return result;
Azure.IoT Build 0:6ae2f7bca550 1125 }
Azure.IoT Build 0:6ae2f7bca550 1126
Azure.IoT Build 0:6ae2f7bca550 1127 static int encode_frame(LINK_ENDPOINT_HANDLE link_endpoint, const AMQP_VALUE performative, PAYLOAD* payloads, size_t payload_count)
Azure.IoT Build 0:6ae2f7bca550 1128 {
AzureIoTClient 28:add19eb7defa 1129 int result;
Azure.IoT Build 0:6ae2f7bca550 1130
AzureIoTClient 28:add19eb7defa 1131 if ((link_endpoint == NULL) ||
AzureIoTClient 28:add19eb7defa 1132 (performative == NULL))
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 LINK_ENDPOINT_INSTANCE* link_endpoint_instance = (LINK_ENDPOINT_INSTANCE*)link_endpoint;
AzureIoTClient 28:add19eb7defa 1139 SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)link_endpoint_instance->session;
Azure.IoT Build 0:6ae2f7bca550 1140
AzureIoTClient 28:add19eb7defa 1141 if (connection_encode_frame(session_instance->endpoint, performative, payloads, payload_count, NULL, NULL) != 0)
AzureIoTClient 28:add19eb7defa 1142 {
AzureIoTClient 28:add19eb7defa 1143 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1144 }
AzureIoTClient 28:add19eb7defa 1145 else
AzureIoTClient 28:add19eb7defa 1146 {
AzureIoTClient 28:add19eb7defa 1147 result = 0;
AzureIoTClient 28:add19eb7defa 1148 }
AzureIoTClient 28:add19eb7defa 1149 }
Azure.IoT Build 0:6ae2f7bca550 1150
AzureIoTClient 28:add19eb7defa 1151 return result;
Azure.IoT Build 0:6ae2f7bca550 1152 }
Azure.IoT Build 0:6ae2f7bca550 1153
Azure.IoT Build 0:6ae2f7bca550 1154 int session_send_flow(LINK_ENDPOINT_HANDLE link_endpoint, FLOW_HANDLE flow)
Azure.IoT Build 0:6ae2f7bca550 1155 {
AzureIoTClient 28:add19eb7defa 1156 int result;
Azure.IoT Build 0:6ae2f7bca550 1157
AzureIoTClient 28:add19eb7defa 1158 if ((link_endpoint == NULL) ||
AzureIoTClient 28:add19eb7defa 1159 (flow == NULL))
AzureIoTClient 28:add19eb7defa 1160 {
AzureIoTClient 28:add19eb7defa 1161 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1162 }
AzureIoTClient 28:add19eb7defa 1163 else
AzureIoTClient 28:add19eb7defa 1164 {
AzureIoTClient 28:add19eb7defa 1165 LINK_ENDPOINT_INSTANCE* link_endpoint_instance = (LINK_ENDPOINT_INSTANCE*)link_endpoint;
AzureIoTClient 28:add19eb7defa 1166 SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)link_endpoint_instance->session;
Azure.IoT Build 0:6ae2f7bca550 1167
AzureIoTClient 28:add19eb7defa 1168 result = 0;
Azure.IoT Build 0:6ae2f7bca550 1169
AzureIoTClient 28:add19eb7defa 1170 if (session_instance->session_state == SESSION_STATE_BEGIN_RCVD)
AzureIoTClient 28:add19eb7defa 1171 {
AzureIoTClient 28:add19eb7defa 1172 if (flow_set_next_incoming_id(flow, session_instance->next_incoming_id) != 0)
AzureIoTClient 28:add19eb7defa 1173 {
AzureIoTClient 28:add19eb7defa 1174 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1175 }
AzureIoTClient 28:add19eb7defa 1176 }
Azure.IoT Build 0:6ae2f7bca550 1177
AzureIoTClient 28:add19eb7defa 1178 if (result == 0)
AzureIoTClient 28:add19eb7defa 1179 {
AzureIoTClient 12:b30dacf113f2 1180 if ((flow_set_next_incoming_id(flow, session_instance->next_incoming_id) != 0) ||
AzureIoTClient 12:b30dacf113f2 1181 (flow_set_incoming_window(flow, session_instance->incoming_window) != 0) ||
AzureIoTClient 12:b30dacf113f2 1182 (flow_set_next_outgoing_id(flow, session_instance->next_outgoing_id) != 0) ||
AzureIoTClient 28:add19eb7defa 1183 (flow_set_outgoing_window(flow, session_instance->outgoing_window) != 0) ||
AzureIoTClient 28:add19eb7defa 1184 (flow_set_handle(flow, link_endpoint_instance->output_handle) != 0))
AzureIoTClient 28:add19eb7defa 1185 {
AzureIoTClient 28:add19eb7defa 1186 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1187 }
AzureIoTClient 28:add19eb7defa 1188 else
AzureIoTClient 28:add19eb7defa 1189 {
AzureIoTClient 28:add19eb7defa 1190 AMQP_VALUE flow_performative_value = amqpvalue_create_flow(flow);
AzureIoTClient 28:add19eb7defa 1191 if (flow_performative_value == NULL)
AzureIoTClient 28:add19eb7defa 1192 {
AzureIoTClient 28:add19eb7defa 1193 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1194 }
AzureIoTClient 28:add19eb7defa 1195 else
AzureIoTClient 28:add19eb7defa 1196 {
AzureIoTClient 28:add19eb7defa 1197 if (encode_frame(link_endpoint, flow_performative_value, NULL, 0) != 0)
AzureIoTClient 28:add19eb7defa 1198 {
AzureIoTClient 28:add19eb7defa 1199 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1200 }
AzureIoTClient 28:add19eb7defa 1201 else
AzureIoTClient 28:add19eb7defa 1202 {
AzureIoTClient 28:add19eb7defa 1203 result = 0;
AzureIoTClient 28:add19eb7defa 1204 }
Azure.IoT Build 0:6ae2f7bca550 1205
AzureIoTClient 28:add19eb7defa 1206 amqpvalue_destroy(flow_performative_value);
AzureIoTClient 28:add19eb7defa 1207 }
AzureIoTClient 28:add19eb7defa 1208 }
AzureIoTClient 28:add19eb7defa 1209 }
AzureIoTClient 28:add19eb7defa 1210 }
Azure.IoT Build 0:6ae2f7bca550 1211
AzureIoTClient 28:add19eb7defa 1212 return result;
Azure.IoT Build 0:6ae2f7bca550 1213 }
Azure.IoT Build 0:6ae2f7bca550 1214
Azure.IoT Build 0:6ae2f7bca550 1215 int session_send_attach(LINK_ENDPOINT_HANDLE link_endpoint, ATTACH_HANDLE attach)
Azure.IoT Build 0:6ae2f7bca550 1216 {
AzureIoTClient 28:add19eb7defa 1217 int result;
Azure.IoT Build 0:6ae2f7bca550 1218
AzureIoTClient 28:add19eb7defa 1219 if ((link_endpoint == NULL) ||
AzureIoTClient 28:add19eb7defa 1220 (attach == NULL))
AzureIoTClient 28:add19eb7defa 1221 {
AzureIoTClient 28:add19eb7defa 1222 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1223 }
AzureIoTClient 28:add19eb7defa 1224 else
AzureIoTClient 28:add19eb7defa 1225 {
AzureIoTClient 28:add19eb7defa 1226 LINK_ENDPOINT_INSTANCE* link_endpoint_instance = (LINK_ENDPOINT_INSTANCE*)link_endpoint;
Azure.IoT Build 0:6ae2f7bca550 1227
AzureIoTClient 28:add19eb7defa 1228 if (attach_set_handle(attach, link_endpoint_instance->output_handle) != 0)
AzureIoTClient 28:add19eb7defa 1229 {
AzureIoTClient 28:add19eb7defa 1230 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1231 }
AzureIoTClient 28:add19eb7defa 1232 else
AzureIoTClient 28:add19eb7defa 1233 {
AzureIoTClient 28:add19eb7defa 1234 AMQP_VALUE attach_performative_value = amqpvalue_create_attach(attach);
AzureIoTClient 28:add19eb7defa 1235 if (attach_performative_value == NULL)
AzureIoTClient 28:add19eb7defa 1236 {
AzureIoTClient 28:add19eb7defa 1237 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1238 }
AzureIoTClient 28:add19eb7defa 1239 else
AzureIoTClient 28:add19eb7defa 1240 {
AzureIoTClient 28:add19eb7defa 1241 if (encode_frame(link_endpoint, attach_performative_value, NULL, 0) != 0)
AzureIoTClient 28:add19eb7defa 1242 {
AzureIoTClient 28:add19eb7defa 1243 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1244 }
AzureIoTClient 28:add19eb7defa 1245 else
AzureIoTClient 28:add19eb7defa 1246 {
AzureIoTClient 28:add19eb7defa 1247 result = 0;
AzureIoTClient 28:add19eb7defa 1248 }
Azure.IoT Build 0:6ae2f7bca550 1249
AzureIoTClient 28:add19eb7defa 1250 amqpvalue_destroy(attach_performative_value);
AzureIoTClient 28:add19eb7defa 1251 }
AzureIoTClient 28:add19eb7defa 1252 }
AzureIoTClient 28:add19eb7defa 1253 }
Azure.IoT Build 0:6ae2f7bca550 1254
AzureIoTClient 28:add19eb7defa 1255 return result;
Azure.IoT Build 0:6ae2f7bca550 1256 }
Azure.IoT Build 0:6ae2f7bca550 1257
Azure.IoT Build 0:6ae2f7bca550 1258 int session_send_disposition(LINK_ENDPOINT_HANDLE link_endpoint, DISPOSITION_HANDLE disposition)
Azure.IoT Build 0:6ae2f7bca550 1259 {
AzureIoTClient 28:add19eb7defa 1260 int result;
Azure.IoT Build 0:6ae2f7bca550 1261
AzureIoTClient 28:add19eb7defa 1262 if ((link_endpoint == NULL) ||
AzureIoTClient 28:add19eb7defa 1263 (disposition == NULL))
AzureIoTClient 28:add19eb7defa 1264 {
AzureIoTClient 28:add19eb7defa 1265 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1266 }
AzureIoTClient 28:add19eb7defa 1267 else
AzureIoTClient 28:add19eb7defa 1268 {
AzureIoTClient 28:add19eb7defa 1269 AMQP_VALUE disposition_performative_value = amqpvalue_create_disposition(disposition);
AzureIoTClient 28:add19eb7defa 1270 if (disposition_performative_value == NULL)
AzureIoTClient 28:add19eb7defa 1271 {
AzureIoTClient 28:add19eb7defa 1272 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1273 }
AzureIoTClient 28:add19eb7defa 1274 else
AzureIoTClient 28:add19eb7defa 1275 {
AzureIoTClient 28:add19eb7defa 1276 if (encode_frame(link_endpoint, disposition_performative_value, NULL, 0) != 0)
AzureIoTClient 28:add19eb7defa 1277 {
AzureIoTClient 28:add19eb7defa 1278 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1279 }
AzureIoTClient 28:add19eb7defa 1280 else
AzureIoTClient 28:add19eb7defa 1281 {
AzureIoTClient 28:add19eb7defa 1282 result = 0;
AzureIoTClient 28:add19eb7defa 1283 }
Azure.IoT Build 0:6ae2f7bca550 1284
AzureIoTClient 28:add19eb7defa 1285 amqpvalue_destroy(disposition_performative_value);
AzureIoTClient 28:add19eb7defa 1286 }
AzureIoTClient 28:add19eb7defa 1287 }
Azure.IoT Build 0:6ae2f7bca550 1288
AzureIoTClient 28:add19eb7defa 1289 return result;
Azure.IoT Build 0:6ae2f7bca550 1290 }
Azure.IoT Build 0:6ae2f7bca550 1291
Azure.IoT Build 0:6ae2f7bca550 1292 int session_send_detach(LINK_ENDPOINT_HANDLE link_endpoint, DETACH_HANDLE detach)
Azure.IoT Build 0:6ae2f7bca550 1293 {
AzureIoTClient 28:add19eb7defa 1294 int result;
Azure.IoT Build 0:6ae2f7bca550 1295
AzureIoTClient 28:add19eb7defa 1296 if ((link_endpoint == NULL) ||
AzureIoTClient 28:add19eb7defa 1297 (detach == NULL))
AzureIoTClient 28:add19eb7defa 1298 {
AzureIoTClient 28:add19eb7defa 1299 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1300 }
AzureIoTClient 28:add19eb7defa 1301 else
AzureIoTClient 28:add19eb7defa 1302 {
AzureIoTClient 28:add19eb7defa 1303 LINK_ENDPOINT_INSTANCE* link_endpoint_instance = (LINK_ENDPOINT_INSTANCE*)link_endpoint;
Azure.IoT Build 0:6ae2f7bca550 1304
AzureIoTClient 28:add19eb7defa 1305 if (detach_set_handle(detach, link_endpoint_instance->output_handle) != 0)
AzureIoTClient 28:add19eb7defa 1306 {
AzureIoTClient 28:add19eb7defa 1307 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1308 }
AzureIoTClient 28:add19eb7defa 1309 else
AzureIoTClient 28:add19eb7defa 1310 {
AzureIoTClient 28:add19eb7defa 1311 AMQP_VALUE detach_performative_value = amqpvalue_create_detach(detach);
AzureIoTClient 28:add19eb7defa 1312 if (detach_performative_value == NULL)
AzureIoTClient 28:add19eb7defa 1313 {
AzureIoTClient 28:add19eb7defa 1314 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1315 }
AzureIoTClient 28:add19eb7defa 1316 else
AzureIoTClient 28:add19eb7defa 1317 {
AzureIoTClient 28:add19eb7defa 1318 if (encode_frame(link_endpoint, detach_performative_value, NULL, 0) != 0)
AzureIoTClient 28:add19eb7defa 1319 {
AzureIoTClient 28:add19eb7defa 1320 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 1321 }
AzureIoTClient 28:add19eb7defa 1322 else
AzureIoTClient 28:add19eb7defa 1323 {
AzureIoTClient 28:add19eb7defa 1324 result = 0;
AzureIoTClient 28:add19eb7defa 1325 }
Azure.IoT Build 0:6ae2f7bca550 1326
AzureIoTClient 28:add19eb7defa 1327 amqpvalue_destroy(detach_performative_value);
AzureIoTClient 28:add19eb7defa 1328 }
AzureIoTClient 28:add19eb7defa 1329 }
AzureIoTClient 28:add19eb7defa 1330 }
Azure.IoT Build 0:6ae2f7bca550 1331
AzureIoTClient 28:add19eb7defa 1332 return result;
Azure.IoT Build 0:6ae2f7bca550 1333 }
Azure.IoT Build 0:6ae2f7bca550 1334
Azure.IoT Build 0:6ae2f7bca550 1335 /* 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 1336 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 1337 {
AzureIoTClient 28:add19eb7defa 1338 SESSION_SEND_TRANSFER_RESULT result;
Azure.IoT Build 0:6ae2f7bca550 1339
AzureIoTClient 28:add19eb7defa 1340 /* 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 1341 if ((link_endpoint == NULL) ||
AzureIoTClient 28:add19eb7defa 1342 (transfer == NULL))
AzureIoTClient 28:add19eb7defa 1343 {
AzureIoTClient 28:add19eb7defa 1344 result = SESSION_SEND_TRANSFER_ERROR;
AzureIoTClient 28:add19eb7defa 1345 }
AzureIoTClient 28:add19eb7defa 1346 else
AzureIoTClient 28:add19eb7defa 1347 {
AzureIoTClient 28:add19eb7defa 1348 LINK_ENDPOINT_INSTANCE* link_endpoint_instance = (LINK_ENDPOINT_INSTANCE*)link_endpoint;
AzureIoTClient 28:add19eb7defa 1349 SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)link_endpoint_instance->session;
Azure.IoT Build 0:6ae2f7bca550 1350
AzureIoTClient 28:add19eb7defa 1351 /* 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 1352 if (session_instance->session_state != SESSION_STATE_MAPPED)
AzureIoTClient 28:add19eb7defa 1353 {
AzureIoTClient 28:add19eb7defa 1354 result = SESSION_SEND_TRANSFER_ERROR;
AzureIoTClient 28:add19eb7defa 1355 }
AzureIoTClient 28:add19eb7defa 1356 else
AzureIoTClient 28:add19eb7defa 1357 {
AzureIoTClient 28:add19eb7defa 1358 size_t payload_size = 0;
AzureIoTClient 28:add19eb7defa 1359 size_t i;
Azure.IoT Build 0:6ae2f7bca550 1360
AzureIoTClient 28:add19eb7defa 1361 for (i = 0; i < payload_count; i++)
AzureIoTClient 28:add19eb7defa 1362 {
AzureIoTClient 6:641a9672db08 1363 if ((payloads[i].length > UINT32_MAX) ||
AzureIoTClient 6:641a9672db08 1364 (payload_size + payloads[i].length < payload_size))
AzureIoTClient 6:641a9672db08 1365 {
AzureIoTClient 6:641a9672db08 1366 break;
AzureIoTClient 6:641a9672db08 1367 }
AzureIoTClient 6:641a9672db08 1368
AzureIoTClient 6:641a9672db08 1369 payload_size += payloads[i].length;
AzureIoTClient 6:641a9672db08 1370 }
Azure.IoT Build 0:6ae2f7bca550 1371
AzureIoTClient 6:641a9672db08 1372 if ((i < payload_count) ||
AzureIoTClient 6:641a9672db08 1373 (payload_size > UINT32_MAX))
AzureIoTClient 6:641a9672db08 1374 {
AzureIoTClient 6:641a9672db08 1375 result = SESSION_SEND_TRANSFER_ERROR;
AzureIoTClient 6:641a9672db08 1376 }
AzureIoTClient 6:641a9672db08 1377 else
AzureIoTClient 6:641a9672db08 1378 {
AzureIoTClient 6:641a9672db08 1379 if (session_instance->remote_incoming_window == 0)
AzureIoTClient 6:641a9672db08 1380 {
AzureIoTClient 6:641a9672db08 1381 result = SESSION_SEND_TRANSFER_BUSY;
AzureIoTClient 6:641a9672db08 1382 }
AzureIoTClient 6:641a9672db08 1383 else
AzureIoTClient 6:641a9672db08 1384 {
AzureIoTClient 6:641a9672db08 1385 /* 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 1386 /* Codes_SRS_SESSION_01_027: [sending a transfer Upon sending a transfer, the sending endpoint will increment its next-outgoing-id] */
AzureIoTClient 6:641a9672db08 1387 *delivery_id = session_instance->next_outgoing_id;
AzureIoTClient 6:641a9672db08 1388 if ((transfer_set_handle(transfer, link_endpoint_instance->output_handle) != 0) ||
AzureIoTClient 6:641a9672db08 1389 (transfer_set_delivery_id(transfer, *delivery_id) != 0) ||
AzureIoTClient 6:641a9672db08 1390 (transfer_set_more(transfer, false) != 0))
AzureIoTClient 6:641a9672db08 1391 {
AzureIoTClient 6:641a9672db08 1392 /* Codes_SRS_SESSION_01_058: [When any other error occurs, session_send_transfer shall fail and return a non-zero value.] */
AzureIoTClient 6:641a9672db08 1393 result = SESSION_SEND_TRANSFER_ERROR;
AzureIoTClient 6:641a9672db08 1394 }
AzureIoTClient 6:641a9672db08 1395 else
AzureIoTClient 6:641a9672db08 1396 {
AzureIoTClient 6:641a9672db08 1397 AMQP_VALUE transfer_value;
Azure.IoT Build 0:6ae2f7bca550 1398
AzureIoTClient 6:641a9672db08 1399 transfer_value = amqpvalue_create_transfer(transfer);
AzureIoTClient 6:641a9672db08 1400 if (transfer_value == NULL)
AzureIoTClient 6:641a9672db08 1401 {
AzureIoTClient 6:641a9672db08 1402 /* Codes_SRS_SESSION_01_058: [When any other error occurs, session_send_transfer shall fail and return a non-zero value.] */
AzureIoTClient 6:641a9672db08 1403 result = SESSION_SEND_TRANSFER_ERROR;
AzureIoTClient 6:641a9672db08 1404 }
AzureIoTClient 6:641a9672db08 1405 else
AzureIoTClient 6:641a9672db08 1406 {
AzureIoTClient 6:641a9672db08 1407 uint32_t available_frame_size;
AzureIoTClient 6:641a9672db08 1408 size_t encoded_size;
Azure.IoT Build 0:6ae2f7bca550 1409
AzureIoTClient 6:641a9672db08 1410 if ((connection_get_remote_max_frame_size(session_instance->connection, &available_frame_size) != 0) ||
AzureIoTClient 6:641a9672db08 1411 (amqpvalue_get_encoded_size(transfer_value, &encoded_size) != 0))
AzureIoTClient 6:641a9672db08 1412 {
AzureIoTClient 6:641a9672db08 1413 result = SESSION_SEND_TRANSFER_ERROR;
AzureIoTClient 6:641a9672db08 1414 }
AzureIoTClient 6:641a9672db08 1415 else
AzureIoTClient 6:641a9672db08 1416 {
AzureIoTClient 6:641a9672db08 1417 payload_size = 0;
Azure.IoT Build 0:6ae2f7bca550 1418
AzureIoTClient 6:641a9672db08 1419 for (i = 0; i < payload_count; i++)
AzureIoTClient 6:641a9672db08 1420 {
AzureIoTClient 6:641a9672db08 1421 payload_size += payloads[i].length;
AzureIoTClient 6:641a9672db08 1422 }
AzureIoTClient 6:641a9672db08 1423
AzureIoTClient 6:641a9672db08 1424 available_frame_size -= (uint32_t)encoded_size;
AzureIoTClient 6:641a9672db08 1425 available_frame_size -= 8;
Azure.IoT Build 0:6ae2f7bca550 1426
AzureIoTClient 6:641a9672db08 1427 if (available_frame_size >= payload_size)
AzureIoTClient 6:641a9672db08 1428 {
AzureIoTClient 6:641a9672db08 1429 /* 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 1430 if (connection_encode_frame(session_instance->endpoint, transfer_value, payloads, payload_count, on_send_complete, callback_context) != 0)
AzureIoTClient 6:641a9672db08 1431 {
AzureIoTClient 6:641a9672db08 1432 /* 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 1433 result = SESSION_SEND_TRANSFER_ERROR;
AzureIoTClient 6:641a9672db08 1434 }
AzureIoTClient 6:641a9672db08 1435 else
AzureIoTClient 6:641a9672db08 1436 {
AzureIoTClient 6:641a9672db08 1437 /* Codes_SRS_SESSION_01_018: [is incremented after each successive transfer according to RFC-1982 [RFC1982] serial number arithmetic.] */
AzureIoTClient 6:641a9672db08 1438 session_instance->next_outgoing_id++;
AzureIoTClient 6:641a9672db08 1439 session_instance->remote_incoming_window--;
AzureIoTClient 6:641a9672db08 1440 session_instance->outgoing_window--;
Azure.IoT Build 0:6ae2f7bca550 1441
AzureIoTClient 6:641a9672db08 1442 /* Codes_SRS_SESSION_01_053: [On success, session_send_transfer shall return 0.] */
AzureIoTClient 6:641a9672db08 1443 result = SESSION_SEND_TRANSFER_OK;
AzureIoTClient 6:641a9672db08 1444 }
AzureIoTClient 6:641a9672db08 1445 }
AzureIoTClient 6:641a9672db08 1446 else
AzureIoTClient 6:641a9672db08 1447 {
AzureIoTClient 6:641a9672db08 1448 size_t current_payload_index = 0;
AzureIoTClient 6:641a9672db08 1449 uint32_t current_payload_pos = 0;
Azure.IoT Build 0:6ae2f7bca550 1450
AzureIoTClient 6:641a9672db08 1451 /* break it down into different deliveries */
AzureIoTClient 6:641a9672db08 1452 while (payload_size > 0)
AzureIoTClient 6:641a9672db08 1453 {
AzureIoTClient 6:641a9672db08 1454 uint32_t transfer_frame_payload_count = 0;
AzureIoTClient 6:641a9672db08 1455 uint32_t current_transfer_frame_payload_size = (uint32_t)payload_size;
AzureIoTClient 6:641a9672db08 1456 uint32_t byte_counter;
AzureIoTClient 6:641a9672db08 1457 size_t temp_current_payload_index = current_payload_index;
AzureIoTClient 6:641a9672db08 1458 uint32_t temp_current_payload_pos = current_payload_pos;
AzureIoTClient 6:641a9672db08 1459 AMQP_VALUE multi_transfer_amqp_value;
AzureIoTClient 28:add19eb7defa 1460 PAYLOAD* transfer_frame_payloads;
AzureIoTClient 6:641a9672db08 1461 bool more;
AzureIoTClient 6:641a9672db08 1462
AzureIoTClient 6:641a9672db08 1463 if (current_transfer_frame_payload_size > available_frame_size)
AzureIoTClient 6:641a9672db08 1464 {
AzureIoTClient 6:641a9672db08 1465 current_transfer_frame_payload_size = available_frame_size;
AzureIoTClient 6:641a9672db08 1466 }
Azure.IoT Build 0:6ae2f7bca550 1467
AzureIoTClient 6:641a9672db08 1468 if (available_frame_size >= payload_size)
AzureIoTClient 6:641a9672db08 1469 {
AzureIoTClient 6:641a9672db08 1470 more = false;
AzureIoTClient 6:641a9672db08 1471 }
AzureIoTClient 6:641a9672db08 1472 else
AzureIoTClient 6:641a9672db08 1473 {
AzureIoTClient 6:641a9672db08 1474 more = true;
AzureIoTClient 6:641a9672db08 1475 }
Azure.IoT Build 0:6ae2f7bca550 1476
AzureIoTClient 6:641a9672db08 1477 if (transfer_set_more(transfer, more) != 0)
AzureIoTClient 6:641a9672db08 1478 {
AzureIoTClient 6:641a9672db08 1479 break;
AzureIoTClient 6:641a9672db08 1480 }
Azure.IoT Build 0:6ae2f7bca550 1481
AzureIoTClient 6:641a9672db08 1482 multi_transfer_amqp_value = amqpvalue_create_transfer(transfer);
AzureIoTClient 6:641a9672db08 1483 if (multi_transfer_amqp_value == NULL)
AzureIoTClient 6:641a9672db08 1484 {
AzureIoTClient 6:641a9672db08 1485 break;
AzureIoTClient 6:641a9672db08 1486 }
Azure.IoT Build 0:6ae2f7bca550 1487
AzureIoTClient 6:641a9672db08 1488 byte_counter = current_transfer_frame_payload_size;
AzureIoTClient 6:641a9672db08 1489 while (byte_counter > 0)
AzureIoTClient 6:641a9672db08 1490 {
AzureIoTClient 6:641a9672db08 1491 if (payloads[temp_current_payload_index].length - temp_current_payload_pos >= byte_counter)
AzureIoTClient 6:641a9672db08 1492 {
AzureIoTClient 6:641a9672db08 1493 /* more data than we need */
AzureIoTClient 6:641a9672db08 1494 temp_current_payload_pos += byte_counter;
AzureIoTClient 6:641a9672db08 1495 byte_counter = 0;
AzureIoTClient 6:641a9672db08 1496 }
AzureIoTClient 6:641a9672db08 1497 else
AzureIoTClient 6:641a9672db08 1498 {
AzureIoTClient 6:641a9672db08 1499 byte_counter -= (uint32_t)payloads[temp_current_payload_index].length - temp_current_payload_pos;
AzureIoTClient 6:641a9672db08 1500 temp_current_payload_index++;
AzureIoTClient 6:641a9672db08 1501 temp_current_payload_pos = 0;
AzureIoTClient 6:641a9672db08 1502 }
AzureIoTClient 6:641a9672db08 1503 }
Azure.IoT Build 0:6ae2f7bca550 1504
AzureIoTClient 6:641a9672db08 1505 transfer_frame_payload_count = (uint32_t)(temp_current_payload_index - current_payload_index + 1);
AzureIoTClient 25:1101516ee67d 1506 transfer_frame_payloads = (PAYLOAD*)malloc(transfer_frame_payload_count * sizeof(PAYLOAD));
AzureIoTClient 6:641a9672db08 1507 if (transfer_frame_payloads == NULL)
AzureIoTClient 6:641a9672db08 1508 {
AzureIoTClient 6:641a9672db08 1509 amqpvalue_destroy(multi_transfer_amqp_value);
AzureIoTClient 6:641a9672db08 1510 break;
AzureIoTClient 6:641a9672db08 1511 }
Azure.IoT Build 0:6ae2f7bca550 1512
AzureIoTClient 6:641a9672db08 1513 /* copy data */
AzureIoTClient 6:641a9672db08 1514 byte_counter = current_transfer_frame_payload_size;
AzureIoTClient 6:641a9672db08 1515 transfer_frame_payload_count = 0;
Azure.IoT Build 0:6ae2f7bca550 1516
AzureIoTClient 6:641a9672db08 1517 while (byte_counter > 0)
AzureIoTClient 6:641a9672db08 1518 {
AzureIoTClient 6:641a9672db08 1519 if (payloads[current_payload_index].length - current_payload_pos > byte_counter)
AzureIoTClient 6:641a9672db08 1520 {
AzureIoTClient 6:641a9672db08 1521 /* more data than we need */
AzureIoTClient 6:641a9672db08 1522 transfer_frame_payloads[transfer_frame_payload_count].bytes = payloads[current_payload_index].bytes + current_payload_pos;
AzureIoTClient 6:641a9672db08 1523 transfer_frame_payloads[transfer_frame_payload_count].length = byte_counter;
AzureIoTClient 6:641a9672db08 1524 current_payload_pos += byte_counter;
AzureIoTClient 6:641a9672db08 1525 byte_counter = 0;
AzureIoTClient 6:641a9672db08 1526 }
AzureIoTClient 6:641a9672db08 1527 else
AzureIoTClient 6:641a9672db08 1528 {
AzureIoTClient 6:641a9672db08 1529 /* copy entire payload and move to the next */
AzureIoTClient 6:641a9672db08 1530 transfer_frame_payloads[transfer_frame_payload_count].bytes = payloads[current_payload_index].bytes + current_payload_pos;
AzureIoTClient 6:641a9672db08 1531 transfer_frame_payloads[transfer_frame_payload_count].length = payloads[current_payload_index].length - current_payload_pos;
AzureIoTClient 6:641a9672db08 1532 byte_counter -= (uint32_t)payloads[current_payload_index].length - current_payload_pos;
AzureIoTClient 6:641a9672db08 1533 current_payload_index++;
AzureIoTClient 6:641a9672db08 1534 current_payload_pos = 0;
AzureIoTClient 6:641a9672db08 1535 }
Azure.IoT Build 0:6ae2f7bca550 1536
AzureIoTClient 6:641a9672db08 1537 transfer_frame_payload_count++;
AzureIoTClient 6:641a9672db08 1538 }
Azure.IoT Build 0:6ae2f7bca550 1539
AzureIoTClient 6:641a9672db08 1540 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 1541 {
AzureIoTClient 21:f9c433d8e6ca 1542 free(transfer_frame_payloads);
AzureIoTClient 6:641a9672db08 1543 amqpvalue_destroy(multi_transfer_amqp_value);
AzureIoTClient 6:641a9672db08 1544 break;
AzureIoTClient 6:641a9672db08 1545 }
Azure.IoT Build 0:6ae2f7bca550 1546
AzureIoTClient 21:f9c433d8e6ca 1547 free(transfer_frame_payloads);
AzureIoTClient 6:641a9672db08 1548 amqpvalue_destroy(multi_transfer_amqp_value);
AzureIoTClient 6:641a9672db08 1549 payload_size -= current_transfer_frame_payload_size;
AzureIoTClient 6:641a9672db08 1550 }
Azure.IoT Build 0:6ae2f7bca550 1551
AzureIoTClient 6:641a9672db08 1552 if (payload_size > 0)
AzureIoTClient 6:641a9672db08 1553 {
AzureIoTClient 6:641a9672db08 1554 result = SESSION_SEND_TRANSFER_ERROR;
AzureIoTClient 6:641a9672db08 1555 }
AzureIoTClient 6:641a9672db08 1556 else
AzureIoTClient 6:641a9672db08 1557 {
AzureIoTClient 6:641a9672db08 1558 /* Codes_SRS_SESSION_01_018: [is incremented after each successive transfer according to RFC-1982 [RFC1982] serial number arithmetic.] */
AzureIoTClient 6:641a9672db08 1559 session_instance->next_outgoing_id++;
AzureIoTClient 6:641a9672db08 1560 session_instance->remote_incoming_window--;
AzureIoTClient 6:641a9672db08 1561 session_instance->outgoing_window--;
Azure.IoT Build 0:6ae2f7bca550 1562
AzureIoTClient 6:641a9672db08 1563 result = SESSION_SEND_TRANSFER_OK;
AzureIoTClient 6:641a9672db08 1564 }
AzureIoTClient 6:641a9672db08 1565 }
AzureIoTClient 6:641a9672db08 1566 }
Azure.IoT Build 0:6ae2f7bca550 1567
AzureIoTClient 6:641a9672db08 1568 amqpvalue_destroy(transfer_value);
AzureIoTClient 6:641a9672db08 1569 }
AzureIoTClient 6:641a9672db08 1570 }
AzureIoTClient 6:641a9672db08 1571 }
AzureIoTClient 6:641a9672db08 1572 }
AzureIoTClient 28:add19eb7defa 1573 }
AzureIoTClient 28:add19eb7defa 1574 }
Azure.IoT Build 0:6ae2f7bca550 1575
AzureIoTClient 28:add19eb7defa 1576 return result;
Azure.IoT Build 0:6ae2f7bca550 1577 }