forked from net-amqp-rabbitmq/net-amqp-rabbitmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RabbitMQ.xs
1981 lines (1734 loc) · 57.5 KB
/
RabbitMQ.xs
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
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <float.h>
#include <math.h>
/* perl -MDevel::PPPort -e'Devel::PPPort::WriteFile();' */
/* perl ppport.h --compat-version=5.8.0 --cplusplus RabbitMQ.xs */
#define NEED_newSVpvn_flags
#include "ppport.h"
/* ppport.h knows about MUTABLE_PTR and MUTABLE_SV, but not these?! */
#ifndef MUTABLE_AV
# define MUTABLE_AV(p) ((AV*)MUTABLE_PTR(p))
#endif
#ifndef MUTABLE_HV
# define MUTABLE_HV(p) ((HV*)MUTABLE_PTR(p))
#endif
#include "amqp.h"
#include "amqp_tcp_socket.h"
#include "amqp_ssl_socket.h"
/* For struct timeval */
#include "amqp_time.h"
/* This is for the Math::UInt64 integration */
#include "perl_math_int64.h"
/* perl Makefile.PL; make CCFLAGS=-DDEBUG */
#if DEBUG
#define __DEBUG__(X) X
#else
#define __DEBUG__(X) /* NOOP */
#endif
typedef amqp_connection_state_t Net__AMQP__RabbitMQ;
#define AMQP_STATUS_UNKNOWN_TYPE 0x500
#ifdef USE_LONG_DOUBLE
/* stolen from Cpanel::JSON::XS
* so we don't mess up double => long double for perls with -Duselongdouble */
#if defined(_AIX) && (!defined(HAS_LONG_DOUBLE) || AIX_WORKAROUND)
#define HAVE_NO_POWL
#endif
#ifdef HAVE_NO_POWL
/* Ulisse Monari: this is a patch for AIX 5.3, perl 5.8.8 without HAS_LONG_DOUBLE
There Perl_pow maps to pow(...) - NOT TO powl(...), core dumps at Perl_pow(...)
Base code is from http://bytes.com/topic/c/answers/748317-replacement-pow-function
This is my change to fs_pow that goes into libc/libm for calling fmod/exp/log.
NEED TO MODIFY Makefile, after perl Makefile.PL by adding "-lm" onto the LDDLFLAGS line */
static double fs_powEx(double x, double y)
{
double p = 0;
if (0 > x && fmod(y, 1) == 0) {
if (fmod(y, 2) == 0) {
p = exp(log(-x) * y);
} else {
p = -exp(log(-x) * y);
}
} else {
if (x != 0 || 0 >= y) {
p = exp(log( x) * y);
}
}
return p;
}
/* powf() unfortunately is not accurate enough */
const NV DOUBLE_POW = fs_powEx(10., DBL_DIG );
#else
const NV DOUBLE_POW = Perl_pow(10., DBL_DIG );
#endif
#endif
/* This is a place to put some stuff that we convert from perl,
it's transient and we recycle it as soon as it's finished being used
which means we keep memory we've used with the aim of reusing it */
/* temp_memory_pool is ugly and suffers from code smell */
static amqp_pool_t temp_memory_pool;
/* Parallels amqp_maybe_release_buffers */
static void maybe_release_buffers(amqp_connection_state_t state) {
if (amqp_release_buffers_ok(state)) {
amqp_release_buffers(state);
recycle_amqp_pool(&temp_memory_pool);
}
}
#define int_from_hv(hv,name) \
do { SV **v; if(NULL != (v = hv_fetchs(hv, #name, 0))) name = SvIV(*v); } while(0)
#define double_from_hv(hv,name) \
do { SV **v; if(NULL != (v = hv_fetchs(hv, #name, 0))) name = SvNV(*v); } while(0)
#define str_from_hv(hv,name) \
do { SV **v; if(NULL != (v = hv_fetchs(hv, #name, 0))) name = SvPV_nolen(*v); } while(0)
#define has_valid_connection(conn) \
( amqp_get_socket( conn ) != NULL && amqp_get_sockfd( conn ) > -1 )
#define assert_amqp_connected(conn) \
do { \
if ( ! has_valid_connection(conn) ) { \
Perl_croak(aTHX_ "AMQP socket not connected"); \
} \
} while(0)
void hash_to_amqp_table(HV *hash, amqp_table_t *table, short force_utf8);
void array_to_amqp_array(AV *perl_array, amqp_array_t *mq_array, short force_utf8);
SV* mq_array_to_arrayref(amqp_array_t *array);
SV* mq_table_to_hashref(amqp_table_t *table);
void die_on_error(pTHX_ int x, amqp_connection_state_t conn, char const *context) {
/* Handle socket errors */
if ( x == AMQP_STATUS_CONNECTION_CLOSED || x == AMQP_STATUS_SOCKET_ERROR ) {
amqp_socket_close( amqp_get_socket( conn ) );
Perl_croak(aTHX_ "%s failed because AMQP socket connection was closed.", context);
}
/* Handle everything else */
else if (x < 0) {
Perl_croak(aTHX_ "%s: %s\n", context, amqp_error_string2(x));
}
}
void die_on_amqp_error(pTHX_ amqp_rpc_reply_t x, amqp_connection_state_t conn, char const *context) {
switch (x.reply_type) {
case AMQP_RESPONSE_NORMAL:
return;
case AMQP_RESPONSE_NONE:
Perl_croak(aTHX_ "%s: missing RPC reply type!", context);
break;
case AMQP_RESPONSE_LIBRARY_EXCEPTION:
/* If we got a library error saying that there's a socket problem,
kill the connection and croak. */
if (
x.library_error == AMQP_STATUS_CONNECTION_CLOSED
||
x.library_error == AMQP_STATUS_SOCKET_ERROR
) {
amqp_socket_close( amqp_get_socket( conn ) );
Perl_croak(aTHX_ "%s: failed since AMQP socket connection closed.\n", context);
}
/* Otherwise, give a more generic croak. */
else {
Perl_croak(aTHX_ "%s: %s\n", context,
(!x.library_error) ? "(end-of-stream)" :
(x.library_error == AMQP_STATUS_UNKNOWN_TYPE) ? "unknown AMQP type id" :
amqp_error_string2(x.library_error));
}
break;
case AMQP_RESPONSE_SERVER_EXCEPTION:
switch (x.reply.id) {
case AMQP_CONNECTION_CLOSE_METHOD:
{
amqp_connection_close_ok_t req;
req.dummy = '\0';
/* res = */ amqp_send_method(conn, 0, AMQP_CONNECTION_CLOSE_OK_METHOD, &req);
}
amqp_set_socket(conn, NULL);
{
amqp_connection_close_t *m = (amqp_connection_close_t *) x.reply.decoded;
Perl_croak(aTHX_ "%s: server connection error %d, message: %.*s",
context,
m->reply_code,
(int) m->reply_text.len, (char *) m->reply_text.bytes);
}
break;
case AMQP_CHANNEL_CLOSE_METHOD:
/* We don't know what channel provoked this error!
This information should be in amqp_rpc_reply_t, but it isn't.
{
amqp_channel_close_ok_t req;
req.dummy = '\0';
/ * res = * / amqp_send_method(conn, channel, AMQP_CHANNEL_CLOSE_OK_METHOD, &req);
}
*/
/* Only the channel should be invalidated, but we have no means of doing so! */
/* Even if we knew which channel we needed to invalidate! */
amqp_set_socket(conn, NULL);
{
amqp_channel_close_t *m = (amqp_channel_close_t *) x.reply.decoded;
Perl_croak(aTHX_ "%s: server channel error %d, message: %.*s",
context,
m->reply_code,
(int) m->reply_text.len, (char *) m->reply_text.bytes);
}
break;
default:
Perl_croak(aTHX_ "%s: unknown server error, method id 0x%08X", context, x.reply.id);
break;
}
break;
}
}
/*
* amqp_kind_for_sv(SV**)
* Note: We could handle more types here... but we're trying to take Perl and go to
* C. We don't really need to handle much more than this from what I can tell.
*/
amqp_field_value_kind_t amqp_kind_for_sv(SV** perl_value, short force_utf8) {
switch (SvTYPE( *perl_value ))
{
// Integer types (and references beyond 5.10)
case SVt_IV:
// References
if ( SvROK( *perl_value ) ) {
// Array Reference
if ( SvTYPE( SvRV( *perl_value ) ) == SVt_PVAV ) {
return AMQP_FIELD_KIND_ARRAY;
}
// Hash Reference
if ( SvTYPE( SvRV( *perl_value ) ) == SVt_PVHV ) {
return AMQP_FIELD_KIND_TABLE;
}
Perl_croak(
aTHX_ "Unsupported Perl Reference Type: %d",
SvTYPE( SvRV( *perl_value ) )
);
}
// Regular integers
// In the event that it could be unsigned
if ( SvUOK( *perl_value ) ) {
return AMQP_FIELD_KIND_U64;
}
return AMQP_FIELD_KIND_I64;
// Numeric type
case SVt_NV:
return AMQP_FIELD_KIND_F64;
// String (handle types which are upgraded to handle IV/UV/NV as well as PV)
case SVt_PVIV:
if ( SvI64OK( *perl_value ) ) {
return AMQP_FIELD_KIND_I64;
}
if ( SvU64OK( *perl_value ) ) {
return AMQP_FIELD_KIND_U64;
}
// It could be a PV or an IV/UV!
if ( SvIOK( *perl_value ) ) {
if ( SvUOK( *perl_value ) ) {
return AMQP_FIELD_KIND_U64;
}
return AMQP_FIELD_KIND_I64;
}
case SVt_PVNV:
// It could be a PV or an NV
if ( SvNOK( *perl_value ) ) {
return AMQP_FIELD_KIND_F64;
}
case SVt_PV:
// UTF-8?
if ( force_utf8 || SvUTF8( *perl_value ) ) {
return AMQP_FIELD_KIND_UTF8;
}
return AMQP_FIELD_KIND_BYTES;
case SVt_PVMG:
if ( SvPOK( *perl_value ) || SvPOKp( *perl_value ) ) {
if ( force_utf8 || SvUTF8( *perl_value ) ) {
return AMQP_FIELD_KIND_UTF8;
}
return AMQP_FIELD_KIND_BYTES;
}
if ( SvIOK( *perl_value ) || SvIOKp( *perl_value ) ) {
if ( SvUOK( *perl_value ) ) {
return AMQP_FIELD_KIND_U64;
}
return AMQP_FIELD_KIND_I64;
}
if ( SvNOK( *perl_value ) || SvNOKp( *perl_value ) ) {
return AMQP_FIELD_KIND_F64;
}
default:
if ( SvROK( *perl_value ) ) {
// Array Reference
if ( SvTYPE( SvRV( *perl_value ) ) == SVt_PVAV ) {
return AMQP_FIELD_KIND_ARRAY;
}
// Hash Reference
if ( SvTYPE( SvRV( *perl_value ) ) == SVt_PVHV ) {
return AMQP_FIELD_KIND_TABLE;
}
Perl_croak(
aTHX_ "Unsupported Perl Reference Type: %d",
SvTYPE( SvRV( *perl_value ) )
);
}
Perl_croak(
aTHX_ "Unsupported scalar type detected >%s<(%d)",
SvPV_nolen(*perl_value),
SvTYPE( *perl_value )
);
}
/* If we're still here... wtf */
Perl_croak( aTHX_ "The wheels have fallen off. Please call for help." );
}
/* Parallels amqp_read_message */
static amqp_rpc_reply_t read_message(amqp_connection_state_t state, amqp_channel_t channel, SV **props_sv_ptr, SV **body_sv_ptr) {
HV *props_hv;
SV *body_sv;
amqp_rpc_reply_t ret;
int res;
amqp_frame_t frame;
int is_utf8_body = 1; /* The body is UTF-8 by default */
memset(&ret, 0, sizeof(amqp_rpc_reply_t));
res = amqp_simple_wait_frame_on_channel(state, channel, &frame);
if (AMQP_STATUS_OK != res) {
ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
ret.library_error = res;
goto error_out1;
}
if (AMQP_FRAME_HEADER != frame.frame_type) {
if (AMQP_FRAME_METHOD == frame.frame_type &&
(AMQP_CHANNEL_CLOSE_METHOD == frame.payload.method.id ||
AMQP_CONNECTION_CLOSE_METHOD == frame.payload.method.id)) {
ret.reply_type = AMQP_RESPONSE_SERVER_EXCEPTION;
ret.reply = frame.payload.method;
} else {
ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
ret.library_error = AMQP_STATUS_UNEXPECTED_STATE;
amqp_put_back_frame(state, &frame);
}
goto error_out1;
}
{
amqp_basic_properties_t *p;
props_hv = newHV();
p = (amqp_basic_properties_t *) frame.payload.properties.decoded;
if (p->_flags & AMQP_BASIC_CONTENT_TYPE_FLAG) {
hv_stores(props_hv, "content_type", newSVpvn(p->content_type.bytes, p->content_type.len));
}
if (p->_flags & AMQP_BASIC_CONTENT_ENCODING_FLAG) {
hv_stores(props_hv, "content_encoding", newSVpvn(p->content_encoding.bytes, p->content_encoding.len));
/*
* Since we could have UTF-8 in our content-encoding, and most people seem like they
* treat this like the default, we're looking for the presence of content-encoding but
* the absence of a case-insensitive "UTF-8".
*/
if (
strnlen(p->content_encoding.bytes, p->content_encoding.len) > 0
&&
(strncasecmp(p->content_encoding.bytes, "UTF-8", p->content_encoding.len) != 0)
) {
is_utf8_body = 0;
}
}
if (p->_flags & AMQP_BASIC_CORRELATION_ID_FLAG) {
hv_stores(props_hv, "correlation_id", newSVpvn(p->correlation_id.bytes, p->correlation_id.len));
}
if (p->_flags & AMQP_BASIC_REPLY_TO_FLAG) {
hv_stores(props_hv, "reply_to", newSVpvn(p->reply_to.bytes, p->reply_to.len));
}
if (p->_flags & AMQP_BASIC_EXPIRATION_FLAG) {
hv_stores(props_hv, "expiration", newSVpvn(p->expiration.bytes, p->expiration.len));
}
if (p->_flags & AMQP_BASIC_MESSAGE_ID_FLAG) {
hv_stores(props_hv, "message_id", newSVpvn(p->message_id.bytes, p->message_id.len));
}
if (p->_flags & AMQP_BASIC_TYPE_FLAG) {
hv_stores(props_hv, "type", newSVpvn(p->type.bytes, p->type.len));
}
if (p->_flags & AMQP_BASIC_USER_ID_FLAG) {
hv_stores(props_hv, "user_id", newSVpvn(p->user_id.bytes, p->user_id.len));
}
if (p->_flags & AMQP_BASIC_APP_ID_FLAG) {
hv_stores(props_hv, "app_id", newSVpvn(p->app_id.bytes, p->app_id.len));
}
if (p->_flags & AMQP_BASIC_DELIVERY_MODE_FLAG) {
hv_stores(props_hv, "delivery_mode", newSViv(p->delivery_mode));
}
if (p->_flags & AMQP_BASIC_PRIORITY_FLAG) {
hv_stores(props_hv, "priority", newSViv(p->priority));
}
if (p->_flags & AMQP_BASIC_TIMESTAMP_FLAG) {
hv_stores(props_hv, "timestamp", newSViv(p->timestamp));
}
if (p->_flags & AMQP_BASIC_HEADERS_FLAG) {
int i;
HV *headers = newHV();
hv_stores(props_hv, "headers", newRV_noinc(MUTABLE_SV(headers)));
__DEBUG__( dump_table( p->headers ) );
for( i=0; i < p->headers.num_entries; ++i ) {
amqp_table_entry_t *header_entry = &(p->headers.entries[i]);
__DEBUG__(
fprintf(stderr,
"~~~ Length: %ld/%d, Key: %.*s, Kind: %c\n",
header_entry->key.len,
(int)header_entry->key.len,
(int)header_entry->key.len,
(char*)header_entry->key.bytes,
header_entry->value.kind
)
);
switch (header_entry->value.kind) {
case AMQP_FIELD_KIND_BOOLEAN:
hv_store( headers,
header_entry->key.bytes, header_entry->key.len,
newSViv(header_entry->value.value.boolean),
0
);
break;
// Integer types
case AMQP_FIELD_KIND_I8:
hv_store( headers,
header_entry->key.bytes, header_entry->key.len,
newSViv(header_entry->value.value.i8),
0
);
break;
case AMQP_FIELD_KIND_I16:
hv_store( headers,
header_entry->key.bytes, header_entry->key.len,
newSViv(header_entry->value.value.i16),
0
);
break;
case AMQP_FIELD_KIND_I32:
hv_store( headers,
header_entry->key.bytes, header_entry->key.len,
newSViv(header_entry->value.value.i32),
0
);
break;
case AMQP_FIELD_KIND_I64:
hv_store( headers,
header_entry->key.bytes, header_entry->key.len,
newSVi64(header_entry->value.value.i64),
0
);
break;
case AMQP_FIELD_KIND_U8:
hv_store( headers,
header_entry->key.bytes, header_entry->key.len,
newSVuv(header_entry->value.value.u8),
0
);
break;
case AMQP_FIELD_KIND_U16:
hv_store( headers,
header_entry->key.bytes, header_entry->key.len,
newSVuv(header_entry->value.value.u16),
0
);
break;
case AMQP_FIELD_KIND_U32:
hv_store( headers,
header_entry->key.bytes, header_entry->key.len,
newSVuv(header_entry->value.value.u32),
0
);
break;
case AMQP_FIELD_KIND_U64:
hv_store( headers,
header_entry->key.bytes, header_entry->key.len,
newSVu64(header_entry->value.value.u64),
0
);
break;
// Floating point precision
case AMQP_FIELD_KIND_F32:
hv_store( headers,
header_entry->key.bytes, header_entry->key.len,
newSVnv(header_entry->value.value.f32),
0
);
break;
case AMQP_FIELD_KIND_F64:
// TODO: I don't think this is a natively supported type on all Perls.
hv_store( headers,
header_entry->key.bytes, header_entry->key.len,
#ifdef USE_LONG_DOUBLE
/* amqp uses doubles, if perl is -Duselongdouble it messes up the precision
* so we always want take the max precision from a double and discard the rest
* because it can't be any more precise than a double */
newSVnv( ( rint( header_entry->value.value.f64 * DOUBLE_POW ) / DOUBLE_POW ) ),
#else
/* both of these are doubles so it's ok */
newSVnv( header_entry->value.value.f64 ),
#endif
0
);
break;
// Handle kind UTF8 and kind BYTES
case AMQP_FIELD_KIND_UTF8:
case AMQP_FIELD_KIND_BYTES:
hv_store( headers,
header_entry->key.bytes, header_entry->key.len,
newSVpvn_utf8(
header_entry->value.value.bytes.bytes,
header_entry->value.value.bytes.len,
AMQP_FIELD_KIND_UTF8 == header_entry->value.kind
),
0
);
break;
// Handle arrays
case AMQP_FIELD_KIND_ARRAY:
__DEBUG__(
fprintf(stderr, "ARRAY KIND FOR KEY:>%.*s< KIND:>%c< AMQP_FIELD_KIND_ARRAY:[%c].\n",
(int)header_entry->key.len,
(char*)header_entry->key.bytes,
header_entry->value.kind,
AMQP_FIELD_KIND_ARRAY
)
);
hv_store( headers,
header_entry->key.bytes, header_entry->key.len,
mq_array_to_arrayref( &header_entry->value.value.array ),
0
);
break;
// Handle tables (hashes when translated to Perl)
case AMQP_FIELD_KIND_TABLE:
hv_store( headers,
header_entry->key.bytes, header_entry->key.len,
mq_table_to_hashref( &header_entry->value.value.table ),
0
);
break;
default:
ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
ret.library_error = AMQP_STATUS_UNKNOWN_TYPE;
goto error_out2;
}
}
}
}
{
char *body;
size_t body_target = frame.payload.properties.body_size;
size_t body_remaining = body_target;
body_sv = newSV(0);
sv_grow(body_sv, body_target + 1);
SvCUR_set(body_sv, body_target);
SvPOK_on(body_sv);
if (is_utf8_body)
SvUTF8_on(body_sv);
body = SvPVX(body_sv);
while (body_remaining > 0) {
size_t fragment_len;
res = amqp_simple_wait_frame_on_channel(state, channel, &frame);
if (AMQP_STATUS_OK != res) {
ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
ret.library_error = res;
goto error_out3;
}
if (AMQP_FRAME_BODY != frame.frame_type) {
if (AMQP_FRAME_METHOD == frame.frame_type &&
(AMQP_CHANNEL_CLOSE_METHOD == frame.payload.method.id ||
AMQP_CONNECTION_CLOSE_METHOD == frame.payload.method.id)) {
ret.reply_type = AMQP_RESPONSE_SERVER_EXCEPTION;
ret.reply = frame.payload.method;
} else {
ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
ret.library_error = AMQP_STATUS_BAD_AMQP_DATA;
}
goto error_out3;
}
fragment_len = frame.payload.body_fragment.len;
if (fragment_len > body_remaining) {
ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
ret.library_error = AMQP_STATUS_BAD_AMQP_DATA;
goto error_out3;
}
memcpy(body, frame.payload.body_fragment.bytes, fragment_len);
body += fragment_len;
body_remaining -= fragment_len;
}
*body = '\0';
}
*props_sv_ptr = newRV_noinc(MUTABLE_SV(props_hv));
*body_sv_ptr = body_sv;
ret.reply_type = AMQP_RESPONSE_NORMAL;
return ret;
error_out3:
SvREFCNT_dec(props_hv);
error_out2:
SvREFCNT_dec(body_sv);
error_out1:
*props_sv_ptr = &PL_sv_undef;
*body_sv_ptr = &PL_sv_undef;
return ret;
}
/* Parallels amqp_consume_message */
static amqp_rpc_reply_t consume_message(amqp_connection_state_t state, SV **envelope_sv_ptr, struct timeval *timeout) {
amqp_rpc_reply_t ret;
HV *envelope_hv;
int res;
amqp_frame_t frame;
amqp_channel_t channel;
SV *props;
SV *body;
memset(&ret, 0, sizeof(amqp_rpc_reply_t));
*envelope_sv_ptr = &PL_sv_undef;
res = amqp_simple_wait_frame_noblock(state, &frame, timeout);
if (AMQP_STATUS_OK != res) {
ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
ret.library_error = res;
goto error_out1;
}
if (AMQP_FRAME_METHOD != frame.frame_type ||
AMQP_BASIC_DELIVER_METHOD != frame.payload.method.id) {
if (AMQP_FRAME_METHOD == frame.frame_type &&
(AMQP_CHANNEL_CLOSE_METHOD == frame.payload.method.id ||
AMQP_CONNECTION_CLOSE_METHOD == frame.payload.method.id)) {
ret.reply_type = AMQP_RESPONSE_SERVER_EXCEPTION;
ret.reply = frame.payload.method;
} else {
amqp_put_back_frame(state, &frame);
ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
ret.library_error = AMQP_STATUS_UNEXPECTED_STATE;
}
goto error_out1;
}
channel = frame.channel;
envelope_hv = newHV();
{
amqp_basic_deliver_t *d = (amqp_basic_deliver_t *) frame.payload.method.decoded;
hv_stores(envelope_hv, "channel", newSViv(channel));
hv_stores(envelope_hv, "delivery_tag", newSVu64(d->delivery_tag));
hv_stores(envelope_hv, "redelivered", newSViv(d->redelivered));
hv_stores(envelope_hv, "exchange", newSVpvn(d->exchange.bytes, d->exchange.len));
hv_stores(envelope_hv, "consumer_tag", newSVpvn(d->consumer_tag.bytes, d->consumer_tag.len));
hv_stores(envelope_hv, "routing_key", newSVpvn(d->routing_key.bytes, d->routing_key.len));
}
ret = read_message(state, channel, &props, &body );
if (AMQP_RESPONSE_NORMAL != ret.reply_type)
goto error_out2;
hv_stores(envelope_hv, "props", props);
hv_stores(envelope_hv, "body", body);
*envelope_sv_ptr = newRV_noinc(MUTABLE_SV(envelope_hv));
ret.reply_type = AMQP_RESPONSE_NORMAL;
return ret;
error_out2:
SvREFCNT_dec(envelope_hv);
error_out1:
*envelope_sv_ptr = &PL_sv_undef;
return ret;
}
void array_to_amqp_array(AV *perl_array, amqp_array_t *mq_array, short force_utf8) {
int idx = 0;
SV **value;
amqp_field_value_t *new_elements = amqp_pool_alloc(
&temp_memory_pool,
((av_len(perl_array)+1) * sizeof(amqp_field_value_t))
);
amqp_field_value_t *element;
mq_array->entries = new_elements;
mq_array->num_entries = 0;
for ( idx = 0; idx <= av_len(perl_array); idx += 1) {
value = av_fetch( perl_array, idx, 0 );
// We really should never see NULL here.
assert(value != NULL);
// Let's start getting the type...
element = &mq_array->entries[mq_array->num_entries];
mq_array->num_entries += 1;
element->kind = amqp_kind_for_sv(value, force_utf8);
__DEBUG__( warn("%d KIND >%c<", __LINE__, (unsigned char)element->kind) );
switch (element->kind) {
case AMQP_FIELD_KIND_I64:
element->value.i64 = (int64_t) SvI64(*value);
break;
case AMQP_FIELD_KIND_U64:
element->value.u64 = (uint64_t) SvU64(*value);
break;
case AMQP_FIELD_KIND_F64:
// TODO: I don't think this is a native type on all Perls
element->value.f64 = (double) SvNV(*value);
break;
case AMQP_FIELD_KIND_UTF8:
case AMQP_FIELD_KIND_BYTES:
element->value.bytes = amqp_cstring_bytes(SvPV_nolen(*value));
break;
case AMQP_FIELD_KIND_ARRAY:
array_to_amqp_array(MUTABLE_AV(SvRV(*value)), &(element->value.array), force_utf8);
break;
case AMQP_FIELD_KIND_TABLE:
hash_to_amqp_table(MUTABLE_HV(SvRV(*value)), &(element->value.table), force_utf8);
break;
default:
Perl_croak( aTHX_ "Unsupported SvType for array index %d", idx );
}
}
}
// Iterate over the array entries and decode them to Perl...
SV* mq_array_to_arrayref(amqp_array_t *mq_array) {
AV* perl_array = newAV();
SV* perl_element = &PL_sv_undef;
amqp_field_value_t* mq_element;
int current_entry = 0;
for (; current_entry < mq_array->num_entries; current_entry += 1) {
mq_element = &mq_array->entries[current_entry];
__DEBUG__( warn("%d KIND >%c<", __LINE__, mq_element->kind) );
switch (mq_element->kind) {
// Boolean
case AMQP_FIELD_KIND_BOOLEAN:
perl_element = newSViv(mq_element->value.boolean);
break;
// Signed values
case AMQP_FIELD_KIND_I8:
perl_element = newSViv(mq_element->value.i8);
break;
case AMQP_FIELD_KIND_I16:
perl_element = newSViv(mq_element->value.i16);
break;
case AMQP_FIELD_KIND_I32:
perl_element = newSViv(mq_element->value.i32);
break;
case AMQP_FIELD_KIND_I64:
perl_element = newSVi64(mq_element->value.i64);
break;
// Unsigned values
case AMQP_FIELD_KIND_U8:
perl_element = newSViv(mq_element->value.u8);
break;
case AMQP_FIELD_KIND_U16:
perl_element = newSViv(mq_element->value.u16);
break;
case AMQP_FIELD_KIND_U32:
perl_element = newSVuv(mq_element->value.u32);
break;
case AMQP_FIELD_KIND_TIMESTAMP: /* Timestamps */
case AMQP_FIELD_KIND_U64:
perl_element = newSVu64(mq_element->value.u64);
break;
// Floats
case AMQP_FIELD_KIND_F32:
perl_element = newSVnv(mq_element->value.f32);
break;
case AMQP_FIELD_KIND_F64:
// TODO: I don't think this is a native type on all Perls
perl_element = newSVnv(mq_element->value.f64);
break;
// Strings and bytes
case AMQP_FIELD_KIND_BYTES:
perl_element = newSVpvn(
mq_element->value.bytes.bytes,
mq_element->value.bytes.len
);
break;
// UTF-8 strings
case AMQP_FIELD_KIND_UTF8:
perl_element = newSVpvn(
mq_element->value.bytes.bytes,
mq_element->value.bytes.len
);
SvUTF8_on(perl_element); // It's UTF-8!
break;
// Arrays
case AMQP_FIELD_KIND_ARRAY:
perl_element = mq_array_to_arrayref(&(mq_element->value.array));
break;
// Tables
case AMQP_FIELD_KIND_TABLE:
perl_element = mq_table_to_hashref(&(mq_element->value.table));
break;
// WTF
default:
// ACK!
Perl_croak(
aTHX_ "Unsupported Perl type >%c< at index %d",
(unsigned char)mq_element->kind,
current_entry
);
}
av_push(perl_array, perl_element);
}
return newRV_noinc(MUTABLE_SV(perl_array));
}
SV* mq_table_to_hashref( amqp_table_t *mq_table ) {
// Iterate over the table keys and decode them to Perl...
int i;
SV *perl_element;
HV *perl_hash = newHV();
amqp_table_entry_t *hash_entry = (amqp_table_entry_t*)NULL;
for( i=0; i < mq_table->num_entries; i += 1 ) {
hash_entry = &(mq_table->entries[i]);
__DEBUG__(
fprintf(
stderr,
"!!! Key: >%.*s< Kind: >%c<\n",
(int)hash_entry->key.len,
(char*)hash_entry->key.bytes,
hash_entry->value.kind
);
);
switch (hash_entry->value.kind) {
// Boolean
case AMQP_FIELD_KIND_BOOLEAN:
perl_element = newSViv(hash_entry->value.value.boolean);
break;
// Integers
case AMQP_FIELD_KIND_I8:
perl_element = newSViv(hash_entry->value.value.i8);
break;
case AMQP_FIELD_KIND_I16:
perl_element = newSViv(hash_entry->value.value.i16);
break;
case AMQP_FIELD_KIND_I32:
perl_element = newSViv(hash_entry->value.value.i32);
break;
case AMQP_FIELD_KIND_I64:
perl_element = newSVi64(hash_entry->value.value.i64);
break;
case AMQP_FIELD_KIND_U8:
perl_element = newSViv(hash_entry->value.value.u8);
break;
case AMQP_FIELD_KIND_U16:
perl_element = newSViv(hash_entry->value.value.u16);
break;
case AMQP_FIELD_KIND_U32:
perl_element = newSVuv(hash_entry->value.value.u32);
break;
case AMQP_FIELD_KIND_TIMESTAMP: /* Timestamps */
case AMQP_FIELD_KIND_U64:
perl_element = newSVu64(hash_entry->value.value.u64);
break;
// Foats
case AMQP_FIELD_KIND_F32:
perl_element = newSVnv(hash_entry->value.value.f32);
break;
case AMQP_FIELD_KIND_F64:
// TODO: I don't think this is a native type on all Perls.
perl_element = newSVnv(hash_entry->value.value.f64);
break;
case AMQP_FIELD_KIND_BYTES:
perl_element = newSVpvn(
hash_entry->value.value.bytes.bytes,
hash_entry->value.value.bytes.len
);
break;
case AMQP_FIELD_KIND_UTF8:
perl_element = newSVpvn(
hash_entry->value.value.bytes.bytes,
hash_entry->value.value.bytes.len
);
SvUTF8_on(perl_element); // It's UTF-8!
break;
case AMQP_FIELD_KIND_ARRAY:
perl_element = mq_array_to_arrayref(&(hash_entry->value.value.array));
break;
case AMQP_FIELD_KIND_TABLE:
perl_element = mq_table_to_hashref(&(hash_entry->value.value.table));
break;
default:
// ACK!
Perl_croak(
aTHX_ "Unsupported Perl type >%c< at index %d",
(unsigned char)hash_entry->value.kind,
i
);
}
// Stash this in our hash.
hv_store(
perl_hash,
hash_entry->key.bytes, hash_entry->key.len,
perl_element,
0
);
}
return newRV_noinc(MUTABLE_SV(perl_hash));
}
void hash_to_amqp_table(HV *hash, amqp_table_t *table, short force_utf8) {
HE *he;
char *key;
SV *value;
I32 retlen;
amqp_table_entry_t *entry;
amqp_table_entry_t *new_entries = amqp_pool_alloc( &temp_memory_pool, HvKEYS(hash) * sizeof(amqp_table_entry_t) );
table->entries = new_entries;
hv_iterinit(hash);
while (NULL != (he = hv_iternext(hash))) {
key = hv_iterkey(he, &retlen);
__DEBUG__( warn("Key: %s\n", key) );
value = hv_iterval(hash, he);
if (SvGMAGICAL(value)) {
mg_get(value);
}