-
Notifications
You must be signed in to change notification settings - Fork 1
/
php_thrift_protocol.cpp
1100 lines (949 loc) · 37 KB
/
php_thrift_protocol.cpp
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
extern "C" {
#include "php_smthrift.h"
#include "time.h"
}
#include <sys/types.h>
#include <arpa/inet.h>
#include <cstdint>
#include <stdexcept>
#include <algorithm>
#include <vector>
#ifndef bswap_64
#define bswap_64(x) (((uint64_t)(x) << 56) | \
(((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
(((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
(((uint64_t)(x) << 8) & 0xff00000000ULL) | \
(((uint64_t)(x) >> 8) & 0xff000000ULL) | \
(((uint64_t)(x) >> 24) & 0xff0000ULL) | \
(((uint64_t)(x) >> 40) & 0xff00ULL) | \
((uint64_t)(x) >> 56))
#endif
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define htonll(x) bswap_64(x)
#define ntohll(x) bswap_64(x)
#elif __BYTE_ORDER == __BIG_ENDIAN
#define htonll(x) x
#define ntohll(x) x
#else
#error Unknown __BYTE_ORDER
#endif
enum TType {
T_STOP = 0,
T_VOID = 1,
T_BOOL = 2,
T_BYTE = 3,
T_I08 = 3,
T_I16 = 6,
T_I32 = 8,
T_U64 = 9,
T_I64 = 10,
T_DOUBLE = 4,
T_STRING = 11,
T_UTF7 = 11,
T_STRUCT = 12,
T_MAP = 13,
T_SET = 14,
T_LIST = 15,
T_UTF8 = 16,
T_UTF16 = 17
};
const int32_t VERSION_MASK = 0xffff0000;
const int32_t VERSION_1 = 0x80010000;
const int8_t T_CALL = 1;
const int8_t T_REPLY = 2;
const int8_t T_EXCEPTION = 3;
// tprotocolexception
const int INVALID_DATA = 1;
const int IO_WRITE_FAILED = 2;
const int IO_READ_FAILED = 3;
const int BAD_VERSION = 4;
static void throw_tprotocolexception(const char *what, long errorcode);
size_t read_bytes(php_stream *stream, char *reply, int bytes) {
size_t offset = 0;
while (offset < bytes) {
size_t got = php_stream_read(stream, reply + offset, bytes - offset);
if (got <= 0) {
return got;
}
offset += got;
}
return offset;
}
size_t write_bytes(php_stream *stream, char *reply, int bytes) {
size_t offset = 0;
while (offset < bytes) {
size_t got = php_stream_write(stream, reply + offset, bytes - offset);
if (got <= 0) {
return got;
}
offset += got;
}
return offset;
}
// 定义Php的异常?
class PHPExceptionWrapper : public std::exception {
public:
PHPExceptionWrapper(zval *_ex) throw() {
// 拷贝异常, 记录原因
ZVAL_COPY(&ex, _ex);
snprintf(_what, 40, "PHP exception zval=%p", _ex);
}
PHPExceptionWrapper(zend_object *_exobj) throw() {
ZVAL_OBJ(&ex, _exobj);
snprintf(_what, 40, "PHP exception zval=%p", _exobj);
}
~PHPExceptionWrapper() throw() {
zval_dtor(&ex);
}
const char *what() const throw() {
return _what;
}
// zval属性
operator zval *() const throw() {
return const_cast<zval *>(&ex);
} // Zend API doesn't do 'const'...
protected:
zval ex;
char _what[40];
};
class PHPTransport {
protected:
PHPTransport(zval *_p) {
assert(Z_TYPE_P(_p) == IS_OBJECT);
socket = Z_SMTHRIFT_OBJ_P(_p); // 通过TBinaryProtocolAccelerated 获取socket
// php_printf("PHPTransport socket: %p\n", socket);
// 自带Buffer
buffer.resize(4); // 预留四个字节
}
~PHPTransport() {
}
protected:
std::vector<char> buffer;
smthrift_t *socket;
char errbuf[128];
};
class PHPOutputTransport : public PHPTransport {
public:
PHPOutputTransport(zval *_p) : PHPTransport(_p) { }
~PHPOutputTransport() { }
// 底层io
void write(const char *data, size_t len) {
#ifdef DEBUG_LOG
php_printf("PHPOutputTransport write data: %d to buffer\n", len);
#endif
buffer.insert(std::end(buffer), data, data + len);
}
void writeI64(int64_t i) {
i = htonll(i);
write((const char *) &i, 8);
}
void writeU32(uint32_t i) {
i = htonl(i);
write((const char *) &i, 4);
}
void writeI32(int32_t i) {
i = htonl(i);
write((const char *) &i, 4);
}
void writeI16(int16_t i) {
i = htons(i);
write((const char *) &i, 2);
}
void writeI8(int8_t i) {
write((const char *) &i, 1);
}
void writeString(const char *str, size_t len) {
writeU32(len);
write(str, len);
}
void flush() {
// Flush一个Frame的数据
uint32_t i = buffer.size() - 4;
i = htonl(i);
memcpy(buffer.data(), (const char *) &i, 4);
#ifdef DEBUG_LOG
php_printf("flush data len: %d to socket: %p\n", buffer.size(), socket->stream);
#endif
size_t len = write_bytes(socket->stream, buffer.data(), buffer.size());
if (len != buffer.size()) {
sprintf(errbuf, "php_stream_write to flush data failed, len: %d vs. %d, pid: %d",
len, buffer.size(), socket->current_pid);
throw_tprotocolexception(errbuf, IO_WRITE_FAILED);
}
}
};
class PHPInputTransport : public PHPTransport {
protected:
size_t buffer_used;
char *buffer_ptr;
public:
PHPInputTransport(zval *_p) : PHPTransport(_p) {
}
~PHPInputTransport() {
}
void skip(size_t len) {
if (len > buffer_used) {
// 跑出异常
sprintf(errbuf, "socket data length invalid, pid: %d", socket->current_pid);
throw_tprotocolexception(errbuf, INVALID_DATA);
}
buffer_ptr = buffer_ptr + len;
buffer_used -= len;
}
// 理论上 readBytes 会保证读取到足够的长度
void readBytes(void *buf, size_t len) {
// 读取一帧数据
if (buffer.size() <= 4) {
// 读取一帧数据
uint32_t c;
//
// EAGAIN
// EINPROGRESS 不用考虑, 这里使用blocking io
//
buffer_used = read_bytes(socket->stream, (char *) &c, 4);
if (buffer_used != 4) {
sprintf(errbuf, "php_stream_read of frame size failed, buffer_used: %d, pid: %d",
buffer_used, socket->current_pid);
throw_tprotocolexception(errbuf, IO_READ_FAILED);
}
c = (uint32_t) ntohl(c);
buffer.resize(4 + c);
buffer_ptr = buffer.data() + 4;
buffer_used = read_bytes(socket->stream, buffer_ptr, c);
if (buffer_used != c) {
sprintf(errbuf, "php_stream_read of frame body failed, buffer_used: %d, pid: %d",
buffer_used, socket->current_pid);
throw_tprotocolexception(errbuf, IO_READ_FAILED);
}
}
if (len > buffer_used) {
// 抛出异常
sprintf(errbuf, "socket data length invalid, buffer_used: %d < len: %d, pid: %d",
buffer_used, len, socket->current_pid);
throw_tprotocolexception(errbuf, INVALID_DATA);
}
// 读取指定长度的数据
memcpy(buf, buffer_ptr, len);
buffer_ptr = buffer_ptr + len;
buffer_used -= len;
}
int8_t readI8() {
int8_t c;
readBytes(&c, 1);
return c;
}
int16_t readI16() {
int16_t c;
readBytes(&c, 2);
return (int16_t) ntohs(c);
}
uint32_t readU32() {
uint32_t c;
readBytes(&c, 4);
return (uint32_t) ntohl(c);
}
int32_t readI32() {
int32_t c;
readBytes(&c, 4);
return (int32_t) ntohl(c);
}
};
static void binary_deserialize_spec(zval *zthis, PHPInputTransport &transport, HashTable *spec);
static void binary_serialize_spec(zval *zthis, PHPOutputTransport &transport, HashTable *spec);
static void binary_serialize(int8_t thrift_typeID, PHPOutputTransport &transport, zval *value, HashTable *fieldspec);
// 通过Classname反射并且创建php对象
// Create a PHP object given a typename and call the ctor, optionally passing up to 2 arguments
static void createObject(const char *obj_typename, zval *return_value,
int nargs = 0, zval *arg1 = nullptr, zval *arg2 = nullptr) {
/* is there a better way to do that on the stack ? */
// 创建zend_string
zend_string *obj_name = zend_string_init(obj_typename, strlen(obj_typename), 0);
// 从classname获取class entry(反射)
zend_class_entry *ce = zend_fetch_class(obj_name, ZEND_FETCH_CLASS_DEFAULT);
// 释放obj_name
zend_string_release(obj_name);
if (!ce) {
php_error_docref(nullptr, E_ERROR, "Class %s does not exist", obj_typename);
RETURN_NULL();
}
// 通过ce创建一个对象, 复制给return_value
object_and_properties_init(return_value, ce, nullptr);
zend_function *constructor = zend_std_get_constructor(Z_OBJ_P(return_value));
zval ctor_rv;
// 调用构造函数
zend_call_method(return_value, ce, &constructor, NULL, 0, &ctor_rv, nargs, arg1, arg2);
zval_dtor(&ctor_rv);
}
static void throw_tprotocolexception(const char *what, long errorcode) {
zval zwhat, zerrorcode;
ZVAL_STRING(&zwhat, what);
ZVAL_LONG(&zerrorcode, errorcode);
zval ex;
createObject("\\Thrift\\Exception\\TProtocolException", &ex, 2, &zwhat, &zerrorcode);
// 构建对象,释放对象
zval_dtor(&zwhat);
zval_dtor(&zerrorcode);
throw PHPExceptionWrapper(&ex);
}
// Sets EG(exception), call this and then RETURN_NULL();
static void throw_zend_exception_from_std_exception(const std::exception &ex) {
zend_throw_exception(zend_exception_get_default(), const_cast<char *>(ex.what()), 0);
}
static void skip_element(long thrift_typeID, PHPInputTransport &transport) {
switch (thrift_typeID) {
case T_STOP:
case T_VOID:
return;
case T_STRUCT:
while (true) {
int8_t ttype = transport.readI8(); // get field type
if (ttype == T_STOP) break;
transport.skip(2); // skip field number, I16
skip_element(ttype, transport); // skip field payload
}
return;
case T_BOOL:
case T_BYTE:
transport.skip(1);
return;
case T_I16:
transport.skip(2);
return;
case T_I32:
transport.skip(4);
return;
case T_U64:
case T_I64:
case T_DOUBLE:
transport.skip(8);
return;
//case T_UTF7: // aliases T_STRING
case T_UTF8:
case T_UTF16:
case T_STRING: {
uint32_t len = transport.readU32();
transport.skip(len);
}
return;
case T_MAP: {
int8_t keytype = transport.readI8();
int8_t valtype = transport.readI8();
uint32_t size = transport.readU32();
for (uint32_t i = 0; i < size; ++i) {
skip_element(keytype, transport);
skip_element(valtype, transport);
}
}
return;
case T_LIST:
case T_SET: {
int8_t valtype = transport.readI8();
uint32_t size = transport.readU32();
for (uint32_t i = 0; i < size; ++i) {
skip_element(valtype, transport);
}
}
return;
};
char errbuf[128];
sprintf(errbuf, "Unknown thrift typeID %ld", thrift_typeID);
throw_tprotocolexception(errbuf, INVALID_DATA);
}
static inline bool zval_is_bool(zval *v) {
return Z_TYPE_P(v) == IS_TRUE || Z_TYPE_P(v) == IS_FALSE;
}
static void binary_deserialize(int8_t thrift_typeID, PHPInputTransport &transport, zval *return_value,
HashTable *fieldspec) {
// 初始情况下赋值为NULL
ZVAL_NULL(return_value);
switch (thrift_typeID) {
case T_STOP:
case T_VOID: RETURN_NULL();
return;
case T_STRUCT: {
// 如何查找hashmap
zval *val_ptr = zend_hash_str_find(fieldspec, "class", sizeof("class") - 1);
if (val_ptr == nullptr) {
throw_tprotocolexception("no class type in spec", INVALID_DATA);
skip_element(T_STRUCT, transport);
RETURN_NULL();
}
char *structType = Z_STRVAL_P(val_ptr);
// Create an object in PHP userland based on our spec
createObject(structType, return_value);
if (Z_TYPE_P(return_value) == IS_NULL) {
// unable to create class entry
skip_element(T_STRUCT, transport);
RETURN_NULL();
}
zval *spec = zend_read_static_property(Z_OBJCE_P(return_value), "_TSPEC", sizeof("_TSPEC") - 1, false);
if (Z_TYPE_P(spec) != IS_ARRAY) {
char errbuf[128];
snprintf(errbuf, 128, "spec for %s is wrong type: %d\n", structType, Z_TYPE_P(spec));
throw_tprotocolexception(errbuf, INVALID_DATA);
RETURN_NULL();
}
binary_deserialize_spec(return_value, transport, Z_ARRVAL_P(spec));
return;
}
break;
case T_BOOL: {
uint8_t c;
transport.readBytes(&c, 1);
RETURN_BOOL(c != 0);
}
//case T_I08: // same numeric value as T_BYTE
case T_BYTE: {
uint8_t c;
transport.readBytes(&c, 1);
RETURN_LONG((int8_t) c);
}
case T_I16: {
uint16_t c;
transport.readBytes(&c, 2);
RETURN_LONG((int16_t) ntohs(c));
}
case T_I32: {
uint32_t c;
transport.readBytes(&c, 4);
RETURN_LONG((int32_t) ntohl(c));
}
case T_U64:
case T_I64: {
uint64_t c;
transport.readBytes(&c, 8);
RETURN_LONG((int64_t) ntohll(c));
}
case T_DOUBLE: {
union {
uint64_t c;
double d;
} a;
transport.readBytes(&(a.c), 8);
a.c = ntohll(a.c);
RETURN_DOUBLE(a.d);
}
//case T_UTF7: // aliases T_STRING
case T_UTF8:
case T_UTF16:
case T_STRING: {
uint32_t size = transport.readU32();
if (size) {
char strbuf[size + 1];
transport.readBytes(strbuf, size);
strbuf[size] = '\0';
ZVAL_STRINGL(return_value, strbuf, size);
} else {
ZVAL_EMPTY_STRING(return_value);
}
return;
}
case T_MAP: { // array of key -> value
uint8_t types[2];
transport.readBytes(types, 2);
uint32_t size = transport.readU32();
array_init(return_value);
zval *val_ptr;
val_ptr = zend_hash_str_find(fieldspec, "key", sizeof("key") - 1);
HashTable *keyspec = Z_ARRVAL_P(val_ptr);
val_ptr = zend_hash_str_find(fieldspec, "val", sizeof("val") - 1);
HashTable *valspec = Z_ARRVAL_P(val_ptr);
for (uint32_t s = 0; s < size; ++s) {
zval key, value;
binary_deserialize(types[0], transport, &key, keyspec);
binary_deserialize(types[1], transport, &value, valspec);
if (Z_TYPE(key) == IS_LONG) {
zend_hash_index_update(Z_ARR_P(return_value), Z_LVAL(key), &value);
} else {
if (Z_TYPE(key) != IS_STRING) convert_to_string(&key);
zend_symtable_update(Z_ARR_P(return_value), Z_STR(key), &value);
}
zval_dtor(&key);
}
return; // return_value already populated
}
case T_LIST: { // array with autogenerated numeric keys
int8_t type = transport.readI8();
uint32_t size = transport.readU32();
zval *val_ptr = zend_hash_str_find(fieldspec, "elem", sizeof("elem") - 1);
HashTable *elemspec = Z_ARRVAL_P(val_ptr);
array_init(return_value);
for (uint32_t s = 0; s < size; ++s) {
zval value;
binary_deserialize(type, transport, &value, elemspec);
zend_hash_next_index_insert(Z_ARR_P(return_value), &value);
}
return;
}
case T_SET: { // array of key -> TRUE
uint8_t type;
uint32_t size;
transport.readBytes(&type, 1);
transport.readBytes(&size, 4);
size = ntohl(size);
zval *val_ptr = zend_hash_str_find(fieldspec, "elem", sizeof("elem") - 1);
HashTable *elemspec = Z_ARRVAL_P(val_ptr);
array_init(return_value);
// 所谓SET, 就是Arra对应的Value全部都是true
for (uint32_t s = 0; s < size; ++s) {
zval key, value;
ZVAL_TRUE(&value);
binary_deserialize(type, transport, &key, elemspec);
if (Z_TYPE(key) == IS_LONG) {
zend_hash_index_update(Z_ARR_P(return_value), Z_LVAL(key), &value);
} else {
if (Z_TYPE(key) != IS_STRING) convert_to_string(&key);
zend_symtable_update(Z_ARR_P(return_value), Z_STR(key), &value);
}
zval_dtor(&key);
}
return;
}
};
char errbuf[128];
sprintf(errbuf, "Unknown thrift typeID %d", thrift_typeID);
throw_tprotocolexception(errbuf, INVALID_DATA);
}
static void binary_serialize_hashtable_key(int8_t keytype, PHPOutputTransport &transport, HashTable *ht,
HashPosition &ht_pos) {
bool keytype_is_numeric = (!((keytype == T_STRING) || (keytype == T_UTF8) || (keytype == T_UTF16)));
zend_string *key;
uint key_len;
long index = 0;
zval z;
int res = zend_hash_get_current_key_ex(ht, &key, (zend_ulong *) &index, &ht_pos);
if (keytype_is_numeric) {
if (res == HASH_KEY_IS_STRING) {
index = strtol(ZSTR_VAL(key), nullptr, 10);
}
ZVAL_LONG(&z, index);
} else {
char buf[64];
if (res == HASH_KEY_IS_STRING) {
ZVAL_STR_COPY(&z, key);
} else {
snprintf(buf, 64, "%ld", index);
ZVAL_STRING(&z, buf);
}
}
binary_serialize(keytype, transport, &z, nullptr);
zval_dtor(&z);
}
static void binary_serialize(int8_t thrift_typeID, PHPOutputTransport &transport, zval *value, HashTable *fieldspec) {
// At this point the typeID (and field num, if applicable) should've already been written to the output so all we need to do is write the payload.
// 如何序列化一个对象呢?
switch (thrift_typeID) {
case T_STOP:
case T_VOID:
return;
case T_STRUCT: {
if (Z_TYPE_P(value) != IS_OBJECT) {
throw_tprotocolexception("Attempt to send non-object type as a T_STRUCT", INVALID_DATA);
}
// 如何读取static属性呢?
// class OAuthUser {
// static $isValidate = false;
//
// static $_TSPEC = array(
// 1 => array(
// 'var' => 'user_id',
// 'isRequired' => false,
// 'type' => TType::I64,
// ),
// 2 => array(
// 'var' => 'oauth_key',
// 'isRequired' => false,
// 'type' => TType::STRING,
// ),
// 3 => array(
// 'var' => 'oauth_secret',
// 'isRequired' => false,
// 'type' => TType::STRING,
// ),
// );
// 通过value --> class entry --> TSpec
zval *spec = zend_read_static_property(Z_OBJCE_P(value), "_TSPEC", sizeof("_TSPEC") - 1, false);
if (Z_TYPE_P(spec) != IS_ARRAY) {
throw_tprotocolexception("Attempt to send non-Thrift object as a T_STRUCT", INVALID_DATA);
}
binary_serialize_spec(value, transport, Z_ARRVAL_P(spec));
}
return;
case T_BOOL:
// 如何序列化BOOL呢?
if (!zval_is_bool(value)) convert_to_boolean(value);
transport.writeI8(Z_TYPE_INFO_P(value) == IS_TRUE ? 1 : 0);
return;
case T_BYTE:
if (Z_TYPE_P(value) != IS_LONG) convert_to_long(value); // 如何将value转换成为long呢?
transport.writeI8(Z_LVAL_P(value));
return;
case T_I16:
if (Z_TYPE_P(value) != IS_LONG) convert_to_long(value);
transport.writeI16(Z_LVAL_P(value));
return;
case T_I32:
if (Z_TYPE_P(value) != IS_LONG) convert_to_long(value);
transport.writeI32(Z_LVAL_P(value));
return;
case T_I64:
case T_U64: {
int64_t l_data;
#if defined(_LP64) || defined(_WIN64)
if (Z_TYPE_P(value) != IS_LONG) convert_to_long(value);
l_data = Z_LVAL_P(value);
#else
if (Z_TYPE_P(value) != IS_DOUBLE) convert_to_double(value);
l_data = (int64_t)Z_DVAL_P(value);
#endif
transport.writeI64(l_data);
}
return;
case T_DOUBLE: {
union {
int64_t c;
double d;
} a;
if (Z_TYPE_P(value) != IS_DOUBLE) convert_to_double(value);
a.d = Z_DVAL_P(value);
transport.writeI64(a.c);
}
return;
case T_UTF8:
case T_UTF16:
case T_STRING:
if (Z_TYPE_P(value) != IS_STRING) convert_to_string(value);
transport.writeString(Z_STRVAL_P(value), Z_STRLEN_P(value));
return;
case T_MAP: {
if (Z_TYPE_P(value) != IS_ARRAY) convert_to_array(value);
if (Z_TYPE_P(value) != IS_ARRAY) {
throw_tprotocolexception("Attempt to send an incompatible type as an array (T_MAP)", INVALID_DATA);
}
HashTable *ht = Z_ARRVAL_P(value);
zval *val_ptr;
val_ptr = zend_hash_str_find(fieldspec, "ktype", sizeof("ktype") - 1);
if (Z_TYPE_P(val_ptr) != IS_LONG) convert_to_long(val_ptr);
uint8_t keytype = Z_LVAL_P(val_ptr);
transport.writeI8(keytype);
val_ptr = zend_hash_str_find(fieldspec, "vtype", sizeof("vtype") - 1);
if (Z_TYPE_P(val_ptr) != IS_LONG) convert_to_long(val_ptr);
uint8_t valtype = Z_LVAL_P(val_ptr);
transport.writeI8(valtype);
val_ptr = zend_hash_str_find(fieldspec, "val", sizeof("val") - 1);
HashTable *valspec = Z_ARRVAL_P(val_ptr);
transport.writeI32(zend_hash_num_elements(ht));
// 如何遍历hashmap
// HashPosition, zend_hash_get_current_data_ex, zend_hash_get_current_key_ex, zend_hash_move_forward_ex
HashPosition key_ptr;
for (zend_hash_internal_pointer_reset_ex(ht, &key_ptr);
(val_ptr = zend_hash_get_current_data_ex(ht, &key_ptr)) != nullptr;
zend_hash_move_forward_ex(ht, &key_ptr)) {
binary_serialize_hashtable_key(keytype, transport, ht, key_ptr);
binary_serialize(valtype, transport, val_ptr, valspec);
}
}
return;
case T_LIST: {
if (Z_TYPE_P(value) != IS_ARRAY) convert_to_array(value);
if (Z_TYPE_P(value) != IS_ARRAY) {
throw_tprotocolexception("Attempt to send an incompatible type as an array (T_LIST)", INVALID_DATA);
}
HashTable *ht = Z_ARRVAL_P(value);
zval *val_ptr;
// 通过字符串方式访问hash
val_ptr = zend_hash_str_find(fieldspec, "etype", sizeof("etype") - 1);
if (Z_TYPE_P(val_ptr) != IS_LONG) convert_to_long(val_ptr);
// 写入value type
uint8_t valtype = Z_LVAL_P(val_ptr);
transport.writeI8(valtype);
val_ptr = zend_hash_str_find(fieldspec, "elem", sizeof("elem") - 1);
HashTable *valspec = Z_ARRVAL_P(val_ptr);
transport.writeI32(zend_hash_num_elements(ht));
HashPosition key_ptr;
for (zend_hash_internal_pointer_reset_ex(ht, &key_ptr);
(val_ptr = zend_hash_get_current_data_ex(ht, &key_ptr)) != nullptr;
zend_hash_move_forward_ex(ht, &key_ptr)) {
binary_serialize(valtype, transport, val_ptr, valspec);
}
}
return;
case T_SET: {
if (Z_TYPE_P(value) != IS_ARRAY) convert_to_array(value);
if (Z_TYPE_P(value) != IS_ARRAY) {
throw_tprotocolexception("Attempt to send an incompatible type as an array (T_SET)", INVALID_DATA);
}
HashTable *ht = Z_ARRVAL_P(value);
zval *val_ptr;
val_ptr = zend_hash_str_find(fieldspec, "etype", sizeof("etype") - 1);
if (Z_TYPE_P(val_ptr) != IS_LONG) convert_to_long(val_ptr);
uint8_t keytype = Z_LVAL_P(val_ptr);
transport.writeI8(keytype);
transport.writeI32(zend_hash_num_elements(ht));
HashPosition key_ptr;
for (zend_hash_internal_pointer_reset_ex(ht, &key_ptr);
(val_ptr = zend_hash_get_current_data_ex(ht, &key_ptr)) != nullptr;
zend_hash_move_forward_ex(ht, &key_ptr)) {
binary_serialize_hashtable_key(keytype, transport, ht, key_ptr);
}
}
return;
};
char errbuf[128];
snprintf(errbuf, 128, "Unknown thrift typeID %d", thrift_typeID);
throw_tprotocolexception(errbuf, INVALID_DATA);
}
static void protocol_writeMessageBegin(PHPOutputTransport *transport,
zend_string *method_name, int32_t msgtype, int32_t seqID) {
int32_t version = VERSION_1 | msgtype;
transport->writeI32(version);
transport->writeString(method_name->val, method_name->len);
transport->writeI32(seqID);
}
static inline bool ttype_is_int(int8_t t) {
return ((t == T_BYTE) || ((t >= T_I16) && (t <= T_I64)));
}
static inline bool ttypes_are_compatible(int8_t t1, int8_t t2) {
// Integer types of different widths are considered compatible;
// otherwise the typeID must match.
return ((t1 == t2) || (ttype_is_int(t1) && ttype_is_int(t2)));
}
static void binary_deserialize_spec(zval *zthis, PHPInputTransport &transport, HashTable *spec) {
// SET and LIST have 'elem' => array('type', [optional] 'class')
// MAP has 'val' => array('type', [optiona] 'class')
zend_class_entry *ce = Z_OBJCE_P(zthis);
while (true) {
int8_t ttype = transport.readI8();
if (ttype == T_STOP) {
return;
}
int16_t fieldno = transport.readI16();
// 通过index来读取array
zval *val_ptr = zend_hash_index_find(spec, fieldno);
if (val_ptr != nullptr) {
// 类型转换: zend_array的处理
HashTable *fieldspec = Z_ARRVAL_P(val_ptr);
// pull the field name
val_ptr = zend_hash_str_find(fieldspec, "var", sizeof("var") - 1);
char *varname = Z_STRVAL_P(val_ptr);
// and the type
val_ptr = zend_hash_str_find(fieldspec, "type", sizeof("type") - 1);
if (Z_TYPE_P(val_ptr) != IS_LONG) convert_to_long(val_ptr);
int8_t expected_ttype = Z_LVAL_P(val_ptr);
// 实际类型和期待类型是否兼容呢?
if (ttypes_are_compatible(ttype, expected_ttype)) {
zval rv;
ZVAL_UNDEF(&rv);
binary_deserialize(ttype, transport, &rv, fieldspec);
// 如何反序列化呢?
// zthis->$varname --> &rv
zend_update_property(ce, zthis, varname, strlen(varname), &rv);
// 删除rv(rv作用)
zval_ptr_dtor(&rv);
} else {
skip_element(ttype, transport);
}
} else {
skip_element(ttype, transport);
}
}
}
//is used to validate objects before serialization and after deserialization. For now, only required fields are validated.
static
void validate_thrift_object(zval *object) {
zend_class_entry *object_class_entry = Z_OBJCE_P(object);
zval *is_validate = zend_read_static_property(object_class_entry, "isValidate", sizeof("isValidate") - 1, false);
zval *spec = zend_read_static_property(object_class_entry, "_TSPEC", sizeof("_TSPEC") - 1, false);
HashPosition key_ptr;
zval *val_ptr;
if (Z_TYPE_INFO_P(is_validate) == IS_TRUE) {
for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(spec), &key_ptr);
(val_ptr = zend_hash_get_current_data_ex(Z_ARRVAL_P(spec), &key_ptr)) != nullptr;
zend_hash_move_forward_ex(Z_ARRVAL_P(spec), &key_ptr)) {
zend_ulong fieldno;
if (zend_hash_get_current_key_ex(Z_ARRVAL_P(spec), nullptr, &fieldno, &key_ptr) != HASH_KEY_IS_LONG) {
throw_tprotocolexception("Bad keytype in TSPEC (expected 'long')", INVALID_DATA);
return;
}
HashTable *fieldspec = Z_ARRVAL_P(val_ptr);
// field name
zval *zvarname = zend_hash_str_find(fieldspec, "var", sizeof("var") - 1);
char *varname = Z_STRVAL_P(zvarname);
zval *is_required = zend_hash_str_find(fieldspec, "isRequired", sizeof("isRequired") - 1);
zval rv;
zval *prop = zend_read_property(object_class_entry, object, varname, strlen(varname), false, &rv);
if (Z_TYPE_INFO_P(is_required) == IS_TRUE && Z_TYPE_P(prop) == IS_NULL) {
char errbuf[128];
snprintf(errbuf, 128, "Required field %s.%s is unset!", ZSTR_VAL(object_class_entry->name), varname);
throw_tprotocolexception(errbuf, INVALID_DATA);
}
}
}
}
static void binary_serialize_spec(zval *zthis, PHPOutputTransport &transport, HashTable *spec) {
validate_thrift_object(zthis);
HashPosition key_ptr;
zval *val_ptr;
// 通过 HashPosition 来遍历hashmap
for (zend_hash_internal_pointer_reset_ex(spec, &key_ptr);
(val_ptr = zend_hash_get_current_data_ex(spec, &key_ptr)) != nullptr; zend_hash_move_forward_ex(spec,
&key_ptr)) {
// zend_hash_get_current_data_ex 读取Value
// zend_hash_get_current_key_ex 读取Key
zend_ulong fieldno;
if (zend_hash_get_current_key_ex(spec, nullptr, &fieldno, &key_ptr) != HASH_KEY_IS_LONG) {
throw_tprotocolexception("Bad keytype in TSPEC (expected 'long')", INVALID_DATA);
return;
}
HashTable *fieldspec = Z_ARRVAL_P(val_ptr);
// field name
val_ptr = zend_hash_str_find(fieldspec, "var", sizeof("var") - 1);
char *varname = Z_STRVAL_P(val_ptr);
// thrift type
val_ptr = zend_hash_str_find(fieldspec, "type", sizeof("type") - 1);
if (Z_TYPE_P(val_ptr) != IS_LONG) convert_to_long(val_ptr);
int8_t ttype = Z_LVAL_P(val_ptr);
// Z_OBJCE_P
// Object Class Entry Pointer
// 如何调用 zend_read_property: class, instance, varname, 其他
//
zval rv;
zval *prop = zend_read_property(Z_OBJCE_P(zthis), zthis, varname, strlen(varname), false, &rv);
// 类型, fieldno, 以及具体的value
if (Z_TYPE_P(prop) != IS_NULL) {
transport.writeI8(ttype);
transport.writeI16(fieldno);
binary_serialize(ttype, transport, prop, fieldspec);
}
}
// 结束struct
transport.writeI8(T_STOP); // struct end
}
// 6 params: $transport $method_name $ttype $request_struct $seqID $strict_write
PHP_FUNCTION (sm_thrift_protocol_write_binary) {
zval *protocol;
zval *request_struct;
zend_string *method_name;
long msgtype, seqID;
zend_bool strict_write;
// s 表示字符串, 接受者为 char*, &len
// S表示字符串,接受者为 zend_string, 内存的分配由zend_parse_parameters_ex来处理,并且不用用户考虑如何释放
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "oSlolb",
&protocol, &method_name, &msgtype,
&request_struct, &seqID, &strict_write) == FAILURE) {
return;
}
#ifdef DEBUG_LOG
php_printf("thrift_protocol_write_binary in cpp\n");
#endif
try {
// 如何读取object的类(Class Entry)
// zval --> value --> obj --> ce