forked from yahoojapan/chmpx_phpext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chmpxpx_func.c
1205 lines (1061 loc) · 37.3 KB
/
chmpxpx_func.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
/*
* CHMPX PHP Extension library
*
* Copyright 2015 Yahoo Japan corporation.
*
* CHMPX is inprocess data exchange by MQ with consistent hashing.
* CHMPX is made for the purpose of the construction of
* original messaging system and the offer of the client
* library.
* CHMPX transfers messages between the client and the server/
* slave. CHMPX based servers are dispersed by consistent
* hashing and are automatically layouted. As a result, it
* provides a high performance, a high scalability.
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* AUTHOR: [email protected]
* CREATE: Mon Mar 16 2015
* REVISION:
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
// cppcheck-suppress missingInclude
#include "php.h"
// cppcheck-suppress missingInclude
#include "ext/standard/info.h"
#include "php_chmpxpx.h"
#include "chmpxpx_arginfo.h"
// class entries
zend_class_entry* chmpx_class_entry;
zend_class_entry* chmpx_server_class_entry;
zend_class_entry* chmpx_slave_class_entry;
zval* chmpxpx_read_property_resource(zend_class_entry* scope, zval* object, const char* name) {
zval* pzval, tmp;
#if PHP_VERSION_ID < 80000
pzval = zend_read_property(scope, object, name, strlen(name), 1, &tmp);
#else
pzval = zend_read_property(scope, Z_OBJ_P(object), name, strlen(name), 1, &tmp);
#endif
if(IS_RESOURCE != Z_TYPE_P(pzval)){
php_error(E_ERROR, "The property(%s) is not a resource type.", name);
}
return pzval;
}
void chmpxpx_update_property_resource(zend_class_entry* scope, zval* object, const char* name, size_t name_length, zval* value) {
#if PHP_VERSION_ID < 80000
zend_update_property(scope, object, name, name_length, value);
#else
zend_update_property(scope, Z_OBJ_P(object), name, name_length, value);
#endif
}
int chmpx_handle_resource_number;
zend_resource* chmpx_handle_resource;
static void chmpx_handle_resource_dtor(zend_resource* rsrc)
{
efree((chmpx_h*)rsrc->ptr);
}
int msgid_handle_resource_number;
zend_resource* msgid_handle_resource;
static void msgid_handle_resource_dtor(zend_resource* rsrc)
{
efree((msgid_t*)rsrc->ptr);
}
int chmpx_pkt_handle_resource_number;
zend_resource* chmpx_pkt_handle_resource;
static void chmpx_pkt_handle_resource_dtor(zend_resource* rsrc)
{
efree((chmpx_pkt_h*)rsrc->ptr);
}
int is_server_mode_resource_number;
zend_resource* is_server_mode_resource;
static void is_server_mode_resource_dtor(zend_resource* rsrc)
{
efree((bool*)rsrc->ptr);
}
PHP_FUNCTION(chmpxpx_destroy)
{
// declare_variables
zval* zval_chmpx_handle_ptr = NULL;
// assign_after_declare
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_RESOURCE(zval_chmpx_handle_ptr)
ZEND_PARSE_PARAMETERS_END();
// validate_after_assign
ZEND_ASSERT(Z_TYPE_P(zval_chmpx_handle_ptr) == IS_RESOURCE);
if(!zval_chmpx_handle_ptr) {
php_error(E_ERROR, "chmpxpx_destroy: handle is empty.");
RETURN_FALSE;
}
// get_resources
chmpx_h* chmpx_handle_ptr = (chmpx_h *)zend_fetch_resource(Z_RES_P(zval_chmpx_handle_ptr), "chmpx_handle_resource", chmpx_handle_resource_number);
// call_some_functions after get_resources
chmpx_destroy(*chmpx_handle_ptr);
// return_result after call_some_functions
RETURN_TRUE;
}
PHP_FUNCTION(chmpxpx_slv_receive)
{
// declare_variables
zval* zval_chmpx_handle_ptr = NULL;
zval* zval_msgid_handle_ptr = NULL;
zval* strbinary = NULL; // Pass by Reference
zend_long timeout_ms = 0;
// assign_after_declare
ZEND_PARSE_PARAMETERS_START(3, 4)
Z_PARAM_RESOURCE(zval_chmpx_handle_ptr)
Z_PARAM_RESOURCE(zval_msgid_handle_ptr)
Z_PARAM_ZVAL(strbinary)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(timeout_ms)
ZEND_PARSE_PARAMETERS_END();
// validate_after_assign
ZEND_ASSERT(Z_TYPE_P(zval_chmpx_handle_ptr) == IS_RESOURCE);
if(!zval_chmpx_handle_ptr) {
php_error(E_ERROR, "chmpx_slv_receive: handle is empty.");
RETURN_FALSE;
}
ZEND_ASSERT(Z_TYPE_P(zval_msgid_handle_ptr) == IS_RESOURCE);
if(!zval_msgid_handle_ptr) {
php_error(E_ERROR, "chmpx_slv_receive: handle is empty.");
RETURN_FALSE;
}
// get_resources
// chmpx_handle resource
chmpx_h* chmpx_handle_ptr = (chmpx_h *)zend_fetch_resource(Z_RES_P(zval_chmpx_handle_ptr), "chmpx_handle_resource", chmpx_handle_resource_number);
if(CHM_INVALID_CHMPXHANDLE == *chmpx_handle_ptr){
// no created handle
php_error(E_ERROR, "chmpx_handle is CHM_INVALID_CHMPXHANDLE");
RETURN_FALSE;
}
// msgid_handle resource
msgid_t* msgid_handle_ptr = (msgid_t *)zend_fetch_resource(Z_RES_P(zval_msgid_handle_ptr), "msgid_handle_resource", msgid_handle_resource_number);
if(CHM_INVALID_MSGID == *msgid_handle_ptr){
php_error(E_NOTICE, "msgid_handle is CHM_INVALID_MSGID");
RETURN_FALSE;
}
// call_some_functions after get_resources
// chmpx_msg_receive
chmpx_pkt_h chmpx_pkt_handle = 0L;
unsigned char* pbody = NULL;
size_t length = 0;
bool bResult = chmpx_msg_receive(*chmpx_handle_ptr, *msgid_handle_ptr, &chmpx_pkt_handle, &pbody, &length, (int)timeout_ms);
// return_result after call_some_functions
if (!bResult) {
php_error(E_ERROR, "chmpxpx_slv_receive: could not receive data, probably there is no received data by timeout.");
RETURN_FALSE;
}
if(Z_ISREF_P(strbinary)) {
// See https://www.phpinternalsbook.com/php7/zvals/references.html
ZVAL_DEREF(strbinary); // dereferencing
}
zval_ptr_dtor(strbinary); // initialize the zval(decrements to refcount=0, and destroys the string if exists)
ZVAL_STRINGL(strbinary, (char*)pbody, length);
if(pbody){
free(pbody);
}
// chmpx_pkth_destroy
if(0L != chmpx_pkt_handle && !chmpx_pkth_destroy(chmpx_pkt_handle)){
php_error(E_WARNING, "chmpxpx_slv_receive: could not close chmpx packet handler.");
}
RETURN_TRUE;
}
PHP_FUNCTION(chmpxpx_svr_receive_kv)
{
// declare_variables
zval* zval_chmpx_handle_ptr = NULL;
zval* strbinkey = NULL; // Pass by Reference
zval* strbinval = NULL; // Pass by Reference
zend_long timeout_ms = RCV_NO_WAIT;
zend_bool no_giveup_rejoin = false;
// assign_after_declare
ZEND_PARSE_PARAMETERS_START(3, 5)
Z_PARAM_RESOURCE(zval_chmpx_handle_ptr)
Z_PARAM_ZVAL(strbinkey)
Z_PARAM_ZVAL(strbinval)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(timeout_ms)
Z_PARAM_BOOL(no_giveup_rejoin)
ZEND_PARSE_PARAMETERS_END();
// validate_after_assign
ZEND_ASSERT(Z_TYPE_P(zval_chmpx_handle_ptr) == IS_RESOURCE);
if(!zval_chmpx_handle_ptr) {
php_error(E_ERROR, "chmpx_svr_receive_kv: handle is empty.");
RETURN_FALSE;
}
// get_resources
chmpx_h* chmpx_handle_ptr = (chmpx_h *)zend_fetch_resource(Z_RES_P(zval_chmpx_handle_ptr), "chmpx_handle_resource", chmpx_handle_resource_number);
if(CHM_INVALID_CHMPXHANDLE == *chmpx_handle_ptr){
// no created handle
php_error(E_ERROR, "chmpx_handle is CHM_INVALID_CHMPXHANDLE");
RETURN_FALSE;
}
// call_some_functions after get_resources
// chmpx_svr_receive
chmpx_pkt_h chmpx_pkt_handle = 0L;
unsigned char* pbody = NULL;
size_t length = 0;
if(!chmpx_svr_receive(*chmpx_handle_ptr, &chmpx_pkt_handle, &pbody, &length, (int)timeout_ms, 0 != no_giveup_rejoin ? true : false)){
php_error(E_WARNING, "chmpxpx_svr_receive: could not receive data, probably there is no received data by timeout.");
RETURN_FALSE;
}
if(0L == chmpx_pkt_handle){
// no error, but no received data.
RETURN_FALSE;
}
CHMKVP kvp;
if(cvt_bin_kvp(&kvp, pbody, length)){
// copy received data to each
if(Z_ISREF_P(strbinkey)) {
// See https://www.phpinternalsbook.com/php7/zvals/references.html
ZVAL_DEREF(strbinkey); // dereferencing
}
zval_ptr_dtor(strbinkey); // initialize the zval(decrements to refcount=0, and destroys the string if exists)
ZVAL_STRINGL(strbinkey, (char*)kvp.key.byptr, kvp.key.length);
if(Z_ISREF_P(strbinval)) {
// See https://www.phpinternalsbook.com/php7/zvals/references.html
ZVAL_DEREF(strbinval); // dereferencing
}
zval_ptr_dtor(strbinval); // initialize the zval(decrements to refcount=0, and destroys the string if exists)
ZVAL_STRINGL(strbinval, (char*)kvp.val.byptr, kvp.val.length);
}
if(pbody){
free(pbody);
}
// return_result after call_some_functions
chmpx_pkt_h* chmpx_pkt_handle_ptr; // like a file pointer
chmpx_pkt_handle_ptr = emalloc(sizeof(long));
if (chmpx_pkt_handle_ptr == NULL) {
php_error(E_ERROR, "malloc error");
RETURN_FALSE;
}
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress nullPointerRedundantCheck
*chmpx_pkt_handle_ptr = chmpx_pkt_handle;
RETURN_RES(zend_register_resource(chmpx_pkt_handle_ptr, chmpx_pkt_handle_resource_number)); // RETVAL_RES(r) and ZVAL_RES(return_value, r)
}
PHP_FUNCTION(chmpxpx_svr_receive)
{
// declare_variables
zval* zval_chmpx_handle_ptr = NULL; // chmpx_handle_resource
zval* strbinary = NULL; // Pass by Reference
zend_long timeout_ms = RCV_NO_WAIT;
zend_bool no_giveup_rejoin = false;
// assign_after_declare
ZEND_PARSE_PARAMETERS_START(2, 4)
Z_PARAM_RESOURCE(zval_chmpx_handle_ptr)
Z_PARAM_ZVAL(strbinary)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(timeout_ms)
Z_PARAM_BOOL(no_giveup_rejoin)
ZEND_PARSE_PARAMETERS_END();
// validate_after_assign
ZEND_ASSERT(Z_TYPE_P(zval_chmpx_handle_ptr) == IS_RESOURCE);
if(!zval_chmpx_handle_ptr) {
php_error(E_ERROR, "chmpx_msgid_close: handle is empty.");
RETURN_FALSE;
}
// get_resources
chmpx_h* chmpx_handle_ptr = (chmpx_h *)zend_fetch_resource(Z_RES_P(zval_chmpx_handle_ptr), "chmpx_handle_resource", chmpx_handle_resource_number);
if(CHM_INVALID_CHMPXHANDLE == *chmpx_handle_ptr){
// no created handle
php_error(E_ERROR, "chmpx_handle is CHM_INVALID_CHMPXHANDLE");
RETURN_FALSE;
}
// call_some_functions after get_resources
chmpx_pkt_h chmpx_pkt_handle = 0L;
unsigned char* pbody = NULL;
size_t length = 0;
bool bResult = chmpx_svr_receive(*chmpx_handle_ptr, &chmpx_pkt_handle, &pbody, &length, (int)timeout_ms, 0 != no_giveup_rejoin ? true : false);
// return_result after call_some_functions
if (!bResult) {
php_error(E_WARNING, "chmpxpx_svr_receive: could not receive data, probably there is no received data by timeout.");
RETURN_FALSE;
}
if(0L == chmpx_pkt_handle){
// no error, but no received data.
RETURN_FALSE;
}
if(Z_ISREF_P(strbinary)) { // copy received data
// See https://www.phpinternalsbook.com/php7/zvals/references.html
ZVAL_DEREF(strbinary); // dereferencing
}
zval_ptr_dtor(strbinary); // initialize the zval(decrements to refcount=0, and destroys the string if exists)
ZVAL_STRINGL(strbinary, (char*)pbody, length - 1);
if(pbody){
free(pbody);
}
// register the chmpx_pkt_handle to chmpx_pkt_handle_resource_number. The resource will destroy by using chmpx_pkt_handle_resource_dtor
chmpx_pkt_h* chmpx_pkt_handle_ptr; // like a file pointer
chmpx_pkt_handle_ptr = emalloc(sizeof(long));
if (chmpx_pkt_handle_ptr == NULL) {
php_error(E_ERROR, "malloc error");
RETURN_FALSE;
}
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress nullPointerRedundantCheck
*chmpx_pkt_handle_ptr = chmpx_pkt_handle;
RETURN_RES(zend_register_resource(chmpx_pkt_handle_ptr, chmpx_pkt_handle_resource_number)); // RETVAL_RES(r) and ZVAL_RES(return_value, r)
}
PHP_FUNCTION(chmpxpx_slv_send)
{
// declare_variables
zval* zval_chmpx_handle_ptr = NULL;
zval* zval_msgid_handle_ptr = NULL;
char* strbinary = NULL;
size_t strbinary_len = 0;
zend_bool is_routing = true;
zend_bool is_broadcast = false;
// assign_after_declare
ZEND_PARSE_PARAMETERS_START(3, 5)
Z_PARAM_RESOURCE(zval_chmpx_handle_ptr)
Z_PARAM_RESOURCE(zval_msgid_handle_ptr)
Z_PARAM_STRING(strbinary, strbinary_len)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(is_routing)
Z_PARAM_BOOL(is_broadcast)
ZEND_PARSE_PARAMETERS_END();
// validate_after_assign
ZEND_ASSERT(Z_TYPE_P(zval_chmpx_handle_ptr) == IS_RESOURCE);
if(!zval_chmpx_handle_ptr) {
php_error(E_ERROR, "chmpx_slv_send: handle is empty.");
RETURN_FALSE;
}
ZEND_ASSERT(Z_TYPE_P(zval_msgid_handle_ptr) == IS_RESOURCE);
if(!zval_msgid_handle_ptr) {
php_error(E_ERROR, "chmpx_slv_send: handle is empty.");
RETURN_FALSE;
}
// get_resources
chmpx_h* chmpx_handle_ptr = (chmpx_h *)zend_fetch_resource(Z_RES_P(zval_chmpx_handle_ptr), "chmpx_handle_resource", chmpx_handle_resource_number);
if(CHM_INVALID_CHMPXHANDLE == *chmpx_handle_ptr){
// no created handle
php_error(E_ERROR, "chmpx_handle is CHM_INVALID_CHMPXHANDLE");
RETURN_FALSE;
}
msgid_t* msgid_handle_ptr = (msgid_t *)zend_fetch_resource(Z_RES_P(zval_msgid_handle_ptr), "msgid_handle_resource", msgid_handle_resource_number);
if(CHM_INVALID_MSGID == *msgid_handle_ptr){
php_error(E_ERROR, "msgid_handle is CHM_INVALID_MSGID");
RETURN_FALSE;
}
// call_some_functions after get_resources
CHMBIN chmbinary;
chmbinary.byptr = (unsigned char*)strbinary;
chmbinary.length = (size_t)(strbinary_len + 1);
chmhash_t hashval = make_chmbin_hash(&chmbinary);
long rcvcnt = 0;
// return_result after call_some_functions
bool bResult;
if(is_broadcast){
// broadcast
if(false == (bResult = chmpx_msg_broadcast(*chmpx_handle_ptr, *msgid_handle_ptr, (const unsigned char*)strbinary, (size_t)(strbinary_len + 1), hashval, &rcvcnt))){
php_error(E_ERROR, "chmpxpx_slv_send: failed send binary data. result %d rcvcnt %ld", bResult, rcvcnt);
RETURN_FALSE;
}
}else{
if(false == (bResult = chmpx_msg_send(*chmpx_handle_ptr, *msgid_handle_ptr, (const unsigned char*)strbinary, (size_t)(strbinary_len + 1), hashval, &rcvcnt, 0 != is_routing ? true : false))){
php_error(E_ERROR, "chmpxpx_slv_send: failed send binary data. result %d rcvcnt %ld", bResult, rcvcnt);
RETURN_FALSE;
}
}
// return_result after call_some_functions
RETURN_LONG((long)rcvcnt); // RETVAL_LONG(l) and ZVAL_LONG(return_value, l)
}
PHP_FUNCTION(chmpxpx_msgid_open)
{
// declare_variables
zval* zval_zval_chmpx_handle_ptr = NULL; // userland zval
zend_bool no_giveup_rejoin = false;
// assign_after_declare
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_RESOURCE(zval_zval_chmpx_handle_ptr)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(no_giveup_rejoin)
ZEND_PARSE_PARAMETERS_END();
// validate_after_assign
ZEND_ASSERT(Z_TYPE_P(zval_zval_chmpx_handle_ptr) == IS_RESOURCE); /* just a check to be sure */
if(!zval_zval_chmpx_handle_ptr) {
php_error(E_ERROR, "msgid_createy: handle is empty.");
RETURN_FALSE;
}
// get_resources
chmpx_h* chmpx_handle_ptr = (chmpx_h *)zend_fetch_resource(Z_RES_P(zval_zval_chmpx_handle_ptr), "chmpx_handle_resource", chmpx_handle_resource_number);
if(CHM_INVALID_CHMPXHANDLE == *chmpx_handle_ptr){
// no created handle
php_error(E_ERROR, "chmpx_handle is CHM_INVALID_CHMPXHANDLE");
RETURN_FALSE;
}
// call_some_functions after get_resources
msgid_t msgid_handle = chmpx_open(*chmpx_handle_ptr, 0 != no_giveup_rejoin ? true : false);
if(CHM_INVALID_CHMPXHANDLE == msgid_handle) {
php_error(E_NOTICE, "chmpxpx_open: failed to open msgid on slave CHMPX.");
RETURN_FALSE;
}
// return_result after call_some_functions
msgid_t* msgid_handle_ptr; // like a file pointer
msgid_handle_ptr = emalloc(sizeof(long));
if (msgid_handle_ptr == NULL) {
php_error(E_ERROR, "malloc error");
RETURN_FALSE;
}
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress nullPointerRedundantCheck
*msgid_handle_ptr = msgid_handle;
RETURN_RES(zend_register_resource(msgid_handle_ptr, msgid_handle_resource_number)); // RETVAL_RES(r) and ZVAL_RES(return_value, r)
}
static void chmpx_create_opt(INTERNAL_FUNCTION_PARAMETERS, int opt)
{
// declare_variables
char* filepath = NULL;
size_t filepath_len = 0;
zend_bool is_on_server = true;
zend_bool is_auto_rejoin = false;
switch (opt) {
case 0:
case 1:
is_on_server = true;
break;
case 2:
is_on_server = false;
break;
default:
RETURN_FALSE;
}
// assign_after_declare
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_STRING(filepath, filepath_len)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(is_on_server)
Z_PARAM_BOOL(is_auto_rejoin)
ZEND_PARSE_PARAMETERS_END();
// validate_after_assign
if(!filepath || 0 == filepath_len){
php_error(E_ERROR, "chmpxpx_create: CHMPX configuration file path is empty.");
RETURN_FALSE;
}
// call_some_functions after get_resources
chmpx_h chmpx_handle = chmpx_create(filepath, 0 != is_on_server ? true : false, 0 != is_auto_rejoin ? true : false);
if(CHM_INVALID_CHMPXHANDLE == chmpx_handle) {
php_error(E_NOTICE, "chmpxpx_create: failed to open(join) CHMPX.");
RETURN_FALSE;
}
// return_result after call_some_functions
chmpx_h* chmpx_handle_ptr = emalloc(sizeof(long)); // this resource destroy by using chmpx_handle_resource_dtor
if (chmpx_handle_ptr == NULL) {
php_error(E_ERROR, "malloc error");
RETURN_FALSE;
}
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress nullPointerRedundantCheck
*chmpx_handle_ptr = chmpx_handle;
RETURN_RES(zend_register_resource(chmpx_handle_ptr, chmpx_handle_resource_number)); // RETVAL_RES(r) and ZVAL_RES(return_value, r)
}
PHP_FUNCTION(chmpxpx_create)
{
chmpx_create_opt(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
PHP_FUNCTION(chmpxpx_create_server)
{
chmpx_create_opt(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
PHP_FUNCTION(chmpxpx_create_slave)
{
chmpx_create_opt(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
}
PHP_FUNCTION(chmpxpx_bump_debug_level)
{
chmpx_bump_debug_level();
}
PHP_FUNCTION(chmpxpx_set_debug_level_silent)
{
chmpx_set_debug_level_silent();
}
PHP_FUNCTION(chmpxpx_set_debug_level_error)
{
chmpx_set_debug_level_error();
}
PHP_FUNCTION(chmpxpx_set_debug_level_warning)
{
chmpx_set_debug_level_warning();
}
PHP_FUNCTION(chmpxpx_set_debug_level_message)
{
chmpx_set_debug_level_message();
}
PHP_FUNCTION(chmpxpx_set_debug_file)
{
// declare_variables
char* filepath = NULL;
size_t filepath_len = 0;
// assign_after_declare
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(filepath, filepath_len)
ZEND_PARSE_PARAMETERS_END();
// validate_after_assign
if(!filepath || 0 == filepath_len) {
php_error_docref(NULL, E_ERROR, "chmpx_set_debug_file: filepath is empty.");
RETURN_FALSE;
}
// call_some_functions after get_resources
bool bResult = chmpx_set_debug_file(filepath);
// return_result after call_some_functions
if (!bResult) {
php_error(E_NOTICE, "chmpxpx_set_debug_file: failed to set debugging file path.");
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(chmpxpx_unset_debug_file)
{
if(!chmpx_unset_debug_file()){
php_error(E_NOTICE, "chmpxpx_unset_debug_file: failed to unset debugging file path.");
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(chmpxpx_load_debug_env)
{
if(!chmpx_load_debug_env()){
php_error(E_NOTICE, "chmpxpx_load_debug_env: failed to load environment about debugging.");
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(chmpxpx_load_hash_library)
{
// declare_variables
char* filepath = NULL;
size_t filepath_len = 0;
// assign_after_declare
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(filepath, filepath_len)
ZEND_PARSE_PARAMETERS_END();
// validate_after_assign
if(!filepath || 0 == filepath_len){
php_error(E_ERROR, "chmpxpx_load_hash_library: filepath is empty.");
RETURN_FALSE;
}
// call_some_functions after get_resources
bool bResult = chmpx_load_hash_library(filepath);
// return_result after call_some_functions
if (!bResult) {
php_error(E_NOTICE, "chmpxpx_load_hash_library: failed to set hash library file path.");
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(chmpxpx_unload_hash_library)
{
if(!chmpx_unload_hash_library()){
php_error(E_NOTICE, "chmpxpx_unload_hash_library: failed to unset hash library file path.");
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(chmpxpx_process_running)
{
// declare_variables
zval* zval_zval_chmpx_handle_ptr = NULL; // userland zval
// assign_after_declare
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_RESOURCE(zval_zval_chmpx_handle_ptr)
ZEND_PARSE_PARAMETERS_END();
// validate_after_assign
ZEND_ASSERT(Z_TYPE_P(zval_zval_chmpx_handle_ptr) == IS_RESOURCE); /* just a check to be sure */
if(!zval_zval_chmpx_handle_ptr) {
php_error(E_ERROR, "chmpxpx_destroy: handle is empty.");
RETURN_FALSE;
}
// get_resources
chmpx_h* chmpx_handle_ptr = (chmpx_h *)zend_fetch_resource(Z_RES_P(zval_zval_chmpx_handle_ptr), "chmpx_handle_resource", chmpx_handle_resource_number);
if(CHM_INVALID_CHMPXHANDLE == *chmpx_handle_ptr){
// no created handle
php_error(E_ERROR, "chmpx_handle is CHM_INVALID_CHMPXHANDLE");
RETURN_FALSE;
}
// call_some_functions after get_resources
bool bResult = is_chmpx_proc_exists(*chmpx_handle_ptr);
// return_result after call_some_functions
if (!bResult) {
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(chmpxpx_msgid_close)
{
// declare_variables
zval* zval_zval_chmpx_handle_ptr = NULL; // userland zval
zval* zval_zval_msgid_handle_ptr = NULL; // userland zval
// assign_after_declare
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_RESOURCE(zval_zval_chmpx_handle_ptr)
Z_PARAM_RESOURCE(zval_zval_msgid_handle_ptr)
ZEND_PARSE_PARAMETERS_END();
// validate_after_assign
ZEND_ASSERT(Z_TYPE_P(zval_zval_chmpx_handle_ptr) == IS_RESOURCE); /* just a check to be sure */
if(!zval_zval_chmpx_handle_ptr) {
php_error(E_ERROR, "chmpx_msgid_close: handle is empty.");
RETURN_FALSE;
}
ZEND_ASSERT(Z_TYPE_P(zval_zval_msgid_handle_ptr) == IS_RESOURCE); /* just a check to be sure */
if(!zval_zval_msgid_handle_ptr) {
php_error(E_ERROR, "chmpx_msgid_close: handle is empty.");
RETURN_FALSE;
}
// get_resources
chmpx_h* chmpx_handle_ptr = (chmpx_h *)zend_fetch_resource(Z_RES_P(zval_zval_chmpx_handle_ptr), "chmpx_handle_resource", chmpx_handle_resource_number);
if(CHM_INVALID_CHMPXHANDLE == *chmpx_handle_ptr){
// no created handle
php_error(E_ERROR, "chmpx_handle is CHM_INVALID_CHMPXHANDLE");
RETURN_FALSE;
}
msgid_t* msgid_handle_ptr = (msgid_t *)zend_fetch_resource(Z_RES_P(zval_zval_msgid_handle_ptr), "msgid_handle_resource", msgid_handle_resource_number);
if(CHM_INVALID_MSGID == *msgid_handle_ptr){
php_error(E_NOTICE, "msgid_handle is CHM_INVALID_MSGID");
RETURN_FALSE;
}
// call_some_functions after get_resources
bool bResult = chmpx_close(*chmpx_handle_ptr, *msgid_handle_ptr);
// return_result after call_some_functions
if (!bResult) {
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(chmpxpx_pkth_close)
{
// declare_variables
zval* zval_chmpx_pkt_handle_resource_ptr = NULL;
// assign_after_declare
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_RESOURCE(zval_chmpx_pkt_handle_resource_ptr)
ZEND_PARSE_PARAMETERS_END();
// validate_after_assign
ZEND_ASSERT(Z_TYPE_P(zval_chmpx_pkt_handle_resource_ptr) == IS_RESOURCE); /* just a check to be sure */
if(!zval_chmpx_pkt_handle_resource_ptr) {
php_error(E_ERROR, "chmpxpx_destroy: handle is empty.");
RETURN_FALSE;
}
// get_resources
chmpx_pkt_h* chmpx_pkt_handle_ptr = (chmpx_pkt_h *)zend_fetch_resource(Z_RES_P(zval_chmpx_pkt_handle_resource_ptr), "chmpx_pkt_handle_resource", chmpx_pkt_handle_resource_number);
// call_some_functions after get_resources
bool bResult = chmpx_pkth_destroy(*chmpx_pkt_handle_ptr);
// return_result after call_some_functions
if(0L != *chmpx_pkt_handle_ptr && !bResult) {
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(chmpxpx_svr_reply)
{
// declare_variables
zval* zval_zval_chmpx_handle_ptr = NULL;
zval* zval_chmpx_pkt_handle_resource_ptr = NULL;
char* strbinary = NULL;
size_t strbinary_len = 0;
// assign_after_declare
ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_RESOURCE(zval_zval_chmpx_handle_ptr)
Z_PARAM_RESOURCE(zval_chmpx_pkt_handle_resource_ptr)
Z_PARAM_STRING(strbinary, strbinary_len)
ZEND_PARSE_PARAMETERS_END();
// validate_after_assign
ZEND_ASSERT(Z_TYPE_P(zval_zval_chmpx_handle_ptr) == IS_RESOURCE); /* just a check to be sure */
if(!zval_zval_chmpx_handle_ptr) {
php_error(E_ERROR, "chmpx_svr_reply: handle is empty.");
RETURN_FALSE;
}
ZEND_ASSERT(Z_TYPE_P(zval_chmpx_pkt_handle_resource_ptr) == IS_RESOURCE); /* just a check to be sure */
if(!zval_chmpx_pkt_handle_resource_ptr) {
php_error(E_ERROR, "chmpx_svr_reply: handle is empty.");
RETURN_FALSE;
}
// get_resources
chmpx_h* chmpx_handle_ptr = (chmpx_h *)zend_fetch_resource(Z_RES_P(zval_zval_chmpx_handle_ptr), "chmpx_handle_resource", chmpx_handle_resource_number);
if(CHM_INVALID_CHMPXHANDLE == *chmpx_handle_ptr){
// no created handle
php_error(E_ERROR, "chmpx_handle is CHM_INVALID_CHMPXHANDLE");
RETURN_FALSE;
}
chmpx_pkt_h* chmpx_pkt_handle_ptr = (chmpx_pkt_h *)zend_fetch_resource(Z_RES_P(zval_chmpx_pkt_handle_resource_ptr), "chmpx_pkt_handle_resource", chmpx_pkt_handle_resource_number);
if(CHM_INVALID_CHMPXHANDLE == *chmpx_pkt_handle_ptr){
php_error(E_NOTICE, "chmpx_pkt_handle is CHM_INVALID_CHMPXHANDLE");
RETURN_FALSE;
}
// call_some_functions after get_resources
bool bResult = chmpx_msg_reply(*chmpx_handle_ptr, *chmpx_pkt_handle_ptr, (const unsigned char*)strbinary, (size_t)strbinary_len);
// return_result after call_some_functions
if (!bResult) {
php_error(E_ERROR, "chmpxpx_svr_reply: failed send binary data.");
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(chmpxpx_svr_reply_kv)
{
// declare_variables
zval* zval_zval_chmpx_handle_ptr = NULL; // chmpx_handle_resource
zval* zval_chmpx_pkt_handle_resource_ptr = NULL; // chmpx_pkt_handle_resource
char* strbinkey = NULL;
size_t strbinkey_len = 0;
char* strbinval = NULL;
size_t strbinval_len = 0;
// assign_after_declare
ZEND_PARSE_PARAMETERS_START(4, 4)
Z_PARAM_RESOURCE(zval_zval_chmpx_handle_ptr)
Z_PARAM_RESOURCE(zval_chmpx_pkt_handle_resource_ptr)
Z_PARAM_STRING(strbinkey, strbinkey_len)
Z_PARAM_STRING(strbinval, strbinval_len)
ZEND_PARSE_PARAMETERS_END();
// validate_after_assign
ZEND_ASSERT(Z_TYPE_P(zval_zval_chmpx_handle_ptr) == IS_RESOURCE);
if(!zval_zval_chmpx_handle_ptr) {
php_error(E_ERROR, "chmpx_svr_reply: handle is empty.");
RETURN_FALSE;
}
ZEND_ASSERT(Z_TYPE_P(zval_chmpx_pkt_handle_resource_ptr) == IS_RESOURCE);
if(!zval_chmpx_pkt_handle_resource_ptr) {
php_error(E_ERROR, "chmpx_svr_reply: handle is empty.");
RETURN_FALSE;
}
// get_resources
chmpx_h* chmpx_handle_ptr = (chmpx_h *)zend_fetch_resource(Z_RES_P(zval_zval_chmpx_handle_ptr), "chmpx_handle_resource", chmpx_handle_resource_number);
if(CHM_INVALID_CHMPXHANDLE == *chmpx_handle_ptr){
// no created handle
php_error(E_ERROR, "chmpx_handle is CHM_INVALID_CHMPXHANDLE");
RETURN_LONG(-1);
}
chmpx_pkt_h* chmpx_pkt_handle_ptr = (chmpx_pkt_h *)zend_fetch_resource(Z_RES_P(zval_chmpx_pkt_handle_resource_ptr), "chmpx_pkt_handle_resource", chmpx_pkt_handle_resource_number);
if(CHM_INVALID_CHMPXHANDLE == *chmpx_pkt_handle_ptr){
php_error(E_NOTICE, "chmpx_pkt_handle is CHM_INVALID_CHMPXHANDLE");
RETURN_FALSE;
}
// call_some_functions after get_resources
CHMKVP kvp;
kvp.key.byptr = (unsigned char*)strbinkey;
kvp.key.length = (size_t)strbinkey_len;
kvp.val.byptr = (unsigned char*)strbinval;
kvp.val.length = (size_t)strbinval_len;
size_t length = 0;
unsigned char* pdata;
if(NULL == (pdata = cvt_kvp_bin(&kvp, &length))){
php_error(E_ERROR, "chmpxpx_svr_reply_kv: something error occurred in converting key and value to binary data.");
RETURN_FALSE;
}
bool bResult = chmpx_msg_reply(*chmpx_handle_ptr, *chmpx_pkt_handle_ptr, pdata, length);
// return_result after call_some_functions
if (!bResult) {
php_error(E_ERROR, "chmpxpx_svr_reply_kv: failed send binary data.");
if(pdata){
free(pdata);
}
RETURN_FALSE;
} else {
if(pdata){
free(pdata);
}
}
RETURN_TRUE;
}
PHP_FUNCTION(chmpxpx_slv_receive_kv)
{
// declare_variables
zval* zval_chmpx_handle_ptr = NULL; // chmpx_handle_resource
zval* zval_msgid_handle_ptr = NULL; // msgid_handle_resource
zval* strbinkey = NULL;
zval* strbinval = NULL;
zend_long timeout_ms = RCV_NO_WAIT;
// validate_after_assign
ZEND_PARSE_PARAMETERS_START(4, 5)
Z_PARAM_RESOURCE(zval_chmpx_handle_ptr)
Z_PARAM_RESOURCE(zval_msgid_handle_ptr)
Z_PARAM_ZVAL(strbinkey)
Z_PARAM_ZVAL(strbinval)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(timeout_ms)
ZEND_PARSE_PARAMETERS_END();
// validate_after_assign
ZEND_ASSERT(Z_TYPE_P(zval_chmpx_handle_ptr) == IS_RESOURCE);
if(!zval_chmpx_handle_ptr) {
php_error(E_ERROR, "chmpx_slv_receive_kv: handle is empty.");
RETURN_FALSE;
}
ZEND_ASSERT(Z_TYPE_P(zval_msgid_handle_ptr) == IS_RESOURCE);
if(!zval_msgid_handle_ptr) {
php_error(E_ERROR, "chmpx_slv_receive_kv: handle is empty.");
RETURN_FALSE;
}
// get_resources
chmpx_h* chmpx_handle_ptr = (chmpx_h *)zend_fetch_resource(Z_RES_P(zval_chmpx_handle_ptr), "chmpx_handle_resource", chmpx_handle_resource_number);
if(CHM_INVALID_CHMPXHANDLE == *chmpx_handle_ptr){
// no created handle
php_error(E_NOTICE, "chmpx_handle is CHM_INVALID_CHMPXHANDLE");
RETURN_FALSE;
}
msgid_t* msgid_handle_ptr = (msgid_t *)zend_fetch_resource(Z_RES_P(zval_msgid_handle_ptr), "msgid_handle_resource", msgid_handle_resource_number);
if(CHM_INVALID_MSGID == *msgid_handle_ptr){
php_error(E_NOTICE, "msgid_handle is CHM_INVALID_MSGID");
RETURN_FALSE;
}
// call chmpx_msg_receive using the chmpx_h pointer and get chmpx_pkt_handle
chmpx_pkt_h chmpx_pkt_handle = 0L;
unsigned char* pbody = NULL;
size_t length = 0;
bool bResult = chmpx_msg_receive(*chmpx_handle_ptr, *msgid_handle_ptr, &chmpx_pkt_handle, &pbody, &length, (int)timeout_ms);
// return_result after call_some_functions
if (!bResult) {
php_error(E_ERROR, "chmpxpx_slv_receive_kv: could not receive data, probably there is no received data by timeout.");
RETURN_FALSE;
}
CHMKVP kvp; // copy received data
if(cvt_bin_kvp(&kvp, pbody, length)){
if(Z_ISREF_P(strbinkey)) {
// See https://www.phpinternalsbook.com/php7/zvals/references.html
ZVAL_DEREF(strbinkey); // dereferencing
}
zval_ptr_dtor(strbinkey); // initialize the zval(decrements to refcount=0, and destroys the string if exists)
ZVAL_STRINGL(strbinkey, (char*)kvp.key.byptr, kvp.key.length);
if(Z_ISREF_P(strbinval)) {
// See https://www.phpinternalsbook.com/php7/zvals/references.html
ZVAL_DEREF(strbinval); // dereferencing
}
zval_ptr_dtor(strbinval); // initialize the zval(decrements to refcount=0, and destroys the string if exists)
ZVAL_STRINGL(strbinval, (char*)kvp.val.byptr, kvp.val.length);
}
if(pbody){
free(pbody);
}
if(0L != chmpx_pkt_handle && !chmpx_pkth_destroy(chmpx_pkt_handle)){
php_error(E_WARNING, "chmpxpx_slv_receive_kv: could not close chmpx packet handler.");
}
RETURN_TRUE;
}
PHP_FUNCTION(chmpxpx_slv_send_kv)
{
// declare_variables
zval* zval_chmpx_handle_ptr = NULL;
zval* zval_msgid_handle_ptr = NULL;
char* strbinkey = NULL;
size_t strbinkey_len = 0;
char* strbinval = NULL;
size_t strbinval_len = 0;
zend_bool is_routing = true;
zend_bool is_broadcast = false;
// assign_after_declare
ZEND_PARSE_PARAMETERS_START(4, 6)
Z_PARAM_RESOURCE(zval_chmpx_handle_ptr)
Z_PARAM_RESOURCE(zval_msgid_handle_ptr)
Z_PARAM_STRING(strbinkey, strbinkey_len)
Z_PARAM_STRING(strbinval, strbinval_len)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(is_routing)
Z_PARAM_BOOL(is_broadcast)
ZEND_PARSE_PARAMETERS_END();
// validate_after_assign
ZEND_ASSERT(Z_TYPE_P(zval_chmpx_handle_ptr) == IS_RESOURCE);
if(!zval_chmpx_handle_ptr) {
php_error(E_ERROR, "chmpx_slv_send_kv: handle is empty.");
RETURN_FALSE;
}
ZEND_ASSERT(Z_TYPE_P(zval_msgid_handle_ptr) == IS_RESOURCE);
if(!zval_msgid_handle_ptr) {
php_error(E_ERROR, "chmpx_slv_send_kv: handle is empty.");
RETURN_FALSE;
}
// get_resources
chmpx_h* chmpx_handle_ptr = (chmpx_h *)zend_fetch_resource(Z_RES_P(zval_chmpx_handle_ptr), "chmpx_handle_resource", chmpx_handle_resource_number);
if(CHM_INVALID_CHMPXHANDLE == *chmpx_handle_ptr){
// no created handle
php_error(E_NOTICE, "chmpx_handle is CHM_INVALID_CHMPXHANDLE");
RETURN_FALSE;
}
msgid_t* msgid_handle_ptr = (msgid_t *)zend_fetch_resource(Z_RES_P(zval_msgid_handle_ptr), "msgid_handle_resource", msgid_handle_resource_number);
if(CHM_INVALID_MSGID == *msgid_handle_ptr){
php_error(E_NOTICE, "msgid_handle is CHM_INVALID_MSGID");
RETURN_FALSE;
}
// call_some_functions after get_resources
CHMKVP kvp;
kvp.key.byptr = (unsigned char*)strbinkey;
kvp.key.length = (size_t)(strbinkey_len);
kvp.val.byptr = (unsigned char*)strbinval;
kvp.val.length = (size_t)(strbinval_len);