/
zmsg.c
1267 lines (1078 loc) · 35.5 KB
/
zmsg.c
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* =========================================================================
zmsg - working with multipart messages
Copyright (c) the Contributors as noted in the AUTHORS file.
This file is part of CZMQ, the high-level C binding for 0MQ:
http://czmq.zeromq.org.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
=========================================================================
*/
/*
@header
The zmsg class provides methods to send and receive multipart messages
across 0MQ sockets. This class provides a list-like container interface,
with methods to work with the overall container. zmsg_t messages are
composed of zero or more zframe_t frames.
@discuss
@end
*/
#include "czmq_classes.h"
// zmsg_t instances always have this tag as the first 4 octets of
// their data, which lets us do runtime object typing & validation.
#define ZMSG_TAG 0xcafe0003
// Structure of our class
struct _zmsg_t {
uint32_t tag; // Object tag for runtime detection
zlist_t *frames; // List of frames
size_t content_size; // Total content size
uint32_t routing_id; // Routing ID back to sender, if any
};
// --------------------------------------------------------------------------
// Constructor
zmsg_t *
zmsg_new (void)
{
zmsg_t *self = (zmsg_t *) zmalloc (sizeof (zmsg_t));
assert (self);
self->tag = ZMSG_TAG;
self->frames = zlist_new ();
assert (self->frames);
return self;
}
// --------------------------------------------------------------------------
// Destructor
void
zmsg_destroy (zmsg_t **self_p)
{
assert (self_p);
if (*self_p) {
zmsg_t *self = *self_p;
assert (zmsg_is (self));
zframe_t *frame;
while ((frame = (zframe_t *) zlist_pop (self->frames)))
zframe_destroy (&frame);
zlist_destroy (&self->frames);
self->tag = 0xDeadBeef;
freen (self);
*self_p = NULL;
}
}
// --------------------------------------------------------------------------
// Receive a message from a socket. The socket may be a libzmq socket, or a
// zsock_t object. Returns a new zmsg_t object if successful, or NULL if the
// receive was interrupted. The recv call blocks: if you want a non-blocking
// call, use the zloop or zpoller class to receive only from ready sockets.
// Keeps retrying if interrupted in a middle of a multipart sequence.
zmsg_t *
zmsg_recv (void *source)
{
assert (source);
zmsg_t *self = zmsg_new ();
if (!self)
return NULL;
while (true) {
zframe_t *frame = zframe_recv (source);
if (!frame) {
if (errno == EINTR && zlist_head (self->frames))
continue;
else {
zmsg_destroy (&self);
break; // Interrupted or terminated
}
}
#if defined (ZMQ_SERVER)
// Grab routing ID if we're reading from a SERVER socket (ZMQ 4.2 and later)
if (zsock_type (source) == ZMQ_SERVER)
self->routing_id = zframe_routing_id (frame);
#endif
if (zmsg_append (self, &frame)) {
zmsg_destroy (&self);
break;
}
if (!zsock_rcvmore (source))
break; // Last message frame
}
return self;
}
// --------------------------------------------------------------------------
// Send message to destination socket, and destroy the message after sending
// it successfully. If the message has no frames, sends nothing but destroys
// the message anyhow. Nullifies the caller's reference to the message (as
// it is a destructor).
// Keeps retrying if interrupted in a middle of a multipart sequence.
int
zmsg_send (zmsg_t **self_p, void *dest)
{
assert (self_p);
assert (dest);
zmsg_t *self = *self_p;
int rc = 0;
if (self) {
assert (zmsg_is (self));
bool sent_some = false;
zframe_t *frame;
while ((frame = (zframe_t *) zlist_head (self->frames))) {
zframe_set_routing_id (frame, self->routing_id);
rc = zframe_send (&frame, dest,
zlist_size (self->frames) > 1? ZFRAME_MORE: 0);
if (rc != 0) {
if (errno == EINTR && sent_some)
continue;
else
break;
}
sent_some = true;
(void) zlist_pop (self->frames);
}
if (rc == 0)
zmsg_destroy (self_p);
}
return rc;
}
// --------------------------------------------------------------------------
// Send message to destination socket as part of a multipart sequence, and
// destroy the message after sending it successfully. Note that after a
// zmsg_sendm, you must call zmsg_send or another method that sends a final
// message part. If the message has no frames, sends nothing but destroys
// the message anyhow. Nullifies the caller's reference to the message (as
// it is a destructor).
// Keeps retrying if interrupted in a middle of a multipart sequence.
int
zmsg_sendm (zmsg_t **self_p, void *dest)
{
assert (self_p);
assert (dest);
zmsg_t *self = *self_p;
int rc = 0;
if (self) {
assert (zmsg_is (self));
bool sent_some = false;
zframe_t *frame;
while ((frame = (zframe_t *) zlist_head (self->frames))) {
zframe_set_routing_id (frame, self->routing_id);
rc = zframe_send (&frame, dest, ZFRAME_MORE);
if (rc != 0) {
if (errno == EINTR && sent_some)
continue;
else
break;
}
sent_some = true;
(void) zlist_pop (self->frames);
}
if (rc == 0)
zmsg_destroy (self_p);
}
return rc;
}
// --------------------------------------------------------------------------
// Return size of message, i.e. number of frames (0 or more).
size_t
zmsg_size (zmsg_t *self)
{
assert (self);
assert (zmsg_is (self));
return zlist_size (self->frames);
}
// --------------------------------------------------------------------------
// Return total size of all frames in message.
size_t
zmsg_content_size (zmsg_t *self)
{
assert (self);
assert (zmsg_is (self));
return self->content_size;
}
// --------------------------------------------------------------------------
// Return message routing ID, if the message came from a ZMQ_SERVER socket.
// Else returns zero.
uint32_t
zmsg_routing_id (zmsg_t *self)
{
assert (self);
assert (zmsg_is (self));
return self->routing_id;
}
// --------------------------------------------------------------------------
// Set routing ID on message. This is used if/when the message is sent to a
// ZMQ_SERVER socket.
void
zmsg_set_routing_id (zmsg_t *self, uint32_t routing_id)
{
assert (self);
assert (zmsg_is (self));
self->routing_id = routing_id;
}
// --------------------------------------------------------------------------
// Push frame to the front of the message, i.e. before all other frames.
// Message takes ownership of frame, will destroy it when message is sent.
// Returns 0 on success, -1 on error. Deprecates zmsg_push, which did not
// nullify the caller's frame reference.
int
zmsg_prepend (zmsg_t *self, zframe_t **frame_p)
{
assert (self);
assert (zmsg_is (self));
assert (frame_p);
zframe_t *frame = *frame_p;
*frame_p = NULL; // We now own frame
self->content_size += zframe_size (frame);
zlist_push (self->frames, frame);
return 0;
}
// --------------------------------------------------------------------------
// Add frame to the end of the message, i.e. after all other frames.
// Message takes ownership of frame, will destroy it when message is sent.
// Returns 0 on success. Nullifies caller's reference to frame.
int
zmsg_append (zmsg_t *self, zframe_t **frame_p)
{
assert (self);
assert (zmsg_is (self));
assert (frame_p);
zframe_t *frame = *frame_p;
*frame_p = NULL; // We now own frame
self->content_size += zframe_size (frame);
zlist_append (self->frames, frame);
return 0;
}
// --------------------------------------------------------------------------
// Remove first frame from message, if any. Returns frame, or NULL. Caller
// now owns frame and must destroy it when finished with it.
zframe_t *
zmsg_pop (zmsg_t *self)
{
assert (self);
assert (zmsg_is (self));
zframe_t *frame = (zframe_t *) zlist_pop (self->frames);
if (frame)
self->content_size -= zframe_size (frame);
return frame;
}
// --------------------------------------------------------------------------
// Push block of memory to front of message, as a new frame.
// Returns 0 on success, -1 on error.
int
zmsg_pushmem (zmsg_t *self, const void *data, size_t size)
{
assert (self);
assert (zmsg_is (self));
zframe_t *frame = zframe_new (data, size);
assert (frame);
self->content_size += size;
zlist_push (self->frames, frame);
return 0;
}
// --------------------------------------------------------------------------
// Add block of memory to the end of the message, as a new frame.
// Returns 0 on success, -1 on error.
int
zmsg_addmem (zmsg_t *self, const void *data, size_t size)
{
assert (self);
assert (zmsg_is (self));
zframe_t *frame = zframe_new (data, size);
assert (frame);
self->content_size += size;
zlist_append (self->frames, frame);
return 0;
}
// --------------------------------------------------------------------------
// Push string as new frame to front of message.
// Returns 0 on success, -1 on error.
int
zmsg_pushstr (zmsg_t *self, const char *string)
{
assert (self);
assert (zmsg_is (self));
assert (string);
size_t len = strlen (string);
zframe_t *frame = zframe_new (string, len);
assert (frame);
self->content_size += len;
zlist_push (self->frames, frame);
return 0;
}
// --------------------------------------------------------------------------
// Push string as new frame to end of message.
// Returns 0 on success, -1 on error.
int
zmsg_addstr (zmsg_t *self, const char *string)
{
assert (self);
assert (zmsg_is (self));
assert (string);
size_t len = strlen (string);
zframe_t *frame = zframe_new (string, len);
assert (frame);
self->content_size += len;
zlist_append (self->frames, frame);
return 0;
}
// --------------------------------------------------------------------------
// Push formatted string as new frame to front of message.
// Returns 0 on success, -1 on error.
int
zmsg_pushstrf (zmsg_t *self, const char *format, ...)
{
assert (self);
assert (zmsg_is (self));
assert (format);
va_list argptr;
va_start (argptr, format);
char *string = zsys_vprintf (format, argptr);
va_end (argptr);
if (!string)
return -1;
size_t len = strlen (string);
zframe_t *frame = zframe_new (string, len);
assert (frame);
self->content_size += len;
zlist_push (self->frames, frame);
zstr_free (&string);
return 0;
}
// --------------------------------------------------------------------------
// Push formatted string as new frame to end of message.
// Returns 0 on success, -1 on error.
int
zmsg_addstrf (zmsg_t *self, const char *format, ...)
{
assert (self);
assert (zmsg_is (self));
assert (format);
va_list argptr;
va_start (argptr, format);
char *string = zsys_vprintf (format, argptr);
va_end (argptr);
if (!string)
return -1;
size_t len = strlen (string);
zframe_t *frame = zframe_new (string, len);
assert (frame);
self->content_size += len;
zlist_append (self->frames, frame);
zstr_free (&string);
return 0;
}
// --------------------------------------------------------------------------
// Pop frame off front of message, return as fresh string. If there were
// no more frames in the message, returns NULL.
char *
zmsg_popstr (zmsg_t *self)
{
assert (self);
assert (zmsg_is (self));
zframe_t *frame = (zframe_t *) zlist_pop (self->frames);
char *string = NULL;
if (frame) {
self->content_size -= zframe_size (frame);
string = zframe_strdup (frame);
zframe_destroy (&frame);
}
return string;
}
// --------------------------------------------------------------------------
// Push encoded message as a new frame. Message takes ownership of
// submessage, so the original is destroyed in this call. Returns 0 on
// success, -1 on error.
int
zmsg_addmsg (zmsg_t *self, zmsg_t **msg_p)
{
assert (self);
assert (zmsg_is (self));
assert (msg_p);
zframe_t *frame = zmsg_encode (*msg_p);
zmsg_append (self, &frame);
zmsg_destroy (msg_p);
return 0;
}
// --------------------------------------------------------------------------
// Remove first submessage from message, if any. Returns zmsg_t, or NULL if
// decoding was not successful. Caller now owns message and must destroy it
// when finished with it.
zmsg_t *
zmsg_popmsg (zmsg_t *self)
{
assert (self);
assert (zmsg_is (self));
zframe_t *frame = zmsg_pop (self);
if (!frame)
return NULL;
zmsg_t *msg = zmsg_decode (frame);
zframe_destroy (&frame);
return msg;
}
// --------------------------------------------------------------------------
// Remove specified frame from list, if present. Does not destroy frame.
void
zmsg_remove (zmsg_t *self, zframe_t *frame)
{
assert (self);
assert (zmsg_is (self));
// zlist_remove does not give feedback on whether an element was actually
// removed or not. To avoid wrongly decreasing the size in case the frame
// is not actually part of the zmsg, check the size before and after.
// We could do a search but that's a full scan, while the size is saved
// by zlist so checking it is much cheaper.
size_t num_frames_before = zlist_size (self->frames);
zlist_remove (self->frames, frame);
size_t num_frames_after = zlist_size (self->frames);
if (num_frames_before > num_frames_after)
self->content_size -= zframe_size (frame);
}
// --------------------------------------------------------------------------
// Set cursor to first frame in message. Returns frame, or NULL, if the
// message is empty. Use this to navigate the frames as a list.
zframe_t *
zmsg_first (zmsg_t *self)
{
assert (self);
assert (zmsg_is (self));
return (zframe_t *) zlist_first (self->frames);
}
// --------------------------------------------------------------------------
// Return the next frame. If there are no more frames, returns NULL. To move
// to the first frame call zmsg_first(). Advances the cursor.
zframe_t *
zmsg_next (zmsg_t *self)
{
assert (self);
assert (zmsg_is (self));
return (zframe_t *) zlist_next (self->frames);
}
// --------------------------------------------------------------------------
// Return the last frame. If there are no frames, returns NULL.
zframe_t *
zmsg_last (zmsg_t *self)
{
assert (self);
assert (zmsg_is (self));
return (zframe_t *) zlist_last (self->frames);
}
// --------------------------------------------------------------------------
// Save message to an open file, return 0 if OK, else -1. The message is
// saved as a series of frames, each with length and data. Note that the
// file is NOT guaranteed to be portable between operating systems, not
// versions of CZMQ. The file format is at present undocumented and liable
// to arbitrary change.
int
zmsg_save (zmsg_t *self, FILE *file)
{
assert (self);
assert (zmsg_is (self));
assert (file);
zframe_t *frame = zmsg_first (self);
while (frame) {
size_t frame_size = zframe_size (frame);
if (fwrite (&frame_size, sizeof (frame_size), 1, file) != 1)
return -1;
if (fwrite (zframe_data (frame), frame_size, 1, file) != 1)
return -1;
frame = zmsg_next (self);
}
return 0;
}
// --------------------------------------------------------------------------
// Load/append an open file into new message, return the message.
// Returns NULL if the message could not be loaded.
zmsg_t *
zmsg_load (FILE *file)
{
zmsg_t *self = zmsg_new ();
assert (self);
assert (file);
while (true) {
size_t frame_size;
if (fread (&frame_size, sizeof (frame_size), 1, file) == 1) {
zframe_t *frame = zframe_new (NULL, frame_size);
if (fread (zframe_data (frame), frame_size, 1, file) == 1
&& frame_size > 0)
zmsg_append (self, &frame);
else {
zframe_destroy (&frame);
zmsg_destroy (&self);
break;
}
}
else
break; // Unable to read properly, quit
}
return self;
}
// --------------------------------------------------------------------------
// Serialize multipart message to a single message frame. Use this method
// to send structured messages across transports that do not support
// multipart data. Allocates and returns a new frame containing the
// serialized message. To decode a serialized message frame, use
// zmsg_decode ().
//
// Frame lengths are encoded as 1 or 1+4 bytes
// 0..254 bytes octet + data
// 255..4Gb-1 bytes 0xFF + 4octet + data
zframe_t *
zmsg_encode (zmsg_t *self)
{
assert (self);
assert (zmsg_is (self));
// Calculate real size of frame
size_t total_size = 0;
zframe_t *frame = zmsg_first (self);
while (frame) {
size_t frame_size = zframe_size (frame);
if (frame_size < 255)
total_size += frame_size + 1;
else
total_size += frame_size + 1 + 4;
frame = zmsg_next (self);
}
zframe_t *encoded = zframe_new (NULL, total_size);
assert (encoded);
// Encode message into frame
byte *dest = zframe_data (encoded);
frame = zmsg_first (self);
while (frame) {
size_t frame_size = zframe_size (frame);
if (frame_size < 255) {
*dest++ = (byte) frame_size;
memcpy (dest, zframe_data (frame), frame_size);
dest += frame_size;
}
else {
*dest++ = 0xFF;
*dest++ = (frame_size >> 24) & 255;
*dest++ = (frame_size >> 16) & 255;
*dest++ = (frame_size >> 8) & 255;
*dest++ = frame_size & 255;
memcpy (dest, zframe_data (frame), frame_size);
dest += frame_size;
}
frame = zmsg_next (self);
}
assert ((size_t) (dest - zframe_data (encoded)) == total_size);
return encoded;
}
// --------------------------------------------------------------------------
// Decodes a serialized message frame created by zmsg_encode () and returns
// a new zmsg_t object. Returns NULL if the frame was badly formatted or
// there was insufficient memory to work.
zmsg_t *
zmsg_decode (zframe_t *frame)
{
assert (frame);
zmsg_t *self = zmsg_new ();
assert (self);
const byte *source = zframe_data (frame);
const byte *limit = zframe_data (frame) + zframe_size (frame);
while (source < limit) {
size_t frame_size = *source++;
if (frame_size == 255) {
if (source > limit - 4) {
zmsg_destroy (&self);
break;
}
frame_size = (source [0] << 24)
+ (source [1] << 16)
+ (source [2] << 8)
+ source [3];
source += 4;
}
if (source > limit - frame_size) {
zmsg_destroy (&self);
break;
}
zframe_t *decoded = zframe_new (source, frame_size);
if (!decoded) {
zmsg_destroy (&self);
break;
}
zmsg_append (self, &decoded);
source += frame_size;
}
return self;
}
// --------------------------------------------------------------------------
// Create copy of message, as new message object. Returns a fresh zmsg_t
// object. If message is null, or memory was exhausted, returns null.
zmsg_t *
zmsg_dup (zmsg_t *self)
{
if (self) {
assert (zmsg_is (self));
zmsg_t *copy = zmsg_new ();
assert (copy);
zframe_t *frame = zmsg_first (self);
while (frame) {
zmsg_addmem (copy, zframe_data (frame), zframe_size (frame));
frame = zmsg_next (self);
}
return copy;
}
else
return NULL;
}
// --------------------------------------------------------------------------
// Send message to zsys log sink (may be stdout, or system facility as
// configured by zsys_set_logstream).
void
zmsg_print_n (zmsg_t *self, size_t size)
{
assert (self);
assert (zmsg_is (self));
if (!self) {
zsys_debug ("(NULL)");
return;
}
zframe_t *frame = zmsg_first (self);
while (frame) {
zframe_print_n (frame, NULL, size);
frame = zmsg_next (self);
}
}
void
zmsg_print (zmsg_t *self)
{
zmsg_print_n (self, 0);
}
// --------------------------------------------------------------------------
// Return true if the two messages have the same number of frames and each
// frame in the first message is identical to the corresponding frame in the
// other message. As with zframe_eq, return false if either message is NULL.
bool
zmsg_eq (zmsg_t *self, zmsg_t *other)
{
if (!self || !other)
return false;
if (zlist_size (self->frames) != zlist_size (other->frames))
return false;
zframe_t *self_frame = (zframe_t *) zlist_first (self->frames);
zframe_t *other_frame = (zframe_t *) zlist_first (other->frames);
while (self_frame && other_frame) {
if (!zframe_eq (self_frame, other_frame))
return false;
self_frame = (zframe_t *) zlist_next (self->frames);
other_frame = (zframe_t *) zlist_next (other->frames);
}
return true;
}
// --------------------------------------------------------------------------
// Generate a signal message encoding the given status. A signal is a short
// message carrying a 1-byte success/failure code (by convention, 0 means
// OK). Signals are encoded to be distinguishable from "normal" messages.
zmsg_t *
zmsg_new_signal (byte status)
{
zmsg_t *self = zmsg_new ();
int64_t signal_value = 0x7766554433221100L + status;
zmsg_addmem (self, &signal_value, 8);
return self;
}
// --------------------------------------------------------------------------
// Return signal value, 0 or greater, if message is a signal, -1 if not.
int
zmsg_signal (zmsg_t *self)
{
if (zmsg_size (self) == 1
&& zmsg_content_size (self) == 8) {
zframe_t *frame = zmsg_first (self);
int64_t signal_value = *((int64_t *) zframe_data (frame));
if ((signal_value & 0xFFFFFFFFFFFFFF00L) == 0x7766554433221100L)
return signal_value & 255;
}
return -1;
}
// --------------------------------------------------------------------------
// Probe the supplied object, and report if it looks like a zmsg_t.
bool
zmsg_is (void *self)
{
assert (self);
return ((zmsg_t *) self)->tag == ZMSG_TAG;
}
// --------------------------------------------------------------------------
// DEPRECATED as over-engineered, poor style
// Pop frame off front of message, caller now owns frame
// If next frame is empty, pops and destroys that empty frame.
zframe_t *
zmsg_unwrap (zmsg_t *self)
{
assert (self);
zframe_t *frame = zmsg_pop (self);
zframe_t *empty = zmsg_first (self);
if (empty && zframe_size (empty) == 0) {
empty = zmsg_pop (self);
zframe_destroy (&empty);
}
return frame;
}
// --------------------------------------------------------------------------
// DEPRECATED as poor style -- callers should use zloop or zpoller
// Receive message from socket, returns zmsg_t object, or NULL either if
// there was no input waiting, or the recv was interrupted.
zmsg_t *
zmsg_recv_nowait (void *source)
{
assert (source);
zmsg_t *self = zmsg_new ();
if (!self)
return NULL;
while (true) {
zframe_t *frame = zframe_recv_nowait (source);
if (!frame) {
if (errno == EINTR && zlist_head (self->frames))
continue;
else {
zmsg_destroy (&self);
break; // Interrupted or terminated
}
}
#if defined (ZMQ_SERVER)
// Grab routing ID if we're reading from a SERVER socket (ZMQ 4.2 and later)
if (zsock_type (source) == ZMQ_SERVER)
self->routing_id = zframe_routing_id (frame);
#endif
if (zmsg_append (self, &frame)) {
zmsg_destroy (&self);
break;
}
if (!zsock_rcvmore (source))
break; // Last message frame
}
return self;
}
// --------------------------------------------------------------------------
// DEPRECATED as unsafe -- does not nullify frame reference.
// Push frame plus empty frame to front of message, before first frame.
// Message takes ownership of frame, will destroy it when message is sent.
void
zmsg_wrap (zmsg_t *self, zframe_t *frame)
{
assert (self);
assert (frame);
if (zmsg_pushmem (self, "", 0) == 0)
zmsg_push (self, frame);
}
// --------------------------------------------------------------------------
// DEPRECATED by zmsg_prepend ().
// Push frame to the front of the message, i.e. before all other frames.
// Message takes ownership of frame, will destroy it when message is sent.
// Returns 0 on success, -1 on error.
int
zmsg_push (zmsg_t *self, zframe_t *frame)
{
assert (self);
assert (frame);
self->content_size += zframe_size (frame);
zlist_push (self->frames, frame);
return 0;
}
// --------------------------------------------------------------------------
// DEPRECATED by zmsg_append ().
// Add frame to the end of the message, i.e. after all other frames.
// Message takes ownership of frame, will destroy it when message is sent.
// Returns 0 on success.
int
zmsg_add (zmsg_t *self, zframe_t *frame)
{
assert (self);
assert (frame);
self->content_size += zframe_size (frame);
zlist_append (self->frames, frame);
return 0;
}
// --------------------------------------------------------------------------
// DEPRECATED as inconsistent; breaks principle that logging should all go
// to a single destination.
// Dump message to FILE stream, for debugging and tracing.
// Truncates to first 10 frames, for readability.
void
zmsg_fprint (zmsg_t *self, FILE *file)
{
assert (self);
assert (zmsg_is (self));
fprintf (file, "--------------------------------------\n");
if (!self) {
fprintf (file, "NULL");
return;
}
zframe_t *frame = zmsg_first (self);
int frame_nbr = 0;
while (frame && frame_nbr++ < 10) {
zframe_fprint (frame, NULL, file);
frame = zmsg_next (self);
}
}
// --------------------------------------------------------------------------
// Selftest
void
zmsg_test (bool verbose)
{
printf (" * zmsg: ");
int rc = 0;
// @selftest
// Create two PAIR sockets and connect over inproc
zsock_t *output = zsock_new_pair ("@inproc://zmsg.test");
assert (output);
zsock_t *input = zsock_new_pair (">inproc://zmsg.test");
assert (input);
// Test send and receive of single-frame message
zmsg_t *msg = zmsg_new ();
assert (msg);
zframe_t *frame = zframe_new ("Hello", 5);
assert (frame);
zmsg_prepend (msg, &frame);
assert (zmsg_size (msg) == 1);
assert (zmsg_content_size (msg) == 5);
rc = zmsg_send (&msg, output);
assert (msg == NULL);
assert (rc == 0);
msg = zmsg_recv (input);
assert (msg);
assert (zmsg_size (msg) == 1);
assert (zmsg_content_size (msg) == 5);
zmsg_destroy (&msg);