A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
Azure.IoT Build
Date:
Fri Apr 08 12:01:10 2016 -0700
Revision:
0:6ae2f7bca550
Child:
1:eab586236bfe
1.0.4

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 {
Azure.IoT Build 0:6ae2f7bca550 148 BINARY_DATA binary_data = { data_value.bytes, data_value.length };
Azure.IoT Build 0:6ae2f7bca550 149 if (message_add_body_amqp_data(decoded_message, binary_data) != 0)
Azure.IoT Build 0:6ae2f7bca550 150 {
Azure.IoT Build 0:6ae2f7bca550 151 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 152 }
Azure.IoT Build 0:6ae2f7bca550 153 }
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 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 159 {
Azure.IoT Build 0:6ae2f7bca550 160 AMQP_VALUE result = NULL;
Azure.IoT Build 0:6ae2f7bca550 161
Azure.IoT Build 0:6ae2f7bca550 162 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)context;
Azure.IoT Build 0:6ae2f7bca550 163 if (message_receiver_instance->on_message_received != NULL)
Azure.IoT Build 0:6ae2f7bca550 164 {
Azure.IoT Build 0:6ae2f7bca550 165 MESSAGE_HANDLE message = message_create();
Azure.IoT Build 0:6ae2f7bca550 166 if (message == NULL)
Azure.IoT Build 0:6ae2f7bca550 167 {
Azure.IoT Build 0:6ae2f7bca550 168 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 169 }
Azure.IoT Build 0:6ae2f7bca550 170 else
Azure.IoT Build 0:6ae2f7bca550 171 {
Azure.IoT Build 0:6ae2f7bca550 172 message_receiver_instance->decoded_message;
Azure.IoT Build 0:6ae2f7bca550 173 AMQPVALUE_DECODER_HANDLE amqpvalue_decoder = amqpvalue_decoder_create(decode_message_value_callback, message_receiver_instance);
Azure.IoT Build 0:6ae2f7bca550 174 if (amqpvalue_decoder == NULL)
Azure.IoT Build 0:6ae2f7bca550 175 {
Azure.IoT Build 0:6ae2f7bca550 176 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 177 }
Azure.IoT Build 0:6ae2f7bca550 178 else
Azure.IoT Build 0:6ae2f7bca550 179 {
Azure.IoT Build 0:6ae2f7bca550 180 message_receiver_instance->decoded_message = message;
Azure.IoT Build 0:6ae2f7bca550 181 message_receiver_instance->decode_error = false;
Azure.IoT Build 0:6ae2f7bca550 182 if (amqpvalue_decode_bytes(amqpvalue_decoder, payload_bytes, payload_size) != 0)
Azure.IoT Build 0:6ae2f7bca550 183 {
Azure.IoT Build 0:6ae2f7bca550 184 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 185 }
Azure.IoT Build 0:6ae2f7bca550 186 else
Azure.IoT Build 0:6ae2f7bca550 187 {
Azure.IoT Build 0:6ae2f7bca550 188 if (message_receiver_instance->decode_error)
Azure.IoT Build 0:6ae2f7bca550 189 {
Azure.IoT Build 0:6ae2f7bca550 190 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 191 }
Azure.IoT Build 0:6ae2f7bca550 192 else
Azure.IoT Build 0:6ae2f7bca550 193 {
Azure.IoT Build 0:6ae2f7bca550 194 result = message_receiver_instance->on_message_received(message_receiver_instance->callback_context, message);
Azure.IoT Build 0:6ae2f7bca550 195 }
Azure.IoT Build 0:6ae2f7bca550 196 }
Azure.IoT Build 0:6ae2f7bca550 197
Azure.IoT Build 0:6ae2f7bca550 198 amqpvalue_decoder_destroy(amqpvalue_decoder);
Azure.IoT Build 0:6ae2f7bca550 199 }
Azure.IoT Build 0:6ae2f7bca550 200
Azure.IoT Build 0:6ae2f7bca550 201 message_destroy(message);
Azure.IoT Build 0:6ae2f7bca550 202 }
Azure.IoT Build 0:6ae2f7bca550 203 }
Azure.IoT Build 0:6ae2f7bca550 204
Azure.IoT Build 0:6ae2f7bca550 205 return result;
Azure.IoT Build 0:6ae2f7bca550 206 }
Azure.IoT Build 0:6ae2f7bca550 207
Azure.IoT Build 0:6ae2f7bca550 208 static void on_link_state_changed(void* context, LINK_STATE new_link_state, LINK_STATE previous_link_state)
Azure.IoT Build 0:6ae2f7bca550 209 {
Azure.IoT Build 0:6ae2f7bca550 210 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)context;
Azure.IoT Build 0:6ae2f7bca550 211
Azure.IoT Build 0:6ae2f7bca550 212 switch (new_link_state)
Azure.IoT Build 0:6ae2f7bca550 213 {
Azure.IoT Build 0:6ae2f7bca550 214 case LINK_STATE_ATTACHED:
Azure.IoT Build 0:6ae2f7bca550 215 if (message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_OPENING)
Azure.IoT Build 0:6ae2f7bca550 216 {
Azure.IoT Build 0:6ae2f7bca550 217 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_OPEN);
Azure.IoT Build 0:6ae2f7bca550 218 }
Azure.IoT Build 0:6ae2f7bca550 219 break;
Azure.IoT Build 0:6ae2f7bca550 220 case LINK_STATE_DETACHED:
Azure.IoT Build 0:6ae2f7bca550 221 if ((message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_OPEN) ||
Azure.IoT Build 0:6ae2f7bca550 222 (message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_CLOSING))
Azure.IoT Build 0:6ae2f7bca550 223 {
Azure.IoT Build 0:6ae2f7bca550 224 /* User initiated transition, we should be good */
Azure.IoT Build 0:6ae2f7bca550 225 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_IDLE);
Azure.IoT Build 0:6ae2f7bca550 226 }
Azure.IoT Build 0:6ae2f7bca550 227 else if (message_receiver_instance->message_receiver_state != MESSAGE_RECEIVER_STATE_IDLE)
Azure.IoT Build 0:6ae2f7bca550 228 {
Azure.IoT Build 0:6ae2f7bca550 229 /* Any other transition must be an error */
Azure.IoT Build 0:6ae2f7bca550 230 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 231 }
Azure.IoT Build 0:6ae2f7bca550 232 break;
Azure.IoT Build 0:6ae2f7bca550 233 }
Azure.IoT Build 0:6ae2f7bca550 234 }
Azure.IoT Build 0:6ae2f7bca550 235
Azure.IoT Build 0:6ae2f7bca550 236 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 237 {
Azure.IoT Build 0:6ae2f7bca550 238 MESSAGE_RECEIVER_INSTANCE* result = (MESSAGE_RECEIVER_INSTANCE*)amqpalloc_malloc(sizeof(MESSAGE_RECEIVER_INSTANCE));
Azure.IoT Build 0:6ae2f7bca550 239 if (result != NULL)
Azure.IoT Build 0:6ae2f7bca550 240 {
Azure.IoT Build 0:6ae2f7bca550 241 result->link = link;
Azure.IoT Build 0:6ae2f7bca550 242 result->on_message_receiver_state_changed = on_message_receiver_state_changed;
Azure.IoT Build 0:6ae2f7bca550 243 result->on_message_receiver_state_changed_context = context;
Azure.IoT Build 0:6ae2f7bca550 244 result->message_receiver_state = MESSAGE_RECEIVER_STATE_IDLE;
Azure.IoT Build 0:6ae2f7bca550 245 }
Azure.IoT Build 0:6ae2f7bca550 246
Azure.IoT Build 0:6ae2f7bca550 247 return result;
Azure.IoT Build 0:6ae2f7bca550 248 }
Azure.IoT Build 0:6ae2f7bca550 249
Azure.IoT Build 0:6ae2f7bca550 250 void messagereceiver_destroy(MESSAGE_RECEIVER_HANDLE message_receiver)
Azure.IoT Build 0:6ae2f7bca550 251 {
Azure.IoT Build 0:6ae2f7bca550 252 if (message_receiver != NULL)
Azure.IoT Build 0:6ae2f7bca550 253 {
Azure.IoT Build 0:6ae2f7bca550 254 amqpalloc_free(message_receiver);
Azure.IoT Build 0:6ae2f7bca550 255 }
Azure.IoT Build 0:6ae2f7bca550 256 }
Azure.IoT Build 0:6ae2f7bca550 257
Azure.IoT Build 0:6ae2f7bca550 258 int messagereceiver_open(MESSAGE_RECEIVER_HANDLE message_receiver, ON_MESSAGE_RECEIVED on_message_received, const void* callback_context)
Azure.IoT Build 0:6ae2f7bca550 259 {
Azure.IoT Build 0:6ae2f7bca550 260 int result;
Azure.IoT Build 0:6ae2f7bca550 261
Azure.IoT Build 0:6ae2f7bca550 262 if (message_receiver == NULL)
Azure.IoT Build 0:6ae2f7bca550 263 {
Azure.IoT Build 0:6ae2f7bca550 264 result = __LINE__;
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 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)message_receiver;
Azure.IoT Build 0:6ae2f7bca550 269
Azure.IoT Build 0:6ae2f7bca550 270 if (message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_IDLE)
Azure.IoT Build 0:6ae2f7bca550 271 {
Azure.IoT Build 0:6ae2f7bca550 272 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_OPENING);
Azure.IoT Build 0:6ae2f7bca550 273 if (link_attach(message_receiver_instance->link, on_transfer_received, on_link_state_changed, NULL, message_receiver_instance) != 0)
Azure.IoT Build 0:6ae2f7bca550 274 {
Azure.IoT Build 0:6ae2f7bca550 275 result = __LINE__;
Azure.IoT Build 0:6ae2f7bca550 276 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 277 }
Azure.IoT Build 0:6ae2f7bca550 278 else
Azure.IoT Build 0:6ae2f7bca550 279 {
Azure.IoT Build 0:6ae2f7bca550 280 message_receiver_instance->on_message_received = on_message_received;
Azure.IoT Build 0:6ae2f7bca550 281 message_receiver_instance->callback_context = callback_context;
Azure.IoT Build 0:6ae2f7bca550 282
Azure.IoT Build 0:6ae2f7bca550 283 result = 0;
Azure.IoT Build 0:6ae2f7bca550 284 }
Azure.IoT Build 0:6ae2f7bca550 285 }
Azure.IoT Build 0:6ae2f7bca550 286 else
Azure.IoT Build 0:6ae2f7bca550 287 {
Azure.IoT Build 0:6ae2f7bca550 288 result = 0;
Azure.IoT Build 0:6ae2f7bca550 289 }
Azure.IoT Build 0:6ae2f7bca550 290 }
Azure.IoT Build 0:6ae2f7bca550 291
Azure.IoT Build 0:6ae2f7bca550 292 return result;
Azure.IoT Build 0:6ae2f7bca550 293 }
Azure.IoT Build 0:6ae2f7bca550 294
Azure.IoT Build 0:6ae2f7bca550 295 int messagereceiver_close(MESSAGE_RECEIVER_HANDLE message_receiver)
Azure.IoT Build 0:6ae2f7bca550 296 {
Azure.IoT Build 0:6ae2f7bca550 297 int result;
Azure.IoT Build 0:6ae2f7bca550 298
Azure.IoT Build 0:6ae2f7bca550 299 if (message_receiver == NULL)
Azure.IoT Build 0:6ae2f7bca550 300 {
Azure.IoT Build 0:6ae2f7bca550 301 result = __LINE__;
Azure.IoT Build 0:6ae2f7bca550 302 }
Azure.IoT Build 0:6ae2f7bca550 303 else
Azure.IoT Build 0:6ae2f7bca550 304 {
Azure.IoT Build 0:6ae2f7bca550 305 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)message_receiver;
Azure.IoT Build 0:6ae2f7bca550 306
Azure.IoT Build 0:6ae2f7bca550 307 if ((message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_OPENING) ||
Azure.IoT Build 0:6ae2f7bca550 308 (message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_OPEN))
Azure.IoT Build 0:6ae2f7bca550 309 {
Azure.IoT Build 0:6ae2f7bca550 310 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_CLOSING);
Azure.IoT Build 0:6ae2f7bca550 311
Azure.IoT Build 0:6ae2f7bca550 312 if (link_detach(message_receiver_instance->link) != 0)
Azure.IoT Build 0:6ae2f7bca550 313 {
Azure.IoT Build 0:6ae2f7bca550 314 result = __LINE__;
Azure.IoT Build 0:6ae2f7bca550 315 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 316 }
Azure.IoT Build 0:6ae2f7bca550 317 else
Azure.IoT Build 0:6ae2f7bca550 318 {
Azure.IoT Build 0:6ae2f7bca550 319 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_IDLE);
Azure.IoT Build 0:6ae2f7bca550 320 result = 0;
Azure.IoT Build 0:6ae2f7bca550 321 }
Azure.IoT Build 0:6ae2f7bca550 322 }
Azure.IoT Build 0:6ae2f7bca550 323 else
Azure.IoT Build 0:6ae2f7bca550 324 {
Azure.IoT Build 0:6ae2f7bca550 325 result = 0;
Azure.IoT Build 0:6ae2f7bca550 326 }
Azure.IoT Build 0:6ae2f7bca550 327 }
Azure.IoT Build 0:6ae2f7bca550 328
Azure.IoT Build 0:6ae2f7bca550 329 return result;
Azure.IoT Build 0:6ae2f7bca550 330 }