A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Fri Jul 29 15:58:39 2016 -0700
Revision:
6:641a9672db08
Parent:
5:ae49385aff34
Child:
7:9e9ab3b0efef
1.0.10

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