A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Fri Jul 29 15:58:39 2016 -0700
Revision:
6:641a9672db08
Parent:
1:eab586236bfe
Child:
12:b30dacf113f2
1.0.10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:6ae2f7bca550 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:6ae2f7bca550 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:6ae2f7bca550 3
Azure.IoT Build 0:6ae2f7bca550 4 #include <stdlib.h>
Azure.IoT Build 0:6ae2f7bca550 5 #ifdef _CRTDBG_MAP_ALLOC
Azure.IoT Build 0:6ae2f7bca550 6 #include <crtdbg.h>
Azure.IoT Build 0:6ae2f7bca550 7 #endif
Azure.IoT Build 0:6ae2f7bca550 8 #include "azure_uamqp_c/message_receiver.h"
Azure.IoT Build 0:6ae2f7bca550 9 #include "azure_uamqp_c/amqpalloc.h"
Azure.IoT Build 0:6ae2f7bca550 10 #include "azure_uamqp_c/amqpvalue.h"
Azure.IoT Build 0:6ae2f7bca550 11 #include "azure_uamqp_c/amqp_definitions.h"
Azure.IoT Build 0:6ae2f7bca550 12
Azure.IoT Build 0:6ae2f7bca550 13 typedef struct MESSAGE_RECEIVER_INSTANCE_TAG
Azure.IoT Build 0:6ae2f7bca550 14 {
Azure.IoT Build 0:6ae2f7bca550 15 LINK_HANDLE link;
Azure.IoT Build 0:6ae2f7bca550 16 ON_MESSAGE_RECEIVED on_message_received;
Azure.IoT Build 0:6ae2f7bca550 17 ON_MESSAGE_RECEIVER_STATE_CHANGED on_message_receiver_state_changed;
Azure.IoT Build 0:6ae2f7bca550 18 MESSAGE_RECEIVER_STATE message_receiver_state;
Azure.IoT Build 0:6ae2f7bca550 19 const void* on_message_receiver_state_changed_context;
Azure.IoT Build 0:6ae2f7bca550 20 const void* callback_context;
Azure.IoT Build 0:6ae2f7bca550 21 MESSAGE_HANDLE decoded_message;
Azure.IoT Build 0:6ae2f7bca550 22 bool decode_error;
Azure.IoT Build 0:6ae2f7bca550 23 } MESSAGE_RECEIVER_INSTANCE;
Azure.IoT Build 0:6ae2f7bca550 24
Azure.IoT Build 0:6ae2f7bca550 25 static void set_message_receiver_state(MESSAGE_RECEIVER_INSTANCE* message_receiver_instance, MESSAGE_RECEIVER_STATE new_state)
Azure.IoT Build 0:6ae2f7bca550 26 {
Azure.IoT Build 0:6ae2f7bca550 27 MESSAGE_RECEIVER_STATE previous_state = message_receiver_instance->message_receiver_state;
Azure.IoT Build 0:6ae2f7bca550 28 message_receiver_instance->message_receiver_state = new_state;
Azure.IoT Build 0:6ae2f7bca550 29 if (message_receiver_instance->on_message_receiver_state_changed != NULL)
Azure.IoT Build 0:6ae2f7bca550 30 {
Azure.IoT Build 0:6ae2f7bca550 31 message_receiver_instance->on_message_receiver_state_changed(message_receiver_instance->on_message_receiver_state_changed_context, new_state, previous_state);
Azure.IoT Build 0:6ae2f7bca550 32 }
Azure.IoT Build 0:6ae2f7bca550 33 }
Azure.IoT Build 0:6ae2f7bca550 34
Azure.IoT Build 0:6ae2f7bca550 35 static void decode_message_value_callback(void* context, AMQP_VALUE decoded_value)
Azure.IoT Build 0:6ae2f7bca550 36 {
Azure.IoT Build 0:6ae2f7bca550 37 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)context;
Azure.IoT Build 0:6ae2f7bca550 38 MESSAGE_HANDLE decoded_message = message_receiver_instance->decoded_message;
Azure.IoT Build 0:6ae2f7bca550 39 AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 40
Azure.IoT Build 0:6ae2f7bca550 41 if (is_application_properties_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 42 {
Azure.IoT Build 0:6ae2f7bca550 43 if (message_set_application_properties(decoded_message, decoded_value) != 0)
Azure.IoT Build 0:6ae2f7bca550 44 {
Azure.IoT Build 0:6ae2f7bca550 45 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 46 }
Azure.IoT Build 0:6ae2f7bca550 47 }
Azure.IoT Build 0:6ae2f7bca550 48 else if (is_properties_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 49 {
Azure.IoT Build 0:6ae2f7bca550 50 PROPERTIES_HANDLE properties;
Azure.IoT Build 0:6ae2f7bca550 51 if (amqpvalue_get_properties(decoded_value, &properties) != 0)
Azure.IoT Build 0:6ae2f7bca550 52 {
Azure.IoT Build 0:6ae2f7bca550 53 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 54 }
Azure.IoT Build 0:6ae2f7bca550 55 else
Azure.IoT Build 0:6ae2f7bca550 56 {
Azure.IoT Build 0:6ae2f7bca550 57 if (message_set_properties(decoded_message, properties) != 0)
Azure.IoT Build 0:6ae2f7bca550 58 {
Azure.IoT Build 0:6ae2f7bca550 59 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 60 }
Azure.IoT Build 0:6ae2f7bca550 61
Azure.IoT Build 0:6ae2f7bca550 62 properties_destroy(properties);
Azure.IoT Build 0:6ae2f7bca550 63 }
Azure.IoT Build 0:6ae2f7bca550 64 }
Azure.IoT Build 0:6ae2f7bca550 65 else if (is_delivery_annotations_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 66 {
Azure.IoT Build 0:6ae2f7bca550 67 annotations delivery_annotations = amqpvalue_get_inplace_described_value(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 68 if ((delivery_annotations == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 69 (message_set_delivery_annotations(decoded_message, delivery_annotations) != 0))
Azure.IoT Build 0:6ae2f7bca550 70 {
Azure.IoT Build 0:6ae2f7bca550 71 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 72 }
Azure.IoT Build 0:6ae2f7bca550 73 }
Azure.IoT Build 0:6ae2f7bca550 74 else if (is_message_annotations_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 75 {
Azure.IoT Build 0:6ae2f7bca550 76 annotations message_annotations = amqpvalue_get_inplace_described_value(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 77 if ((message_annotations == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 78 (message_set_message_annotations(decoded_message, message_annotations) != 0))
Azure.IoT Build 0:6ae2f7bca550 79 {
Azure.IoT Build 0:6ae2f7bca550 80 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 81 }
Azure.IoT Build 0:6ae2f7bca550 82 }
Azure.IoT Build 0:6ae2f7bca550 83 else if (is_header_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 84 {
Azure.IoT Build 0:6ae2f7bca550 85 HEADER_HANDLE header;
Azure.IoT Build 0:6ae2f7bca550 86 if (amqpvalue_get_header(decoded_value, &header) != 0)
Azure.IoT Build 0:6ae2f7bca550 87 {
Azure.IoT Build 0:6ae2f7bca550 88 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 89 }
Azure.IoT Build 0:6ae2f7bca550 90 else
Azure.IoT Build 0:6ae2f7bca550 91 {
Azure.IoT Build 0:6ae2f7bca550 92 if (message_set_header(decoded_message, header) != 0)
Azure.IoT Build 0:6ae2f7bca550 93 {
Azure.IoT Build 0:6ae2f7bca550 94 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 95 }
Azure.IoT Build 0:6ae2f7bca550 96
Azure.IoT Build 0:6ae2f7bca550 97 header_destroy(header);
Azure.IoT Build 0:6ae2f7bca550 98 }
Azure.IoT Build 0:6ae2f7bca550 99 }
Azure.IoT Build 0:6ae2f7bca550 100 else if (is_footer_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 101 {
Azure.IoT Build 0:6ae2f7bca550 102 annotations footer = amqpvalue_get_inplace_described_value(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 103 if ((footer == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 104 (message_set_footer(decoded_message, footer) != 0))
Azure.IoT Build 0:6ae2f7bca550 105 {
Azure.IoT Build 0:6ae2f7bca550 106 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 107 }
Azure.IoT Build 0:6ae2f7bca550 108 }
Azure.IoT Build 0:6ae2f7bca550 109 else if (is_amqp_value_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 110 {
Azure.IoT Build 0:6ae2f7bca550 111 MESSAGE_BODY_TYPE body_type;
Azure.IoT Build 0:6ae2f7bca550 112 message_get_body_type(decoded_message, &body_type);
Azure.IoT Build 0:6ae2f7bca550 113 if (body_type != MESSAGE_BODY_TYPE_NONE)
Azure.IoT Build 0:6ae2f7bca550 114 {
Azure.IoT Build 0:6ae2f7bca550 115 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 116 }
Azure.IoT Build 0:6ae2f7bca550 117 else
Azure.IoT Build 0:6ae2f7bca550 118 {
Azure.IoT Build 0:6ae2f7bca550 119 AMQP_VALUE body_amqp_value = amqpvalue_get_inplace_described_value(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 120 if ((body_amqp_value == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 121 (message_set_body_amqp_value(decoded_message, body_amqp_value) != 0))
Azure.IoT Build 0:6ae2f7bca550 122 {
Azure.IoT Build 0:6ae2f7bca550 123 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 124 }
Azure.IoT Build 0:6ae2f7bca550 125 }
Azure.IoT Build 0:6ae2f7bca550 126 }
Azure.IoT Build 0:6ae2f7bca550 127 else if (is_data_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 128 {
Azure.IoT Build 0:6ae2f7bca550 129 MESSAGE_BODY_TYPE body_type;
Azure.IoT Build 0:6ae2f7bca550 130 message_get_body_type(decoded_message, &body_type);
Azure.IoT Build 0:6ae2f7bca550 131 if ((body_type != MESSAGE_BODY_TYPE_NONE) &&
Azure.IoT Build 0:6ae2f7bca550 132 (body_type != MESSAGE_BODY_TYPE_DATA))
Azure.IoT Build 0:6ae2f7bca550 133 {
Azure.IoT Build 0:6ae2f7bca550 134 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 135 }
Azure.IoT Build 0:6ae2f7bca550 136 else
Azure.IoT Build 0:6ae2f7bca550 137 {
Azure.IoT Build 0:6ae2f7bca550 138 AMQP_VALUE body_data_value = amqpvalue_get_inplace_described_value(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 139 data data_value;
Azure.IoT Build 0:6ae2f7bca550 140
Azure.IoT Build 0:6ae2f7bca550 141 if ((body_data_value == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 142 (amqpvalue_get_data(body_data_value, &data_value) != 0))
Azure.IoT Build 0:6ae2f7bca550 143 {
Azure.IoT Build 0:6ae2f7bca550 144 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 145 }
Azure.IoT Build 0:6ae2f7bca550 146 else
Azure.IoT Build 0:6ae2f7bca550 147 {
AzureIoTClient 6:641a9672db08 148 BINARY_DATA binary_data;
AzureIoTClient 6:641a9672db08 149 binary_data.bytes = data_value.bytes;
AzureIoTClient 6:641a9672db08 150 binary_data.length = data_value.length;
Azure.IoT Build 0:6ae2f7bca550 151 if (message_add_body_amqp_data(decoded_message, binary_data) != 0)
Azure.IoT Build 0:6ae2f7bca550 152 {
Azure.IoT Build 0:6ae2f7bca550 153 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 154 }
Azure.IoT Build 0:6ae2f7bca550 155 }
Azure.IoT Build 0:6ae2f7bca550 156 }
Azure.IoT Build 0:6ae2f7bca550 157 }
Azure.IoT Build 0:6ae2f7bca550 158 }
Azure.IoT Build 0:6ae2f7bca550 159
Azure.IoT Build 0:6ae2f7bca550 160 static AMQP_VALUE on_transfer_received(void* context, TRANSFER_HANDLE transfer, uint32_t payload_size, const unsigned char* payload_bytes)
Azure.IoT Build 0:6ae2f7bca550 161 {
Azure.IoT Build 0:6ae2f7bca550 162 AMQP_VALUE result = NULL;
Azure.IoT Build 0:6ae2f7bca550 163
AzureIoTClient 1:eab586236bfe 164 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)context;
AzureIoTClient 6:641a9672db08 165 (void)transfer;
AzureIoTClient 1:eab586236bfe 166 if (message_receiver_instance->on_message_received != NULL)
AzureIoTClient 1:eab586236bfe 167 {
AzureIoTClient 1:eab586236bfe 168 MESSAGE_HANDLE message = message_create();
Azure.IoT Build 0:6ae2f7bca550 169 if (message == NULL)
Azure.IoT Build 0:6ae2f7bca550 170 {
Azure.IoT Build 0:6ae2f7bca550 171 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 172 }
Azure.IoT Build 0:6ae2f7bca550 173 else
Azure.IoT Build 0:6ae2f7bca550 174 {
Azure.IoT Build 0:6ae2f7bca550 175 message_receiver_instance->decoded_message;
Azure.IoT Build 0:6ae2f7bca550 176 AMQPVALUE_DECODER_HANDLE amqpvalue_decoder = amqpvalue_decoder_create(decode_message_value_callback, message_receiver_instance);
Azure.IoT Build 0:6ae2f7bca550 177 if (amqpvalue_decoder == NULL)
Azure.IoT Build 0:6ae2f7bca550 178 {
Azure.IoT Build 0:6ae2f7bca550 179 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 180 }
Azure.IoT Build 0:6ae2f7bca550 181 else
Azure.IoT Build 0:6ae2f7bca550 182 {
Azure.IoT Build 0:6ae2f7bca550 183 message_receiver_instance->decoded_message = message;
Azure.IoT Build 0:6ae2f7bca550 184 message_receiver_instance->decode_error = false;
Azure.IoT Build 0:6ae2f7bca550 185 if (amqpvalue_decode_bytes(amqpvalue_decoder, payload_bytes, payload_size) != 0)
Azure.IoT Build 0:6ae2f7bca550 186 {
Azure.IoT Build 0:6ae2f7bca550 187 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 188 }
Azure.IoT Build 0:6ae2f7bca550 189 else
Azure.IoT Build 0:6ae2f7bca550 190 {
Azure.IoT Build 0:6ae2f7bca550 191 if (message_receiver_instance->decode_error)
Azure.IoT Build 0:6ae2f7bca550 192 {
Azure.IoT Build 0:6ae2f7bca550 193 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 194 }
Azure.IoT Build 0:6ae2f7bca550 195 else
Azure.IoT Build 0:6ae2f7bca550 196 {
Azure.IoT Build 0:6ae2f7bca550 197 result = message_receiver_instance->on_message_received(message_receiver_instance->callback_context, message);
Azure.IoT Build 0:6ae2f7bca550 198 }
Azure.IoT Build 0:6ae2f7bca550 199 }
Azure.IoT Build 0:6ae2f7bca550 200
Azure.IoT Build 0:6ae2f7bca550 201 amqpvalue_decoder_destroy(amqpvalue_decoder);
Azure.IoT Build 0:6ae2f7bca550 202 }
Azure.IoT Build 0:6ae2f7bca550 203
Azure.IoT Build 0:6ae2f7bca550 204 message_destroy(message);
Azure.IoT Build 0:6ae2f7bca550 205 }
AzureIoTClient 1:eab586236bfe 206 }
Azure.IoT Build 0:6ae2f7bca550 207
Azure.IoT Build 0:6ae2f7bca550 208 return result;
Azure.IoT Build 0:6ae2f7bca550 209 }
Azure.IoT Build 0:6ae2f7bca550 210
Azure.IoT Build 0:6ae2f7bca550 211 static void on_link_state_changed(void* context, LINK_STATE new_link_state, LINK_STATE previous_link_state)
Azure.IoT Build 0:6ae2f7bca550 212 {
Azure.IoT Build 0:6ae2f7bca550 213 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)context;
AzureIoTClient 6:641a9672db08 214 (void)previous_link_state;
Azure.IoT Build 0:6ae2f7bca550 215
Azure.IoT Build 0:6ae2f7bca550 216 switch (new_link_state)
Azure.IoT Build 0:6ae2f7bca550 217 {
Azure.IoT Build 0:6ae2f7bca550 218 case LINK_STATE_ATTACHED:
Azure.IoT Build 0:6ae2f7bca550 219 if (message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_OPENING)
Azure.IoT Build 0:6ae2f7bca550 220 {
Azure.IoT Build 0:6ae2f7bca550 221 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_OPEN);
Azure.IoT Build 0:6ae2f7bca550 222 }
Azure.IoT Build 0:6ae2f7bca550 223 break;
Azure.IoT Build 0:6ae2f7bca550 224 case LINK_STATE_DETACHED:
Azure.IoT Build 0:6ae2f7bca550 225 if ((message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_OPEN) ||
Azure.IoT Build 0:6ae2f7bca550 226 (message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_CLOSING))
Azure.IoT Build 0:6ae2f7bca550 227 {
Azure.IoT Build 0:6ae2f7bca550 228 /* User initiated transition, we should be good */
Azure.IoT Build 0:6ae2f7bca550 229 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_IDLE);
Azure.IoT Build 0:6ae2f7bca550 230 }
Azure.IoT Build 0:6ae2f7bca550 231 else if (message_receiver_instance->message_receiver_state != MESSAGE_RECEIVER_STATE_IDLE)
Azure.IoT Build 0:6ae2f7bca550 232 {
Azure.IoT Build 0:6ae2f7bca550 233 /* Any other transition must be an error */
Azure.IoT Build 0:6ae2f7bca550 234 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 235 }
Azure.IoT Build 0:6ae2f7bca550 236 break;
AzureIoTClient 1:eab586236bfe 237 case LINK_STATE_ERROR:
AzureIoTClient 1:eab586236bfe 238 if (message_receiver_instance->message_receiver_state != MESSAGE_RECEIVER_STATE_ERROR)
AzureIoTClient 1:eab586236bfe 239 {
AzureIoTClient 1:eab586236bfe 240 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
AzureIoTClient 1:eab586236bfe 241 }
AzureIoTClient 1:eab586236bfe 242 break;
Azure.IoT Build 0:6ae2f7bca550 243 }
Azure.IoT Build 0:6ae2f7bca550 244 }
Azure.IoT Build 0:6ae2f7bca550 245
Azure.IoT Build 0:6ae2f7bca550 246 MESSAGE_RECEIVER_HANDLE messagereceiver_create(LINK_HANDLE link, ON_MESSAGE_RECEIVER_STATE_CHANGED on_message_receiver_state_changed, void* context)
Azure.IoT Build 0:6ae2f7bca550 247 {
Azure.IoT Build 0:6ae2f7bca550 248 MESSAGE_RECEIVER_INSTANCE* result = (MESSAGE_RECEIVER_INSTANCE*)amqpalloc_malloc(sizeof(MESSAGE_RECEIVER_INSTANCE));
Azure.IoT Build 0:6ae2f7bca550 249 if (result != NULL)
Azure.IoT Build 0:6ae2f7bca550 250 {
Azure.IoT Build 0:6ae2f7bca550 251 result->link = link;
Azure.IoT Build 0:6ae2f7bca550 252 result->on_message_receiver_state_changed = on_message_receiver_state_changed;
Azure.IoT Build 0:6ae2f7bca550 253 result->on_message_receiver_state_changed_context = context;
Azure.IoT Build 0:6ae2f7bca550 254 result->message_receiver_state = MESSAGE_RECEIVER_STATE_IDLE;
Azure.IoT Build 0:6ae2f7bca550 255 }
Azure.IoT Build 0:6ae2f7bca550 256
Azure.IoT Build 0:6ae2f7bca550 257 return result;
Azure.IoT Build 0:6ae2f7bca550 258 }
Azure.IoT Build 0:6ae2f7bca550 259
Azure.IoT Build 0:6ae2f7bca550 260 void messagereceiver_destroy(MESSAGE_RECEIVER_HANDLE message_receiver)
Azure.IoT Build 0:6ae2f7bca550 261 {
Azure.IoT Build 0:6ae2f7bca550 262 if (message_receiver != NULL)
Azure.IoT Build 0:6ae2f7bca550 263 {
AzureIoTClient 1:eab586236bfe 264 (void)messagereceiver_close(message_receiver);
Azure.IoT Build 0:6ae2f7bca550 265 amqpalloc_free(message_receiver);
Azure.IoT Build 0:6ae2f7bca550 266 }
Azure.IoT Build 0:6ae2f7bca550 267 }
Azure.IoT Build 0:6ae2f7bca550 268
Azure.IoT Build 0:6ae2f7bca550 269 int messagereceiver_open(MESSAGE_RECEIVER_HANDLE message_receiver, ON_MESSAGE_RECEIVED on_message_received, const void* callback_context)
Azure.IoT Build 0:6ae2f7bca550 270 {
Azure.IoT Build 0:6ae2f7bca550 271 int result;
Azure.IoT Build 0:6ae2f7bca550 272
Azure.IoT Build 0:6ae2f7bca550 273 if (message_receiver == NULL)
Azure.IoT Build 0:6ae2f7bca550 274 {
Azure.IoT Build 0:6ae2f7bca550 275 result = __LINE__;
Azure.IoT Build 0:6ae2f7bca550 276 }
Azure.IoT Build 0:6ae2f7bca550 277 else
Azure.IoT Build 0:6ae2f7bca550 278 {
Azure.IoT Build 0:6ae2f7bca550 279 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)message_receiver;
Azure.IoT Build 0:6ae2f7bca550 280
Azure.IoT Build 0:6ae2f7bca550 281 if (message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_IDLE)
Azure.IoT Build 0:6ae2f7bca550 282 {
Azure.IoT Build 0:6ae2f7bca550 283 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_OPENING);
Azure.IoT Build 0:6ae2f7bca550 284 if (link_attach(message_receiver_instance->link, on_transfer_received, on_link_state_changed, NULL, message_receiver_instance) != 0)
Azure.IoT Build 0:6ae2f7bca550 285 {
Azure.IoT Build 0:6ae2f7bca550 286 result = __LINE__;
Azure.IoT Build 0:6ae2f7bca550 287 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 288 }
Azure.IoT Build 0:6ae2f7bca550 289 else
Azure.IoT Build 0:6ae2f7bca550 290 {
Azure.IoT Build 0:6ae2f7bca550 291 message_receiver_instance->on_message_received = on_message_received;
Azure.IoT Build 0:6ae2f7bca550 292 message_receiver_instance->callback_context = callback_context;
Azure.IoT Build 0:6ae2f7bca550 293
Azure.IoT Build 0:6ae2f7bca550 294 result = 0;
Azure.IoT Build 0:6ae2f7bca550 295 }
Azure.IoT Build 0:6ae2f7bca550 296 }
Azure.IoT Build 0:6ae2f7bca550 297 else
Azure.IoT Build 0:6ae2f7bca550 298 {
Azure.IoT Build 0:6ae2f7bca550 299 result = 0;
Azure.IoT Build 0:6ae2f7bca550 300 }
Azure.IoT Build 0:6ae2f7bca550 301 }
Azure.IoT Build 0:6ae2f7bca550 302
Azure.IoT Build 0:6ae2f7bca550 303 return result;
Azure.IoT Build 0:6ae2f7bca550 304 }
Azure.IoT Build 0:6ae2f7bca550 305
Azure.IoT Build 0:6ae2f7bca550 306 int messagereceiver_close(MESSAGE_RECEIVER_HANDLE message_receiver)
Azure.IoT Build 0:6ae2f7bca550 307 {
Azure.IoT Build 0:6ae2f7bca550 308 int result;
Azure.IoT Build 0:6ae2f7bca550 309
Azure.IoT Build 0:6ae2f7bca550 310 if (message_receiver == NULL)
Azure.IoT Build 0:6ae2f7bca550 311 {
Azure.IoT Build 0:6ae2f7bca550 312 result = __LINE__;
Azure.IoT Build 0:6ae2f7bca550 313 }
Azure.IoT Build 0:6ae2f7bca550 314 else
Azure.IoT Build 0:6ae2f7bca550 315 {
Azure.IoT Build 0:6ae2f7bca550 316 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)message_receiver;
Azure.IoT Build 0:6ae2f7bca550 317
Azure.IoT Build 0:6ae2f7bca550 318 if ((message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_OPENING) ||
Azure.IoT Build 0:6ae2f7bca550 319 (message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_OPEN))
Azure.IoT Build 0:6ae2f7bca550 320 {
Azure.IoT Build 0:6ae2f7bca550 321 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_CLOSING);
Azure.IoT Build 0:6ae2f7bca550 322
Azure.IoT Build 0:6ae2f7bca550 323 if (link_detach(message_receiver_instance->link) != 0)
Azure.IoT Build 0:6ae2f7bca550 324 {
Azure.IoT Build 0:6ae2f7bca550 325 result = __LINE__;
Azure.IoT Build 0:6ae2f7bca550 326 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 327 }
Azure.IoT Build 0:6ae2f7bca550 328 else
Azure.IoT Build 0:6ae2f7bca550 329 {
Azure.IoT Build 0:6ae2f7bca550 330 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_IDLE);
Azure.IoT Build 0:6ae2f7bca550 331 result = 0;
Azure.IoT Build 0:6ae2f7bca550 332 }
Azure.IoT Build 0:6ae2f7bca550 333 }
Azure.IoT Build 0:6ae2f7bca550 334 else
Azure.IoT Build 0:6ae2f7bca550 335 {
Azure.IoT Build 0:6ae2f7bca550 336 result = 0;
Azure.IoT Build 0:6ae2f7bca550 337 }
Azure.IoT Build 0:6ae2f7bca550 338 }
Azure.IoT Build 0:6ae2f7bca550 339
Azure.IoT Build 0:6ae2f7bca550 340 return result;
Azure.IoT Build 0:6ae2f7bca550 341 }