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