blob: 109eeaaf60f05589fb455f79dd2cab8d7733c7fc [file] [log] [blame]
iproctor9a41a0c2007-07-16 21:59:24 +00001open Thrift
2
3module P = Protocol
4
5let get_byte i b = 255 land (i lsr (8*b))
6let get_byte64 i b = 255 land (Int64.to_int (Int64.shift_right i (8*b)))
7
8
9let tv = P.t_type_to_i
10let vt = P.t_type_of_i
11
12
David Reiss0c90f6f2008-02-06 22:18:40 +000013let comp_int b n =
iproctord4de1e92007-07-24 19:47:55 +000014 let s = ref 0l in
15 let sb = 32 - 8*n in
iproctor9a41a0c2007-07-16 21:59:24 +000016 for i=0 to (n-1) do
iproctord4de1e92007-07-24 19:47:55 +000017 s:= Int32.logor !s (Int32.shift_left (Int32.of_int (int_of_char b.[i])) (8*(n-1-i)))
iproctor9a41a0c2007-07-16 21:59:24 +000018 done;
iproctord4de1e92007-07-24 19:47:55 +000019 Int32.to_int (Int32.shift_right (Int32.shift_left !s sb) sb)
iproctor9a41a0c2007-07-16 21:59:24 +000020
21let comp_int64 b n =
22 let s = ref 0L in
23 for i=0 to (n-1) do
24 s:=Int64.logor !s (Int64.shift_left (Int64.of_int (int_of_char b.[i])) (8*(n-1-i)))
25 done;
26 !s
27
iproctord4de1e92007-07-24 19:47:55 +000028let version_mask = 0xffff0000
29let version_1 = 0x80010000
30
iproctor9a41a0c2007-07-16 21:59:24 +000031class t trans =
32object (self)
33 inherit P.t trans
34 val ibyte = String.create 8
David Reiss0c90f6f2008-02-06 22:18:40 +000035 method writeBool b =
iproctor9a41a0c2007-07-16 21:59:24 +000036 ibyte.[0] <- char_of_int (if b then 1 else 0);
37 trans#write ibyte 0 1
38 method writeByte i =
39 ibyte.[0] <- char_of_int (get_byte i 0);
40 trans#write ibyte 0 1
41 method writeI16 i =
42 let gb = get_byte i in
43 ibyte.[1] <- char_of_int (gb 0);
44 ibyte.[0] <- char_of_int (gb 1);
45 trans#write ibyte 0 2
46 method writeI32 i =
47 let gb = get_byte i in
48 for i=0 to 3 do
49 ibyte.[3-i] <- char_of_int (gb i)
50 done;
51 trans#write ibyte 0 4
52 method writeI64 i=
53 let gb = get_byte64 i in
54 for i=0 to 7 do
55 ibyte.[7-i] <- char_of_int (gb i)
56 done;
57 trans#write ibyte 0 8
58 method writeDouble d =
59 self#writeI64 (Int64.bits_of_float d)
60 method writeString s=
61 let n = String.length s in
62 self#writeI32(n);
63 trans#write s 0 n
64 method writeBinary a = self#writeString a
65 method writeMessageBegin (n,t,s) =
iproctord4de1e92007-07-24 19:47:55 +000066 self#writeI32 (version_1 lor (P.message_type_to_i t));
iproctor9a41a0c2007-07-16 21:59:24 +000067 self#writeString n;
iproctor9a41a0c2007-07-16 21:59:24 +000068 self#writeI32 s
69 method writeMessageEnd = ()
70 method writeStructBegin s = ()
71 method writeStructEnd = ()
72 method writeFieldBegin (n,t,i) =
73 self#writeByte (tv t);
74 self#writeI16 i
75 method writeFieldEnd = ()
76 method writeFieldStop =
77 self#writeByte (tv (Protocol.T_STOP))
78 method writeMapBegin (k,v,s) =
79 self#writeByte (tv k);
80 self#writeByte (tv v);
81 self#writeI32 s
82 method writeMapEnd = ()
83 method writeListBegin (t,s) =
84 self#writeByte (tv t);
85 self#writeI32 s
86 method writeListEnd = ()
87 method writeSetBegin (t,s) =
88 self#writeByte (tv t);
89 self#writeI32 s
90 method writeSetEnd = ()
David Reiss0c90f6f2008-02-06 22:18:40 +000091 method readByte =
iproctor9a41a0c2007-07-16 21:59:24 +000092 ignore (trans#readAll ibyte 0 1);
93 (comp_int ibyte 1)
94 method readI16 =
95 ignore (trans#readAll ibyte 0 2);
96 comp_int ibyte 2
97 method readI32 =
98 ignore (trans#readAll ibyte 0 4);
99 comp_int ibyte 4
100 method readI64 =
101 ignore (trans#readAll ibyte 0 8);
102 comp_int64 ibyte 8
103 method readDouble =
104 Int64.float_of_bits (self#readI64)
105 method readBool =
106 self#readByte = 1
107 method readString =
108 let sz = self#readI32 in
109 let buf = String.create sz in
110 ignore (trans#readAll buf 0 sz);
111 buf
112 method readBinary = self#readString
113 method readMessageBegin =
iproctord4de1e92007-07-24 19:47:55 +0000114 let ver = self#readI32 in
115 if (ver land version_mask != version_1) then
116 (print_int ver;
iproctore470aa32007-08-10 20:48:12 +0000117 raise (P.E (P.BAD_VERSION, "Missing version identifier")))
iproctord4de1e92007-07-24 19:47:55 +0000118 else
119 let s = self#readString in
120 let mt = P.message_type_of_i (ver land 0xFF) in
121 (s,mt, self#readI32)
iproctor9a41a0c2007-07-16 21:59:24 +0000122 method readMessageEnd = ()
123 method readStructBegin =
124 ""
125 method readStructEnd = ()
126 method readFieldBegin =
David Reiss0c90f6f2008-02-06 22:18:40 +0000127 let t = (vt (self#readByte))
iproctor9a41a0c2007-07-16 21:59:24 +0000128 in
129 if t != P.T_STOP then
130 ("",t,self#readI16)
131 else ("",t,0);
132 method readFieldEnd = ()
133 method readMapBegin =
134 let kt = vt (self#readByte) in
135 let vt = vt (self#readByte) in
136 (kt,vt, self#readI32)
137 method readMapEnd = ()
138 method readListBegin =
139 let t = vt (self#readByte) in
140 (t,self#readI32)
141 method readListEnd = ()
142 method readSetBegin =
143 let t = vt (self#readByte) in
144 (t, self#readI32);
145 method readSetEnd = ()
146end
147
148class factory =
149object
150 inherit P.factory
151 method getProtocol tr = new t tr
152end