blob: 9216e474a0bab4d94b95660a67b42df88fc6f5ef [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
David Reiss6d0be722010-08-30 22:05:09 +000022-behaviour(thrift_protocol).
David Reissac549552008-06-10 22:56:59 +000023
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 Reiss5e6637b2010-08-30 22:05:18 +000040-type state() :: #binary_protocol{}.
David Reisscf7f3972010-08-30 22:05:55 +000041-include("thrift_protocol_behaviour.hrl").
David Reissac549552008-06-10 22:56:59 +000042
43-define(VERSION_MASK, 16#FFFF0000).
44-define(VERSION_1, 16#80010000).
David Reiss914ebb42008-06-11 01:01:48 +000045-define(TYPE_MASK, 16#000000ff).
David Reissac549552008-06-10 22:56:59 +000046
47new(Transport) ->
David Reiss914ebb42008-06-11 01:01:48 +000048 new(Transport, _Options = []).
49
50new(Transport, Options) ->
51 State = #binary_protocol{transport = Transport},
52 State1 = parse_options(Options, State),
53 thrift_protocol:new(?MODULE, State1).
54
55parse_options([], State) ->
56 State;
57parse_options([{strict_read, Bool} | Rest], State) when is_boolean(Bool) ->
58 parse_options(Rest, State#binary_protocol{strict_read=Bool});
59parse_options([{strict_write, Bool} | Rest], State) when is_boolean(Bool) ->
60 parse_options(Rest, State#binary_protocol{strict_write=Bool}).
61
David Reissac549552008-06-10 22:56:59 +000062
David Reiss63f61262010-08-30 22:05:34 +000063flush_transport(This = #binary_protocol{transport = Transport}) ->
David Reiss639e1cf2010-08-30 22:05:43 +000064 {NewTransport, Result} = thrift_transport:flush(Transport),
65 {This#binary_protocol{transport = NewTransport}, Result}.
David Reissac549552008-06-10 22:56:59 +000066
David Reiss63f61262010-08-30 22:05:34 +000067close_transport(This = #binary_protocol{transport = Transport}) ->
David Reiss639e1cf2010-08-30 22:05:43 +000068 {NewTransport, Result} = thrift_transport:close(Transport),
69 {This#binary_protocol{transport = NewTransport}, Result}.
David Reissc11734e2008-06-11 00:59:48 +000070
David Reissac549552008-06-10 22:56:59 +000071%%%
72%%% instance methods
73%%%
74
David Reiss63f61262010-08-30 22:05:34 +000075write(This0, #protocol_message_begin{
David Reissac549552008-06-10 22:56:59 +000076 name = Name,
77 type = Type,
78 seqid = Seqid}) ->
David Reiss63f61262010-08-30 22:05:34 +000079 case This0#binary_protocol.strict_write of
David Reiss914ebb42008-06-11 01:01:48 +000080 true ->
David Reiss63f61262010-08-30 22:05:34 +000081 {This1, ok} = write(This0, {i32, ?VERSION_1 bor Type}),
82 {This2, ok} = write(This1, {string, Name}),
83 {This3, ok} = write(This2, {i32, Seqid}),
84 {This3, ok};
David Reiss914ebb42008-06-11 01:01:48 +000085 false ->
David Reiss63f61262010-08-30 22:05:34 +000086 {This1, ok} = write(This0, {string, Name}),
87 {This2, ok} = write(This1, {byte, Type}),
88 {This3, ok} = write(This2, {i32, Seqid}),
89 {This3, ok}
90 end;
David Reissac549552008-06-10 22:56:59 +000091
David Reiss63f61262010-08-30 22:05:34 +000092write(This, message_end) -> {This, ok};
David Reissac549552008-06-10 22:56:59 +000093
David Reiss63f61262010-08-30 22:05:34 +000094write(This0, #protocol_field_begin{
David Reissac549552008-06-10 22:56:59 +000095 name = _Name,
96 type = Type,
97 id = Id}) ->
David Reiss63f61262010-08-30 22:05:34 +000098 {This1, ok} = write(This0, {byte, Type}),
99 {This2, ok} = write(This1, {i16, Id}),
100 {This2, ok};
David Reissac549552008-06-10 22:56:59 +0000101
102write(This, field_stop) ->
David Reiss63f61262010-08-30 22:05:34 +0000103 write(This, {byte, ?tType_STOP});
David Reissac549552008-06-10 22:56:59 +0000104
David Reiss63f61262010-08-30 22:05:34 +0000105write(This, field_end) -> {This, ok};
David Reissac549552008-06-10 22:56:59 +0000106
David Reiss63f61262010-08-30 22:05:34 +0000107write(This0, #protocol_map_begin{
David Reissac549552008-06-10 22:56:59 +0000108 ktype = Ktype,
109 vtype = Vtype,
110 size = Size}) ->
David Reiss63f61262010-08-30 22:05:34 +0000111 {This1, ok} = write(This0, {byte, Ktype}),
112 {This2, ok} = write(This1, {byte, Vtype}),
113 {This3, ok} = write(This2, {i32, Size}),
114 {This3, ok};
David Reissac549552008-06-10 22:56:59 +0000115
David Reiss63f61262010-08-30 22:05:34 +0000116write(This, map_end) -> {This, ok};
David Reissac549552008-06-10 22:56:59 +0000117
David Reiss63f61262010-08-30 22:05:34 +0000118write(This0, #protocol_list_begin{
David Reissac549552008-06-10 22:56:59 +0000119 etype = Etype,
120 size = Size}) ->
David Reiss63f61262010-08-30 22:05:34 +0000121 {This1, ok} = write(This0, {byte, Etype}),
122 {This2, ok} = write(This1, {i32, Size}),
123 {This2, ok};
David Reissac549552008-06-10 22:56:59 +0000124
David Reiss63f61262010-08-30 22:05:34 +0000125write(This, list_end) -> {This, ok};
David Reissac549552008-06-10 22:56:59 +0000126
David Reiss63f61262010-08-30 22:05:34 +0000127write(This0, #protocol_set_begin{
David Reissac549552008-06-10 22:56:59 +0000128 etype = Etype,
129 size = Size}) ->
David Reiss63f61262010-08-30 22:05:34 +0000130 {This1, ok} = write(This0, {byte, Etype}),
131 {This2, ok} = write(This1, {i32, Size}),
132 {This2, ok};
David Reissac549552008-06-10 22:56:59 +0000133
David Reiss63f61262010-08-30 22:05:34 +0000134write(This, set_end) -> {This, ok};
David Reissac549552008-06-10 22:56:59 +0000135
David Reiss63f61262010-08-30 22:05:34 +0000136write(This, #protocol_struct_begin{}) -> {This, ok};
137write(This, struct_end) -> {This, ok};
David Reissac549552008-06-10 22:56:59 +0000138
David Reissac549552008-06-10 22:56:59 +0000139write(This, {bool, true}) -> write(This, {byte, 1});
140write(This, {bool, false}) -> write(This, {byte, 0});
141
142write(This, {byte, Byte}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000143 write(This, <<Byte:8/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000144
145write(This, {i16, I16}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000146 write(This, <<I16:16/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000147
148write(This, {i32, I32}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000149 write(This, <<I32:32/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000150
David Reiss07a725f2008-06-10 22:57:59 +0000151write(This, {i64, I64}) ->
152 write(This, <<I64:64/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000153
154write(This, {double, Double}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000155 write(This, <<Double:64/big-signed-float>>);
David Reissac549552008-06-10 22:56:59 +0000156
David Reiss63f61262010-08-30 22:05:34 +0000157write(This0, {string, Str}) when is_list(Str) ->
158 {This1, ok} = write(This0, {i32, length(Str)}),
159 {This2, ok} = write(This1, list_to_binary(Str)),
160 {This2, ok};
David Reissac549552008-06-10 22:56:59 +0000161
David Reiss63f61262010-08-30 22:05:34 +0000162write(This0, {string, Bin}) when is_binary(Bin) ->
163 {This1, ok} = write(This0, {i32, size(Bin)}),
164 {This2, ok} = write(This1, Bin),
165 {This2, ok};
David Reiss225db732008-06-11 00:58:48 +0000166
David Reiss914ebb42008-06-11 01:01:48 +0000167%% Data :: iolist()
David Reiss63f61262010-08-30 22:05:34 +0000168write(This = #binary_protocol{transport = Trans}, Data) ->
David Reiss639e1cf2010-08-30 22:05:43 +0000169 {NewTransport, Result} = thrift_transport:write(Trans, Data),
170 {This#binary_protocol{transport = NewTransport}, Result}.
David Reissac549552008-06-10 22:56:59 +0000171
172%%
173
David Reiss63f61262010-08-30 22:05:34 +0000174read(This0, message_begin) ->
175 {This1, Initial} = read(This0, ui32),
176 case Initial of
David Reiss914ebb42008-06-11 01:01:48 +0000177 {ok, Sz} when Sz band ?VERSION_MASK =:= ?VERSION_1 ->
178 %% we're at version 1
David Reiss63f61262010-08-30 22:05:34 +0000179 {This2, {ok, Name}} = read(This1, string),
180 {This3, {ok, SeqId}} = read(This2, i32),
181 Type = Sz band ?TYPE_MASK,
182 {This3, #protocol_message_begin{name = binary_to_list(Name),
183 type = Type,
184 seqid = SeqId}};
David Reiss914ebb42008-06-11 01:01:48 +0000185
186 {ok, Sz} when Sz < 0 ->
187 %% there's a version number but it's unexpected
David Reiss63f61262010-08-30 22:05:34 +0000188 {This1, {error, {bad_binary_protocol_version, Sz}}};
David Reiss914ebb42008-06-11 01:01:48 +0000189
David Reiss63f61262010-08-30 22:05:34 +0000190 {ok, Sz} when This1#binary_protocol.strict_read =:= true ->
David Reiss914ebb42008-06-11 01:01:48 +0000191 %% strict_read is true and there's no version header; that's an error
David Reiss63f61262010-08-30 22:05:34 +0000192 {This1, {error, no_binary_protocol_version}};
David Reiss914ebb42008-06-11 01:01:48 +0000193
David Reiss63f61262010-08-30 22:05:34 +0000194 {ok, Sz} when This1#binary_protocol.strict_read =:= false ->
David Reiss914ebb42008-06-11 01:01:48 +0000195 %% strict_read is false, so just read the old way
David Reiss63f61262010-08-30 22:05:34 +0000196 {This2, {ok, Name}} = read_data(This1, Sz),
197 {This3, {ok, Type}} = read(This2, byte),
198 {This4, {ok, SeqId}} = read(This3, i32),
199 {This4, #protocol_message_begin{name = binary_to_list(Name),
200 type = Type,
201 seqid = SeqId}};
David Reiss914ebb42008-06-11 01:01:48 +0000202
David Reiss63f61262010-08-30 22:05:34 +0000203 Else ->
204 {This1, Else}
David Reissac549552008-06-10 22:56:59 +0000205 end;
206
David Reiss63f61262010-08-30 22:05:34 +0000207read(This, message_end) -> {This, ok};
David Reissac549552008-06-10 22:56:59 +0000208
David Reiss63f61262010-08-30 22:05:34 +0000209read(This, struct_begin) -> {This, ok};
210read(This, struct_end) -> {This, ok};
David Reissac549552008-06-10 22:56:59 +0000211
David Reiss63f61262010-08-30 22:05:34 +0000212read(This0, field_begin) ->
213 {This1, Result} = read(This0, byte),
214 case Result of
David Reissac549552008-06-10 22:56:59 +0000215 {ok, Type = ?tType_STOP} ->
David Reiss63f61262010-08-30 22:05:34 +0000216 {This1, #protocol_field_begin{type = Type}};
David Reissac549552008-06-10 22:56:59 +0000217 {ok, Type} ->
David Reiss63f61262010-08-30 22:05:34 +0000218 {This2, {ok, Id}} = read(This1, i16),
219 {This2, #protocol_field_begin{type = Type,
220 id = Id}}
David Reissac549552008-06-10 22:56:59 +0000221 end;
222
David Reiss63f61262010-08-30 22:05:34 +0000223read(This, field_end) -> {This, ok};
David Reissac549552008-06-10 22:56:59 +0000224
David Reiss63f61262010-08-30 22:05:34 +0000225read(This0, map_begin) ->
226 {This1, {ok, Ktype}} = read(This0, byte),
227 {This2, {ok, Vtype}} = read(This1, byte),
228 {This3, {ok, Size}} = read(This2, i32),
229 {This3, #protocol_map_begin{ktype = Ktype,
230 vtype = Vtype,
231 size = Size}};
232read(This, map_end) -> {This, ok};
David Reissac549552008-06-10 22:56:59 +0000233
David Reiss63f61262010-08-30 22:05:34 +0000234read(This0, list_begin) ->
235 {This1, {ok, Etype}} = read(This0, byte),
236 {This2, {ok, Size}} = read(This1, i32),
237 {This2, #protocol_list_begin{etype = Etype,
238 size = Size}};
239read(This, list_end) -> {This, ok};
David Reissac549552008-06-10 22:56:59 +0000240
David Reiss63f61262010-08-30 22:05:34 +0000241read(This0, set_begin) ->
242 {This1, {ok, Etype}} = read(This0, byte),
243 {This2, {ok, Size}} = read(This1, i32),
244 {This2, #protocol_set_begin{etype = Etype,
245 size = Size}};
246read(This, set_end) -> {This, ok};
David Reissac549552008-06-10 22:56:59 +0000247
David Reiss63f61262010-08-30 22:05:34 +0000248read(This0, field_stop) ->
249 {This1, {ok, ?tType_STOP}} = read(This0, byte),
250 {This1, ok};
David Reissac549552008-06-10 22:56:59 +0000251
252%%
253
David Reiss63f61262010-08-30 22:05:34 +0000254read(This0, bool) ->
255 {This1, Result} = read(This0, byte),
256 case Result of
257 {ok, Byte} -> {This1, {ok, Byte /= 0}};
258 Else -> {This1, Else}
Kevin Clark9a863ee2009-03-24 16:04:36 +0000259 end;
David Reissac549552008-06-10 22:56:59 +0000260
David Reiss63f61262010-08-30 22:05:34 +0000261read(This0, byte) ->
262 {This1, Bytes} = read_data(This0, 1),
263 case Bytes of
264 {ok, <<Val:8/integer-signed-big, _/binary>>} -> {This1, {ok, Val}};
265 Else -> {This1, Else}
David Reissac549552008-06-10 22:56:59 +0000266 end;
267
David Reiss63f61262010-08-30 22:05:34 +0000268read(This0, i16) ->
269 {This1, Bytes} = read_data(This0, 2),
270 case Bytes of
271 {ok, <<Val:16/integer-signed-big, _/binary>>} -> {This1, {ok, Val}};
272 Else -> {This1, Else}
David Reissac549552008-06-10 22:56:59 +0000273 end;
274
David Reiss63f61262010-08-30 22:05:34 +0000275read(This0, i32) ->
276 {This1, Bytes} = read_data(This0, 4),
277 case Bytes of
278 {ok, <<Val:32/integer-signed-big, _/binary>>} -> {This1, {ok, Val}};
279 Else -> {This1, Else}
David Reissac549552008-06-10 22:56:59 +0000280 end;
281
David Reiss9ad6a312008-06-11 01:12:45 +0000282%% unsigned ints aren't used by thrift itself, but it's used for the parsing
283%% of the packet version header. Without this special function BEAM works fine
284%% but hipe thinks it received a bad version header.
David Reiss63f61262010-08-30 22:05:34 +0000285read(This0, ui32) ->
286 {This1, Bytes} = read_data(This0, 4),
287 case Bytes of
288 {ok, <<Val:32/integer-unsigned-big, _/binary>>} -> {This1, {ok, Val}};
289 Else -> {This1, Else}
David Reiss9ad6a312008-06-11 01:12:45 +0000290 end;
291
David Reiss63f61262010-08-30 22:05:34 +0000292read(This0, i64) ->
293 {This1, Bytes} = read_data(This0, 8),
294 case Bytes of
295 {ok, <<Val:64/integer-signed-big, _/binary>>} -> {This1, {ok, Val}};
296 Else -> {This1, Else}
David Reissac549552008-06-10 22:56:59 +0000297 end;
298
David Reiss63f61262010-08-30 22:05:34 +0000299read(This0, double) ->
300 {This1, Bytes} = read_data(This0, 8),
301 case Bytes of
302 {ok, <<Val:64/float-signed-big, _/binary>>} -> {This1, {ok, Val}};
303 Else -> {This1, Else}
David Reissac549552008-06-10 22:56:59 +0000304 end;
305
David Reiss4ec777e2008-06-11 01:01:29 +0000306% returns a binary directly, call binary_to_list if necessary
David Reiss63f61262010-08-30 22:05:34 +0000307read(This0, string) ->
308 {This1, {ok, Sz}} = read(This0, i32),
309 read_data(This1, Sz).
David Reissac549552008-06-10 22:56:59 +0000310
David Reiss63f61262010-08-30 22:05:34 +0000311-spec read_data(#binary_protocol{}, non_neg_integer()) ->
312 {#binary_protocol{}, {ok, binary()} | {error, _Reason}}.
313read_data(This, 0) -> {This, {ok, <<>>}};
314read_data(This = #binary_protocol{transport = Trans}, Len) when is_integer(Len) andalso Len > 0 ->
David Reiss639e1cf2010-08-30 22:05:43 +0000315 {NewTransport, Result} = thrift_transport:read(Trans, Len),
316 {This#binary_protocol{transport = NewTransport}, Result}.
David Reissfc427af2008-06-11 01:11:57 +0000317
318
319%%%% FACTORY GENERATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
320
321-record(tbp_opts, {strict_read = true,
322 strict_write = true}).
323
324parse_factory_options([], Opts) ->
325 Opts;
326parse_factory_options([{strict_read, Bool} | Rest], Opts) when is_boolean(Bool) ->
327 parse_factory_options(Rest, Opts#tbp_opts{strict_read=Bool});
328parse_factory_options([{strict_write, Bool} | Rest], Opts) when is_boolean(Bool) ->
329 parse_factory_options(Rest, Opts#tbp_opts{strict_write=Bool}).
330
331
332%% returns a (fun() -> thrift_protocol())
333new_protocol_factory(TransportFactory, Options) ->
334 ParsedOpts = parse_factory_options(Options, #tbp_opts{}),
335 F = fun() ->
336 {ok, Transport} = TransportFactory(),
337 thrift_binary_protocol:new(
338 Transport,
339 [{strict_read, ParsedOpts#tbp_opts.strict_read},
340 {strict_write, ParsedOpts#tbp_opts.strict_write}])
341 end,
342 {ok, F}.
David Reiss1a2f2182008-06-11 01:14:01 +0000343