blob: c84701efa08d80dfaec181b4ffe4f4ee41f960c1 [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;
David Reiss7956f232008-06-11 01:02:47 +0000181 Err = {error, timeout}-> Err;
David Reissae756f42008-06-10 22:57:11 +0000182 Err = {error, ebadf} -> Err
David Reissac549552008-06-10 22:56:59 +0000183 end;
184
185read(This, message_end) -> ok;
186
187read(This, struct_begin) -> ok;
188read(This, struct_end) -> ok;
189
190read(This, field_begin) ->
191 case read(This, byte) of
192 {ok, Type = ?tType_STOP} ->
193 #protocol_field_begin{type = Type,
194 id = 0}; % TODO(todd) 0 or undefined?
195 {ok, Type} ->
David Reissae756f42008-06-10 22:57:11 +0000196 {ok, Id} = read(This, i16),
David Reissac549552008-06-10 22:56:59 +0000197 #protocol_field_begin{type = Type,
198 id = Id}
199 end;
200
201read(This, field_end) -> ok;
202
203read(This, map_begin) ->
204 {ok, Ktype} = read(This, byte),
205 {ok, Vtype} = read(This, byte),
206 {ok, Size} = read(This, i32),
207 #protocol_map_begin{ktype = Ktype,
208 vtype = Vtype,
209 size = Size};
210read(This, map_end) -> ok;
211
212read(This, list_begin) ->
213 {ok, Etype} = read(This, byte),
214 {ok, Size} = read(This, i32),
215 #protocol_list_begin{etype = Etype,
216 size = Size};
217read(This, list_end) -> ok;
218
219read(This, set_begin) ->
220 {ok, Etype} = read(This, byte),
221 {ok, Size} = read(This, i32),
222 #protocol_set_begin{etype = Etype,
223 size = Size};
224read(This, set_end) -> ok;
225
226read(This, field_stop) ->
227 {ok, ?tType_STOP} = read(This, byte),
David Reiss225db732008-06-11 00:58:48 +0000228 ok;
David Reissac549552008-06-10 22:56:59 +0000229
230%%
231
232read(This, bool) ->
233 Byte = read(This, byte),
234 {ok, (Byte /= 0)};
235
236
237read(This, byte) ->
238 case read(This, 1) of
239 {ok, <<Val:8/integer-signed-big, _/binary>>} -> {ok, Val};
240 Else -> Else
241 end;
242
243read(This, i16) ->
244 case read(This, 2) of
245 {ok, <<Val:16/integer-signed-big, _/binary>>} -> {ok, Val};
246 Else -> Else
247 end;
248
249read(This, i32) ->
250 case read(This, 4) of
251 {ok, <<Val:32/integer-signed-big, _/binary>>} -> {ok, Val};
252 Else -> Else
253 end;
254
255read(This, i64) ->
256 case read(This, 8) of
257 {ok, <<Val:64/integer-signed-big, _/binary>>} -> {ok, Val};
258 Else -> Else
259 end;
260
261read(This, double) ->
262 case read(This, 8) of
263 {ok, <<Val:64/float-signed-big, _/binary>>} -> {ok, Val};
264 Else -> Else
265 end;
266
David Reiss4ec777e2008-06-11 01:01:29 +0000267% returns a binary directly, call binary_to_list if necessary
David Reissac549552008-06-10 22:56:59 +0000268read(This, string) ->
269 {ok, Sz} = read(This, i32),
David Reiss4ec777e2008-06-11 01:01:29 +0000270 {ok, Bin} = read(This, Sz);
David Reissac549552008-06-10 22:56:59 +0000271
David Reissd74b0232008-06-11 01:02:55 +0000272read(This, 0) -> {ok, <<>>};
David Reissac549552008-06-10 22:56:59 +0000273read(This, Len) when is_integer(Len), Len >= 0 ->
274 thrift_transport:read(This#binary_protocol.transport, Len).