A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

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