-
Notifications
You must be signed in to change notification settings - Fork 119
/
zmq.c
3047 lines (2436 loc) · 79.9 KB
/
zmq.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
/*
+-----------------------------------------------------------------------------------+
| ZMQ extension for PHP |
| Copyright (c) 2010-2013, Mikko Koppanen <[email protected]> |
| All rights reserved. |
+-----------------------------------------------------------------------------------+
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met: |
| * Redistributions of source code must retain the above copyright |
| notice, this list of conditions and the following disclaimer. |
| * Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| * Neither the name of the copyright holder nor the |
| names of its contributors may be used to endorse or promote products |
| derived from this software without specific prior written permission. |
+-----------------------------------------------------------------------------------+
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| DISCLAIMED. IN NO EVENT SHALL MIKKO KOPPANEN BE LIABLE FOR ANY |
| DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+-----------------------------------------------------------------------------------+
*/
#include "php_zmq.h"
#include "php_zmq_private.h"
#include "php_zmq_pollset.h"
ZEND_DECLARE_MODULE_GLOBALS(php_zmq);
static zend_class_entry *php_zmq_sc_entry;
static zend_class_entry *php_zmq_context_sc_entry;
static zend_class_entry *php_zmq_socket_sc_entry;
static zend_class_entry *php_zmq_poll_sc_entry;
static zend_class_entry *php_zmq_device_sc_entry;
static zend_class_entry *php_zmq_exception_sc_entry;
static zend_class_entry *php_zmq_context_exception_sc_entry;
static zend_class_entry *php_zmq_socket_exception_sc_entry;
static zend_class_entry *php_zmq_poll_exception_sc_entry;
static zend_class_entry *php_zmq_device_exception_sc_entry;
static zend_object_handlers zmq_object_handlers;
static zend_object_handlers zmq_socket_object_handlers;
static zend_object_handlers zmq_context_object_handlers;
static zend_object_handlers zmq_poll_object_handlers;
static zend_object_handlers zmq_device_object_handlers;
#ifdef HAVE_CZMQ_2
static zend_class_entry *php_zmq_cert_sc_entry;
static zend_class_entry *php_zmq_auth_sc_entry;
static zend_class_entry *php_zmq_cert_exception_sc_entry;
static zend_class_entry *php_zmq_auth_exception_sc_entry;
static zend_object_handlers zmq_cert_object_handlers;
static zend_object_handlers zmq_auth_object_handlers;
#endif
#include "zmq_object_access.c"
zend_class_entry *php_zmq_context_exception_sc_entry_get ()
{
return php_zmq_context_exception_sc_entry;
}
zend_class_entry *php_zmq_socket_exception_sc_entry_get ()
{
return php_zmq_socket_exception_sc_entry;
}
zend_class_entry *php_zmq_device_exception_sc_entry_get ()
{
return php_zmq_device_exception_sc_entry;
}
static
zend_long php_zmq_context_socket_count_get(php_zmq_context *context)
{
zend_long value = 0;
if (context->use_shared_ctx) {
value = php_zmq_shared_ctx_socket_count();
}
else {
value = context->socket_count;
}
return value;
}
static
void php_zmq_context_socket_count_incr(php_zmq_context *context)
{
if (context->use_shared_ctx) {
php_zmq_shared_ctx_socket_count_incr();
}
else {
context->socket_count++;
}
}
static
void php_zmq_context_socket_count_decr(php_zmq_context *context)
{
if (context->use_shared_ctx) {
php_zmq_shared_ctx_socket_count_decr();
}
else {
context->socket_count--;
}
}
/* list entries */
static int le_zmq_socket, le_zmq_context;
/** {{{ static int php_zmq_socket_list_entry(void)
*/
static int php_zmq_socket_list_entry(void)
{
return le_zmq_socket;
}
/* }}} */
/* {{{ static int php_zmq_context_list_entry(void)
*/
static int php_zmq_context_list_entry(void)
{
return le_zmq_context;
}
/* }}} */
/* {{{ static void php_zmq_context_destroy(php_zmq_context *context)
Destroy the context
*/
static void php_zmq_context_destroy(php_zmq_context *context)
{
if (context->pid == getpid()) {
(void) zmq_term(context->z_ctx);
}
pefree(context, context->is_persistent);
}
/* }}} */
/* {{{ static void php_zmq_socket_destroy(php_zmq_socket *zmq_sock)
Destroy the socket (note: does not touch context)
*/
static void php_zmq_socket_destroy(php_zmq_socket *zmq_sock)
{
zend_hash_destroy(&(zmq_sock->connect));
zend_hash_destroy(&(zmq_sock->bind));
/* Decrement socket count */
php_zmq_context_socket_count_decr(zmq_sock->ctx);
if (zmq_sock->pid == getpid ()) {
(void) zmq_close(zmq_sock->z_socket);
}
pefree(zmq_sock, zmq_sock->is_persistent);
}
/* }}} */
/* --- START ZMQContext --- */
/* {{{ static php_zmq_context *php_zmq_context_new(zend_long io_threads, zend_bool is_persistent, zend_bool use_shared_ctx)
Create a new zmq context
*/
static
php_zmq_context *php_zmq_context_new(zend_long io_threads, zend_bool is_persistent, zend_bool use_shared_ctx)
{
php_zmq_context *context = pecalloc(1, sizeof(php_zmq_context), is_persistent);
if (use_shared_ctx) {
php_zmq_shared_ctx_assign_to(context);
}
else {
context->z_ctx = zmq_init(io_threads);
}
if (!context->z_ctx) {
pefree(context, is_persistent);
return NULL;
}
context->io_threads = io_threads;
context->is_persistent = is_persistent;
context->pid = getpid();
context->socket_count = 0;
context->use_shared_ctx = use_shared_ctx;
return context;
}
/* }}} */
/* {{{ static php_zmq_context *php_zmq_context_get(zend_long io_threads, zend_bool is_persistent)
*/
static
php_zmq_context *php_zmq_context_get(zend_long io_threads, zend_bool is_persistent)
{
php_zmq_context *context;
zend_string *plist_key = NULL;
if (is_persistent) {
zend_resource *le_p = NULL;
plist_key = strpprintf(0, "zmq_context=[%ld]", (long)io_threads);
if ((le_p = zend_hash_find_ptr(&EG(persistent_list), plist_key)) != NULL) {
if (le_p->type == php_zmq_context_list_entry()) {
if (plist_key) {
zend_string_release(plist_key);
}
return (php_zmq_context *) le_p->ptr;
}
}
}
context = php_zmq_context_new(io_threads, is_persistent, 0);
if (!context) {
if (plist_key) {
zend_string_release(plist_key);
}
return NULL;
}
if (is_persistent) {
zend_resource le;
le.type = php_zmq_context_list_entry();
le.ptr = context;
#if PHP_VERSION_ID < 70300
GC_REFCOUNT(&le) = 1;
#else
GC_SET_REFCOUNT(&le, 1);
#endif
/* plist_key is not a persistent allocated key, thus we use str_update here */
if (zend_hash_str_update_mem(&EG(persistent_list), plist_key->val, plist_key->len, &le, sizeof(le)) == NULL) {
if (plist_key) {
zend_string_release(plist_key);
}
php_error_docref(NULL, E_ERROR, "Could not register persistent entry for the context");
/* not reached */
}
}
if (plist_key) {
zend_string_release(plist_key);
}
return context;
}
/* }}} */
/* {{{ proto ZMQ ZMQ::__construct()
Private constructor
*/
PHP_METHOD(zmq, __construct) {}
/* }}} */
/* {{{ proto integer ZMQ::clock()
A monotonic clock
*/
PHP_METHOD(zmq, clock)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
RETURN_LONG((long) php_zmq_clock (ZMQ_G (clock_ctx)));
}
/* }}} */
#ifdef PHP_ZMQ_HAVE_Z85
/* {{{ proto string ZMQ::z85Encode(string $data)
*/
PHP_METHOD(zmq, z85encode)
{
zend_string *data;
char *buffer;
size_t buffer_size;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &data) == FAILURE) {
return;
}
if (!data->len) {
RETURN_NULL();
}
buffer_size = 5 * data->len / 4 + 1;
buffer = emalloc(buffer_size);
if (!zmq_z85_encode (buffer, (const uint8_t *) data->val, data->len)) {
efree(buffer);
RETURN_NULL();
}
RETVAL_STRINGL(buffer, buffer_size - 1);
efree(buffer);
return;
}
/* }}} */
/* {{{ proto string ZMQ::z85Decode(string $z85_encoded_data)
*/
PHP_METHOD(zmq, z85decode)
{
zend_string *data;
uint8_t *buffer;
size_t buffer_size;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &data) == FAILURE) {
return;
}
if (!data->len) {
RETURN_NULL();
}
buffer_size = 4 * data->len / 5;
buffer = emalloc(buffer_size);
if (!zmq_z85_decode (buffer, data->val)) {
efree(buffer);
RETURN_NULL();
}
RETVAL_STRINGL((char *) buffer, buffer_size);
efree(buffer);
return;
}
/* }}} */
#endif
#ifdef PHP_ZMQ_HAVE_CURVE_KEYPAIR
#define PHP_ZMQ_KEY_SIZE 41
/* {{{ proto array ZMQ::curveKeypair()
*/
PHP_METHOD(zmq, curvekeypair)
{
char public_key[PHP_ZMQ_KEY_SIZE], secret_key[PHP_ZMQ_KEY_SIZE];
if (zend_parse_parameters_none() == FAILURE) {
return;
}
if (zmq_curve_keypair(public_key, secret_key) == 0) {
array_init(return_value);
add_assoc_stringl(return_value, "public_key", public_key, PHP_ZMQ_KEY_SIZE - 1);
add_assoc_stringl(return_value, "secret_key", secret_key, PHP_ZMQ_KEY_SIZE - 1);
}
}
/* }}} */
#endif
/* {{{ proto ZMQContext ZMQContext::__construct(integer $io_threads[, boolean $is_persistent = true])
Build a new ZMQContext object
*/
PHP_METHOD(zmqcontext, __construct)
{
php_zmq_context_object *intern;
zend_long io_threads = 1;
zend_bool is_persistent = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|lb", &io_threads, &is_persistent) == FAILURE) {
return;
}
intern = php_zmq_context_fetch_object(Z_OBJ_P(getThis()));
intern->context = php_zmq_context_get(io_threads, is_persistent);
if (!intern->context) {
zend_throw_exception_ex(php_zmq_context_exception_sc_entry, errno, "Error creating context: %s", zmq_strerror(errno));
return;
}
return;
}
/* }}} */
/* {{{ proto ZMQContext ZMQContext::acquire()
Acquires a handle to the process global context
*/
PHP_METHOD(zmqcontext, acquire)
{
php_zmq_context *context;
php_zmq_context_object *intern;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
context = php_zmq_context_new(1, 1, 1);
// Create a global context
object_init_ex(return_value, php_zmq_context_sc_entry);
intern = php_zmq_context_fetch_object(Z_OBJ_P(return_value));
intern->context = context;
return;
}
/* }}} */
/* {{{ proto integer ZMQContext::getSocketCount()
Number of active sockets in this context
*/
PHP_METHOD(zmqcontext, getsocketcount)
{
php_zmq_context_object *intern;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
intern = PHP_ZMQ_CONTEXT_OBJECT;
RETURN_LONG(php_zmq_context_socket_count_get(intern->context));
}
/* }}} */
#ifdef PHP_ZMQ_HAVE_CTX_OPTIONS
/* {{{ proto ZMQContext ZMQContext::setOpt(int option, int value)
Set a context option
*/
PHP_METHOD(zmqcontext, setOpt)
{
php_zmq_context_object *intern;
zend_long option, value;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &option, &value) == FAILURE) {
return;
}
intern = PHP_ZMQ_CONTEXT_OBJECT;
switch (option) {
case ZMQ_MAX_SOCKETS:
{
if (zmq_ctx_set(intern->context->z_ctx, option, value) != 0) {
zend_throw_exception_ex(php_zmq_context_exception_sc_entry_get (), errno, "Failed to set the option ZMQ::CTXOPT_MAX_SOCKETS value: %s", zmq_strerror(errno));
return;
}
}
break;
default:
{
zend_throw_exception(php_zmq_context_exception_sc_entry_get (), "Unknown option key", PHP_ZMQ_INTERNAL_ERROR);
return;
}
}
return;
}
/* }}} */
/* {{{ proto ZMQContext ZMQContext::getOpt(int option)
Set a context option
*/
PHP_METHOD(zmqcontext, getOpt)
{
php_zmq_context_object *intern;
zend_long option;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &option) == FAILURE) {
return;
}
intern = PHP_ZMQ_CONTEXT_OBJECT;
switch (option) {
case ZMQ_MAX_SOCKETS:
{
int value = zmq_ctx_get(intern->context->z_ctx, option);
RETURN_LONG(value);
}
break;
default:
{
zend_throw_exception(php_zmq_context_exception_sc_entry_get (), "Unknown option key", PHP_ZMQ_INTERNAL_ERROR);
return;
}
}
return;
}
/* }}} */
#endif
/* {{{ static php_zmq_socket *php_zmq_socket_new(php_zmq_context *context, int type, zend_bool is_persistent)
Create a new zmq socket
*/
static
php_zmq_socket *php_zmq_socket_new(php_zmq_context *context, zend_long type, zend_bool is_persistent)
{
php_zmq_socket *zmq_sock;
zmq_sock = pecalloc(1, sizeof(php_zmq_socket), is_persistent);
zmq_sock->z_socket = zmq_socket(context->z_ctx, type);
zmq_sock->pid = getpid();
zmq_sock->ctx = context;
zmq_sock->socket_type = type;
if (!zmq_sock->z_socket) {
pefree(zmq_sock, is_persistent);
return NULL;
}
/* Increment socket count */
php_zmq_context_socket_count_incr(context);
zmq_sock->is_persistent = is_persistent;
zend_hash_init(&(zmq_sock->connect), 0, NULL, NULL, is_persistent);
zend_hash_init(&(zmq_sock->bind), 0, NULL, NULL, is_persistent);
return zmq_sock;
}
/* }}} */
static
zend_string *php_zmq_socket_plist_key(zend_long type, zend_string *persistent_id, zend_bool use_shared_ctx)
{
return strpprintf(0, "zmq_socket:[%ld]-[%s]-[%d]", (long)type, persistent_id->val, use_shared_ctx);
}
static
void php_zmq_socket_store(php_zmq_socket *zmq_sock_p, zend_long type, zend_string *persistent_id, zend_bool use_shared_ctx)
{
zend_string *plist_key = NULL;
zend_resource le;
le.type = php_zmq_socket_list_entry();
le.ptr = zmq_sock_p;
#if PHP_VERSION_ID < 70300
GC_REFCOUNT(&le) = 1;
#else
GC_SET_REFCOUNT(&le, 1);
#endif
plist_key = php_zmq_socket_plist_key(type, persistent_id, use_shared_ctx);
/* plist_key is not a persistent allocated key, thus we use str_update here */
if (zend_hash_str_update_mem(&EG(persistent_list), plist_key->val, plist_key->len, &le, sizeof(le)) == NULL) {
if (plist_key) {
zend_string_release(plist_key);
}
php_error_docref(NULL, E_ERROR, "Could not register persistent entry for the socket");
/* not reached */
}
if (plist_key) {
zend_string_release(plist_key);
}
}
/* {{{ static php_zmq_socket *php_zmq_socket_get(php_zmq_context *context, zend_long type, zend_string *persistent_id, zend_bool *is_new)
Tries to get context from plist and allocates a new context if context does not exist
*/
static php_zmq_socket *php_zmq_socket_get(php_zmq_context *context, zend_long type, zend_string *persistent_id, zend_bool *is_new)
{
php_zmq_socket *zmq_sock_p;
zend_string *plist_key = NULL;
zend_bool is_persistent = 0;
is_persistent = (context->is_persistent && persistent_id && persistent_id->len) ? 1 : 0;
*is_new = 0;
if (is_persistent) {
zend_resource *le_p = NULL;
plist_key = php_zmq_socket_plist_key(type, persistent_id, context->use_shared_ctx);
if ((le_p = zend_hash_find_ptr(&EG(persistent_list), plist_key)) != NULL) {
if (le_p->type == php_zmq_socket_list_entry()) {
if (plist_key) {
zend_string_release(plist_key);
}
return (php_zmq_socket *) le_p->ptr;
}
}
}
zmq_sock_p = php_zmq_socket_new(context, type, is_persistent);
if (plist_key) {
zend_string_release(plist_key);
}
if (!zmq_sock_p) {
return NULL;
}
*is_new = 1;
return zmq_sock_p;
}
/* }}} */
static
zend_bool php_zmq_connect_callback(zval *socket, zend_fcall_info *fci, zend_fcall_info_cache *fci_cache, zend_string *persistent_id)
{
zval retval;
zval params[2];
zend_bool rv = 1;
/* Call the cb */
params[0] = *socket;
if (persistent_id && persistent_id->len) {
ZVAL_STR(¶ms[1], zend_string_copy(persistent_id));
} else {
ZVAL_NULL(¶ms[1]);
}
fci->params = params;
fci->param_count = 2;
fci->retval = &retval;
#if PHP_VERSION_ID < 80000
fci->no_separation = 1;
#endif
if (zend_call_function(fci, fci_cache) == FAILURE) {
if (!EG(exception)) {
char *func_name = php_zmq_printable_func(fci, fci_cache);
zend_throw_exception_ex(php_zmq_socket_exception_sc_entry, 0, "Failed to invoke 'on_new_socket' callback %s()", func_name);
efree(func_name);
}
rv = 0;
}
zval_ptr_dtor(¶ms[1]);
if (Z_TYPE(retval) != IS_UNDEF) {
zval_ptr_dtor(&retval);
}
if (EG(exception)) {
rv = 0;
}
return rv;
}
/* {{{ proto ZMQContext ZMQContext::getSocket(integer $type[, string $persistent_id = null[, callback $on_new_socket = null]])
Build a new ZMQContext object
*/
PHP_METHOD(zmqcontext, getsocket)
{
php_zmq_socket *socket;
php_zmq_socket_object *interns;
php_zmq_context_object *intern;
zend_long type;
zend_string *persistent_id = NULL;
int rc;
zend_bool is_new;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
PHP_ZMQ_ERROR_HANDLING_INIT()
PHP_ZMQ_ERROR_HANDLING_THROW()
fci.size = 0;
rc = zend_parse_parameters(ZEND_NUM_ARGS(), "l|S!f!", &type, &persistent_id, &fci, &fci_cache);
PHP_ZMQ_ERROR_HANDLING_RESTORE()
if (rc == FAILURE) {
return;
}
intern = php_zmq_context_fetch_object(Z_OBJ_P(getThis()));
socket = php_zmq_socket_get(intern->context, type, persistent_id, &is_new);
if (!socket) {
zend_throw_exception_ex(php_zmq_socket_exception_sc_entry, errno, "Error creating socket: %s", zmq_strerror(errno));
return;
}
object_init_ex(return_value, php_zmq_socket_sc_entry);
interns = php_zmq_socket_fetch_object(Z_OBJ_P(return_value));
interns->socket = socket;
/* Need to add refcount if context is not persistent */
if (!intern->context->is_persistent) {
ZVAL_OBJ(&interns->context_obj, &intern->zo);
Z_ADDREF(interns->context_obj);
}
if (is_new) {
if (fci.size) {
if (!php_zmq_connect_callback(return_value, &fci, &fci_cache, persistent_id)) {
php_zmq_socket_destroy(socket);
interns->socket = NULL;
return;
}
}
if (socket->is_persistent) {
php_zmq_socket_store(socket, type, persistent_id, intern->context->use_shared_ctx);
}
}
if (socket->is_persistent) {
interns->persistent_id = estrdup(persistent_id->val);
}
return;
}
/* }}} */
/* {{{ proto ZMQContext ZMQContext::isPersistent()
Whether the context is persistent
*/
PHP_METHOD(zmqcontext, ispersistent)
{
php_zmq_context_object *intern;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "") == FAILURE) {
return;
}
intern = PHP_ZMQ_CONTEXT_OBJECT;
RETURN_BOOL(intern->context->is_persistent);
}
/* }}} */
/* {{{ proto ZMQContext ZMQContext::__clone()
Clones the instance of the ZMQContext class
*/
PHP_METHOD(zmqcontext, __clone) { }
/* }}} */
/* --- END ZMQContext --- */
/* --- START ZMQSocket --- */
/* {{{ proto ZMQSocket ZMQSocket::__construct(ZMQContext $context, integer $type[, string $persistent_id = null[, callback $on_new_socket = null]])
Build a new ZMQSocket object
*/
PHP_METHOD(zmqsocket, __construct)
{
php_zmq_socket *socket;
php_zmq_socket_object *intern;
php_zmq_context_object *internc;
zend_long type;
zend_string *persistent_id = NULL;
int rc;
zval *obj;
zend_bool is_new;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
PHP_ZMQ_ERROR_HANDLING_INIT()
PHP_ZMQ_ERROR_HANDLING_THROW()
fci.size = 0;
rc = zend_parse_parameters(ZEND_NUM_ARGS(), "Ol|S!f!", &obj, php_zmq_context_sc_entry, &type, &persistent_id, &fci, &fci_cache);
PHP_ZMQ_ERROR_HANDLING_RESTORE()
if (rc == FAILURE) {
return;
}
internc = php_zmq_context_fetch_object(Z_OBJ_P(obj));
socket = php_zmq_socket_get(internc->context, type, persistent_id, &is_new);
if (!socket) {
zend_throw_exception_ex(php_zmq_socket_exception_sc_entry, errno, "Error creating socket: %s", zmq_strerror(errno));
return;
}
intern = PHP_ZMQ_SOCKET_OBJECT;
intern->socket = socket;
/* Need to add refcount if context is not persistent */
if (!internc->context->is_persistent) {
ZVAL_OBJ(&intern->context_obj, &internc->zo);
Z_ADDREF(intern->context_obj);
}
if (is_new) {
if (fci.size) {
if (!php_zmq_connect_callback(getThis(), &fci, &fci_cache, persistent_id)) {
php_zmq_socket_destroy(socket);
intern->socket = NULL;
return;
}
}
if (socket->is_persistent) {
php_zmq_socket_store(socket, type, persistent_id, internc->context->use_shared_ctx);
}
}
if (socket->is_persistent) {
intern->persistent_id = estrdup(persistent_id->val);
}
return;
}
/* }}} */
/* {{{ static zend_bool php_zmq_send(php_zmq_socket_object *intern, char *message_param, long flags)
*/
static zend_bool php_zmq_send(php_zmq_socket_object *intern, zend_string *message_param, zend_long flags)
{
int rc, errno_;
zmq_msg_t message;
if (zmq_msg_init_size(&message, message_param->len) != 0) {
zend_throw_exception_ex(php_zmq_socket_exception_sc_entry, errno, "Failed to initialize message structure: %s", zmq_strerror(errno));
return 0;
}
memcpy(zmq_msg_data(&message), message_param->val, message_param->len);
rc = zmq_sendmsg(intern->socket->z_socket, &message, flags);
errno_ = errno;
zmq_msg_close(&message);
if (rc == -1) {
if (errno_ == EAGAIN) {
return 0;
}
zend_throw_exception_ex(php_zmq_socket_exception_sc_entry, errno_, "Failed to send message: %s", zmq_strerror(errno_));
return 0;
}
return 1;
}
/* }}} */
static void php_zmq_sendmsg_impl(INTERNAL_FUNCTION_PARAMETERS)
{
php_zmq_socket_object *intern;
zend_string *message_param;
zend_long flags = 0;
zend_bool ret;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &message_param, &flags) == FAILURE) {
return;
}
intern = PHP_ZMQ_SOCKET_OBJECT;
ret = php_zmq_send(intern, message_param, flags);
if (ret) {
ZMQ_RETURN_THIS;
} else {
RETURN_FALSE;
}
}
/* {{{ proto ZMQSocket ZMQSocket::send(string $message[, integer $flags = 0])
Send a message. Return true if message was sent and false on EAGAIN
*/
PHP_METHOD(zmqsocket, send)
{
php_zmq_sendmsg_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
static
int php_zmq_send_cb(zval *zv, int num_args, va_list args, zend_hash_key *hash_key)
{
php_zmq_socket_object *intern;
int flags, *rc, *to_send;
intern = va_arg(args, php_zmq_socket_object *);
flags = va_arg(args, int);
to_send = va_arg(args, int *);
rc = va_arg(args, int *);
if (--(*to_send)) {
flags = flags | ZMQ_SNDMORE;
} else {
flags = flags & ~ZMQ_SNDMORE;
}
zend_string *str = zval_get_string(zv);
*rc = php_zmq_send(intern, str, flags);
zend_string_release(str);
if (!*rc) {
return ZEND_HASH_APPLY_STOP;
}
return ZEND_HASH_APPLY_KEEP;
}
/* {{{ proto ZMQSocket ZMQSocket::sendmulti(arrays $messages[, integer $flags = 0])
Send a multipart message. Return true if message was sent and false on EAGAIN
*/
PHP_METHOD(zmqsocket, sendmulti)
{
zval *messages;
php_zmq_socket_object *intern;
int to_send, ret = 0;
zend_long flags = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|l", &messages, &flags) == FAILURE) {
return;
}
intern = PHP_ZMQ_SOCKET_OBJECT;
to_send = zend_hash_num_elements(Z_ARRVAL_P(messages));
zend_hash_apply_with_arguments(Z_ARRVAL_P(messages), (apply_func_args_t) php_zmq_send_cb, 4, intern, flags, &to_send, &ret);
if (ret) {
ZMQ_RETURN_THIS;
} else {
RETURN_FALSE;
}
}
/* {{{ static zend_bool php_zmq_recv(php_zmq_socket_object *intern, long flags, zval *return_value)
*/
static
zend_string *php_zmq_recv(php_zmq_socket_object *intern, zend_long flags)
{
int rc, errno_;
zmq_msg_t message;
zend_string *str = NULL;
if (zmq_msg_init(&message) != 0) {
zend_throw_exception_ex(php_zmq_socket_exception_sc_entry, errno, "Failed to initialize message structure: %s", zmq_strerror(errno));
return NULL;
}
rc = zmq_recvmsg(intern->socket->z_socket, &message, flags);
errno_ = errno;
if (rc == -1) {
zmq_msg_close(&message);
if (errno == EAGAIN) {
return NULL;
}
zend_throw_exception_ex(php_zmq_socket_exception_sc_entry, errno_, "Failed to receive message: %s", zmq_strerror(errno_));
return NULL;
}
str = zend_string_init(zmq_msg_data(&message), zmq_msg_size(&message), 0);
zmq_msg_close(&message);
return str;
}
/* }}} */
static void php_zmq_recvmsg_impl(INTERNAL_FUNCTION_PARAMETERS)
{
zend_string *str = NULL;
php_zmq_socket_object *intern;
zend_long flags = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &flags) == FAILURE) {
return;
}
intern = PHP_ZMQ_SOCKET_OBJECT;
str = php_zmq_recv(intern, flags);
if (!str) {
RETURN_FALSE;
}
RETURN_STR(str);
}
/* {{{ proto string ZMQ::recv([integer $flags = 0])
Receive a message
*/
PHP_METHOD(zmqsocket, recv)
{
php_zmq_recvmsg_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto array ZMQ::recvmulti([integer $flags = 0])
Receive an array of message parts
*/
PHP_METHOD(zmqsocket, recvmulti)
{
php_zmq_socket_object *intern;
size_t value_len;
zend_long flags = 0;
#if ZMQ_VERSION_MAJOR < 3
int64_t value;
#else
int value;
#endif
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &flags) == FAILURE) {
return;
}
intern = PHP_ZMQ_SOCKET_OBJECT;
array_init(return_value);
value_len = sizeof (value);