blob: da43d148e8de0c3efdb70d88c50cab56092cb604 [file] [log] [blame]
David Reissac549552008-06-10 22:56:59 +00001%%% Copyright (c) 2007- Facebook
2%%% Distributed under the Thrift Software License
3%%%
4%%% See accompanying file LICENSE or visit the Thrift site at:
5%%% http://developers.facebook.com/thrift/
6
7-module(thrift_binary_protocol).
8
9-behavior(thrift_protocol).
10
11-include("thrift_constants.hrl").
12-include("thrift_protocol.hrl").
13
David Reiss914ebb42008-06-11 01:01:48 +000014-export([new/1, new/2,
David Reissac549552008-06-10 22:56:59 +000015 read/2,
David Reiss90b40832008-06-10 22:58:52 +000016 write/2,
David Reissc11734e2008-06-11 00:59:48 +000017 flush_transport/1,
18 close_transport/1
David Reiss914ebb42008-06-11 01:01:48 +000019 ]).
David Reissac549552008-06-10 22:56:59 +000020
David Reiss914ebb42008-06-11 01:01:48 +000021-record(binary_protocol, {transport,
22 strict_read=true,
23 strict_write=true
24 }).
David Reissac549552008-06-10 22:56:59 +000025
26-define(VERSION_MASK, 16#FFFF0000).
27-define(VERSION_1, 16#80010000).
David Reiss914ebb42008-06-11 01:01:48 +000028-define(TYPE_MASK, 16#000000ff).
David Reissac549552008-06-10 22:56:59 +000029
30new(Transport) ->
David Reiss914ebb42008-06-11 01:01:48 +000031 new(Transport, _Options = []).
32
33new(Transport, Options) ->
34 State = #binary_protocol{transport = Transport},
35 State1 = parse_options(Options, State),
36 thrift_protocol:new(?MODULE, State1).
37
38parse_options([], State) ->
39 State;
40parse_options([{strict_read, Bool} | Rest], State) when is_boolean(Bool) ->
41 parse_options(Rest, State#binary_protocol{strict_read=Bool});
42parse_options([{strict_write, Bool} | Rest], State) when is_boolean(Bool) ->
43 parse_options(Rest, State#binary_protocol{strict_write=Bool}).
44
David Reissac549552008-06-10 22:56:59 +000045
David Reiss90b40832008-06-10 22:58:52 +000046flush_transport(#binary_protocol{transport = Transport}) ->
47 thrift_transport:flush(Transport).
David Reissac549552008-06-10 22:56:59 +000048
David Reissc11734e2008-06-11 00:59:48 +000049close_transport(#binary_protocol{transport = Transport}) ->
David Reissc11734e2008-06-11 00:59:48 +000050 thrift_transport:close(Transport).
51
David Reissac549552008-06-10 22:56:59 +000052%%%
53%%% instance methods
54%%%
55
56write(This, #protocol_message_begin{
57 name = Name,
58 type = Type,
59 seqid = Seqid}) ->
David Reiss914ebb42008-06-11 01:01:48 +000060 case This#binary_protocol.strict_write of
61 true ->
62 write(This, {i32, ?VERSION_1 bor Type}),
63 write(This, {string, Name}),
64 write(This, {i32, Seqid});
65 false ->
66 write(This, {string, Name}),
67 write(This, {byte, Type}),
68 write(This, {i32, Seqid})
69 end,
David Reissac549552008-06-10 22:56:59 +000070 ok;
71
72write(This, message_end) -> ok;
73
74write(This, #protocol_field_begin{
75 name = _Name,
76 type = Type,
77 id = Id}) ->
78 write(This, {byte, Type}),
79 write(This, {i16, Id}),
80 ok;
81
82write(This, field_stop) ->
83 write(This, {byte, ?tType_STOP}),
84 ok;
85
86write(This, field_end) -> ok;
87
88write(This, #protocol_map_begin{
89 ktype = Ktype,
90 vtype = Vtype,
91 size = Size}) ->
92 write(This, {byte, Ktype}),
93 write(This, {byte, Vtype}),
94 write(This, {i32, Size}),
95 ok;
96
97write(This, map_end) -> ok;
98
99write(This, #protocol_list_begin{
100 etype = Etype,
101 size = Size}) ->
102 write(This, {byte, Etype}),
103 write(This, {i32, Size}),
104 ok;
105
106write(This, list_end) -> ok;
107
108write(This, #protocol_set_begin{
109 etype = Etype,
110 size = Size}) ->
111 write(This, {byte, Etype}),
112 write(This, {i32, Size}),
113 ok;
114
115write(This, set_end) -> ok;
116
David Reissae756f42008-06-10 22:57:11 +0000117write(This, #protocol_struct_begin{}) -> ok;
David Reissac549552008-06-10 22:56:59 +0000118write(This, struct_end) -> ok;
119
David Reissac549552008-06-10 22:56:59 +0000120write(This, {bool, true}) -> write(This, {byte, 1});
121write(This, {bool, false}) -> write(This, {byte, 0});
122
123write(This, {byte, Byte}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000124 write(This, <<Byte:8/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000125
126write(This, {i16, I16}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000127 write(This, <<I16:16/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000128
129write(This, {i32, I32}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000130 write(This, <<I32:32/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000131
David Reiss07a725f2008-06-10 22:57:59 +0000132write(This, {i64, I64}) ->
133 write(This, <<I64:64/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000134
135write(This, {double, Double}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000136 write(This, <<Double:64/big-signed-float>>);
David Reissac549552008-06-10 22:56:59 +0000137
138write(This, {string, Str}) when is_list(Str) ->
139 write(This, {i32, length(Str)}),
140 write(This, list_to_binary(Str));
141
David Reiss225db732008-06-11 00:58:48 +0000142write(This, {string, Bin}) when is_binary(Bin) ->
143 write(This, {i32, size(Bin)}),
144 write(This, Bin);
145
David Reiss914ebb42008-06-11 01:01:48 +0000146%% Data :: iolist()
147write(This, Data) ->
148 thrift_transport:write(This#binary_protocol.transport, Data).
David Reissac549552008-06-10 22:56:59 +0000149
150%%
151
152read(This, message_begin) ->
153 case read(This, i32) of
David Reiss914ebb42008-06-11 01:01:48 +0000154 {ok, Sz} when Sz band ?VERSION_MASK =:= ?VERSION_1 ->
155 %% we're at version 1
David Reissac549552008-06-10 22:56:59 +0000156 {ok, Name} = read(This, string),
David Reiss914ebb42008-06-11 01:01:48 +0000157 Type = Sz band ?TYPE_MASK,
David Reissac549552008-06-10 22:56:59 +0000158 {ok, SeqId} = read(This, i32),
David Reiss4ec777e2008-06-11 01:01:29 +0000159 #protocol_message_begin{name = binary_to_list(Name),
160 type = Type,
David Reissac549552008-06-10 22:56:59 +0000161 seqid = SeqId};
David Reiss914ebb42008-06-11 01:01:48 +0000162
163 {ok, Sz} when Sz < 0 ->
164 %% there's a version number but it's unexpected
165 {error, {bad_binary_protocol_version, Sz}};
166
167 {ok, Sz} when This#binary_protocol.strict_read =:= true ->
168 %% strict_read is true and there's no version header; that's an error
169 {error, no_binary_protocol_version};
170
171 {ok, Sz} when This#binary_protocol.strict_read =:= false ->
172 %% strict_read is false, so just read the old way
173 {ok, Name} = read(This, Sz),
174 {ok, Type} = read(This, byte),
175 {ok, SeqId} = read(This, i32),
176 #protocol_message_begin{name = binary_to_list(Name),
177 type = Type,
178 seqid = SeqId};
179
David Reissae756f42008-06-10 22:57:11 +0000180 Err = {error, closed} -> Err;
181 Err = {error, ebadf} -> Err
David Reissac549552008-06-10 22:56:59 +0000182 end;
183
184read(This, message_end) -> ok;
185
186read(This, struct_begin) -> ok;
187read(This, struct_end) -> ok;
188
189read(This, field_begin) ->
190 case read(This, byte) of
191 {ok, Type = ?tType_STOP} ->
192 #protocol_field_begin{type = Type,
193 id = 0}; % TODO(todd) 0 or undefined?
194 {ok, Type} ->
David Reissae756f42008-06-10 22:57:11 +0000195 {ok, Id} = read(This, i16),
David Reissac549552008-06-10 22:56:59 +0000196 #protocol_field_begin{type = Type,
197 id = Id}
198 end;
199
200read(This, field_end) -> ok;
201
202read(This, map_begin) ->
203 {ok, Ktype} = read(This, byte),
204 {ok, Vtype} = read(This, byte),
205 {ok, Size} = read(This, i32),
206 #protocol_map_begin{ktype = Ktype,
207 vtype = Vtype,
208 size = Size};
209read(This, map_end) -> ok;
210
211read(This, list_begin) ->
212 {ok, Etype} = read(This, byte),
213 {ok, Size} = read(This, i32),
214 #protocol_list_begin{etype = Etype,
215 size = Size};
216read(This, list_end) -> ok;
217
218read(This, set_begin) ->
219 {ok, Etype} = read(This, byte),
220 {ok, Size} = read(This, i32),
221 #protocol_set_begin{etype = Etype,
222 size = Size};
223read(This, set_end) -> ok;
224
225read(This, field_stop) ->
226 {ok, ?tType_STOP} = read(This, byte),
David Reiss225db732008-06-11 00:58:48 +0000227 ok;
David Reissac549552008-06-10 22:56:59 +0000228
229%%
230
231read(This, bool) ->
232 Byte = read(This, byte),
233 {ok, (Byte /= 0)};
234
235
236read(This, byte) ->
237 case read(This, 1) of
238 {ok, <<Val:8/integer-signed-big, _/binary>>} -> {ok, Val};
239 Else -> Else
240 end;
241
242read(This, i16) ->
243 case read(This, 2) of
244 {ok, <<Val:16/integer-signed-big, _/binary>>} -> {ok, Val};
245 Else -> Else
246 end;
247
248read(This, i32) ->
249 case read(This, 4) of
250 {ok, <<Val:32/integer-signed-big, _/binary>>} -> {ok, Val};
251 Else -> Else
252 end;
253
254read(This, i64) ->
255 case read(This, 8) of
256 {ok, <<Val:64/integer-signed-big, _/binary>>} -> {ok, Val};
257 Else -> Else
258 end;
259
260read(This, double) ->
261 case read(This, 8) of
262 {ok, <<Val:64/float-signed-big, _/binary>>} -> {ok, Val};
263 Else -> Else
264 end;
265
David Reiss4ec777e2008-06-11 01:01:29 +0000266% returns a binary directly, call binary_to_list if necessary
David Reissac549552008-06-10 22:56:59 +0000267read(This, string) ->
268 {ok, Sz} = read(This, i32),
David Reiss4ec777e2008-06-11 01:01:29 +0000269 {ok, Bin} = read(This, Sz);
David Reissac549552008-06-10 22:56:59 +0000270
271read(This, Len) when is_integer(Len), Len >= 0 ->
272 thrift_transport:read(This#binary_protocol.transport, Len).