A small memory footprint AMQP implimentation
Dependents: iothub_client_sample_amqp remote_monitoring simplesample_amqp
message_sender.c@34:6be9c2058664, 2017-10-21 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Sat Oct 21 20:12:19 2017 +0000
- Revision:
- 34:6be9c2058664
- Parent:
- 33:08b53020ff0d
- Child:
- 35:d0bed2404ee9
1.1.26
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Azure.IoT Build | 0:6ae2f7bca550 | 1 | // Copyright (c) Microsoft. All rights reserved. |
Azure.IoT Build | 0:6ae2f7bca550 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
Azure.IoT Build | 0:6ae2f7bca550 | 3 | |
Azure.IoT Build | 0:6ae2f7bca550 | 4 | #include <stdlib.h> |
AzureIoTClient | 6:641a9672db08 | 5 | #include <stdbool.h> |
Azure.IoT Build | 0:6ae2f7bca550 | 6 | #include <string.h> |
AzureIoTClient | 19:000ab4e6a2c1 | 7 | #include "azure_c_shared_utility/optimize_size.h" |
AzureIoTClient | 21:f9c433d8e6ca | 8 | #include "azure_c_shared_utility/gballoc.h" |
Azure.IoT Build | 5:ae49385aff34 | 9 | #include "azure_c_shared_utility/xlogging.h" |
AzureIoTClient | 34:6be9c2058664 | 10 | #include "azure_c_shared_utility/tickcounter.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 11 | #include "azure_uamqp_c/message_sender.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 12 | #include "azure_uamqp_c/amqpvalue_to_string.h" |
AzureIoTClient | 34:6be9c2058664 | 13 | #include "azure_uamqp_c/async_operation.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; |
AzureIoTClient | 34:6be9c2058664 | 35 | tickcounter_ms_t timeout; |
Azure.IoT Build | 0:6ae2f7bca550 | 36 | } MESSAGE_WITH_CALLBACK; |
Azure.IoT Build | 0:6ae2f7bca550 | 37 | |
AzureIoTClient | 34:6be9c2058664 | 38 | DEFINE_ASYNC_OPERATION_CONTEXT(MESSAGE_WITH_CALLBACK); |
AzureIoTClient | 34:6be9c2058664 | 39 | |
Azure.IoT Build | 0:6ae2f7bca550 | 40 | typedef struct MESSAGE_SENDER_INSTANCE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 41 | { |
AzureIoTClient | 6:641a9672db08 | 42 | LINK_HANDLE link; |
AzureIoTClient | 6:641a9672db08 | 43 | size_t message_count; |
AzureIoTClient | 34:6be9c2058664 | 44 | ASYNC_OPERATION_HANDLE* messages; |
AzureIoTClient | 6:641a9672db08 | 45 | MESSAGE_SENDER_STATE message_sender_state; |
AzureIoTClient | 6:641a9672db08 | 46 | ON_MESSAGE_SENDER_STATE_CHANGED on_message_sender_state_changed; |
AzureIoTClient | 6:641a9672db08 | 47 | void* on_message_sender_state_changed_context; |
AzureIoTClient | 6:641a9672db08 | 48 | unsigned int is_trace_on : 1; |
Azure.IoT Build | 0:6ae2f7bca550 | 49 | } MESSAGE_SENDER_INSTANCE; |
Azure.IoT Build | 0:6ae2f7bca550 | 50 | |
AzureIoTClient | 34:6be9c2058664 | 51 | static void remove_pending_message_by_index(MESSAGE_SENDER_HANDLE message_sender, size_t index) |
Azure.IoT Build | 0:6ae2f7bca550 | 52 | { |
AzureIoTClient | 34:6be9c2058664 | 53 | ASYNC_OPERATION_HANDLE* new_messages; |
AzureIoTClient | 34:6be9c2058664 | 54 | MESSAGE_WITH_CALLBACK* message_with_callback = GET_ASYNC_OPERATION_CONTEXT(MESSAGE_WITH_CALLBACK, message_sender->messages[index]); |
Azure.IoT Build | 0:6ae2f7bca550 | 55 | |
AzureIoTClient | 34:6be9c2058664 | 56 | if (message_with_callback->message != NULL) |
AzureIoTClient | 6:641a9672db08 | 57 | { |
AzureIoTClient | 34:6be9c2058664 | 58 | message_destroy(message_with_callback->message); |
AzureIoTClient | 34:6be9c2058664 | 59 | message_with_callback->message = NULL; |
AzureIoTClient | 6:641a9672db08 | 60 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 61 | |
AzureIoTClient | 34:6be9c2058664 | 62 | async_operation_destroy(message_sender->messages[index]); |
Azure.IoT Build | 0:6ae2f7bca550 | 63 | |
AzureIoTClient | 34:6be9c2058664 | 64 | if (message_sender->message_count - index > 1) |
AzureIoTClient | 6:641a9672db08 | 65 | { |
AzureIoTClient | 34:6be9c2058664 | 66 | (void)memmove(&message_sender->messages[index], &message_sender->messages[index + 1], sizeof(ASYNC_OPERATION_HANDLE) * (message_sender->message_count - index - 1)); |
AzureIoTClient | 6:641a9672db08 | 67 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 68 | |
AzureIoTClient | 34:6be9c2058664 | 69 | message_sender->message_count--; |
Azure.IoT Build | 0:6ae2f7bca550 | 70 | |
AzureIoTClient | 34:6be9c2058664 | 71 | if (message_sender->message_count > 0) |
AzureIoTClient | 6:641a9672db08 | 72 | { |
AzureIoTClient | 34:6be9c2058664 | 73 | new_messages = (ASYNC_OPERATION_HANDLE*)realloc(message_sender->messages, sizeof(ASYNC_OPERATION_HANDLE) * (message_sender->message_count)); |
AzureIoTClient | 6:641a9672db08 | 74 | if (new_messages != NULL) |
AzureIoTClient | 6:641a9672db08 | 75 | { |
AzureIoTClient | 34:6be9c2058664 | 76 | message_sender->messages = new_messages; |
AzureIoTClient | 6:641a9672db08 | 77 | } |
AzureIoTClient | 6:641a9672db08 | 78 | } |
AzureIoTClient | 6:641a9672db08 | 79 | else |
AzureIoTClient | 6:641a9672db08 | 80 | { |
AzureIoTClient | 34:6be9c2058664 | 81 | free(message_sender->messages); |
AzureIoTClient | 34:6be9c2058664 | 82 | message_sender->messages = NULL; |
AzureIoTClient | 6:641a9672db08 | 83 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 84 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 85 | |
AzureIoTClient | 34:6be9c2058664 | 86 | static void remove_pending_message(MESSAGE_SENDER_INSTANCE* message_sender, ASYNC_OPERATION_HANDLE pending_send) |
Azure.IoT Build | 0:6ae2f7bca550 | 87 | { |
AzureIoTClient | 6:641a9672db08 | 88 | size_t i; |
Azure.IoT Build | 0:6ae2f7bca550 | 89 | |
AzureIoTClient | 34:6be9c2058664 | 90 | for (i = 0; i < message_sender->message_count; i++) |
AzureIoTClient | 6:641a9672db08 | 91 | { |
AzureIoTClient | 34:6be9c2058664 | 92 | if (message_sender->messages[i] == pending_send) |
AzureIoTClient | 6:641a9672db08 | 93 | { |
AzureIoTClient | 34:6be9c2058664 | 94 | remove_pending_message_by_index(message_sender, i); |
AzureIoTClient | 6:641a9672db08 | 95 | break; |
AzureIoTClient | 6:641a9672db08 | 96 | } |
AzureIoTClient | 6:641a9672db08 | 97 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 98 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 99 | |
AzureIoTClient | 23:1111ee8bcba4 | 100 | static void on_delivery_settled(void* context, delivery_number delivery_no, LINK_DELIVERY_SETTLE_REASON reason, AMQP_VALUE delivery_state) |
Azure.IoT Build | 0:6ae2f7bca550 | 101 | { |
AzureIoTClient | 34:6be9c2058664 | 102 | ASYNC_OPERATION_HANDLE pending_send = (ASYNC_OPERATION_HANDLE)context; |
AzureIoTClient | 34:6be9c2058664 | 103 | MESSAGE_WITH_CALLBACK* message_with_callback = GET_ASYNC_OPERATION_CONTEXT(MESSAGE_WITH_CALLBACK, pending_send); |
AzureIoTClient | 34:6be9c2058664 | 104 | MESSAGE_SENDER_INSTANCE* message_sender = (MESSAGE_SENDER_INSTANCE*)message_with_callback->message_sender; |
AzureIoTClient | 6:641a9672db08 | 105 | (void)delivery_no; |
Azure.IoT Build | 0:6ae2f7bca550 | 106 | |
AzureIoTClient | 6:641a9672db08 | 107 | if (message_with_callback->on_message_send_complete != NULL) |
AzureIoTClient | 6:641a9672db08 | 108 | { |
AzureIoTClient | 23:1111ee8bcba4 | 109 | switch (reason) |
AzureIoTClient | 7:9e9ab3b0efef | 110 | { |
AzureIoTClient | 23:1111ee8bcba4 | 111 | case LINK_DELIVERY_SETTLE_REASON_DISPOSITION_RECEIVED: |
AzureIoTClient | 23:1111ee8bcba4 | 112 | if (delivery_state == NULL) |
AzureIoTClient | 7:9e9ab3b0efef | 113 | { |
AzureIoTClient | 23:1111ee8bcba4 | 114 | LogError("delivery state not provided"); |
AzureIoTClient | 7:9e9ab3b0efef | 115 | } |
AzureIoTClient | 7:9e9ab3b0efef | 116 | else |
AzureIoTClient | 7:9e9ab3b0efef | 117 | { |
AzureIoTClient | 23:1111ee8bcba4 | 118 | AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(delivery_state); |
AzureIoTClient | 23:1111ee8bcba4 | 119 | |
AzureIoTClient | 23:1111ee8bcba4 | 120 | if (descriptor == NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 121 | { |
AzureIoTClient | 23:1111ee8bcba4 | 122 | LogError("Error getting descriptor for delivery state"); |
AzureIoTClient | 23:1111ee8bcba4 | 123 | } |
AzureIoTClient | 23:1111ee8bcba4 | 124 | else if (is_accepted_type_by_descriptor(descriptor)) |
AzureIoTClient | 23:1111ee8bcba4 | 125 | { |
AzureIoTClient | 23:1111ee8bcba4 | 126 | message_with_callback->on_message_send_complete(message_with_callback->context, MESSAGE_SEND_OK); |
AzureIoTClient | 23:1111ee8bcba4 | 127 | } |
AzureIoTClient | 23:1111ee8bcba4 | 128 | else |
AzureIoTClient | 23:1111ee8bcba4 | 129 | { |
AzureIoTClient | 23:1111ee8bcba4 | 130 | message_with_callback->on_message_send_complete(message_with_callback->context, MESSAGE_SEND_ERROR); |
AzureIoTClient | 23:1111ee8bcba4 | 131 | } |
AzureIoTClient | 7:9e9ab3b0efef | 132 | } |
AzureIoTClient | 7:9e9ab3b0efef | 133 | |
AzureIoTClient | 23:1111ee8bcba4 | 134 | break; |
AzureIoTClient | 23:1111ee8bcba4 | 135 | case LINK_DELIVERY_SETTLE_REASON_SETTLED: |
AzureIoTClient | 23:1111ee8bcba4 | 136 | message_with_callback->on_message_send_complete(message_with_callback->context, MESSAGE_SEND_OK); |
AzureIoTClient | 23:1111ee8bcba4 | 137 | break; |
AzureIoTClient | 34:6be9c2058664 | 138 | case LINK_DELIVERY_SETTLE_REASON_TIMEOUT: |
AzureIoTClient | 34:6be9c2058664 | 139 | message_with_callback->on_message_send_complete(message_with_callback->context, MESSAGE_SEND_TIMEOUT); |
AzureIoTClient | 34:6be9c2058664 | 140 | break; |
AzureIoTClient | 23:1111ee8bcba4 | 141 | case LINK_DELIVERY_SETTLE_REASON_NOT_DELIVERED: |
AzureIoTClient | 23:1111ee8bcba4 | 142 | default: |
AzureIoTClient | 23:1111ee8bcba4 | 143 | message_with_callback->on_message_send_complete(message_with_callback->context, MESSAGE_SEND_ERROR); |
AzureIoTClient | 23:1111ee8bcba4 | 144 | break; |
AzureIoTClient | 7:9e9ab3b0efef | 145 | } |
AzureIoTClient | 6:641a9672db08 | 146 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 147 | |
AzureIoTClient | 34:6be9c2058664 | 148 | remove_pending_message(message_sender, pending_send); |
Azure.IoT Build | 0:6ae2f7bca550 | 149 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 150 | |
Azure.IoT Build | 0:6ae2f7bca550 | 151 | static int encode_bytes(void* context, const unsigned char* bytes, size_t length) |
Azure.IoT Build | 0:6ae2f7bca550 | 152 | { |
AzureIoTClient | 6:641a9672db08 | 153 | PAYLOAD* payload = (PAYLOAD*)context; |
AzureIoTClient | 6:641a9672db08 | 154 | (void)memcpy((unsigned char*)payload->bytes + payload->length, bytes, length); |
AzureIoTClient | 6:641a9672db08 | 155 | payload->length += length; |
AzureIoTClient | 6:641a9672db08 | 156 | return 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 157 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 158 | |
AzureIoTClient | 34:6be9c2058664 | 159 | static void log_message_chunk(MESSAGE_SENDER_INSTANCE* message_sender, const char* name, AMQP_VALUE value) |
Azure.IoT Build | 0:6ae2f7bca550 | 160 | { |
AzureIoTClient | 9:c22db038556c | 161 | #ifdef NO_LOGGING |
AzureIoTClient | 34:6be9c2058664 | 162 | UNUSED(message_sender); |
AzureIoTClient | 9:c22db038556c | 163 | UNUSED(name); |
AzureIoTClient | 9:c22db038556c | 164 | UNUSED(value); |
AzureIoTClient | 9:c22db038556c | 165 | #else |
AzureIoTClient | 34:6be9c2058664 | 166 | if (xlogging_get_log_function() != NULL && message_sender->is_trace_on == 1) |
AzureIoTClient | 6:641a9672db08 | 167 | { |
AzureIoTClient | 6:641a9672db08 | 168 | char* value_as_string = NULL; |
AzureIoTClient | 34:6be9c2058664 | 169 | LOG(AZ_LOG_TRACE, 0, "%s", P_OR_NULL(name)); |
AzureIoTClient | 34:6be9c2058664 | 170 | LOG(AZ_LOG_TRACE, 0, "%s", P_OR_NULL((value_as_string = amqpvalue_to_string(value)))); |
AzureIoTClient | 6:641a9672db08 | 171 | if (value_as_string != NULL) |
AzureIoTClient | 6:641a9672db08 | 172 | { |
AzureIoTClient | 21:f9c433d8e6ca | 173 | free(value_as_string); |
AzureIoTClient | 6:641a9672db08 | 174 | } |
AzureIoTClient | 6:641a9672db08 | 175 | } |
AzureIoTClient | 9:c22db038556c | 176 | #endif |
Azure.IoT Build | 0:6ae2f7bca550 | 177 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 178 | |
AzureIoTClient | 34:6be9c2058664 | 179 | static SEND_ONE_MESSAGE_RESULT send_one_message(MESSAGE_SENDER_INSTANCE* message_sender, ASYNC_OPERATION_HANDLE pending_send, MESSAGE_HANDLE message) |
Azure.IoT Build | 0:6ae2f7bca550 | 180 | { |
AzureIoTClient | 6:641a9672db08 | 181 | SEND_ONE_MESSAGE_RESULT result; |
Azure.IoT Build | 0:6ae2f7bca550 | 182 | |
AzureIoTClient | 6:641a9672db08 | 183 | size_t encoded_size; |
AzureIoTClient | 6:641a9672db08 | 184 | size_t total_encoded_size = 0; |
AzureIoTClient | 6:641a9672db08 | 185 | MESSAGE_BODY_TYPE message_body_type; |
Azure.IoT Build | 0:6ae2f7bca550 | 186 | message_format message_format; |
Azure.IoT Build | 0:6ae2f7bca550 | 187 | |
AzureIoTClient | 6:641a9672db08 | 188 | if ((message_get_body_type(message, &message_body_type) != 0) || |
Azure.IoT Build | 0:6ae2f7bca550 | 189 | (message_get_message_format(message, &message_format) != 0)) |
AzureIoTClient | 6:641a9672db08 | 190 | { |
AzureIoTClient | 29:4a11413cf217 | 191 | LogError("Failure getting message body type and/or message format"); |
AzureIoTClient | 6:641a9672db08 | 192 | result = SEND_ONE_MESSAGE_ERROR; |
AzureIoTClient | 6:641a9672db08 | 193 | } |
AzureIoTClient | 6:641a9672db08 | 194 | else |
AzureIoTClient | 6:641a9672db08 | 195 | { |
AzureIoTClient | 6:641a9672db08 | 196 | // header |
AzureIoTClient | 31:cafc87baef79 | 197 | HEADER_HANDLE header = NULL; |
AzureIoTClient | 31:cafc87baef79 | 198 | AMQP_VALUE header_amqp_value = NULL; |
AzureIoTClient | 31:cafc87baef79 | 199 | PROPERTIES_HANDLE properties = NULL; |
AzureIoTClient | 31:cafc87baef79 | 200 | AMQP_VALUE properties_amqp_value = NULL; |
AzureIoTClient | 31:cafc87baef79 | 201 | AMQP_VALUE application_properties = NULL; |
AzureIoTClient | 31:cafc87baef79 | 202 | AMQP_VALUE application_properties_value = NULL; |
AzureIoTClient | 6:641a9672db08 | 203 | AMQP_VALUE body_amqp_value = NULL; |
AzureIoTClient | 6:641a9672db08 | 204 | size_t body_data_count = 0; |
AzureIoTClient | 13:9abd748f4e78 | 205 | AMQP_VALUE msg_annotations = NULL; |
AzureIoTClient | 34:6be9c2058664 | 206 | bool is_error = false; |
Azure.IoT Build | 0:6ae2f7bca550 | 207 | |
AzureIoTClient | 34:6be9c2058664 | 208 | // message header |
AzureIoTClient | 34:6be9c2058664 | 209 | if ((message_get_header(message, &header) == 0) && |
AzureIoTClient | 34:6be9c2058664 | 210 | (header != NULL)) |
AzureIoTClient | 6:641a9672db08 | 211 | { |
AzureIoTClient | 34:6be9c2058664 | 212 | header_amqp_value = amqpvalue_create_header(header); |
AzureIoTClient | 34:6be9c2058664 | 213 | if (header_amqp_value == NULL) |
AzureIoTClient | 34:6be9c2058664 | 214 | { |
AzureIoTClient | 34:6be9c2058664 | 215 | LogError("Cannot create header AMQP value"); |
AzureIoTClient | 34:6be9c2058664 | 216 | is_error = true; |
AzureIoTClient | 34:6be9c2058664 | 217 | } |
AzureIoTClient | 34:6be9c2058664 | 218 | else |
AzureIoTClient | 34:6be9c2058664 | 219 | { |
AzureIoTClient | 34:6be9c2058664 | 220 | if (amqpvalue_get_encoded_size(header_amqp_value, &encoded_size) != 0) |
AzureIoTClient | 34:6be9c2058664 | 221 | { |
AzureIoTClient | 34:6be9c2058664 | 222 | LogError("Cannot obtain header encoded size"); |
AzureIoTClient | 34:6be9c2058664 | 223 | is_error = true; |
AzureIoTClient | 34:6be9c2058664 | 224 | } |
AzureIoTClient | 34:6be9c2058664 | 225 | else |
AzureIoTClient | 34:6be9c2058664 | 226 | { |
AzureIoTClient | 34:6be9c2058664 | 227 | total_encoded_size += encoded_size; |
AzureIoTClient | 34:6be9c2058664 | 228 | } |
AzureIoTClient | 34:6be9c2058664 | 229 | } |
AzureIoTClient | 34:6be9c2058664 | 230 | |
AzureIoTClient | 6:641a9672db08 | 231 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 232 | |
AzureIoTClient | 13:9abd748f4e78 | 233 | // message annotations |
AzureIoTClient | 34:6be9c2058664 | 234 | if ((!is_error) && |
AzureIoTClient | 34:6be9c2058664 | 235 | (message_get_message_annotations(message, &msg_annotations) == 0) && |
AzureIoTClient | 13:9abd748f4e78 | 236 | (msg_annotations != NULL)) |
AzureIoTClient | 13:9abd748f4e78 | 237 | { |
AzureIoTClient | 34:6be9c2058664 | 238 | if (amqpvalue_get_encoded_size(msg_annotations, &encoded_size) != 0) |
AzureIoTClient | 34:6be9c2058664 | 239 | { |
AzureIoTClient | 34:6be9c2058664 | 240 | LogError("Cannot obtain message annotations encoded size"); |
AzureIoTClient | 34:6be9c2058664 | 241 | is_error = true; |
AzureIoTClient | 34:6be9c2058664 | 242 | } |
AzureIoTClient | 34:6be9c2058664 | 243 | else |
AzureIoTClient | 34:6be9c2058664 | 244 | { |
AzureIoTClient | 34:6be9c2058664 | 245 | total_encoded_size += encoded_size; |
AzureIoTClient | 34:6be9c2058664 | 246 | } |
AzureIoTClient | 13:9abd748f4e78 | 247 | } |
AzureIoTClient | 13:9abd748f4e78 | 248 | |
AzureIoTClient | 6:641a9672db08 | 249 | // properties |
AzureIoTClient | 34:6be9c2058664 | 250 | if ((!is_error) && |
AzureIoTClient | 34:6be9c2058664 | 251 | (message_get_properties(message, &properties) == 0) && |
AzureIoTClient | 34:6be9c2058664 | 252 | (properties != NULL)) |
AzureIoTClient | 6:641a9672db08 | 253 | { |
AzureIoTClient | 34:6be9c2058664 | 254 | properties_amqp_value = amqpvalue_create_properties(properties); |
AzureIoTClient | 34:6be9c2058664 | 255 | if (properties_amqp_value == NULL) |
AzureIoTClient | 34:6be9c2058664 | 256 | { |
AzureIoTClient | 34:6be9c2058664 | 257 | LogError("Cannot create message properties AMQP value"); |
AzureIoTClient | 34:6be9c2058664 | 258 | is_error = true; |
AzureIoTClient | 34:6be9c2058664 | 259 | } |
AzureIoTClient | 34:6be9c2058664 | 260 | else |
AzureIoTClient | 34:6be9c2058664 | 261 | { |
AzureIoTClient | 34:6be9c2058664 | 262 | if (amqpvalue_get_encoded_size(properties_amqp_value, &encoded_size) != 0) |
AzureIoTClient | 34:6be9c2058664 | 263 | { |
AzureIoTClient | 34:6be9c2058664 | 264 | LogError("Cannot obtain message properties encoded size"); |
AzureIoTClient | 34:6be9c2058664 | 265 | is_error = true; |
AzureIoTClient | 34:6be9c2058664 | 266 | } |
AzureIoTClient | 34:6be9c2058664 | 267 | else |
AzureIoTClient | 34:6be9c2058664 | 268 | { |
AzureIoTClient | 34:6be9c2058664 | 269 | total_encoded_size += encoded_size; |
AzureIoTClient | 34:6be9c2058664 | 270 | } |
AzureIoTClient | 34:6be9c2058664 | 271 | } |
AzureIoTClient | 6:641a9672db08 | 272 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 273 | |
AzureIoTClient | 6:641a9672db08 | 274 | // application properties |
AzureIoTClient | 34:6be9c2058664 | 275 | if ((!is_error) && |
AzureIoTClient | 34:6be9c2058664 | 276 | (message_get_application_properties(message, &application_properties) == 0) && |
AzureIoTClient | 34:6be9c2058664 | 277 | (application_properties != NULL)) |
AzureIoTClient | 6:641a9672db08 | 278 | { |
AzureIoTClient | 23:1111ee8bcba4 | 279 | application_properties_value = amqpvalue_create_application_properties(application_properties); |
AzureIoTClient | 34:6be9c2058664 | 280 | if (application_properties_value == NULL) |
AzureIoTClient | 34:6be9c2058664 | 281 | { |
AzureIoTClient | 34:6be9c2058664 | 282 | LogError("Cannot create application properties AMQP value"); |
AzureIoTClient | 34:6be9c2058664 | 283 | is_error = true; |
AzureIoTClient | 34:6be9c2058664 | 284 | } |
AzureIoTClient | 34:6be9c2058664 | 285 | else |
AzureIoTClient | 34:6be9c2058664 | 286 | { |
AzureIoTClient | 34:6be9c2058664 | 287 | if (amqpvalue_get_encoded_size(application_properties_value, &encoded_size) != 0) |
AzureIoTClient | 34:6be9c2058664 | 288 | { |
AzureIoTClient | 34:6be9c2058664 | 289 | LogError("Cannot obtain application properties encoded size"); |
AzureIoTClient | 34:6be9c2058664 | 290 | is_error = true; |
AzureIoTClient | 34:6be9c2058664 | 291 | } |
AzureIoTClient | 34:6be9c2058664 | 292 | else |
AzureIoTClient | 34:6be9c2058664 | 293 | { |
AzureIoTClient | 34:6be9c2058664 | 294 | total_encoded_size += encoded_size; |
AzureIoTClient | 34:6be9c2058664 | 295 | } |
AzureIoTClient | 34:6be9c2058664 | 296 | } |
AzureIoTClient | 34:6be9c2058664 | 297 | } |
AzureIoTClient | 34:6be9c2058664 | 298 | |
AzureIoTClient | 34:6be9c2058664 | 299 | if (is_error) |
AzureIoTClient | 34:6be9c2058664 | 300 | { |
AzureIoTClient | 34:6be9c2058664 | 301 | result = SEND_ONE_MESSAGE_ERROR; |
AzureIoTClient | 6:641a9672db08 | 302 | } |
AzureIoTClient | 23:1111ee8bcba4 | 303 | else |
AzureIoTClient | 23:1111ee8bcba4 | 304 | { |
AzureIoTClient | 34:6be9c2058664 | 305 | result = SEND_ONE_MESSAGE_OK; |
Azure.IoT Build | 0:6ae2f7bca550 | 306 | |
AzureIoTClient | 34:6be9c2058664 | 307 | // body - amqp data |
AzureIoTClient | 34:6be9c2058664 | 308 | switch (message_body_type) |
AzureIoTClient | 34:6be9c2058664 | 309 | { |
AzureIoTClient | 6:641a9672db08 | 310 | default: |
AzureIoTClient | 34:6be9c2058664 | 311 | LogError("Unknown body type"); |
AzureIoTClient | 6:641a9672db08 | 312 | result = SEND_ONE_MESSAGE_ERROR; |
AzureIoTClient | 6:641a9672db08 | 313 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 314 | |
AzureIoTClient | 6:641a9672db08 | 315 | case MESSAGE_BODY_TYPE_VALUE: |
AzureIoTClient | 6:641a9672db08 | 316 | { |
AzureIoTClient | 6:641a9672db08 | 317 | AMQP_VALUE message_body_amqp_value; |
AzureIoTClient | 24:2c59c2d43ebf | 318 | if (message_get_body_amqp_value_in_place(message, &message_body_amqp_value) != 0) |
AzureIoTClient | 6:641a9672db08 | 319 | { |
AzureIoTClient | 34:6be9c2058664 | 320 | LogError("Cannot obtain AMQP value from body"); |
AzureIoTClient | 6:641a9672db08 | 321 | result = SEND_ONE_MESSAGE_ERROR; |
AzureIoTClient | 6:641a9672db08 | 322 | } |
AzureIoTClient | 6:641a9672db08 | 323 | else |
AzureIoTClient | 6:641a9672db08 | 324 | { |
AzureIoTClient | 6:641a9672db08 | 325 | body_amqp_value = amqpvalue_create_amqp_value(message_body_amqp_value); |
AzureIoTClient | 34:6be9c2058664 | 326 | if (body_amqp_value == NULL) |
AzureIoTClient | 6:641a9672db08 | 327 | { |
AzureIoTClient | 34:6be9c2058664 | 328 | LogError("Cannot create body AMQP value"); |
AzureIoTClient | 6:641a9672db08 | 329 | result = SEND_ONE_MESSAGE_ERROR; |
AzureIoTClient | 6:641a9672db08 | 330 | } |
AzureIoTClient | 6:641a9672db08 | 331 | else |
AzureIoTClient | 6:641a9672db08 | 332 | { |
AzureIoTClient | 34:6be9c2058664 | 333 | if (amqpvalue_get_encoded_size(body_amqp_value, &encoded_size) != 0) |
AzureIoTClient | 34:6be9c2058664 | 334 | { |
AzureIoTClient | 34:6be9c2058664 | 335 | LogError("Cannot get body AMQP value encoded size"); |
AzureIoTClient | 34:6be9c2058664 | 336 | result = SEND_ONE_MESSAGE_ERROR; |
AzureIoTClient | 34:6be9c2058664 | 337 | } |
AzureIoTClient | 34:6be9c2058664 | 338 | else |
AzureIoTClient | 34:6be9c2058664 | 339 | { |
AzureIoTClient | 34:6be9c2058664 | 340 | total_encoded_size += encoded_size; |
AzureIoTClient | 34:6be9c2058664 | 341 | } |
AzureIoTClient | 6:641a9672db08 | 342 | } |
AzureIoTClient | 6:641a9672db08 | 343 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 344 | |
AzureIoTClient | 6:641a9672db08 | 345 | break; |
AzureIoTClient | 6:641a9672db08 | 346 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 347 | |
AzureIoTClient | 6:641a9672db08 | 348 | case MESSAGE_BODY_TYPE_DATA: |
AzureIoTClient | 6:641a9672db08 | 349 | { |
AzureIoTClient | 6:641a9672db08 | 350 | BINARY_DATA binary_data; |
Azure.IoT Build | 0:6ae2f7bca550 | 351 | size_t i; |
Azure.IoT Build | 0:6ae2f7bca550 | 352 | |
Azure.IoT Build | 0:6ae2f7bca550 | 353 | if (message_get_body_amqp_data_count(message, &body_data_count) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 354 | { |
AzureIoTClient | 34:6be9c2058664 | 355 | LogError("Cannot get body AMQP data count"); |
Azure.IoT Build | 0:6ae2f7bca550 | 356 | result = SEND_ONE_MESSAGE_ERROR; |
Azure.IoT Build | 0:6ae2f7bca550 | 357 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 358 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 359 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 360 | for (i = 0; i < body_data_count; i++) |
Azure.IoT Build | 0:6ae2f7bca550 | 361 | { |
AzureIoTClient | 24:2c59c2d43ebf | 362 | if (message_get_body_amqp_data_in_place(message, i, &binary_data) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 363 | { |
AzureIoTClient | 34:6be9c2058664 | 364 | LogError("Cannot get body AMQP data %u", (unsigned int)i); |
Azure.IoT Build | 0:6ae2f7bca550 | 365 | result = SEND_ONE_MESSAGE_ERROR; |
Azure.IoT Build | 0:6ae2f7bca550 | 366 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 367 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 368 | { |
AzureIoTClient | 28:add19eb7defa | 369 | AMQP_VALUE body_amqp_data; |
AzureIoTClient | 6:641a9672db08 | 370 | amqp_binary binary_value; |
AzureIoTClient | 6:641a9672db08 | 371 | binary_value.bytes = binary_data.bytes; |
AzureIoTClient | 6:641a9672db08 | 372 | binary_value.length = (uint32_t)binary_data.length; |
AzureIoTClient | 25:1101516ee67d | 373 | body_amqp_data = amqpvalue_create_data(binary_value); |
Azure.IoT Build | 0:6ae2f7bca550 | 374 | if (body_amqp_data == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 375 | { |
AzureIoTClient | 34:6be9c2058664 | 376 | LogError("Cannot create body AMQP data"); |
Azure.IoT Build | 0:6ae2f7bca550 | 377 | result = SEND_ONE_MESSAGE_ERROR; |
Azure.IoT Build | 0:6ae2f7bca550 | 378 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 379 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 380 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 381 | if (amqpvalue_get_encoded_size(body_amqp_data, &encoded_size) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 382 | { |
AzureIoTClient | 34:6be9c2058664 | 383 | LogError("Cannot get body AMQP data encoded size"); |
Azure.IoT Build | 0:6ae2f7bca550 | 384 | result = SEND_ONE_MESSAGE_ERROR; |
Azure.IoT Build | 0:6ae2f7bca550 | 385 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 386 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 387 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 388 | total_encoded_size += encoded_size; |
Azure.IoT Build | 0:6ae2f7bca550 | 389 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 390 | |
Azure.IoT Build | 0:6ae2f7bca550 | 391 | amqpvalue_destroy(body_amqp_data); |
Azure.IoT Build | 0:6ae2f7bca550 | 392 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 393 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 394 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 395 | } |
AzureIoTClient | 6:641a9672db08 | 396 | break; |
AzureIoTClient | 6:641a9672db08 | 397 | } |
AzureIoTClient | 13:9abd748f4e78 | 398 | } |
AzureIoTClient | 13:9abd748f4e78 | 399 | |
AzureIoTClient | 34:6be9c2058664 | 400 | if (result == 0) |
AzureIoTClient | 6:641a9672db08 | 401 | { |
AzureIoTClient | 34:6be9c2058664 | 402 | void* data_bytes = malloc(total_encoded_size); |
AzureIoTClient | 34:6be9c2058664 | 403 | PAYLOAD payload; |
AzureIoTClient | 34:6be9c2058664 | 404 | payload.bytes = (const unsigned char*)data_bytes; |
AzureIoTClient | 34:6be9c2058664 | 405 | payload.length = 0; |
AzureIoTClient | 34:6be9c2058664 | 406 | result = SEND_ONE_MESSAGE_OK; |
AzureIoTClient | 34:6be9c2058664 | 407 | |
AzureIoTClient | 34:6be9c2058664 | 408 | if (header != NULL) |
AzureIoTClient | 6:641a9672db08 | 409 | { |
AzureIoTClient | 34:6be9c2058664 | 410 | if (amqpvalue_encode(header_amqp_value, encode_bytes, &payload) != 0) |
AzureIoTClient | 34:6be9c2058664 | 411 | { |
AzureIoTClient | 34:6be9c2058664 | 412 | LogError("Cannot encode header value"); |
AzureIoTClient | 34:6be9c2058664 | 413 | result = SEND_ONE_MESSAGE_ERROR; |
AzureIoTClient | 34:6be9c2058664 | 414 | } |
AzureIoTClient | 34:6be9c2058664 | 415 | |
AzureIoTClient | 34:6be9c2058664 | 416 | log_message_chunk(message_sender, "Header:", header_amqp_value); |
AzureIoTClient | 6:641a9672db08 | 417 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 418 | |
AzureIoTClient | 34:6be9c2058664 | 419 | if ((result == SEND_ONE_MESSAGE_OK) && (msg_annotations != NULL)) |
AzureIoTClient | 34:6be9c2058664 | 420 | { |
AzureIoTClient | 34:6be9c2058664 | 421 | if (amqpvalue_encode(msg_annotations, encode_bytes, &payload) != 0) |
AzureIoTClient | 34:6be9c2058664 | 422 | { |
AzureIoTClient | 34:6be9c2058664 | 423 | LogError("Cannot encode message annotations value"); |
AzureIoTClient | 34:6be9c2058664 | 424 | result = SEND_ONE_MESSAGE_ERROR; |
AzureIoTClient | 34:6be9c2058664 | 425 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 426 | |
AzureIoTClient | 34:6be9c2058664 | 427 | log_message_chunk(message_sender, "Message Annotations:", msg_annotations); |
AzureIoTClient | 6:641a9672db08 | 428 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 429 | |
AzureIoTClient | 34:6be9c2058664 | 430 | if ((result == SEND_ONE_MESSAGE_OK) && (properties != NULL)) |
AzureIoTClient | 6:641a9672db08 | 431 | { |
AzureIoTClient | 34:6be9c2058664 | 432 | if (amqpvalue_encode(properties_amqp_value, encode_bytes, &payload) != 0) |
AzureIoTClient | 6:641a9672db08 | 433 | { |
AzureIoTClient | 34:6be9c2058664 | 434 | LogError("Cannot encode message properties value"); |
AzureIoTClient | 6:641a9672db08 | 435 | result = SEND_ONE_MESSAGE_ERROR; |
AzureIoTClient | 6:641a9672db08 | 436 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 437 | |
AzureIoTClient | 34:6be9c2058664 | 438 | log_message_chunk(message_sender, "Properties:", properties_amqp_value); |
AzureIoTClient | 6:641a9672db08 | 439 | } |
AzureIoTClient | 34:6be9c2058664 | 440 | |
AzureIoTClient | 34:6be9c2058664 | 441 | if ((result == SEND_ONE_MESSAGE_OK) && (application_properties != NULL)) |
AzureIoTClient | 6:641a9672db08 | 442 | { |
AzureIoTClient | 34:6be9c2058664 | 443 | if (amqpvalue_encode(application_properties_value, encode_bytes, &payload) != 0) |
AzureIoTClient | 34:6be9c2058664 | 444 | { |
AzureIoTClient | 34:6be9c2058664 | 445 | LogError("Cannot encode application properties value"); |
AzureIoTClient | 34:6be9c2058664 | 446 | result = SEND_ONE_MESSAGE_ERROR; |
AzureIoTClient | 34:6be9c2058664 | 447 | } |
AzureIoTClient | 34:6be9c2058664 | 448 | |
AzureIoTClient | 34:6be9c2058664 | 449 | log_message_chunk(message_sender, "Application properties:", application_properties_value); |
AzureIoTClient | 34:6be9c2058664 | 450 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 451 | |
AzureIoTClient | 34:6be9c2058664 | 452 | if (result == SEND_ONE_MESSAGE_OK) |
AzureIoTClient | 34:6be9c2058664 | 453 | { |
AzureIoTClient | 34:6be9c2058664 | 454 | switch (message_body_type) |
Azure.IoT Build | 0:6ae2f7bca550 | 455 | { |
AzureIoTClient | 34:6be9c2058664 | 456 | default: |
AzureIoTClient | 34:6be9c2058664 | 457 | LogError("Unknown message type"); |
AzureIoTClient | 34:6be9c2058664 | 458 | result = SEND_ONE_MESSAGE_ERROR; |
AzureIoTClient | 34:6be9c2058664 | 459 | break; |
AzureIoTClient | 34:6be9c2058664 | 460 | |
AzureIoTClient | 34:6be9c2058664 | 461 | case MESSAGE_BODY_TYPE_VALUE: |
AzureIoTClient | 34:6be9c2058664 | 462 | { |
AzureIoTClient | 34:6be9c2058664 | 463 | if (amqpvalue_encode(body_amqp_value, encode_bytes, &payload) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 464 | { |
AzureIoTClient | 34:6be9c2058664 | 465 | LogError("Cannot encode body AMQP value"); |
Azure.IoT Build | 0:6ae2f7bca550 | 466 | result = SEND_ONE_MESSAGE_ERROR; |
Azure.IoT Build | 0:6ae2f7bca550 | 467 | } |
AzureIoTClient | 34:6be9c2058664 | 468 | |
AzureIoTClient | 34:6be9c2058664 | 469 | log_message_chunk(message_sender, "Body - amqp value:", body_amqp_value); |
AzureIoTClient | 34:6be9c2058664 | 470 | break; |
AzureIoTClient | 34:6be9c2058664 | 471 | } |
AzureIoTClient | 34:6be9c2058664 | 472 | case MESSAGE_BODY_TYPE_DATA: |
AzureIoTClient | 34:6be9c2058664 | 473 | { |
AzureIoTClient | 34:6be9c2058664 | 474 | BINARY_DATA binary_data; |
AzureIoTClient | 34:6be9c2058664 | 475 | size_t i; |
AzureIoTClient | 34:6be9c2058664 | 476 | |
AzureIoTClient | 34:6be9c2058664 | 477 | for (i = 0; i < body_data_count; i++) |
Azure.IoT Build | 0:6ae2f7bca550 | 478 | { |
AzureIoTClient | 34:6be9c2058664 | 479 | if (message_get_body_amqp_data_in_place(message, i, &binary_data) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 480 | { |
AzureIoTClient | 34:6be9c2058664 | 481 | LogError("Cannot get AMQP data %u", (unsigned int)i); |
Azure.IoT Build | 0:6ae2f7bca550 | 482 | result = SEND_ONE_MESSAGE_ERROR; |
Azure.IoT Build | 0:6ae2f7bca550 | 483 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 484 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 485 | { |
AzureIoTClient | 34:6be9c2058664 | 486 | AMQP_VALUE body_amqp_data; |
AzureIoTClient | 34:6be9c2058664 | 487 | amqp_binary binary_value; |
AzureIoTClient | 34:6be9c2058664 | 488 | binary_value.bytes = binary_data.bytes; |
AzureIoTClient | 34:6be9c2058664 | 489 | binary_value.length = (uint32_t)binary_data.length; |
AzureIoTClient | 34:6be9c2058664 | 490 | body_amqp_data = amqpvalue_create_data(binary_value); |
AzureIoTClient | 34:6be9c2058664 | 491 | if (body_amqp_data == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 492 | { |
AzureIoTClient | 34:6be9c2058664 | 493 | LogError("Cannot create body AMQP data %u", (unsigned int)i); |
Azure.IoT Build | 0:6ae2f7bca550 | 494 | result = SEND_ONE_MESSAGE_ERROR; |
Azure.IoT Build | 0:6ae2f7bca550 | 495 | } |
AzureIoTClient | 34:6be9c2058664 | 496 | else |
AzureIoTClient | 34:6be9c2058664 | 497 | { |
AzureIoTClient | 34:6be9c2058664 | 498 | if (amqpvalue_encode(body_amqp_data, encode_bytes, &payload) != 0) |
AzureIoTClient | 34:6be9c2058664 | 499 | { |
AzureIoTClient | 34:6be9c2058664 | 500 | LogError("Cannot encode body AMQP data %u", (unsigned int)i); |
AzureIoTClient | 34:6be9c2058664 | 501 | result = SEND_ONE_MESSAGE_ERROR; |
AzureIoTClient | 34:6be9c2058664 | 502 | break; |
AzureIoTClient | 34:6be9c2058664 | 503 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 504 | |
AzureIoTClient | 34:6be9c2058664 | 505 | amqpvalue_destroy(body_amqp_data); |
AzureIoTClient | 34:6be9c2058664 | 506 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 507 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 508 | } |
AzureIoTClient | 34:6be9c2058664 | 509 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 510 | } |
AzureIoTClient | 34:6be9c2058664 | 511 | } |
AzureIoTClient | 6:641a9672db08 | 512 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 513 | |
AzureIoTClient | 34:6be9c2058664 | 514 | if (result == SEND_ONE_MESSAGE_OK) |
AzureIoTClient | 6:641a9672db08 | 515 | { |
AzureIoTClient | 34:6be9c2058664 | 516 | MESSAGE_WITH_CALLBACK* message_with_callback = GET_ASYNC_OPERATION_CONTEXT(MESSAGE_WITH_CALLBACK, pending_send); |
AzureIoTClient | 34:6be9c2058664 | 517 | message_with_callback->message_send_state = MESSAGE_SEND_STATE_PENDING; |
AzureIoTClient | 34:6be9c2058664 | 518 | switch (link_transfer_async(message_sender->link, message_format, &payload, 1, on_delivery_settled, pending_send, message_with_callback->timeout)) |
AzureIoTClient | 34:6be9c2058664 | 519 | { |
AzureIoTClient | 34:6be9c2058664 | 520 | default: |
AzureIoTClient | 34:6be9c2058664 | 521 | case LINK_TRANSFER_ERROR: |
AzureIoTClient | 34:6be9c2058664 | 522 | LogError("Error in link transfer"); |
AzureIoTClient | 34:6be9c2058664 | 523 | result = SEND_ONE_MESSAGE_ERROR; |
AzureIoTClient | 34:6be9c2058664 | 524 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 525 | |
AzureIoTClient | 34:6be9c2058664 | 526 | case LINK_TRANSFER_BUSY: |
AzureIoTClient | 34:6be9c2058664 | 527 | message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT; |
AzureIoTClient | 34:6be9c2058664 | 528 | result = SEND_ONE_MESSAGE_BUSY; |
AzureIoTClient | 34:6be9c2058664 | 529 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 530 | |
AzureIoTClient | 34:6be9c2058664 | 531 | case LINK_TRANSFER_OK: |
AzureIoTClient | 34:6be9c2058664 | 532 | result = SEND_ONE_MESSAGE_OK; |
AzureIoTClient | 34:6be9c2058664 | 533 | break; |
AzureIoTClient | 34:6be9c2058664 | 534 | } |
AzureIoTClient | 6:641a9672db08 | 535 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 536 | |
AzureIoTClient | 34:6be9c2058664 | 537 | free(data_bytes); |
Azure.IoT Build | 0:6ae2f7bca550 | 538 | |
AzureIoTClient | 34:6be9c2058664 | 539 | if (body_amqp_value != NULL) |
AzureIoTClient | 34:6be9c2058664 | 540 | { |
AzureIoTClient | 34:6be9c2058664 | 541 | amqpvalue_destroy(body_amqp_value); |
AzureIoTClient | 34:6be9c2058664 | 542 | } |
AzureIoTClient | 6:641a9672db08 | 543 | } |
AzureIoTClient | 20:206846c14c80 | 544 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 545 | |
AzureIoTClient | 31:cafc87baef79 | 546 | if (header != NULL) |
AzureIoTClient | 31:cafc87baef79 | 547 | { |
AzureIoTClient | 31:cafc87baef79 | 548 | header_destroy(header); |
AzureIoTClient | 31:cafc87baef79 | 549 | } |
AzureIoTClient | 31:cafc87baef79 | 550 | |
AzureIoTClient | 31:cafc87baef79 | 551 | if (header_amqp_value != NULL) |
AzureIoTClient | 31:cafc87baef79 | 552 | { |
AzureIoTClient | 31:cafc87baef79 | 553 | amqpvalue_destroy(header_amqp_value); |
AzureIoTClient | 31:cafc87baef79 | 554 | } |
AzureIoTClient | 31:cafc87baef79 | 555 | |
AzureIoTClient | 31:cafc87baef79 | 556 | if (msg_annotations != NULL) |
AzureIoTClient | 31:cafc87baef79 | 557 | { |
AzureIoTClient | 31:cafc87baef79 | 558 | annotations_destroy(msg_annotations); |
AzureIoTClient | 31:cafc87baef79 | 559 | } |
AzureIoTClient | 31:cafc87baef79 | 560 | |
AzureIoTClient | 23:1111ee8bcba4 | 561 | if (application_properties != NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 562 | { |
AzureIoTClient | 23:1111ee8bcba4 | 563 | amqpvalue_destroy(application_properties); |
AzureIoTClient | 23:1111ee8bcba4 | 564 | } |
AzureIoTClient | 34:6be9c2058664 | 565 | |
AzureIoTClient | 23:1111ee8bcba4 | 566 | if (application_properties_value != NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 567 | { |
AzureIoTClient | 23:1111ee8bcba4 | 568 | amqpvalue_destroy(application_properties_value); |
AzureIoTClient | 23:1111ee8bcba4 | 569 | } |
AzureIoTClient | 34:6be9c2058664 | 570 | |
AzureIoTClient | 23:1111ee8bcba4 | 571 | if (properties_amqp_value != NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 572 | { |
AzureIoTClient | 23:1111ee8bcba4 | 573 | amqpvalue_destroy(properties_amqp_value); |
AzureIoTClient | 23:1111ee8bcba4 | 574 | } |
AzureIoTClient | 34:6be9c2058664 | 575 | |
AzureIoTClient | 23:1111ee8bcba4 | 576 | if (properties != NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 577 | { |
AzureIoTClient | 23:1111ee8bcba4 | 578 | properties_destroy(properties); |
AzureIoTClient | 23:1111ee8bcba4 | 579 | } |
AzureIoTClient | 6:641a9672db08 | 580 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 581 | |
AzureIoTClient | 6:641a9672db08 | 582 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 583 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 584 | |
AzureIoTClient | 34:6be9c2058664 | 585 | static void send_all_pending_messages(MESSAGE_SENDER_HANDLE message_sender) |
Azure.IoT Build | 0:6ae2f7bca550 | 586 | { |
AzureIoTClient | 6:641a9672db08 | 587 | size_t i; |
Azure.IoT Build | 0:6ae2f7bca550 | 588 | |
AzureIoTClient | 34:6be9c2058664 | 589 | for (i = 0; i < message_sender->message_count; i++) |
AzureIoTClient | 6:641a9672db08 | 590 | { |
AzureIoTClient | 34:6be9c2058664 | 591 | MESSAGE_WITH_CALLBACK* message_with_callback = GET_ASYNC_OPERATION_CONTEXT(MESSAGE_WITH_CALLBACK, message_sender->messages[i]); |
AzureIoTClient | 34:6be9c2058664 | 592 | if (message_with_callback->message_send_state == MESSAGE_SEND_STATE_NOT_SENT) |
AzureIoTClient | 6:641a9672db08 | 593 | { |
AzureIoTClient | 34:6be9c2058664 | 594 | switch (send_one_message(message_sender, message_sender->messages[i], message_with_callback->message)) |
AzureIoTClient | 6:641a9672db08 | 595 | { |
AzureIoTClient | 6:641a9672db08 | 596 | default: |
AzureIoTClient | 34:6be9c2058664 | 597 | LogError("Invalid send one message result"); |
AzureIoTClient | 34:6be9c2058664 | 598 | break; |
AzureIoTClient | 34:6be9c2058664 | 599 | |
AzureIoTClient | 6:641a9672db08 | 600 | case SEND_ONE_MESSAGE_ERROR: |
AzureIoTClient | 6:641a9672db08 | 601 | { |
AzureIoTClient | 34:6be9c2058664 | 602 | ON_MESSAGE_SEND_COMPLETE on_message_send_complete = message_with_callback->on_message_send_complete; |
AzureIoTClient | 34:6be9c2058664 | 603 | void* context = message_with_callback->context; |
AzureIoTClient | 34:6be9c2058664 | 604 | remove_pending_message_by_index(message_sender, i); |
Azure.IoT Build | 0:6ae2f7bca550 | 605 | |
AzureIoTClient | 23:1111ee8bcba4 | 606 | if (on_message_send_complete != NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 607 | { |
AzureIoTClient | 23:1111ee8bcba4 | 608 | on_message_send_complete(context, MESSAGE_SEND_ERROR); |
AzureIoTClient | 23:1111ee8bcba4 | 609 | } |
AzureIoTClient | 18:a922a4a30a82 | 610 | |
AzureIoTClient | 34:6be9c2058664 | 611 | i = message_sender->message_count; |
AzureIoTClient | 6:641a9672db08 | 612 | break; |
AzureIoTClient | 6:641a9672db08 | 613 | } |
AzureIoTClient | 6:641a9672db08 | 614 | case SEND_ONE_MESSAGE_BUSY: |
AzureIoTClient | 34:6be9c2058664 | 615 | i = message_sender->message_count + 1; |
AzureIoTClient | 6:641a9672db08 | 616 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 617 | |
AzureIoTClient | 6:641a9672db08 | 618 | case SEND_ONE_MESSAGE_OK: |
AzureIoTClient | 6:641a9672db08 | 619 | break; |
AzureIoTClient | 6:641a9672db08 | 620 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 621 | |
AzureIoTClient | 6:641a9672db08 | 622 | i--; |
AzureIoTClient | 6:641a9672db08 | 623 | } |
AzureIoTClient | 6:641a9672db08 | 624 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 625 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 626 | |
AzureIoTClient | 34:6be9c2058664 | 627 | static void set_message_sender_state(MESSAGE_SENDER_INSTANCE* message_sender, MESSAGE_SENDER_STATE new_state) |
Azure.IoT Build | 0:6ae2f7bca550 | 628 | { |
AzureIoTClient | 34:6be9c2058664 | 629 | MESSAGE_SENDER_STATE previous_state = message_sender->message_sender_state; |
AzureIoTClient | 34:6be9c2058664 | 630 | message_sender->message_sender_state = new_state; |
AzureIoTClient | 34:6be9c2058664 | 631 | if (message_sender->on_message_sender_state_changed != NULL) |
AzureIoTClient | 6:641a9672db08 | 632 | { |
AzureIoTClient | 34:6be9c2058664 | 633 | message_sender->on_message_sender_state_changed(message_sender->on_message_sender_state_changed_context, new_state, previous_state); |
AzureIoTClient | 6:641a9672db08 | 634 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 635 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 636 | |
AzureIoTClient | 34:6be9c2058664 | 637 | static void indicate_all_messages_as_error(MESSAGE_SENDER_INSTANCE* message_sender) |
AzureIoTClient | 2:64b4feb67cd3 | 638 | { |
AzureIoTClient | 2:64b4feb67cd3 | 639 | size_t i; |
AzureIoTClient | 2:64b4feb67cd3 | 640 | |
AzureIoTClient | 34:6be9c2058664 | 641 | for (i = 0; i < message_sender->message_count; i++) |
AzureIoTClient | 2:64b4feb67cd3 | 642 | { |
AzureIoTClient | 34:6be9c2058664 | 643 | MESSAGE_WITH_CALLBACK* message_with_callback = GET_ASYNC_OPERATION_CONTEXT(MESSAGE_WITH_CALLBACK, message_sender->messages[i]); |
AzureIoTClient | 34:6be9c2058664 | 644 | if (message_with_callback->on_message_send_complete != NULL) |
AzureIoTClient | 2:64b4feb67cd3 | 645 | { |
AzureIoTClient | 34:6be9c2058664 | 646 | message_with_callback->on_message_send_complete(message_with_callback->context, MESSAGE_SEND_ERROR); |
AzureIoTClient | 2:64b4feb67cd3 | 647 | } |
AzureIoTClient | 2:64b4feb67cd3 | 648 | |
AzureIoTClient | 34:6be9c2058664 | 649 | if (message_with_callback->message != NULL) |
AzureIoTClient | 33:08b53020ff0d | 650 | { |
AzureIoTClient | 34:6be9c2058664 | 651 | message_destroy(message_with_callback->message); |
AzureIoTClient | 33:08b53020ff0d | 652 | } |
AzureIoTClient | 34:6be9c2058664 | 653 | async_operation_destroy(message_sender->messages[i]); |
AzureIoTClient | 2:64b4feb67cd3 | 654 | } |
AzureIoTClient | 2:64b4feb67cd3 | 655 | |
AzureIoTClient | 34:6be9c2058664 | 656 | if (message_sender->messages != NULL) |
AzureIoTClient | 2:64b4feb67cd3 | 657 | { |
AzureIoTClient | 34:6be9c2058664 | 658 | message_sender->message_count = 0; |
AzureIoTClient | 2:64b4feb67cd3 | 659 | |
AzureIoTClient | 34:6be9c2058664 | 660 | free(message_sender->messages); |
AzureIoTClient | 34:6be9c2058664 | 661 | message_sender->messages = NULL; |
AzureIoTClient | 2:64b4feb67cd3 | 662 | } |
AzureIoTClient | 2:64b4feb67cd3 | 663 | } |
AzureIoTClient | 2:64b4feb67cd3 | 664 | |
Azure.IoT Build | 0:6ae2f7bca550 | 665 | static void on_link_state_changed(void* context, LINK_STATE new_link_state, LINK_STATE previous_link_state) |
Azure.IoT Build | 0:6ae2f7bca550 | 666 | { |
AzureIoTClient | 34:6be9c2058664 | 667 | MESSAGE_SENDER_INSTANCE* message_sender = (MESSAGE_SENDER_INSTANCE*)context; |
AzureIoTClient | 6:641a9672db08 | 668 | (void)previous_link_state; |
Azure.IoT Build | 0:6ae2f7bca550 | 669 | |
AzureIoTClient | 6:641a9672db08 | 670 | switch (new_link_state) |
AzureIoTClient | 6:641a9672db08 | 671 | { |
AzureIoTClient | 17:923575db8b2d | 672 | default: |
AzureIoTClient | 17:923575db8b2d | 673 | break; |
AzureIoTClient | 17:923575db8b2d | 674 | |
AzureIoTClient | 6:641a9672db08 | 675 | case LINK_STATE_ATTACHED: |
AzureIoTClient | 34:6be9c2058664 | 676 | if (message_sender->message_sender_state == MESSAGE_SENDER_STATE_OPENING) |
AzureIoTClient | 6:641a9672db08 | 677 | { |
AzureIoTClient | 34:6be9c2058664 | 678 | set_message_sender_state(message_sender, MESSAGE_SENDER_STATE_OPEN); |
AzureIoTClient | 6:641a9672db08 | 679 | } |
AzureIoTClient | 6:641a9672db08 | 680 | break; |
AzureIoTClient | 6:641a9672db08 | 681 | case LINK_STATE_DETACHED: |
AzureIoTClient | 34:6be9c2058664 | 682 | if ((message_sender->message_sender_state == MESSAGE_SENDER_STATE_OPEN) || |
AzureIoTClient | 34:6be9c2058664 | 683 | (message_sender->message_sender_state == MESSAGE_SENDER_STATE_CLOSING)) |
Azure.IoT Build | 0:6ae2f7bca550 | 684 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 685 | /* User initiated transition, we should be good */ |
AzureIoTClient | 34:6be9c2058664 | 686 | set_message_sender_state(message_sender, MESSAGE_SENDER_STATE_IDLE); |
AzureIoTClient | 34:6be9c2058664 | 687 | indicate_all_messages_as_error(message_sender); |
Azure.IoT Build | 0:6ae2f7bca550 | 688 | } |
AzureIoTClient | 34:6be9c2058664 | 689 | else if (message_sender->message_sender_state != MESSAGE_SENDER_STATE_IDLE) |
Azure.IoT Build | 0:6ae2f7bca550 | 690 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 691 | /* Any other transition must be an error */ |
AzureIoTClient | 34:6be9c2058664 | 692 | set_message_sender_state(message_sender, MESSAGE_SENDER_STATE_ERROR); |
Azure.IoT Build | 0:6ae2f7bca550 | 693 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 694 | break; |
AzureIoTClient | 6:641a9672db08 | 695 | case LINK_STATE_ERROR: |
AzureIoTClient | 34:6be9c2058664 | 696 | if (message_sender->message_sender_state != MESSAGE_SENDER_STATE_ERROR) |
AzureIoTClient | 6:641a9672db08 | 697 | { |
AzureIoTClient | 34:6be9c2058664 | 698 | set_message_sender_state(message_sender, MESSAGE_SENDER_STATE_ERROR); |
AzureIoTClient | 34:6be9c2058664 | 699 | indicate_all_messages_as_error(message_sender); |
AzureIoTClient | 6:641a9672db08 | 700 | } |
AzureIoTClient | 6:641a9672db08 | 701 | break; |
AzureIoTClient | 6:641a9672db08 | 702 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 703 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 704 | |
Azure.IoT Build | 0:6ae2f7bca550 | 705 | static void on_link_flow_on(void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 706 | { |
AzureIoTClient | 34:6be9c2058664 | 707 | MESSAGE_SENDER_HANDLE message_sender = (MESSAGE_SENDER_INSTANCE*)context; |
AzureIoTClient | 34:6be9c2058664 | 708 | send_all_pending_messages(message_sender); |
Azure.IoT Build | 0:6ae2f7bca550 | 709 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 710 | |
Azure.IoT Build | 5:ae49385aff34 | 711 | 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 | 712 | { |
AzureIoTClient | 34:6be9c2058664 | 713 | MESSAGE_SENDER_INSTANCE* message_sender = (MESSAGE_SENDER_INSTANCE*)malloc(sizeof(MESSAGE_SENDER_INSTANCE)); |
AzureIoTClient | 34:6be9c2058664 | 714 | if (message_sender == NULL) |
AzureIoTClient | 34:6be9c2058664 | 715 | { |
AzureIoTClient | 34:6be9c2058664 | 716 | LogError("Failed allocating message sender"); |
AzureIoTClient | 34:6be9c2058664 | 717 | } |
AzureIoTClient | 34:6be9c2058664 | 718 | else |
AzureIoTClient | 6:641a9672db08 | 719 | { |
AzureIoTClient | 34:6be9c2058664 | 720 | message_sender->messages = NULL; |
AzureIoTClient | 34:6be9c2058664 | 721 | message_sender->message_count = 0; |
AzureIoTClient | 34:6be9c2058664 | 722 | message_sender->link = link; |
AzureIoTClient | 34:6be9c2058664 | 723 | message_sender->on_message_sender_state_changed = on_message_sender_state_changed; |
AzureIoTClient | 34:6be9c2058664 | 724 | message_sender->on_message_sender_state_changed_context = context; |
AzureIoTClient | 34:6be9c2058664 | 725 | message_sender->message_sender_state = MESSAGE_SENDER_STATE_IDLE; |
AzureIoTClient | 34:6be9c2058664 | 726 | message_sender->is_trace_on = 0; |
AzureIoTClient | 6:641a9672db08 | 727 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 728 | |
AzureIoTClient | 34:6be9c2058664 | 729 | return message_sender; |
Azure.IoT Build | 0:6ae2f7bca550 | 730 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 731 | |
Azure.IoT Build | 0:6ae2f7bca550 | 732 | void messagesender_destroy(MESSAGE_SENDER_HANDLE message_sender) |
Azure.IoT Build | 0:6ae2f7bca550 | 733 | { |
AzureIoTClient | 34:6be9c2058664 | 734 | if (message_sender == NULL) |
AzureIoTClient | 6:641a9672db08 | 735 | { |
AzureIoTClient | 34:6be9c2058664 | 736 | LogError("NULL message_sender"); |
AzureIoTClient | 34:6be9c2058664 | 737 | } |
AzureIoTClient | 34:6be9c2058664 | 738 | else |
AzureIoTClient | 34:6be9c2058664 | 739 | { |
AzureIoTClient | 34:6be9c2058664 | 740 | (void)messagesender_close(message_sender); |
AzureIoTClient | 34:6be9c2058664 | 741 | indicate_all_messages_as_error(message_sender); |
Azure.IoT Build | 0:6ae2f7bca550 | 742 | |
AzureIoTClient | 21:f9c433d8e6ca | 743 | free(message_sender); |
AzureIoTClient | 6:641a9672db08 | 744 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 745 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 746 | |
Azure.IoT Build | 0:6ae2f7bca550 | 747 | int messagesender_open(MESSAGE_SENDER_HANDLE message_sender) |
Azure.IoT Build | 0:6ae2f7bca550 | 748 | { |
AzureIoTClient | 6:641a9672db08 | 749 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 750 | |
AzureIoTClient | 6:641a9672db08 | 751 | if (message_sender == NULL) |
AzureIoTClient | 6:641a9672db08 | 752 | { |
AzureIoTClient | 34:6be9c2058664 | 753 | LogError("NULL message_sender"); |
AzureIoTClient | 19:000ab4e6a2c1 | 754 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 755 | } |
AzureIoTClient | 6:641a9672db08 | 756 | else |
AzureIoTClient | 6:641a9672db08 | 757 | { |
AzureIoTClient | 34:6be9c2058664 | 758 | if (message_sender->message_sender_state == MESSAGE_SENDER_STATE_IDLE) |
AzureIoTClient | 6:641a9672db08 | 759 | { |
AzureIoTClient | 34:6be9c2058664 | 760 | set_message_sender_state(message_sender, MESSAGE_SENDER_STATE_OPENING); |
AzureIoTClient | 34:6be9c2058664 | 761 | if (link_attach(message_sender->link, NULL, on_link_state_changed, on_link_flow_on, message_sender) != 0) |
AzureIoTClient | 6:641a9672db08 | 762 | { |
AzureIoTClient | 34:6be9c2058664 | 763 | LogError("attach link failed"); |
AzureIoTClient | 19:000ab4e6a2c1 | 764 | result = __FAILURE__; |
AzureIoTClient | 34:6be9c2058664 | 765 | set_message_sender_state(message_sender, MESSAGE_SENDER_STATE_ERROR); |
AzureIoTClient | 6:641a9672db08 | 766 | } |
AzureIoTClient | 6:641a9672db08 | 767 | else |
AzureIoTClient | 6:641a9672db08 | 768 | { |
AzureIoTClient | 6:641a9672db08 | 769 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 770 | } |
AzureIoTClient | 6:641a9672db08 | 771 | } |
AzureIoTClient | 6:641a9672db08 | 772 | else |
AzureIoTClient | 6:641a9672db08 | 773 | { |
AzureIoTClient | 6:641a9672db08 | 774 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 775 | } |
AzureIoTClient | 6:641a9672db08 | 776 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 777 | |
AzureIoTClient | 6:641a9672db08 | 778 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 779 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 780 | |
Azure.IoT Build | 0:6ae2f7bca550 | 781 | int messagesender_close(MESSAGE_SENDER_HANDLE message_sender) |
Azure.IoT Build | 0:6ae2f7bca550 | 782 | { |
AzureIoTClient | 6:641a9672db08 | 783 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 784 | |
AzureIoTClient | 6:641a9672db08 | 785 | if (message_sender == NULL) |
AzureIoTClient | 6:641a9672db08 | 786 | { |
AzureIoTClient | 34:6be9c2058664 | 787 | LogError("NULL message_sender"); |
AzureIoTClient | 19:000ab4e6a2c1 | 788 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 789 | } |
AzureIoTClient | 6:641a9672db08 | 790 | else |
AzureIoTClient | 6:641a9672db08 | 791 | { |
AzureIoTClient | 34:6be9c2058664 | 792 | if ((message_sender->message_sender_state == MESSAGE_SENDER_STATE_OPENING) || |
AzureIoTClient | 34:6be9c2058664 | 793 | (message_sender->message_sender_state == MESSAGE_SENDER_STATE_OPEN)) |
AzureIoTClient | 6:641a9672db08 | 794 | { |
AzureIoTClient | 34:6be9c2058664 | 795 | set_message_sender_state(message_sender, MESSAGE_SENDER_STATE_CLOSING); |
AzureIoTClient | 34:6be9c2058664 | 796 | if (link_detach(message_sender->link, true) != 0) |
AzureIoTClient | 6:641a9672db08 | 797 | { |
AzureIoTClient | 34:6be9c2058664 | 798 | LogError("Detaching link failed"); |
AzureIoTClient | 19:000ab4e6a2c1 | 799 | result = __FAILURE__; |
AzureIoTClient | 34:6be9c2058664 | 800 | set_message_sender_state(message_sender, MESSAGE_SENDER_STATE_ERROR); |
AzureIoTClient | 6:641a9672db08 | 801 | } |
AzureIoTClient | 6:641a9672db08 | 802 | else |
AzureIoTClient | 6:641a9672db08 | 803 | { |
AzureIoTClient | 6:641a9672db08 | 804 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 805 | } |
AzureIoTClient | 6:641a9672db08 | 806 | } |
AzureIoTClient | 6:641a9672db08 | 807 | else |
AzureIoTClient | 6:641a9672db08 | 808 | { |
AzureIoTClient | 6:641a9672db08 | 809 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 810 | } |
AzureIoTClient | 6:641a9672db08 | 811 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 812 | |
AzureIoTClient | 6:641a9672db08 | 813 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 814 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 815 | |
AzureIoTClient | 34:6be9c2058664 | 816 | static void messagesender_send_cancel_handler(ASYNC_OPERATION_HANDLE send_operation) |
Azure.IoT Build | 0:6ae2f7bca550 | 817 | { |
AzureIoTClient | 34:6be9c2058664 | 818 | MESSAGE_WITH_CALLBACK* message_with_callback = GET_ASYNC_OPERATION_CONTEXT(MESSAGE_WITH_CALLBACK, send_operation); |
AzureIoTClient | 34:6be9c2058664 | 819 | if (message_with_callback->on_message_send_complete != NULL) |
AzureIoTClient | 34:6be9c2058664 | 820 | { |
AzureIoTClient | 34:6be9c2058664 | 821 | message_with_callback->on_message_send_complete(message_with_callback->context, MESSAGE_SEND_CANCELLED); |
AzureIoTClient | 34:6be9c2058664 | 822 | } |
AzureIoTClient | 34:6be9c2058664 | 823 | |
AzureIoTClient | 34:6be9c2058664 | 824 | remove_pending_message(message_with_callback->message_sender, send_operation); |
AzureIoTClient | 34:6be9c2058664 | 825 | } |
AzureIoTClient | 34:6be9c2058664 | 826 | |
AzureIoTClient | 34:6be9c2058664 | 827 | ASYNC_OPERATION_HANDLE messagesender_send_async(MESSAGE_SENDER_HANDLE message_sender, MESSAGE_HANDLE message, ON_MESSAGE_SEND_COMPLETE on_message_send_complete, void* callback_context, tickcounter_ms_t timeout) |
AzureIoTClient | 34:6be9c2058664 | 828 | { |
AzureIoTClient | 34:6be9c2058664 | 829 | ASYNC_OPERATION_HANDLE result; |
Azure.IoT Build | 0:6ae2f7bca550 | 830 | |
AzureIoTClient | 6:641a9672db08 | 831 | if ((message_sender == NULL) || |
AzureIoTClient | 6:641a9672db08 | 832 | (message == NULL)) |
AzureIoTClient | 6:641a9672db08 | 833 | { |
AzureIoTClient | 34:6be9c2058664 | 834 | LogError("Bad parameters: message_sender = %p, message = %p"); |
AzureIoTClient | 34:6be9c2058664 | 835 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 836 | } |
AzureIoTClient | 6:641a9672db08 | 837 | else |
AzureIoTClient | 6:641a9672db08 | 838 | { |
AzureIoTClient | 34:6be9c2058664 | 839 | if (message_sender->message_sender_state == MESSAGE_SENDER_STATE_ERROR) |
AzureIoTClient | 6:641a9672db08 | 840 | { |
AzureIoTClient | 34:6be9c2058664 | 841 | LogError("Message sender in ERROR state"); |
AzureIoTClient | 34:6be9c2058664 | 842 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 843 | } |
AzureIoTClient | 6:641a9672db08 | 844 | else |
AzureIoTClient | 6:641a9672db08 | 845 | { |
AzureIoTClient | 34:6be9c2058664 | 846 | result = CREATE_ASYNC_OPERATION(MESSAGE_WITH_CALLBACK, messagesender_send_cancel_handler); |
AzureIoTClient | 34:6be9c2058664 | 847 | if (result == NULL) |
AzureIoTClient | 6:641a9672db08 | 848 | { |
AzureIoTClient | 34:6be9c2058664 | 849 | LogError("Failed allocating context for send"); |
AzureIoTClient | 6:641a9672db08 | 850 | } |
AzureIoTClient | 6:641a9672db08 | 851 | else |
AzureIoTClient | 6:641a9672db08 | 852 | { |
AzureIoTClient | 34:6be9c2058664 | 853 | MESSAGE_WITH_CALLBACK* message_with_callback = GET_ASYNC_OPERATION_CONTEXT(MESSAGE_WITH_CALLBACK, result); |
AzureIoTClient | 34:6be9c2058664 | 854 | ASYNC_OPERATION_HANDLE* new_messages = (ASYNC_OPERATION_HANDLE*)realloc(message_sender->messages, sizeof(ASYNC_OPERATION_HANDLE) * (message_sender->message_count + 1)); |
AzureIoTClient | 6:641a9672db08 | 855 | if (new_messages == NULL) |
AzureIoTClient | 6:641a9672db08 | 856 | { |
AzureIoTClient | 34:6be9c2058664 | 857 | LogError("Failed allocating memory for pending sends"); |
AzureIoTClient | 34:6be9c2058664 | 858 | async_operation_destroy(result); |
AzureIoTClient | 34:6be9c2058664 | 859 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 860 | } |
AzureIoTClient | 6:641a9672db08 | 861 | else |
AzureIoTClient | 6:641a9672db08 | 862 | { |
AzureIoTClient | 34:6be9c2058664 | 863 | message_with_callback->timeout = timeout; |
AzureIoTClient | 34:6be9c2058664 | 864 | message_sender->messages = new_messages; |
AzureIoTClient | 34:6be9c2058664 | 865 | if (message_sender->message_sender_state != MESSAGE_SENDER_STATE_OPEN) |
AzureIoTClient | 6:641a9672db08 | 866 | { |
AzureIoTClient | 6:641a9672db08 | 867 | message_with_callback->message = message_clone(message); |
AzureIoTClient | 6:641a9672db08 | 868 | if (message_with_callback->message == NULL) |
AzureIoTClient | 6:641a9672db08 | 869 | { |
AzureIoTClient | 34:6be9c2058664 | 870 | LogError("Cannot clone message for placing it in the pending sends list"); |
AzureIoTClient | 34:6be9c2058664 | 871 | async_operation_destroy(result); |
AzureIoTClient | 34:6be9c2058664 | 872 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 873 | } |
AzureIoTClient | 6:641a9672db08 | 874 | |
AzureIoTClient | 6:641a9672db08 | 875 | message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT; |
AzureIoTClient | 6:641a9672db08 | 876 | } |
AzureIoTClient | 6:641a9672db08 | 877 | else |
AzureIoTClient | 6:641a9672db08 | 878 | { |
AzureIoTClient | 6:641a9672db08 | 879 | message_with_callback->message = NULL; |
AzureIoTClient | 6:641a9672db08 | 880 | message_with_callback->message_send_state = MESSAGE_SEND_STATE_PENDING; |
AzureIoTClient | 6:641a9672db08 | 881 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 882 | |
AzureIoTClient | 34:6be9c2058664 | 883 | if (result != NULL) |
AzureIoTClient | 6:641a9672db08 | 884 | { |
AzureIoTClient | 6:641a9672db08 | 885 | message_with_callback->on_message_send_complete = on_message_send_complete; |
AzureIoTClient | 6:641a9672db08 | 886 | message_with_callback->context = callback_context; |
AzureIoTClient | 34:6be9c2058664 | 887 | message_with_callback->message_sender = message_sender; |
AzureIoTClient | 6:641a9672db08 | 888 | |
AzureIoTClient | 34:6be9c2058664 | 889 | message_sender->messages[message_sender->message_count] = result; |
AzureIoTClient | 34:6be9c2058664 | 890 | message_sender->message_count++; |
Azure.IoT Build | 0:6ae2f7bca550 | 891 | |
AzureIoTClient | 34:6be9c2058664 | 892 | if (message_sender->message_sender_state == MESSAGE_SENDER_STATE_OPEN) |
AzureIoTClient | 6:641a9672db08 | 893 | { |
AzureIoTClient | 34:6be9c2058664 | 894 | switch (send_one_message(message_sender, result, message)) |
AzureIoTClient | 6:641a9672db08 | 895 | { |
AzureIoTClient | 6:641a9672db08 | 896 | default: |
AzureIoTClient | 6:641a9672db08 | 897 | case SEND_ONE_MESSAGE_ERROR: |
AzureIoTClient | 34:6be9c2058664 | 898 | LogError("Error sending message"); |
AzureIoTClient | 34:6be9c2058664 | 899 | remove_pending_message_by_index(message_sender, message_sender->message_count - 1); |
AzureIoTClient | 34:6be9c2058664 | 900 | async_operation_destroy(result); |
AzureIoTClient | 34:6be9c2058664 | 901 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 902 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 903 | |
AzureIoTClient | 6:641a9672db08 | 904 | case SEND_ONE_MESSAGE_BUSY: |
AzureIoTClient | 6:641a9672db08 | 905 | message_with_callback->message = message_clone(message); |
AzureIoTClient | 6:641a9672db08 | 906 | if (message_with_callback->message == NULL) |
AzureIoTClient | 6:641a9672db08 | 907 | { |
AzureIoTClient | 34:6be9c2058664 | 908 | LogError("Error cloning message for placing it in the pending sends list"); |
AzureIoTClient | 34:6be9c2058664 | 909 | async_operation_destroy(result); |
AzureIoTClient | 34:6be9c2058664 | 910 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 911 | } |
AzureIoTClient | 6:641a9672db08 | 912 | else |
AzureIoTClient | 6:641a9672db08 | 913 | { |
AzureIoTClient | 6:641a9672db08 | 914 | message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT; |
AzureIoTClient | 6:641a9672db08 | 915 | } |
AzureIoTClient | 6:641a9672db08 | 916 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 917 | |
AzureIoTClient | 6:641a9672db08 | 918 | case SEND_ONE_MESSAGE_OK: |
AzureIoTClient | 6:641a9672db08 | 919 | break; |
AzureIoTClient | 6:641a9672db08 | 920 | } |
AzureIoTClient | 6:641a9672db08 | 921 | } |
AzureIoTClient | 6:641a9672db08 | 922 | } |
AzureIoTClient | 6:641a9672db08 | 923 | } |
AzureIoTClient | 6:641a9672db08 | 924 | } |
AzureIoTClient | 6:641a9672db08 | 925 | } |
AzureIoTClient | 6:641a9672db08 | 926 | } |
AzureIoTClient | 34:6be9c2058664 | 927 | |
AzureIoTClient | 6:641a9672db08 | 928 | return result; |
AzureIoTClient | 6:641a9672db08 | 929 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 930 | |
AzureIoTClient | 6:641a9672db08 | 931 | void messagesender_set_trace(MESSAGE_SENDER_HANDLE message_sender, bool traceOn) |
AzureIoTClient | 6:641a9672db08 | 932 | { |
AzureIoTClient | 34:6be9c2058664 | 933 | if (message_sender == NULL) |
AzureIoTClient | 6:641a9672db08 | 934 | { |
AzureIoTClient | 34:6be9c2058664 | 935 | LogError("NULL message_sender"); |
AzureIoTClient | 34:6be9c2058664 | 936 | } |
AzureIoTClient | 34:6be9c2058664 | 937 | else |
AzureIoTClient | 34:6be9c2058664 | 938 | { |
AzureIoTClient | 34:6be9c2058664 | 939 | message_sender->is_trace_on = traceOn ? 1 : 0; |
AzureIoTClient | 6:641a9672db08 | 940 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 941 | } |