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