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