blob: ad5338422fa32a79c544cfd1330ee9ec2088d5b0 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001%%
2%% Licensed to the Apache Software Foundation (ASF) under one
3%% or more contributor license agreements. See the NOTICE file
4%% distributed with this work for additional information
5%% regarding copyright ownership. The ASF licenses this file
6%% to you under the Apache License, Version 2.0 (the
7%% "License"); you may not use this file except in compliance
8%% with the License. You may obtain a copy of the License at
9%%
10%% http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing,
13%% software distributed under the License is distributed on an
14%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15%% KIND, either express or implied. See the License for the
16%% specific language governing permissions and limitations
17%% under the License.
18%%
David Reissac549552008-06-10 22:56:59 +000019
20-module(thrift_binary_protocol).
21
22-behavior(thrift_protocol).
23
24-include("thrift_constants.hrl").
25-include("thrift_protocol.hrl").
26
David Reiss914ebb42008-06-11 01:01:48 +000027-export([new/1, new/2,
David Reissac549552008-06-10 22:56:59 +000028 read/2,
David Reiss90b40832008-06-10 22:58:52 +000029 write/2,
David Reissc11734e2008-06-11 00:59:48 +000030 flush_transport/1,
David Reissfc427af2008-06-11 01:11:57 +000031 close_transport/1,
32
33 new_protocol_factory/2
David Reiss914ebb42008-06-11 01:01:48 +000034 ]).
David Reissac549552008-06-10 22:56:59 +000035
David Reiss914ebb42008-06-11 01:01:48 +000036-record(binary_protocol, {transport,
37 strict_read=true,
38 strict_write=true
39 }).
David Reissac549552008-06-10 22:56:59 +000040
41-define(VERSION_MASK, 16#FFFF0000).
42-define(VERSION_1, 16#80010000).
David Reiss914ebb42008-06-11 01:01:48 +000043-define(TYPE_MASK, 16#000000ff).
David Reissac549552008-06-10 22:56:59 +000044
45new(Transport) ->
David Reiss914ebb42008-06-11 01:01:48 +000046 new(Transport, _Options = []).
47
48new(Transport, Options) ->
49 State = #binary_protocol{transport = Transport},
50 State1 = parse_options(Options, State),
51 thrift_protocol:new(?MODULE, State1).
52
53parse_options([], State) ->
54 State;
55parse_options([{strict_read, Bool} | Rest], State) when is_boolean(Bool) ->
56 parse_options(Rest, State#binary_protocol{strict_read=Bool});
57parse_options([{strict_write, Bool} | Rest], State) when is_boolean(Bool) ->
58 parse_options(Rest, State#binary_protocol{strict_write=Bool}).
59
David Reissac549552008-06-10 22:56:59 +000060
David Reiss90b40832008-06-10 22:58:52 +000061flush_transport(#binary_protocol{transport = Transport}) ->
62 thrift_transport:flush(Transport).
David Reissac549552008-06-10 22:56:59 +000063
David Reissc11734e2008-06-11 00:59:48 +000064close_transport(#binary_protocol{transport = Transport}) ->
David Reissc11734e2008-06-11 00:59:48 +000065 thrift_transport:close(Transport).
66
David Reissac549552008-06-10 22:56:59 +000067%%%
68%%% instance methods
69%%%
70
71write(This, #protocol_message_begin{
72 name = Name,
73 type = Type,
74 seqid = Seqid}) ->
David Reiss914ebb42008-06-11 01:01:48 +000075 case This#binary_protocol.strict_write of
76 true ->
77 write(This, {i32, ?VERSION_1 bor Type}),
78 write(This, {string, Name}),
79 write(This, {i32, Seqid});
80 false ->
81 write(This, {string, Name}),
82 write(This, {byte, Type}),
83 write(This, {i32, Seqid})
84 end,
David Reissac549552008-06-10 22:56:59 +000085 ok;
86
87write(This, message_end) -> ok;
88
89write(This, #protocol_field_begin{
90 name = _Name,
91 type = Type,
92 id = Id}) ->
93 write(This, {byte, Type}),
94 write(This, {i16, Id}),
95 ok;
96
97write(This, field_stop) ->
98 write(This, {byte, ?tType_STOP}),
99 ok;
100
101write(This, field_end) -> ok;
102
103write(This, #protocol_map_begin{
104 ktype = Ktype,
105 vtype = Vtype,
106 size = Size}) ->
107 write(This, {byte, Ktype}),
108 write(This, {byte, Vtype}),
109 write(This, {i32, Size}),
110 ok;
111
112write(This, map_end) -> ok;
113
114write(This, #protocol_list_begin{
115 etype = Etype,
116 size = Size}) ->
117 write(This, {byte, Etype}),
118 write(This, {i32, Size}),
119 ok;
120
121write(This, list_end) -> ok;
122
123write(This, #protocol_set_begin{
124 etype = Etype,
125 size = Size}) ->
126 write(This, {byte, Etype}),
127 write(This, {i32, Size}),
128 ok;
129
130write(This, set_end) -> ok;
131
David Reissae756f42008-06-10 22:57:11 +0000132write(This, #protocol_struct_begin{}) -> ok;
David Reissac549552008-06-10 22:56:59 +0000133write(This, struct_end) -> ok;
134
David Reissac549552008-06-10 22:56:59 +0000135write(This, {bool, true}) -> write(This, {byte, 1});
136write(This, {bool, false}) -> write(This, {byte, 0});
137
138write(This, {byte, Byte}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000139 write(This, <<Byte:8/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000140
141write(This, {i16, I16}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000142 write(This, <<I16:16/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000143
144write(This, {i32, I32}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000145 write(This, <<I32:32/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000146
David Reiss07a725f2008-06-10 22:57:59 +0000147write(This, {i64, I64}) ->
148 write(This, <<I64:64/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000149
150write(This, {double, Double}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000151 write(This, <<Double:64/big-signed-float>>);
David Reissac549552008-06-10 22:56:59 +0000152
153write(This, {string, Str}) when is_list(Str) ->
154 write(This, {i32, length(Str)}),
155 write(This, list_to_binary(Str));
156
David Reiss225db732008-06-11 00:58:48 +0000157write(This, {string, Bin}) when is_binary(Bin) ->
158 write(This, {i32, size(Bin)}),
159 write(This, Bin);
160
David Reiss914ebb42008-06-11 01:01:48 +0000161%% Data :: iolist()
162write(This, Data) ->
163 thrift_transport:write(This#binary_protocol.transport, Data).
David Reissac549552008-06-10 22:56:59 +0000164
165%%
166
167read(This, message_begin) ->
David Reiss9ad6a312008-06-11 01:12:45 +0000168 case read(This, ui32) of
David Reiss914ebb42008-06-11 01:01:48 +0000169 {ok, Sz} when Sz band ?VERSION_MASK =:= ?VERSION_1 ->
170 %% we're at version 1
David Reissac549552008-06-10 22:56:59 +0000171 {ok, Name} = read(This, string),
David Reiss914ebb42008-06-11 01:01:48 +0000172 Type = Sz band ?TYPE_MASK,
David Reissac549552008-06-10 22:56:59 +0000173 {ok, SeqId} = read(This, i32),
David Reiss4ec777e2008-06-11 01:01:29 +0000174 #protocol_message_begin{name = binary_to_list(Name),
175 type = Type,
David Reissac549552008-06-10 22:56:59 +0000176 seqid = SeqId};
David Reiss914ebb42008-06-11 01:01:48 +0000177
178 {ok, Sz} when Sz < 0 ->
179 %% there's a version number but it's unexpected
180 {error, {bad_binary_protocol_version, Sz}};
181
182 {ok, Sz} when This#binary_protocol.strict_read =:= true ->
183 %% strict_read is true and there's no version header; that's an error
184 {error, no_binary_protocol_version};
185
186 {ok, Sz} when This#binary_protocol.strict_read =:= false ->
187 %% strict_read is false, so just read the old way
188 {ok, Name} = read(This, Sz),
189 {ok, Type} = read(This, byte),
190 {ok, SeqId} = read(This, i32),
191 #protocol_message_begin{name = binary_to_list(Name),
192 type = Type,
193 seqid = SeqId};
194
David Reissae756f42008-06-10 22:57:11 +0000195 Err = {error, closed} -> Err;
David Reiss7956f232008-06-11 01:02:47 +0000196 Err = {error, timeout}-> Err;
David Reissae756f42008-06-10 22:57:11 +0000197 Err = {error, ebadf} -> Err
David Reissac549552008-06-10 22:56:59 +0000198 end;
199
200read(This, message_end) -> ok;
201
202read(This, struct_begin) -> ok;
203read(This, struct_end) -> ok;
204
205read(This, field_begin) ->
206 case read(This, byte) of
207 {ok, Type = ?tType_STOP} ->
David Reissfe7ce1b2008-06-11 01:03:10 +0000208 #protocol_field_begin{type = Type};
David Reissac549552008-06-10 22:56:59 +0000209 {ok, Type} ->
David Reissae756f42008-06-10 22:57:11 +0000210 {ok, Id} = read(This, i16),
David Reissac549552008-06-10 22:56:59 +0000211 #protocol_field_begin{type = Type,
212 id = Id}
213 end;
214
215read(This, field_end) -> ok;
216
217read(This, map_begin) ->
218 {ok, Ktype} = read(This, byte),
219 {ok, Vtype} = read(This, byte),
220 {ok, Size} = read(This, i32),
221 #protocol_map_begin{ktype = Ktype,
222 vtype = Vtype,
223 size = Size};
224read(This, map_end) -> ok;
225
226read(This, list_begin) ->
227 {ok, Etype} = read(This, byte),
228 {ok, Size} = read(This, i32),
229 #protocol_list_begin{etype = Etype,
230 size = Size};
231read(This, list_end) -> ok;
232
233read(This, set_begin) ->
234 {ok, Etype} = read(This, byte),
235 {ok, Size} = read(This, i32),
236 #protocol_set_begin{etype = Etype,
237 size = Size};
238read(This, set_end) -> ok;
239
240read(This, field_stop) ->
241 {ok, ?tType_STOP} = read(This, byte),
David Reiss225db732008-06-11 00:58:48 +0000242 ok;
David Reissac549552008-06-10 22:56:59 +0000243
244%%
245
246read(This, bool) ->
Kevin Clark9a863ee2009-03-24 16:04:36 +0000247 case read(This, byte) of
248 {ok, Byte} -> {ok, Byte /= 0};
249 Else -> Else
250 end;
David Reissac549552008-06-10 22:56:59 +0000251
252read(This, byte) ->
253 case read(This, 1) of
254 {ok, <<Val:8/integer-signed-big, _/binary>>} -> {ok, Val};
255 Else -> Else
256 end;
257
258read(This, i16) ->
259 case read(This, 2) of
260 {ok, <<Val:16/integer-signed-big, _/binary>>} -> {ok, Val};
261 Else -> Else
262 end;
263
264read(This, i32) ->
265 case read(This, 4) of
266 {ok, <<Val:32/integer-signed-big, _/binary>>} -> {ok, Val};
267 Else -> Else
268 end;
269
David Reiss9ad6a312008-06-11 01:12:45 +0000270%% unsigned ints aren't used by thrift itself, but it's used for the parsing
271%% of the packet version header. Without this special function BEAM works fine
272%% but hipe thinks it received a bad version header.
273read(This, ui32) ->
274 case read(This, 4) of
275 {ok, <<Val:32/integer-unsigned-big, _/binary>>} -> {ok, Val};
276 Else -> Else
277 end;
278
David Reissac549552008-06-10 22:56:59 +0000279read(This, i64) ->
280 case read(This, 8) of
281 {ok, <<Val:64/integer-signed-big, _/binary>>} -> {ok, Val};
282 Else -> Else
283 end;
284
285read(This, double) ->
286 case read(This, 8) of
287 {ok, <<Val:64/float-signed-big, _/binary>>} -> {ok, Val};
288 Else -> Else
289 end;
290
David Reiss4ec777e2008-06-11 01:01:29 +0000291% returns a binary directly, call binary_to_list if necessary
David Reissac549552008-06-10 22:56:59 +0000292read(This, string) ->
293 {ok, Sz} = read(This, i32),
David Reiss4ec777e2008-06-11 01:01:29 +0000294 {ok, Bin} = read(This, Sz);
David Reissac549552008-06-10 22:56:59 +0000295
David Reissd74b0232008-06-11 01:02:55 +0000296read(This, 0) -> {ok, <<>>};
David Reissac549552008-06-10 22:56:59 +0000297read(This, Len) when is_integer(Len), Len >= 0 ->
298 thrift_transport:read(This#binary_protocol.transport, Len).
David Reissfc427af2008-06-11 01:11:57 +0000299
300
301%%%% FACTORY GENERATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302
303-record(tbp_opts, {strict_read = true,
304 strict_write = true}).
305
306parse_factory_options([], Opts) ->
307 Opts;
308parse_factory_options([{strict_read, Bool} | Rest], Opts) when is_boolean(Bool) ->
309 parse_factory_options(Rest, Opts#tbp_opts{strict_read=Bool});
310parse_factory_options([{strict_write, Bool} | Rest], Opts) when is_boolean(Bool) ->
311 parse_factory_options(Rest, Opts#tbp_opts{strict_write=Bool}).
312
313
314%% returns a (fun() -> thrift_protocol())
315new_protocol_factory(TransportFactory, Options) ->
316 ParsedOpts = parse_factory_options(Options, #tbp_opts{}),
317 F = fun() ->
318 {ok, Transport} = TransportFactory(),
319 thrift_binary_protocol:new(
320 Transport,
321 [{strict_read, ParsedOpts#tbp_opts.strict_read},
322 {strict_write, ParsedOpts#tbp_opts.strict_write}])
323 end,
324 {ok, F}.
David Reiss1a2f2182008-06-11 01:14:01 +0000325