blob: efcfd779423fc2fc12b5ecdf1ac897504e02dbc5 [file] [log] [blame]
Christopher Piro094823a2007-07-18 00:26:12 +00001%%% Copyright (c) 2007- Facebook
2%%% Distributed under the Thrift Software License
Christopher Piroc2e37c72007-11-15 06:26:30 +00003%%%
Christopher Piro094823a2007-07-18 00:26:12 +00004%%% See accompanying file LICENSE or visit the Thrift site at:
5%%% http://developers.facebook.com/thrift/
6
7-module(tBinaryProtocol).
8
9-include("oop.hrl").
10
11-include("thrift.hrl").
12-include("protocol/tProtocolException.hrl").
13-include("protocol/tBinaryProtocol.hrl").
14
15-behavior(oop).
16
17-export([attr/4, super/0, inspect/1]).
18
19-export([
20 new/1,
21
22 writeMessageBegin/4,
23 writeFieldBegin/4, writeFieldStop/1,
24 writeMapBegin/4,
25 writeListBegin/3,
26 writeSetBegin/3,
27
Christopher Piroc2e37c72007-11-15 06:26:30 +000028 writeBool/2, writeByte/2, writeI16/2, writeI32/2,
29 writeI64/2, writeDouble/2, writeString/2,
Christopher Piro094823a2007-07-18 00:26:12 +000030
Christopher Piroc2e37c72007-11-15 06:26:30 +000031 readMessageBegin/1,
32 readFieldBegin/1,
33 readMapBegin/1,
34 readListBegin/1,
35 readSetBegin/1,
Christopher Piro094823a2007-07-18 00:26:12 +000036
Christopher Piroc2e37c72007-11-15 06:26:30 +000037 readBool/1, readByte/1, readI16/1, readI32/1,
Christopher Piro094823a2007-07-18 00:26:12 +000038 readI64/1, readDouble/1, readString/1
39]).
40
41%%%
42%%% define attributes
43%%% 'super' is required unless ?MODULE is a base class
44%%%
45
46?DEFINE_ATTR(super).
Christopher Piroc2e37c72007-11-15 06:26:30 +000047
Christopher Piro094823a2007-07-18 00:26:12 +000048%%%
49%%% behavior callbacks
50%%%
Christopher Piroc2e37c72007-11-15 06:26:30 +000051
Christopher Piro094823a2007-07-18 00:26:12 +000052%%% super() -> SuperModule = atom()
53%%% | none
54
55super() ->
56 tProtocol.
57
58%%% inspect(This) -> string()
59
60inspect(_This) ->
61 "".
62
63%%%
64%%% class methods
65%%%
66
67new(Trans) ->
68 Super = (super()):new(Trans),
69 #?MODULE{super=Super}.
70
71%%%
72%%% instance methods
73%%%
74
75writeMessageBegin(This, Name, Type, Seqid) ->
76 ?L1(writeI32, ?VERSION_1 bor Type),
77 ?L1(writeString, Name),
78 ?L1(writeI32, Seqid),
79 ok.
80
81writeFieldBegin(This, _Name, Type, Id) ->
82 ?L1(writeByte, Type),
83 ?L1(writeI16, Id),
84 ok.
85
86writeFieldStop(This) ->
87 ?L1(writeByte, ?tType_STOP),
88 ok.
89
90writeMapBegin(This, Ktype, Vtype, Size) ->
91 ?L1(writeByte, Ktype),
92 ?L1(writeByte, Vtype),
93 ?L1(writeI32, Size),
94 ok.
95
96writeListBegin(This, Etype, Size) ->
97 ?L1(writeByte, Etype),
98 ?L1(writeI32, Size),
99 ok.
100
101writeSetBegin(This, Etype, Size) ->
102 ?L1(writeByte, Etype),
103 ?L1(writeI32, Size),
104 ok.
105
106%
107
108writeBool(This, Bool) ->
Christopher Piroc2e37c72007-11-15 06:26:30 +0000109 case Bool of
110 true -> ?L1(writeByte, 1);
111 false -> ?L1(writeByte, 0)
Christopher Piro094823a2007-07-18 00:26:12 +0000112 end.
113
114writeByte(This, Byte) ->
115 Trans = oop:get(This, trans),
Christopher Pirob6dcd2b2007-10-13 01:11:46 +0000116 ?R1(Trans, effectful_write, binary_to_list(<<Byte:8/big>>)).
Christopher Piro094823a2007-07-18 00:26:12 +0000117
118writeI16(This, I16) ->
119 Trans = oop:get(This, trans),
Christopher Pirob6dcd2b2007-10-13 01:11:46 +0000120 ?R1(Trans, effectful_write, binary_to_list(<<I16:16/big>>)).
Christopher Piro094823a2007-07-18 00:26:12 +0000121
122writeI32(This, I32) ->
123 Trans = oop:get(This, trans),
Christopher Pirob6dcd2b2007-10-13 01:11:46 +0000124 ?R1(Trans, effectful_write, binary_to_list(<<I32:32/big>>)).
Christopher Piro094823a2007-07-18 00:26:12 +0000125
126writeI64(This, I64) ->
127 Trans = oop:get(This, trans),
Christopher Pirob6dcd2b2007-10-13 01:11:46 +0000128 ?R1(Trans, effectful_write, binary_to_list(<<I64:64/big>>)).
Christopher Piro094823a2007-07-18 00:26:12 +0000129
130writeDouble(This, Double) ->
131 Trans = oop:get(This, trans),
Christopher Pirob6dcd2b2007-10-13 01:11:46 +0000132 ?R1(Trans, effectful_write, binary_to_list(<<Double:64/big>>)).
Christopher Piro094823a2007-07-18 00:26:12 +0000133
134writeString(This, Str) ->
135 Trans = oop:get(This, trans),
136 ?L1(writeI32, length(Str)),
Christopher Pirob6dcd2b2007-10-13 01:11:46 +0000137 ?R1(Trans, effectful_write, Str).
Christopher Piro094823a2007-07-18 00:26:12 +0000138
Christopher Pirode11d852007-11-18 02:10:20 +0000139%%
Christopher Piro094823a2007-07-18 00:26:12 +0000140
141readMessageBegin(This) ->
142 Version = ?L0(readI32),
Christopher Piroc2e37c72007-11-15 06:26:30 +0000143 if
144 (Version band ?VERSION_MASK) /= ?VERSION_1 ->
Christopher Pirode11d852007-11-18 02:10:20 +0000145 tException:throw(tProtocolException, [?tProtocolException_BAD_VERSION, "Missing version identifier"]);
Christopher Piroc2e37c72007-11-15 06:26:30 +0000146 true -> ok
Christopher Piro094823a2007-07-18 00:26:12 +0000147 end,
148 Type = Version band 16#000000ff,
149 Name = ?L0(readString),
150 Seqid = ?L0(readI32),
151 { Name, Type, Seqid }.
152
153readFieldBegin(This) ->
154 Type = ?L0(readByte),
Christopher Piroc2e37c72007-11-15 06:26:30 +0000155 case Type of
156 ?tType_STOP ->
157 { nil, Type, 0 }; % WATCH
158 _ ->
159 Id = ?L0(readI16),
160 { nil, Type, Id }
Christopher Piro094823a2007-07-18 00:26:12 +0000161 end.
162
163readMapBegin(This) ->
164 Ktype = ?L0(readByte),
165 Vtype = ?L0(readByte),
166 Size = ?L0(readI32),
167 { Ktype, Vtype, Size }.
168
169readListBegin(This) ->
170 Etype = ?L0(readByte),
171 Size = ?L0(readI32),
172 { Etype, Size }.
173
174readSetBegin(This) ->
175 Etype = ?L0(readByte),
176 Size = ?L0(readI32),
177 { Etype, Size }.
178
Christopher Pirode11d852007-11-18 02:10:20 +0000179%%
Christopher Piro094823a2007-07-18 00:26:12 +0000180
181readBool(This) ->
182 Byte = ?L0(readByte),
183 (Byte /= 0).
184
185readByte(This) ->
186 Trans = oop:get(This, trans),
187 <<Val:8/integer-signed-big, _/binary>> = ?R1(Trans, readAll, 1),
188 Val.
189
190readI16(This) ->
191 Trans = oop:get(This, trans),
192 <<Val:16/integer-signed-big, _/binary>> = ?R1(Trans, readAll, 2),
193 Val.
194
195readI32(This) ->
196 Trans = oop:get(This, trans),
197 <<Val:32/integer-signed-big, _/binary>> = ?R1(Trans, readAll, 4),
198 Val.
199
200readI64(This) ->
201 Trans = oop:get(This, trans),
202 <<Val:64/integer-signed-big, _/binary>> = ?R1(Trans, readAll, 8),
203 Val.
204
205readDouble(This) ->
206 Trans = oop:get(This, trans),
207 <<Val:64/float-signed-big, _/binary>> = ?R1(Trans, readAll, 8),
208 Val.
209
210readString(This) ->
211 Trans = oop:get(This, trans),
212 Sz = ?L0(readI32),
213 binary_to_list(?R1(Trans, readAll, Sz)).