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
AzureIoTClient 6:641a9672db08 8 #include <stdbool.h>
Azure.IoT Build 0:6ae2f7bca550 9 #include <string.h>
Azure.IoT Build 5:ae49385aff34 10 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:6ae2f7bca550 11 #include "azure_uamqp_c/message_sender.h"
Azure.IoT Build 0:6ae2f7bca550 12 #include "azure_uamqp_c/amqpalloc.h"
Azure.IoT Build 0:6ae2f7bca550 13 #include "azure_uamqp_c/amqpvalue_to_string.h"
Azure.IoT Build 0:6ae2f7bca550 14
Azure.IoT Build 0:6ae2f7bca550 15 typedef enum MESSAGE_SEND_STATE_TAG
Azure.IoT Build 0:6ae2f7bca550 16 {
AzureIoTClient 6:641a9672db08 17 MESSAGE_SEND_STATE_NOT_SENT,
AzureIoTClient 6:641a9672db08 18 MESSAGE_SEND_STATE_PENDING
Azure.IoT Build 0:6ae2f7bca550 19 } MESSAGE_SEND_STATE;
Azure.IoT Build 0:6ae2f7bca550 20
Azure.IoT Build 0:6ae2f7bca550 21 typedef enum SEND_ONE_MESSAGE_RESULT_TAG
Azure.IoT Build 0:6ae2f7bca550 22 {
AzureIoTClient 6:641a9672db08 23 SEND_ONE_MESSAGE_OK,
AzureIoTClient 6:641a9672db08 24 SEND_ONE_MESSAGE_ERROR,
AzureIoTClient 6:641a9672db08 25 SEND_ONE_MESSAGE_BUSY
Azure.IoT Build 0:6ae2f7bca550 26 } SEND_ONE_MESSAGE_RESULT;
Azure.IoT Build 0:6ae2f7bca550 27
Azure.IoT Build 0:6ae2f7bca550 28 typedef struct MESSAGE_WITH_CALLBACK_TAG
Azure.IoT Build 0:6ae2f7bca550 29 {
AzureIoTClient 6:641a9672db08 30 MESSAGE_HANDLE message;
AzureIoTClient 6:641a9672db08 31 ON_MESSAGE_SEND_COMPLETE on_message_send_complete;
AzureIoTClient 6:641a9672db08 32 void* context;
AzureIoTClient 6:641a9672db08 33 MESSAGE_SENDER_HANDLE message_sender;
AzureIoTClient 6:641a9672db08 34 MESSAGE_SEND_STATE message_send_state;
Azure.IoT Build 0:6ae2f7bca550 35 } MESSAGE_WITH_CALLBACK;
Azure.IoT Build 0:6ae2f7bca550 36
Azure.IoT Build 0:6ae2f7bca550 37 typedef struct MESSAGE_SENDER_INSTANCE_TAG
Azure.IoT Build 0:6ae2f7bca550 38 {
AzureIoTClient 6:641a9672db08 39 LINK_HANDLE link;
AzureIoTClient 6:641a9672db08 40 size_t message_count;
AzureIoTClient 6:641a9672db08 41 MESSAGE_WITH_CALLBACK** messages;
AzureIoTClient 6:641a9672db08 42 MESSAGE_SENDER_STATE message_sender_state;
AzureIoTClient 6:641a9672db08 43 ON_MESSAGE_SENDER_STATE_CHANGED on_message_sender_state_changed;
AzureIoTClient 6:641a9672db08 44 void* on_message_sender_state_changed_context;
AzureIoTClient 6:641a9672db08 45 unsigned int is_trace_on : 1;
Azure.IoT Build 0:6ae2f7bca550 46 } MESSAGE_SENDER_INSTANCE;
Azure.IoT Build 0:6ae2f7bca550 47
Azure.IoT Build 0:6ae2f7bca550 48 static void remove_pending_message_by_index(MESSAGE_SENDER_INSTANCE* message_sender_instance, size_t index)
Azure.IoT Build 0:6ae2f7bca550 49 {
AzureIoTClient 6:641a9672db08 50 MESSAGE_WITH_CALLBACK** new_messages;
Azure.IoT Build 0:6ae2f7bca550 51
AzureIoTClient 6:641a9672db08 52 if (message_sender_instance->messages[index]->message != NULL)
AzureIoTClient 6:641a9672db08 53 {
AzureIoTClient 6:641a9672db08 54 message_destroy(message_sender_instance->messages[index]->message);
AzureIoTClient 6:641a9672db08 55 message_sender_instance->messages[index]->message = NULL;
AzureIoTClient 6:641a9672db08 56 }
Azure.IoT Build 0:6ae2f7bca550 57
AzureIoTClient 6:641a9672db08 58 amqpalloc_free(message_sender_instance->messages[index]);
Azure.IoT Build 0:6ae2f7bca550 59
AzureIoTClient 6:641a9672db08 60 if (message_sender_instance->message_count - index > 1)
AzureIoTClient 6:641a9672db08 61 {
AzureIoTClient 6:641a9672db08 62 (void)memmove(&message_sender_instance->messages[index], &message_sender_instance->messages[index + 1], sizeof(MESSAGE_WITH_CALLBACK*) * (message_sender_instance->message_count - index - 1));
AzureIoTClient 6:641a9672db08 63 }
Azure.IoT Build 0:6ae2f7bca550 64
AzureIoTClient 6:641a9672db08 65 message_sender_instance->message_count--;
Azure.IoT Build 0:6ae2f7bca550 66
AzureIoTClient 6:641a9672db08 67 if (message_sender_instance->message_count > 0)
AzureIoTClient 6:641a9672db08 68 {
AzureIoTClient 6:641a9672db08 69 new_messages = (MESSAGE_WITH_CALLBACK**)amqpalloc_realloc(message_sender_instance->messages, sizeof(MESSAGE_WITH_CALLBACK*) * (message_sender_instance->message_count));
AzureIoTClient 6:641a9672db08 70 if (new_messages != NULL)
AzureIoTClient 6:641a9672db08 71 {
AzureIoTClient 6:641a9672db08 72 message_sender_instance->messages = new_messages;
AzureIoTClient 6:641a9672db08 73 }
AzureIoTClient 6:641a9672db08 74 }
AzureIoTClient 6:641a9672db08 75 else
AzureIoTClient 6:641a9672db08 76 {
AzureIoTClient 6:641a9672db08 77 amqpalloc_free(message_sender_instance->messages);
AzureIoTClient 6:641a9672db08 78 message_sender_instance->messages = NULL;
AzureIoTClient 6:641a9672db08 79 }
Azure.IoT Build 0:6ae2f7bca550 80 }
Azure.IoT Build 0:6ae2f7bca550 81
Azure.IoT Build 0:6ae2f7bca550 82 static void remove_pending_message(MESSAGE_SENDER_INSTANCE* message_sender_instance, MESSAGE_WITH_CALLBACK* message_with_callback)
Azure.IoT Build 0:6ae2f7bca550 83 {
AzureIoTClient 6:641a9672db08 84 size_t i;
Azure.IoT Build 0:6ae2f7bca550 85
AzureIoTClient 6:641a9672db08 86 for (i = 0; i < message_sender_instance->message_count; i++)
AzureIoTClient 6:641a9672db08 87 {
AzureIoTClient 6:641a9672db08 88 if (message_sender_instance->messages[i] == message_with_callback)
AzureIoTClient 6:641a9672db08 89 {
AzureIoTClient 6:641a9672db08 90 remove_pending_message_by_index(message_sender_instance, i);
AzureIoTClient 6:641a9672db08 91 break;
AzureIoTClient 6:641a9672db08 92 }
AzureIoTClient 6:641a9672db08 93 }
Azure.IoT Build 0:6ae2f7bca550 94 }
Azure.IoT Build 0:6ae2f7bca550 95
Azure.IoT Build 0:6ae2f7bca550 96 static void on_delivery_settled(void* context, delivery_number delivery_no)
Azure.IoT Build 0:6ae2f7bca550 97 {
AzureIoTClient 6:641a9672db08 98 MESSAGE_WITH_CALLBACK* message_with_callback = (MESSAGE_WITH_CALLBACK*)context;
AzureIoTClient 6:641a9672db08 99 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_with_callback->message_sender;
AzureIoTClient 6:641a9672db08 100 (void)delivery_no;
Azure.IoT Build 0:6ae2f7bca550 101
AzureIoTClient 6:641a9672db08 102 if (message_with_callback->on_message_send_complete != NULL)
AzureIoTClient 6:641a9672db08 103 {
AzureIoTClient 6:641a9672db08 104 message_with_callback->on_message_send_complete(message_with_callback->context, MESSAGE_SEND_OK);
AzureIoTClient 6:641a9672db08 105 }
Azure.IoT Build 0:6ae2f7bca550 106
AzureIoTClient 6:641a9672db08 107 remove_pending_message(message_sender_instance, message_with_callback);
Azure.IoT Build 0:6ae2f7bca550 108 }
Azure.IoT Build 0:6ae2f7bca550 109
Azure.IoT Build 0:6ae2f7bca550 110 static int encode_bytes(void* context, const unsigned char* bytes, size_t length)
Azure.IoT Build 0:6ae2f7bca550 111 {
AzureIoTClient 6:641a9672db08 112 PAYLOAD* payload = (PAYLOAD*)context;
AzureIoTClient 6:641a9672db08 113 (void)memcpy((unsigned char*)payload->bytes + payload->length, bytes, length);
AzureIoTClient 6:641a9672db08 114 payload->length += length;
AzureIoTClient 6:641a9672db08 115 return 0;
Azure.IoT Build 0:6ae2f7bca550 116 }
Azure.IoT Build 0:6ae2f7bca550 117
Azure.IoT Build 0:6ae2f7bca550 118 static void log_message_chunk(MESSAGE_SENDER_INSTANCE* message_sender_instance, const char* name, AMQP_VALUE value)
Azure.IoT Build 0:6ae2f7bca550 119 {
AzureIoTClient 6:641a9672db08 120 if (xlogging_get_log_function() != NULL && message_sender_instance->is_trace_on == 1)
AzureIoTClient 6:641a9672db08 121 {
AzureIoTClient 6:641a9672db08 122 char* value_as_string = NULL;
AzureIoTClient 6:641a9672db08 123 LOG(LOG_TRACE, 0, "%s", name);
AzureIoTClient 6:641a9672db08 124 LOG(LOG_TRACE, 0, "%s", (value_as_string = amqpvalue_to_string(value)));
AzureIoTClient 6:641a9672db08 125 if (value_as_string != NULL)
AzureIoTClient 6:641a9672db08 126 {
AzureIoTClient 6:641a9672db08 127 amqpalloc_free(value_as_string);
AzureIoTClient 6:641a9672db08 128 }
AzureIoTClient 6:641a9672db08 129 }
Azure.IoT Build 0:6ae2f7bca550 130 }
Azure.IoT Build 0:6ae2f7bca550 131
Azure.IoT Build 0:6ae2f7bca550 132 static SEND_ONE_MESSAGE_RESULT send_one_message(MESSAGE_SENDER_INSTANCE* message_sender_instance, MESSAGE_WITH_CALLBACK* message_with_callback, MESSAGE_HANDLE message)
Azure.IoT Build 0:6ae2f7bca550 133 {
AzureIoTClient 6:641a9672db08 134 SEND_ONE_MESSAGE_RESULT result;
Azure.IoT Build 0:6ae2f7bca550 135
AzureIoTClient 6:641a9672db08 136 size_t encoded_size;
AzureIoTClient 6:641a9672db08 137 size_t total_encoded_size = 0;
AzureIoTClient 6:641a9672db08 138 MESSAGE_BODY_TYPE message_body_type;
Azure.IoT Build 0:6ae2f7bca550 139 message_format message_format;
Azure.IoT Build 0:6ae2f7bca550 140
AzureIoTClient 6:641a9672db08 141 if ((message_get_body_type(message, &message_body_type) != 0) ||
Azure.IoT Build 0:6ae2f7bca550 142 (message_get_message_format(message, &message_format) != 0))
AzureIoTClient 6:641a9672db08 143 {
AzureIoTClient 6:641a9672db08 144 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 145 }
AzureIoTClient 6:641a9672db08 146 else
AzureIoTClient 6:641a9672db08 147 {
AzureIoTClient 6:641a9672db08 148 // header
AzureIoTClient 6:641a9672db08 149 HEADER_HANDLE header;
AzureIoTClient 6:641a9672db08 150 AMQP_VALUE header_amqp_value;
AzureIoTClient 6:641a9672db08 151 PROPERTIES_HANDLE properties;
AzureIoTClient 6:641a9672db08 152 AMQP_VALUE properties_amqp_value;
AzureIoTClient 6:641a9672db08 153 AMQP_VALUE application_properties;
AzureIoTClient 6:641a9672db08 154 AMQP_VALUE application_properties_value;
AzureIoTClient 6:641a9672db08 155 AMQP_VALUE body_amqp_value = NULL;
AzureIoTClient 6:641a9672db08 156 size_t body_data_count = 0;
Azure.IoT Build 0:6ae2f7bca550 157
AzureIoTClient 6:641a9672db08 158 message_get_header(message, &header);
AzureIoTClient 6:641a9672db08 159 header_amqp_value = amqpvalue_create_header(header);
AzureIoTClient 6:641a9672db08 160 if (header != NULL)
AzureIoTClient 6:641a9672db08 161 {
AzureIoTClient 6:641a9672db08 162 amqpvalue_get_encoded_size(header_amqp_value, &encoded_size);
AzureIoTClient 6:641a9672db08 163 total_encoded_size += encoded_size;
AzureIoTClient 6:641a9672db08 164 }
Azure.IoT Build 0:6ae2f7bca550 165
AzureIoTClient 6:641a9672db08 166 // properties
AzureIoTClient 6:641a9672db08 167 message_get_properties(message, &properties);
AzureIoTClient 6:641a9672db08 168 properties_amqp_value = amqpvalue_create_properties(properties);
AzureIoTClient 6:641a9672db08 169 if (properties != NULL)
AzureIoTClient 6:641a9672db08 170 {
AzureIoTClient 6:641a9672db08 171 amqpvalue_get_encoded_size(properties_amqp_value, &encoded_size);
AzureIoTClient 6:641a9672db08 172 total_encoded_size += encoded_size;
AzureIoTClient 6:641a9672db08 173 }
Azure.IoT Build 0:6ae2f7bca550 174
AzureIoTClient 6:641a9672db08 175 // application properties
AzureIoTClient 6:641a9672db08 176 message_get_application_properties(message, &application_properties);
AzureIoTClient 6:641a9672db08 177 application_properties_value = amqpvalue_create_application_properties(application_properties);
AzureIoTClient 6:641a9672db08 178 if (application_properties != NULL)
AzureIoTClient 6:641a9672db08 179 {
AzureIoTClient 6:641a9672db08 180 amqpvalue_get_encoded_size(application_properties_value, &encoded_size);
AzureIoTClient 6:641a9672db08 181 total_encoded_size += encoded_size;
AzureIoTClient 6:641a9672db08 182 }
Azure.IoT Build 0:6ae2f7bca550 183
AzureIoTClient 6:641a9672db08 184 result = SEND_ONE_MESSAGE_OK;
Azure.IoT Build 0:6ae2f7bca550 185
AzureIoTClient 6:641a9672db08 186 // body - amqp data
AzureIoTClient 6:641a9672db08 187 switch (message_body_type)
AzureIoTClient 6:641a9672db08 188 {
AzureIoTClient 6:641a9672db08 189 default:
AzureIoTClient 6:641a9672db08 190 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 191 break;
Azure.IoT Build 0:6ae2f7bca550 192
AzureIoTClient 6:641a9672db08 193 case MESSAGE_BODY_TYPE_VALUE:
AzureIoTClient 6:641a9672db08 194 {
AzureIoTClient 6:641a9672db08 195 AMQP_VALUE message_body_amqp_value;
AzureIoTClient 6:641a9672db08 196 if (message_get_inplace_body_amqp_value(message, &message_body_amqp_value) != 0)
AzureIoTClient 6:641a9672db08 197 {
AzureIoTClient 6:641a9672db08 198 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 199 }
AzureIoTClient 6:641a9672db08 200 else
AzureIoTClient 6:641a9672db08 201 {
AzureIoTClient 6:641a9672db08 202 body_amqp_value = amqpvalue_create_amqp_value(message_body_amqp_value);
AzureIoTClient 6:641a9672db08 203 if ((body_amqp_value == NULL) ||
AzureIoTClient 6:641a9672db08 204 (amqpvalue_get_encoded_size(body_amqp_value, &encoded_size) != 0))
AzureIoTClient 6:641a9672db08 205 {
AzureIoTClient 6:641a9672db08 206 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 207 }
AzureIoTClient 6:641a9672db08 208 else
AzureIoTClient 6:641a9672db08 209 {
AzureIoTClient 6:641a9672db08 210 total_encoded_size += encoded_size;
AzureIoTClient 6:641a9672db08 211 }
AzureIoTClient 6:641a9672db08 212 }
Azure.IoT Build 0:6ae2f7bca550 213
AzureIoTClient 6:641a9672db08 214 break;
AzureIoTClient 6:641a9672db08 215 }
Azure.IoT Build 0:6ae2f7bca550 216
AzureIoTClient 6:641a9672db08 217 case MESSAGE_BODY_TYPE_DATA:
AzureIoTClient 6:641a9672db08 218 {
AzureIoTClient 6:641a9672db08 219 BINARY_DATA binary_data;
Azure.IoT Build 0:6ae2f7bca550 220 size_t i;
Azure.IoT Build 0:6ae2f7bca550 221
Azure.IoT Build 0:6ae2f7bca550 222 if (message_get_body_amqp_data_count(message, &body_data_count) != 0)
Azure.IoT Build 0:6ae2f7bca550 223 {
Azure.IoT Build 0:6ae2f7bca550 224 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 225 }
Azure.IoT Build 0:6ae2f7bca550 226 else
Azure.IoT Build 0:6ae2f7bca550 227 {
Azure.IoT Build 0:6ae2f7bca550 228 for (i = 0; i < body_data_count; i++)
Azure.IoT Build 0:6ae2f7bca550 229 {
Azure.IoT Build 0:6ae2f7bca550 230 if (message_get_body_amqp_data(message, i, &binary_data) != 0)
Azure.IoT Build 0:6ae2f7bca550 231 {
Azure.IoT Build 0:6ae2f7bca550 232 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 233 }
Azure.IoT Build 0:6ae2f7bca550 234 else
Azure.IoT Build 0:6ae2f7bca550 235 {
AzureIoTClient 6:641a9672db08 236 amqp_binary binary_value;
AzureIoTClient 6:641a9672db08 237 binary_value.bytes = binary_data.bytes;
AzureIoTClient 6:641a9672db08 238 binary_value.length = (uint32_t)binary_data.length;
Azure.IoT Build 0:6ae2f7bca550 239 AMQP_VALUE body_amqp_data = amqpvalue_create_data(binary_value);
Azure.IoT Build 0:6ae2f7bca550 240 if (body_amqp_data == NULL)
Azure.IoT Build 0:6ae2f7bca550 241 {
Azure.IoT Build 0:6ae2f7bca550 242 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 243 }
Azure.IoT Build 0:6ae2f7bca550 244 else
Azure.IoT Build 0:6ae2f7bca550 245 {
Azure.IoT Build 0:6ae2f7bca550 246 if (amqpvalue_get_encoded_size(body_amqp_data, &encoded_size) != 0)
Azure.IoT Build 0:6ae2f7bca550 247 {
Azure.IoT Build 0:6ae2f7bca550 248 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 249 }
Azure.IoT Build 0:6ae2f7bca550 250 else
Azure.IoT Build 0:6ae2f7bca550 251 {
Azure.IoT Build 0:6ae2f7bca550 252 total_encoded_size += encoded_size;
Azure.IoT Build 0:6ae2f7bca550 253 }
Azure.IoT Build 0:6ae2f7bca550 254
Azure.IoT Build 0:6ae2f7bca550 255 amqpvalue_destroy(body_amqp_data);
Azure.IoT Build 0:6ae2f7bca550 256 }
Azure.IoT Build 0:6ae2f7bca550 257 }
Azure.IoT Build 0:6ae2f7bca550 258 }
Azure.IoT Build 0:6ae2f7bca550 259 }
AzureIoTClient 6:641a9672db08 260 break;
AzureIoTClient 6:641a9672db08 261 }
AzureIoTClient 6:641a9672db08 262 }
Azure.IoT Build 0:6ae2f7bca550 263
AzureIoTClient 6:641a9672db08 264 if (result == 0)
AzureIoTClient 6:641a9672db08 265 {
AzureIoTClient 6:641a9672db08 266 void* data_bytes = amqpalloc_malloc(total_encoded_size);
AzureIoTClient 6:641a9672db08 267 PAYLOAD payload;
AzureIoTClient 6:641a9672db08 268 payload.bytes = data_bytes;
AzureIoTClient 6:641a9672db08 269 payload.length = 0;
AzureIoTClient 6:641a9672db08 270 result = SEND_ONE_MESSAGE_OK;
Azure.IoT Build 0:6ae2f7bca550 271
AzureIoTClient 6:641a9672db08 272 if (header != NULL)
AzureIoTClient 6:641a9672db08 273 {
AzureIoTClient 6:641a9672db08 274 if (amqpvalue_encode(header_amqp_value, encode_bytes, &payload) != 0)
AzureIoTClient 6:641a9672db08 275 {
AzureIoTClient 6:641a9672db08 276 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 277 }
Azure.IoT Build 0:6ae2f7bca550 278
AzureIoTClient 6:641a9672db08 279 log_message_chunk(message_sender_instance, "Header:", header_amqp_value);
AzureIoTClient 6:641a9672db08 280 }
Azure.IoT Build 0:6ae2f7bca550 281
AzureIoTClient 6:641a9672db08 282 if ((result == SEND_ONE_MESSAGE_OK) && (properties != NULL))
AzureIoTClient 6:641a9672db08 283 {
AzureIoTClient 6:641a9672db08 284 if (amqpvalue_encode(properties_amqp_value, encode_bytes, &payload) != 0)
AzureIoTClient 6:641a9672db08 285 {
AzureIoTClient 6:641a9672db08 286 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 287 }
Azure.IoT Build 0:6ae2f7bca550 288
AzureIoTClient 6:641a9672db08 289 log_message_chunk(message_sender_instance, "Properties:", properties_amqp_value);
AzureIoTClient 6:641a9672db08 290 }
Azure.IoT Build 0:6ae2f7bca550 291
AzureIoTClient 6:641a9672db08 292 if ((result == SEND_ONE_MESSAGE_OK) && (application_properties != NULL))
AzureIoTClient 6:641a9672db08 293 {
AzureIoTClient 6:641a9672db08 294 if (amqpvalue_encode(application_properties_value, encode_bytes, &payload) != 0)
AzureIoTClient 6:641a9672db08 295 {
AzureIoTClient 6:641a9672db08 296 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 297 }
Azure.IoT Build 0:6ae2f7bca550 298
AzureIoTClient 6:641a9672db08 299 log_message_chunk(message_sender_instance, "Application properties:", application_properties_value);
AzureIoTClient 6:641a9672db08 300 }
Azure.IoT Build 0:6ae2f7bca550 301
AzureIoTClient 6:641a9672db08 302 if (result == SEND_ONE_MESSAGE_OK)
AzureIoTClient 6:641a9672db08 303 {
AzureIoTClient 6:641a9672db08 304 switch (message_body_type)
AzureIoTClient 6:641a9672db08 305 {
AzureIoTClient 6:641a9672db08 306 case MESSAGE_BODY_TYPE_VALUE:
AzureIoTClient 6:641a9672db08 307 {
AzureIoTClient 6:641a9672db08 308 if (amqpvalue_encode(body_amqp_value, encode_bytes, &payload) != 0)
AzureIoTClient 6:641a9672db08 309 {
AzureIoTClient 6:641a9672db08 310 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 311 }
Azure.IoT Build 0:6ae2f7bca550 312
AzureIoTClient 6:641a9672db08 313 log_message_chunk(message_sender_instance, "Body - amqp value:", body_amqp_value);
AzureIoTClient 6:641a9672db08 314 break;
AzureIoTClient 6:641a9672db08 315 }
AzureIoTClient 6:641a9672db08 316 case MESSAGE_BODY_TYPE_DATA:
AzureIoTClient 6:641a9672db08 317 {
Azure.IoT Build 0:6ae2f7bca550 318 BINARY_DATA binary_data;
Azure.IoT Build 0:6ae2f7bca550 319 size_t i;
Azure.IoT Build 0:6ae2f7bca550 320
Azure.IoT Build 0:6ae2f7bca550 321 for (i = 0; i < body_data_count; i++)
Azure.IoT Build 0:6ae2f7bca550 322 {
Azure.IoT Build 0:6ae2f7bca550 323 if (message_get_body_amqp_data(message, i, &binary_data) != 0)
Azure.IoT Build 0:6ae2f7bca550 324 {
Azure.IoT Build 0:6ae2f7bca550 325 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 326 }
Azure.IoT Build 0:6ae2f7bca550 327 else
Azure.IoT Build 0:6ae2f7bca550 328 {
AzureIoTClient 6:641a9672db08 329 amqp_binary binary_value;
AzureIoTClient 6:641a9672db08 330 binary_value.bytes = binary_data.bytes;
AzureIoTClient 6:641a9672db08 331 binary_value.length = (uint32_t)binary_data.length;
Azure.IoT Build 0:6ae2f7bca550 332 AMQP_VALUE body_amqp_data = amqpvalue_create_data(binary_value);
Azure.IoT Build 0:6ae2f7bca550 333 if (body_amqp_data == NULL)
Azure.IoT Build 0:6ae2f7bca550 334 {
Azure.IoT Build 0:6ae2f7bca550 335 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 336 }
Azure.IoT Build 0:6ae2f7bca550 337 else
Azure.IoT Build 0:6ae2f7bca550 338 {
Azure.IoT Build 0:6ae2f7bca550 339 if (amqpvalue_encode(body_amqp_data, encode_bytes, &payload) != 0)
Azure.IoT Build 0:6ae2f7bca550 340 {
Azure.IoT Build 0:6ae2f7bca550 341 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 342 break;
Azure.IoT Build 0:6ae2f7bca550 343 }
Azure.IoT Build 0:6ae2f7bca550 344
Azure.IoT Build 0:6ae2f7bca550 345 amqpvalue_destroy(body_amqp_data);
Azure.IoT Build 0:6ae2f7bca550 346 }
Azure.IoT Build 0:6ae2f7bca550 347 }
Azure.IoT Build 0:6ae2f7bca550 348 }
AzureIoTClient 6:641a9672db08 349 break;
AzureIoTClient 6:641a9672db08 350 }
AzureIoTClient 6:641a9672db08 351 }
AzureIoTClient 6:641a9672db08 352 }
Azure.IoT Build 0:6ae2f7bca550 353
AzureIoTClient 6:641a9672db08 354 if (result == SEND_ONE_MESSAGE_OK)
AzureIoTClient 6:641a9672db08 355 {
AzureIoTClient 6:641a9672db08 356 message_with_callback->message_send_state = MESSAGE_SEND_STATE_PENDING;
AzureIoTClient 6:641a9672db08 357 switch (link_transfer(message_sender_instance->link, message_format, &payload, 1, on_delivery_settled, message_with_callback))
AzureIoTClient 6:641a9672db08 358 {
AzureIoTClient 6:641a9672db08 359 default:
AzureIoTClient 6:641a9672db08 360 case LINK_TRANSFER_ERROR:
AzureIoTClient 6:641a9672db08 361 if (message_with_callback->on_message_send_complete != NULL)
AzureIoTClient 6:641a9672db08 362 {
AzureIoTClient 6:641a9672db08 363 message_with_callback->on_message_send_complete(message_with_callback->context, MESSAGE_SEND_ERROR);
AzureIoTClient 6:641a9672db08 364 }
Azure.IoT Build 0:6ae2f7bca550 365
AzureIoTClient 6:641a9672db08 366 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 367 break;
Azure.IoT Build 0:6ae2f7bca550 368
AzureIoTClient 6:641a9672db08 369 case LINK_TRANSFER_BUSY:
AzureIoTClient 6:641a9672db08 370 message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT;
AzureIoTClient 6:641a9672db08 371 result = SEND_ONE_MESSAGE_BUSY;
AzureIoTClient 6:641a9672db08 372 break;
Azure.IoT Build 0:6ae2f7bca550 373
AzureIoTClient 6:641a9672db08 374 case LINK_TRANSFER_OK:
AzureIoTClient 6:641a9672db08 375 result = SEND_ONE_MESSAGE_OK;
AzureIoTClient 6:641a9672db08 376 break;
AzureIoTClient 6:641a9672db08 377 }
AzureIoTClient 6:641a9672db08 378 }
Azure.IoT Build 0:6ae2f7bca550 379
AzureIoTClient 6:641a9672db08 380 amqpalloc_free(data_bytes);
Azure.IoT Build 0:6ae2f7bca550 381
AzureIoTClient 6:641a9672db08 382 if (body_amqp_value != NULL)
AzureIoTClient 6:641a9672db08 383 {
AzureIoTClient 6:641a9672db08 384 amqpvalue_destroy(body_amqp_value);
AzureIoTClient 6:641a9672db08 385 }
Azure.IoT Build 0:6ae2f7bca550 386
AzureIoTClient 6:641a9672db08 387 amqpvalue_destroy(application_properties);
AzureIoTClient 6:641a9672db08 388 amqpvalue_destroy(application_properties_value);
AzureIoTClient 6:641a9672db08 389 amqpvalue_destroy(properties_amqp_value);
AzureIoTClient 6:641a9672db08 390 properties_destroy(properties);
AzureIoTClient 6:641a9672db08 391 }
AzureIoTClient 6:641a9672db08 392 }
Azure.IoT Build 0:6ae2f7bca550 393
AzureIoTClient 6:641a9672db08 394 return result;
Azure.IoT Build 0:6ae2f7bca550 395 }
Azure.IoT Build 0:6ae2f7bca550 396
Azure.IoT Build 0:6ae2f7bca550 397 static void send_all_pending_messages(MESSAGE_SENDER_INSTANCE* message_sender_instance)
Azure.IoT Build 0:6ae2f7bca550 398 {
AzureIoTClient 6:641a9672db08 399 size_t i;
Azure.IoT Build 0:6ae2f7bca550 400
AzureIoTClient 6:641a9672db08 401 for (i = 0; i < message_sender_instance->message_count; i++)
AzureIoTClient 6:641a9672db08 402 {
AzureIoTClient 6:641a9672db08 403 if (message_sender_instance->messages[i]->message_send_state == MESSAGE_SEND_STATE_NOT_SENT)
AzureIoTClient 6:641a9672db08 404 {
AzureIoTClient 6:641a9672db08 405 switch (send_one_message(message_sender_instance, message_sender_instance->messages[i], message_sender_instance->messages[i]->message))
AzureIoTClient 6:641a9672db08 406 {
AzureIoTClient 6:641a9672db08 407 default:
AzureIoTClient 6:641a9672db08 408 case SEND_ONE_MESSAGE_ERROR:
AzureIoTClient 6:641a9672db08 409 {
AzureIoTClient 6:641a9672db08 410 ON_MESSAGE_SEND_COMPLETE on_message_send_complete = message_sender_instance->messages[i]->on_message_send_complete;
AzureIoTClient 6:641a9672db08 411 void* context = message_sender_instance->messages[i]->context;
AzureIoTClient 6:641a9672db08 412 remove_pending_message_by_index(message_sender_instance, i);
Azure.IoT Build 0:6ae2f7bca550 413
AzureIoTClient 6:641a9672db08 414 on_message_send_complete(context, MESSAGE_SEND_ERROR);
AzureIoTClient 6:641a9672db08 415 i = message_sender_instance->message_count;
AzureIoTClient 6:641a9672db08 416 break;
AzureIoTClient 6:641a9672db08 417 }
AzureIoTClient 6:641a9672db08 418 case SEND_ONE_MESSAGE_BUSY:
AzureIoTClient 6:641a9672db08 419 i = message_sender_instance->message_count + 1;
AzureIoTClient 6:641a9672db08 420 break;
Azure.IoT Build 0:6ae2f7bca550 421
AzureIoTClient 6:641a9672db08 422 case SEND_ONE_MESSAGE_OK:
AzureIoTClient 6:641a9672db08 423 break;
AzureIoTClient 6:641a9672db08 424 }
Azure.IoT Build 0:6ae2f7bca550 425
AzureIoTClient 6:641a9672db08 426 i--;
AzureIoTClient 6:641a9672db08 427 }
AzureIoTClient 6:641a9672db08 428 }
Azure.IoT Build 0:6ae2f7bca550 429 }
Azure.IoT Build 0:6ae2f7bca550 430
Azure.IoT Build 0:6ae2f7bca550 431 static void set_message_sender_state(MESSAGE_SENDER_INSTANCE* message_sender_instance, MESSAGE_SENDER_STATE new_state)
Azure.IoT Build 0:6ae2f7bca550 432 {
AzureIoTClient 6:641a9672db08 433 MESSAGE_SENDER_STATE previous_state = message_sender_instance->message_sender_state;
AzureIoTClient 6:641a9672db08 434 message_sender_instance->message_sender_state = new_state;
AzureIoTClient 6:641a9672db08 435 if (message_sender_instance->on_message_sender_state_changed != NULL)
AzureIoTClient 6:641a9672db08 436 {
AzureIoTClient 6:641a9672db08 437 message_sender_instance->on_message_sender_state_changed(message_sender_instance->on_message_sender_state_changed_context, new_state, previous_state);
AzureIoTClient 6:641a9672db08 438 }
Azure.IoT Build 0:6ae2f7bca550 439 }
Azure.IoT Build 0:6ae2f7bca550 440
AzureIoTClient 2:64b4feb67cd3 441 static void indicate_all_messages_as_error(MESSAGE_SENDER_INSTANCE* message_sender_instance)
AzureIoTClient 2:64b4feb67cd3 442 {
AzureIoTClient 2:64b4feb67cd3 443 size_t i;
AzureIoTClient 2:64b4feb67cd3 444
AzureIoTClient 2:64b4feb67cd3 445 for (i = 0; i < message_sender_instance->message_count; i++)
AzureIoTClient 2:64b4feb67cd3 446 {
AzureIoTClient 2:64b4feb67cd3 447 if (message_sender_instance->messages[i]->on_message_send_complete != NULL)
AzureIoTClient 2:64b4feb67cd3 448 {
AzureIoTClient 2:64b4feb67cd3 449 message_sender_instance->messages[i]->on_message_send_complete(message_sender_instance->messages[i]->context, MESSAGE_SEND_ERROR);
AzureIoTClient 2:64b4feb67cd3 450 }
AzureIoTClient 2:64b4feb67cd3 451
AzureIoTClient 2:64b4feb67cd3 452 message_destroy(message_sender_instance->messages[i]->message);
AzureIoTClient 2:64b4feb67cd3 453 amqpalloc_free(message_sender_instance->messages[i]);
AzureIoTClient 2:64b4feb67cd3 454 }
AzureIoTClient 2:64b4feb67cd3 455
AzureIoTClient 2:64b4feb67cd3 456 if (message_sender_instance->messages != NULL)
AzureIoTClient 2:64b4feb67cd3 457 {
AzureIoTClient 2:64b4feb67cd3 458 message_sender_instance->message_count = 0;
AzureIoTClient 2:64b4feb67cd3 459
AzureIoTClient 2:64b4feb67cd3 460 amqpalloc_free(message_sender_instance->messages);
AzureIoTClient 2:64b4feb67cd3 461 message_sender_instance->messages = NULL;
AzureIoTClient 2:64b4feb67cd3 462 }
AzureIoTClient 2:64b4feb67cd3 463 }
AzureIoTClient 2:64b4feb67cd3 464
Azure.IoT Build 0:6ae2f7bca550 465 static void on_link_state_changed(void* context, LINK_STATE new_link_state, LINK_STATE previous_link_state)
Azure.IoT Build 0:6ae2f7bca550 466 {
AzureIoTClient 6:641a9672db08 467 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)context;
AzureIoTClient 6:641a9672db08 468 (void)previous_link_state;
Azure.IoT Build 0:6ae2f7bca550 469
AzureIoTClient 6:641a9672db08 470 switch (new_link_state)
AzureIoTClient 6:641a9672db08 471 {
AzureIoTClient 6:641a9672db08 472 case LINK_STATE_ATTACHED:
AzureIoTClient 6:641a9672db08 473 if (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPENING)
AzureIoTClient 6:641a9672db08 474 {
AzureIoTClient 6:641a9672db08 475 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_OPEN);
AzureIoTClient 6:641a9672db08 476 }
AzureIoTClient 6:641a9672db08 477 break;
AzureIoTClient 6:641a9672db08 478 case LINK_STATE_DETACHED:
Azure.IoT Build 0:6ae2f7bca550 479 if ((message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPEN) ||
Azure.IoT Build 0:6ae2f7bca550 480 (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_CLOSING))
Azure.IoT Build 0:6ae2f7bca550 481 {
Azure.IoT Build 0:6ae2f7bca550 482 /* User initiated transition, we should be good */
AzureIoTClient 2:64b4feb67cd3 483 indicate_all_messages_as_error(message_sender_instance);
Azure.IoT Build 0:6ae2f7bca550 484 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_IDLE);
Azure.IoT Build 0:6ae2f7bca550 485 }
Azure.IoT Build 0:6ae2f7bca550 486 else if (message_sender_instance->message_sender_state != MESSAGE_SENDER_STATE_IDLE)
Azure.IoT Build 0:6ae2f7bca550 487 {
Azure.IoT Build 0:6ae2f7bca550 488 /* Any other transition must be an error */
Azure.IoT Build 0:6ae2f7bca550 489 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 490 }
Azure.IoT Build 0:6ae2f7bca550 491 break;
AzureIoTClient 6:641a9672db08 492 case LINK_STATE_ERROR:
AzureIoTClient 6:641a9672db08 493 if (message_sender_instance->message_sender_state != MESSAGE_SENDER_STATE_ERROR)
AzureIoTClient 6:641a9672db08 494 {
AzureIoTClient 2:64b4feb67cd3 495 indicate_all_messages_as_error(message_sender_instance);
AzureIoTClient 6:641a9672db08 496 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_ERROR);
AzureIoTClient 6:641a9672db08 497 }
AzureIoTClient 6:641a9672db08 498 break;
AzureIoTClient 6:641a9672db08 499 }
Azure.IoT Build 0:6ae2f7bca550 500 }
Azure.IoT Build 0:6ae2f7bca550 501
Azure.IoT Build 0:6ae2f7bca550 502 static void on_link_flow_on(void* context)
Azure.IoT Build 0:6ae2f7bca550 503 {
AzureIoTClient 6:641a9672db08 504 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)context;
AzureIoTClient 6:641a9672db08 505 send_all_pending_messages(message_sender_instance);
Azure.IoT Build 0:6ae2f7bca550 506 }
Azure.IoT Build 0:6ae2f7bca550 507
Azure.IoT Build 5:ae49385aff34 508 MESSAGE_SENDER_HANDLE messagesender_create(LINK_HANDLE link, ON_MESSAGE_SENDER_STATE_CHANGED on_message_sender_state_changed, void* context)
Azure.IoT Build 0:6ae2f7bca550 509 {
AzureIoTClient 6:641a9672db08 510 MESSAGE_SENDER_INSTANCE* result = amqpalloc_malloc(sizeof(MESSAGE_SENDER_INSTANCE));
AzureIoTClient 6:641a9672db08 511 if (result != NULL)
AzureIoTClient 6:641a9672db08 512 {
AzureIoTClient 6:641a9672db08 513 result->messages = NULL;
AzureIoTClient 6:641a9672db08 514 result->message_count = 0;
AzureIoTClient 6:641a9672db08 515 result->link = link;
AzureIoTClient 6:641a9672db08 516 result->on_message_sender_state_changed = on_message_sender_state_changed;
AzureIoTClient 6:641a9672db08 517 result->on_message_sender_state_changed_context = context;
AzureIoTClient 6:641a9672db08 518 result->message_sender_state = MESSAGE_SENDER_STATE_IDLE;
AzureIoTClient 6:641a9672db08 519 result->is_trace_on = 0;
AzureIoTClient 6:641a9672db08 520 }
Azure.IoT Build 0:6ae2f7bca550 521
AzureIoTClient 6:641a9672db08 522 return result;
Azure.IoT Build 0:6ae2f7bca550 523 }
Azure.IoT Build 0:6ae2f7bca550 524
Azure.IoT Build 0:6ae2f7bca550 525 void messagesender_destroy(MESSAGE_SENDER_HANDLE message_sender)
Azure.IoT Build 0:6ae2f7bca550 526 {
AzureIoTClient 6:641a9672db08 527 if (message_sender != NULL)
AzureIoTClient 6:641a9672db08 528 {
AzureIoTClient 6:641a9672db08 529 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
Azure.IoT Build 0:6ae2f7bca550 530
AzureIoTClient 6:641a9672db08 531 messagesender_close(message_sender_instance);
Azure.IoT Build 0:6ae2f7bca550 532
AzureIoTClient 2:64b4feb67cd3 533 indicate_all_messages_as_error(message_sender_instance);
Azure.IoT Build 0:6ae2f7bca550 534
AzureIoTClient 6:641a9672db08 535 amqpalloc_free(message_sender);
AzureIoTClient 6:641a9672db08 536 }
Azure.IoT Build 0:6ae2f7bca550 537 }
Azure.IoT Build 0:6ae2f7bca550 538
Azure.IoT Build 0:6ae2f7bca550 539 int messagesender_open(MESSAGE_SENDER_HANDLE message_sender)
Azure.IoT Build 0:6ae2f7bca550 540 {
AzureIoTClient 6:641a9672db08 541 int result;
Azure.IoT Build 0:6ae2f7bca550 542
AzureIoTClient 6:641a9672db08 543 if (message_sender == NULL)
AzureIoTClient 6:641a9672db08 544 {
AzureIoTClient 6:641a9672db08 545 result = __LINE__;
AzureIoTClient 6:641a9672db08 546 }
AzureIoTClient 6:641a9672db08 547 else
AzureIoTClient 6:641a9672db08 548 {
AzureIoTClient 6:641a9672db08 549 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
Azure.IoT Build 0:6ae2f7bca550 550
AzureIoTClient 6:641a9672db08 551 if (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_IDLE)
AzureIoTClient 6:641a9672db08 552 {
AzureIoTClient 6:641a9672db08 553 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_OPENING);
AzureIoTClient 6:641a9672db08 554 if (link_attach(message_sender_instance->link, NULL, on_link_state_changed, on_link_flow_on, message_sender_instance) != 0)
AzureIoTClient 6:641a9672db08 555 {
AzureIoTClient 6:641a9672db08 556 result = __LINE__;
AzureIoTClient 6:641a9672db08 557 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_ERROR);
AzureIoTClient 6:641a9672db08 558 }
AzureIoTClient 6:641a9672db08 559 else
AzureIoTClient 6:641a9672db08 560 {
AzureIoTClient 6:641a9672db08 561 result = 0;
AzureIoTClient 6:641a9672db08 562 }
AzureIoTClient 6:641a9672db08 563 }
AzureIoTClient 6:641a9672db08 564 else
AzureIoTClient 6:641a9672db08 565 {
AzureIoTClient 6:641a9672db08 566 result = 0;
AzureIoTClient 6:641a9672db08 567 }
AzureIoTClient 6:641a9672db08 568 }
Azure.IoT Build 0:6ae2f7bca550 569
AzureIoTClient 6:641a9672db08 570 return result;
Azure.IoT Build 0:6ae2f7bca550 571 }
Azure.IoT Build 0:6ae2f7bca550 572
Azure.IoT Build 0:6ae2f7bca550 573 int messagesender_close(MESSAGE_SENDER_HANDLE message_sender)
Azure.IoT Build 0:6ae2f7bca550 574 {
AzureIoTClient 6:641a9672db08 575 int result;
Azure.IoT Build 0:6ae2f7bca550 576
AzureIoTClient 6:641a9672db08 577 if (message_sender == NULL)
AzureIoTClient 6:641a9672db08 578 {
AzureIoTClient 6:641a9672db08 579 result = __LINE__;
AzureIoTClient 6:641a9672db08 580 }
AzureIoTClient 6:641a9672db08 581 else
AzureIoTClient 6:641a9672db08 582 {
AzureIoTClient 6:641a9672db08 583 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
Azure.IoT Build 0:6ae2f7bca550 584
AzureIoTClient 6:641a9672db08 585 if ((message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPENING) ||
AzureIoTClient 6:641a9672db08 586 (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPEN))
AzureIoTClient 6:641a9672db08 587 {
AzureIoTClient 6:641a9672db08 588 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_CLOSING);
AzureIoTClient 6:641a9672db08 589 if (link_detach(message_sender_instance->link) != 0)
AzureIoTClient 6:641a9672db08 590 {
AzureIoTClient 6:641a9672db08 591 result = __LINE__;
AzureIoTClient 6:641a9672db08 592 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_ERROR);
AzureIoTClient 6:641a9672db08 593 }
AzureIoTClient 6:641a9672db08 594 else
AzureIoTClient 6:641a9672db08 595 {
AzureIoTClient 6:641a9672db08 596 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_IDLE);
AzureIoTClient 6:641a9672db08 597 result = 0;
AzureIoTClient 6:641a9672db08 598 }
AzureIoTClient 6:641a9672db08 599 }
AzureIoTClient 6:641a9672db08 600 else
AzureIoTClient 6:641a9672db08 601 {
AzureIoTClient 6:641a9672db08 602 result = 0;
AzureIoTClient 6:641a9672db08 603 }
AzureIoTClient 6:641a9672db08 604 }
Azure.IoT Build 0:6ae2f7bca550 605
AzureIoTClient 6:641a9672db08 606 return result;
Azure.IoT Build 0:6ae2f7bca550 607 }
Azure.IoT Build 0:6ae2f7bca550 608
Azure.IoT Build 0:6ae2f7bca550 609 int messagesender_send(MESSAGE_SENDER_HANDLE message_sender, MESSAGE_HANDLE message, ON_MESSAGE_SEND_COMPLETE on_message_send_complete, void* callback_context)
Azure.IoT Build 0:6ae2f7bca550 610 {
AzureIoTClient 6:641a9672db08 611 int result;
Azure.IoT Build 0:6ae2f7bca550 612
AzureIoTClient 6:641a9672db08 613 if ((message_sender == NULL) ||
AzureIoTClient 6:641a9672db08 614 (message == NULL))
AzureIoTClient 6:641a9672db08 615 {
AzureIoTClient 6:641a9672db08 616 result = __LINE__;
AzureIoTClient 6:641a9672db08 617 }
AzureIoTClient 6:641a9672db08 618 else
AzureIoTClient 6:641a9672db08 619 {
AzureIoTClient 6:641a9672db08 620 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
AzureIoTClient 6:641a9672db08 621 if (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_ERROR)
AzureIoTClient 6:641a9672db08 622 {
AzureIoTClient 6:641a9672db08 623 result = __LINE__;
AzureIoTClient 6:641a9672db08 624 }
AzureIoTClient 6:641a9672db08 625 else
AzureIoTClient 6:641a9672db08 626 {
AzureIoTClient 6:641a9672db08 627 MESSAGE_WITH_CALLBACK* message_with_callback = (MESSAGE_WITH_CALLBACK*)amqpalloc_malloc(sizeof(MESSAGE_WITH_CALLBACK));
AzureIoTClient 6:641a9672db08 628 if (message_with_callback == NULL)
AzureIoTClient 6:641a9672db08 629 {
AzureIoTClient 6:641a9672db08 630 result = __LINE__;
AzureIoTClient 6:641a9672db08 631 }
AzureIoTClient 6:641a9672db08 632 else
AzureIoTClient 6:641a9672db08 633 {
AzureIoTClient 6:641a9672db08 634 MESSAGE_WITH_CALLBACK** new_messages = (MESSAGE_WITH_CALLBACK**)amqpalloc_realloc(message_sender_instance->messages, sizeof(MESSAGE_WITH_CALLBACK*) * (message_sender_instance->message_count + 1));
AzureIoTClient 6:641a9672db08 635 if (new_messages == NULL)
AzureIoTClient 6:641a9672db08 636 {
AzureIoTClient 6:641a9672db08 637 amqpalloc_free(message_with_callback);
AzureIoTClient 6:641a9672db08 638 result = __LINE__;
AzureIoTClient 6:641a9672db08 639 }
AzureIoTClient 6:641a9672db08 640 else
AzureIoTClient 6:641a9672db08 641 {
AzureIoTClient 6:641a9672db08 642 result = 0;
Azure.IoT Build 0:6ae2f7bca550 643
AzureIoTClient 6:641a9672db08 644 message_sender_instance->messages = new_messages;
AzureIoTClient 6:641a9672db08 645 if (message_sender_instance->message_sender_state != MESSAGE_SENDER_STATE_OPEN)
AzureIoTClient 6:641a9672db08 646 {
AzureIoTClient 6:641a9672db08 647 message_with_callback->message = message_clone(message);
AzureIoTClient 6:641a9672db08 648 if (message_with_callback->message == NULL)
AzureIoTClient 6:641a9672db08 649 {
AzureIoTClient 6:641a9672db08 650 amqpalloc_free(message_with_callback);
AzureIoTClient 6:641a9672db08 651 result = __LINE__;
AzureIoTClient 6:641a9672db08 652 }
AzureIoTClient 6:641a9672db08 653
AzureIoTClient 6:641a9672db08 654 message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT;
AzureIoTClient 6:641a9672db08 655 }
AzureIoTClient 6:641a9672db08 656 else
AzureIoTClient 6:641a9672db08 657 {
AzureIoTClient 6:641a9672db08 658 message_with_callback->message = NULL;
AzureIoTClient 6:641a9672db08 659 message_with_callback->message_send_state = MESSAGE_SEND_STATE_PENDING;
AzureIoTClient 6:641a9672db08 660 }
Azure.IoT Build 0:6ae2f7bca550 661
AzureIoTClient 6:641a9672db08 662 if (result == 0)
AzureIoTClient 6:641a9672db08 663 {
AzureIoTClient 6:641a9672db08 664 message_with_callback->on_message_send_complete = on_message_send_complete;
AzureIoTClient 6:641a9672db08 665 message_with_callback->context = callback_context;
AzureIoTClient 6:641a9672db08 666 message_with_callback->message_sender = message_sender_instance;
AzureIoTClient 6:641a9672db08 667
AzureIoTClient 6:641a9672db08 668 message_sender_instance->messages[message_sender_instance->message_count] = message_with_callback;
AzureIoTClient 6:641a9672db08 669 message_sender_instance->message_count++;
Azure.IoT Build 0:6ae2f7bca550 670
AzureIoTClient 6:641a9672db08 671 if (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPEN)
AzureIoTClient 6:641a9672db08 672 {
AzureIoTClient 6:641a9672db08 673 switch (send_one_message(message_sender_instance, message_with_callback, message))
AzureIoTClient 6:641a9672db08 674 {
AzureIoTClient 6:641a9672db08 675 default:
AzureIoTClient 6:641a9672db08 676 case SEND_ONE_MESSAGE_ERROR:
AzureIoTClient 6:641a9672db08 677 remove_pending_message_by_index(message_sender_instance, message_sender_instance->message_count - 1);
AzureIoTClient 6:641a9672db08 678 result = __LINE__;
AzureIoTClient 6:641a9672db08 679 break;
Azure.IoT Build 0:6ae2f7bca550 680
AzureIoTClient 6:641a9672db08 681 case SEND_ONE_MESSAGE_BUSY:
AzureIoTClient 6:641a9672db08 682 message_with_callback->message = message_clone(message);
AzureIoTClient 6:641a9672db08 683 if (message_with_callback->message == NULL)
AzureIoTClient 6:641a9672db08 684 {
AzureIoTClient 6:641a9672db08 685 amqpalloc_free(message_with_callback);
AzureIoTClient 6:641a9672db08 686 result = __LINE__;
AzureIoTClient 6:641a9672db08 687 }
AzureIoTClient 6:641a9672db08 688 else
AzureIoTClient 6:641a9672db08 689 {
AzureIoTClient 6:641a9672db08 690 message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT;
AzureIoTClient 6:641a9672db08 691 result = 0;
AzureIoTClient 6:641a9672db08 692 }
AzureIoTClient 6:641a9672db08 693 break;
Azure.IoT Build 0:6ae2f7bca550 694
AzureIoTClient 6:641a9672db08 695 case SEND_ONE_MESSAGE_OK:
AzureIoTClient 6:641a9672db08 696 result = 0;
AzureIoTClient 6:641a9672db08 697 break;
AzureIoTClient 6:641a9672db08 698 }
AzureIoTClient 6:641a9672db08 699 }
AzureIoTClient 6:641a9672db08 700 }
AzureIoTClient 6:641a9672db08 701 }
AzureIoTClient 6:641a9672db08 702 }
AzureIoTClient 6:641a9672db08 703 }
AzureIoTClient 6:641a9672db08 704 }
AzureIoTClient 6:641a9672db08 705 return result;
AzureIoTClient 6:641a9672db08 706 }
Azure.IoT Build 0:6ae2f7bca550 707
AzureIoTClient 6:641a9672db08 708 void messagesender_set_trace(MESSAGE_SENDER_HANDLE message_sender, bool traceOn)
AzureIoTClient 6:641a9672db08 709 {
AzureIoTClient 6:641a9672db08 710 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
AzureIoTClient 6:641a9672db08 711 if (message_sender_instance != NULL)
AzureIoTClient 6:641a9672db08 712 {
AzureIoTClient 6:641a9672db08 713 message_sender_instance->is_trace_on = traceOn ? 1 : 0;
AzureIoTClient 6:641a9672db08 714 }
Azure.IoT Build 0:6ae2f7bca550 715 }