blob: 00300badf756ab199e97284560980785a3713a6f [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{}.
41-include("thrift_protocol_impl.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 Reissf32d0fb2010-08-30 22:05:00 +000063flush_transport(#binary_protocol{transport = Transport}) ->
64 thrift_transport:flush(Transport).
David Reissac549552008-06-10 22:56:59 +000065
David Reissf32d0fb2010-08-30 22:05:00 +000066close_transport(#binary_protocol{transport = Transport}) ->
67 thrift_transport:close(Transport).
David Reissc11734e2008-06-11 00:59:48 +000068
David Reissac549552008-06-10 22:56:59 +000069%%%
70%%% instance methods
71%%%
72
David Reissf32d0fb2010-08-30 22:05:00 +000073write(This, #protocol_message_begin{
David Reissac549552008-06-10 22:56:59 +000074 name = Name,
75 type = Type,
76 seqid = Seqid}) ->
David Reissf32d0fb2010-08-30 22:05:00 +000077 case This#binary_protocol.strict_write of
David Reiss914ebb42008-06-11 01:01:48 +000078 true ->
David Reissf32d0fb2010-08-30 22:05:00 +000079 write(This, {i32, ?VERSION_1 bor Type}),
80 write(This, {string, Name}),
81 write(This, {i32, Seqid});
David Reiss914ebb42008-06-11 01:01:48 +000082 false ->
David Reissf32d0fb2010-08-30 22:05:00 +000083 write(This, {string, Name}),
84 write(This, {byte, Type}),
85 write(This, {i32, Seqid})
86 end,
87 ok;
David Reissac549552008-06-10 22:56:59 +000088
David Reissf32d0fb2010-08-30 22:05:00 +000089write(This, message_end) -> ok;
David Reissac549552008-06-10 22:56:59 +000090
David Reissf32d0fb2010-08-30 22:05:00 +000091write(This, #protocol_field_begin{
David Reissac549552008-06-10 22:56:59 +000092 name = _Name,
93 type = Type,
94 id = Id}) ->
David Reissf32d0fb2010-08-30 22:05:00 +000095 write(This, {byte, Type}),
96 write(This, {i16, Id}),
97 ok;
David Reissac549552008-06-10 22:56:59 +000098
99write(This, field_stop) ->
David Reissf32d0fb2010-08-30 22:05:00 +0000100 write(This, {byte, ?tType_STOP}),
101 ok;
David Reissac549552008-06-10 22:56:59 +0000102
David Reissf32d0fb2010-08-30 22:05:00 +0000103write(This, field_end) -> ok;
David Reissac549552008-06-10 22:56:59 +0000104
David Reissf32d0fb2010-08-30 22:05:00 +0000105write(This, #protocol_map_begin{
David Reissac549552008-06-10 22:56:59 +0000106 ktype = Ktype,
107 vtype = Vtype,
108 size = Size}) ->
David Reissf32d0fb2010-08-30 22:05:00 +0000109 write(This, {byte, Ktype}),
110 write(This, {byte, Vtype}),
111 write(This, {i32, Size}),
112 ok;
David Reissac549552008-06-10 22:56:59 +0000113
David Reissf32d0fb2010-08-30 22:05:00 +0000114write(This, map_end) -> ok;
David Reissac549552008-06-10 22:56:59 +0000115
David Reissf32d0fb2010-08-30 22:05:00 +0000116write(This, #protocol_list_begin{
David Reissac549552008-06-10 22:56:59 +0000117 etype = Etype,
118 size = Size}) ->
David Reissf32d0fb2010-08-30 22:05:00 +0000119 write(This, {byte, Etype}),
120 write(This, {i32, Size}),
121 ok;
David Reissac549552008-06-10 22:56:59 +0000122
David Reissf32d0fb2010-08-30 22:05:00 +0000123write(This, list_end) -> ok;
David Reissac549552008-06-10 22:56:59 +0000124
David Reissf32d0fb2010-08-30 22:05:00 +0000125write(This, #protocol_set_begin{
David Reissac549552008-06-10 22:56:59 +0000126 etype = Etype,
127 size = Size}) ->
David Reissf32d0fb2010-08-30 22:05:00 +0000128 write(This, {byte, Etype}),
129 write(This, {i32, Size}),
130 ok;
David Reissac549552008-06-10 22:56:59 +0000131
David Reissf32d0fb2010-08-30 22:05:00 +0000132write(This, set_end) -> ok;
David Reissac549552008-06-10 22:56:59 +0000133
David Reissf32d0fb2010-08-30 22:05:00 +0000134write(This, #protocol_struct_begin{}) -> ok;
135write(This, struct_end) -> ok;
David Reissac549552008-06-10 22:56:59 +0000136
David Reissac549552008-06-10 22:56:59 +0000137write(This, {bool, true}) -> write(This, {byte, 1});
138write(This, {bool, false}) -> write(This, {byte, 0});
139
140write(This, {byte, Byte}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000141 write(This, <<Byte:8/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000142
143write(This, {i16, I16}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000144 write(This, <<I16:16/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000145
146write(This, {i32, I32}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000147 write(This, <<I32:32/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000148
David Reiss07a725f2008-06-10 22:57:59 +0000149write(This, {i64, I64}) ->
150 write(This, <<I64:64/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000151
152write(This, {double, Double}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000153 write(This, <<Double:64/big-signed-float>>);
David Reissac549552008-06-10 22:56:59 +0000154
David Reissf32d0fb2010-08-30 22:05:00 +0000155write(This, {string, Str}) when is_list(Str) ->
156 write(This, {i32, length(Str)}),
157 write(This, list_to_binary(Str));
David Reissac549552008-06-10 22:56:59 +0000158
David Reissf32d0fb2010-08-30 22:05:00 +0000159write(This, {string, Bin}) when is_binary(Bin) ->
160 write(This, {i32, size(Bin)}),
161 write(This, Bin);
David Reiss225db732008-06-11 00:58:48 +0000162
David Reiss914ebb42008-06-11 01:01:48 +0000163%% Data :: iolist()
David Reissf32d0fb2010-08-30 22:05:00 +0000164write(This, Data) ->
165 thrift_transport:write(This#binary_protocol.transport, Data).
David Reissac549552008-06-10 22:56:59 +0000166
167%%
168
David Reissf32d0fb2010-08-30 22:05:00 +0000169read(This, message_begin) ->
170 case read(This, ui32) of
David Reiss914ebb42008-06-11 01:01:48 +0000171 {ok, Sz} when Sz band ?VERSION_MASK =:= ?VERSION_1 ->
172 %% we're at version 1
David Reissf32d0fb2010-08-30 22:05:00 +0000173 {ok, Name} = read(This, string),
174 Type = Sz band ?TYPE_MASK,
175 {ok, SeqId} = read(This, i32),
176 #protocol_message_begin{name = binary_to_list(Name),
177 type = Type,
178 seqid = SeqId};
David Reiss914ebb42008-06-11 01:01:48 +0000179
180 {ok, Sz} when Sz < 0 ->
181 %% there's a version number but it's unexpected
David Reissf32d0fb2010-08-30 22:05:00 +0000182 {error, {bad_binary_protocol_version, Sz}};
David Reiss914ebb42008-06-11 01:01:48 +0000183
David Reissf32d0fb2010-08-30 22:05:00 +0000184 {ok, Sz} when This#binary_protocol.strict_read =:= true ->
David Reiss914ebb42008-06-11 01:01:48 +0000185 %% strict_read is true and there's no version header; that's an error
David Reissf32d0fb2010-08-30 22:05:00 +0000186 {error, no_binary_protocol_version};
David Reiss914ebb42008-06-11 01:01:48 +0000187
David Reissf32d0fb2010-08-30 22:05:00 +0000188 {ok, Sz} when This#binary_protocol.strict_read =:= false ->
David Reiss914ebb42008-06-11 01:01:48 +0000189 %% strict_read is false, so just read the old way
David Reiss48b81242010-08-30 22:05:23 +0000190 {ok, Name} = read_data(This, Sz),
David Reissf32d0fb2010-08-30 22:05:00 +0000191 {ok, Type} = read(This, byte),
192 {ok, SeqId} = read(This, i32),
193 #protocol_message_begin{name = binary_to_list(Name),
194 type = Type,
195 seqid = SeqId};
David Reiss914ebb42008-06-11 01:01:48 +0000196
David Reissf32d0fb2010-08-30 22:05:00 +0000197 Err = {error, closed} -> Err;
198 Err = {error, timeout}-> Err;
199 Err = {error, ebadf} -> Err
David Reissac549552008-06-10 22:56:59 +0000200 end;
201
David Reissf32d0fb2010-08-30 22:05:00 +0000202read(This, message_end) -> ok;
David Reissac549552008-06-10 22:56:59 +0000203
David Reissf32d0fb2010-08-30 22:05:00 +0000204read(This, struct_begin) -> ok;
205read(This, struct_end) -> ok;
David Reissac549552008-06-10 22:56:59 +0000206
David Reissf32d0fb2010-08-30 22:05:00 +0000207read(This, field_begin) ->
208 case read(This, byte) of
David Reissac549552008-06-10 22:56:59 +0000209 {ok, Type = ?tType_STOP} ->
David Reissf32d0fb2010-08-30 22:05:00 +0000210 #protocol_field_begin{type = Type};
David Reissac549552008-06-10 22:56:59 +0000211 {ok, Type} ->
David Reissf32d0fb2010-08-30 22:05:00 +0000212 {ok, Id} = read(This, i16),
213 #protocol_field_begin{type = Type,
214 id = Id}
David Reissac549552008-06-10 22:56:59 +0000215 end;
216
David Reissf32d0fb2010-08-30 22:05:00 +0000217read(This, field_end) -> ok;
David Reissac549552008-06-10 22:56:59 +0000218
David Reissf32d0fb2010-08-30 22:05:00 +0000219read(This, map_begin) ->
220 {ok, Ktype} = read(This, byte),
221 {ok, Vtype} = read(This, byte),
222 {ok, Size} = read(This, i32),
223 #protocol_map_begin{ktype = Ktype,
224 vtype = Vtype,
225 size = Size};
226read(This, map_end) -> ok;
David Reissac549552008-06-10 22:56:59 +0000227
David Reissf32d0fb2010-08-30 22:05:00 +0000228read(This, list_begin) ->
229 {ok, Etype} = read(This, byte),
230 {ok, Size} = read(This, i32),
231 #protocol_list_begin{etype = Etype,
232 size = Size};
233read(This, list_end) -> ok;
David Reissac549552008-06-10 22:56:59 +0000234
David Reissf32d0fb2010-08-30 22:05:00 +0000235read(This, set_begin) ->
236 {ok, Etype} = read(This, byte),
237 {ok, Size} = read(This, i32),
238 #protocol_set_begin{etype = Etype,
239 size = Size};
240read(This, set_end) -> ok;
David Reissac549552008-06-10 22:56:59 +0000241
David Reissf32d0fb2010-08-30 22:05:00 +0000242read(This, field_stop) ->
243 {ok, ?tType_STOP} = read(This, byte),
244 ok;
David Reissac549552008-06-10 22:56:59 +0000245
246%%
247
David Reissf32d0fb2010-08-30 22:05:00 +0000248read(This, bool) ->
249 case read(This, byte) of
250 {ok, Byte} -> {ok, Byte /= 0};
251 Else -> Else
Kevin Clark9a863ee2009-03-24 16:04:36 +0000252 end;
David Reissac549552008-06-10 22:56:59 +0000253
David Reissf32d0fb2010-08-30 22:05:00 +0000254read(This, byte) ->
David Reiss48b81242010-08-30 22:05:23 +0000255 case read_data(This, 1) of
David Reissf32d0fb2010-08-30 22:05:00 +0000256 {ok, <<Val:8/integer-signed-big, _/binary>>} -> {ok, Val};
257 Else -> Else
David Reissac549552008-06-10 22:56:59 +0000258 end;
259
David Reissf32d0fb2010-08-30 22:05:00 +0000260read(This, i16) ->
David Reiss48b81242010-08-30 22:05:23 +0000261 case read_data(This, 2) of
David Reissf32d0fb2010-08-30 22:05:00 +0000262 {ok, <<Val:16/integer-signed-big, _/binary>>} -> {ok, Val};
263 Else -> Else
David Reissac549552008-06-10 22:56:59 +0000264 end;
265
David Reissf32d0fb2010-08-30 22:05:00 +0000266read(This, i32) ->
David Reiss48b81242010-08-30 22:05:23 +0000267 case read_data(This, 4) of
David Reissf32d0fb2010-08-30 22:05:00 +0000268 {ok, <<Val:32/integer-signed-big, _/binary>>} -> {ok, Val};
269 Else -> Else
David Reissac549552008-06-10 22:56:59 +0000270 end;
271
David Reiss9ad6a312008-06-11 01:12:45 +0000272%% unsigned ints aren't used by thrift itself, but it's used for the parsing
273%% of the packet version header. Without this special function BEAM works fine
274%% but hipe thinks it received a bad version header.
David Reissf32d0fb2010-08-30 22:05:00 +0000275read(This, ui32) ->
David Reiss48b81242010-08-30 22:05:23 +0000276 case read_data(This, 4) of
David Reissf32d0fb2010-08-30 22:05:00 +0000277 {ok, <<Val:32/integer-unsigned-big, _/binary>>} -> {ok, Val};
278 Else -> Else
David Reiss9ad6a312008-06-11 01:12:45 +0000279 end;
280
David Reissf32d0fb2010-08-30 22:05:00 +0000281read(This, i64) ->
David Reiss48b81242010-08-30 22:05:23 +0000282 case read_data(This, 8) of
David Reissf32d0fb2010-08-30 22:05:00 +0000283 {ok, <<Val:64/integer-signed-big, _/binary>>} -> {ok, Val};
284 Else -> Else
David Reissac549552008-06-10 22:56:59 +0000285 end;
286
David Reissf32d0fb2010-08-30 22:05:00 +0000287read(This, double) ->
David Reiss48b81242010-08-30 22:05:23 +0000288 case read_data(This, 8) of
David Reissf32d0fb2010-08-30 22:05:00 +0000289 {ok, <<Val:64/float-signed-big, _/binary>>} -> {ok, Val};
290 Else -> Else
David Reissac549552008-06-10 22:56:59 +0000291 end;
292
David Reiss4ec777e2008-06-11 01:01:29 +0000293% returns a binary directly, call binary_to_list if necessary
David Reissf32d0fb2010-08-30 22:05:00 +0000294read(This, string) ->
295 {ok, Sz} = read(This, i32),
David Reiss48b81242010-08-30 22:05:23 +0000296 {ok, Bin} = read_data(This, Sz).
David Reissac549552008-06-10 22:56:59 +0000297
David Reiss48b81242010-08-30 22:05:23 +0000298-spec read_data(#binary_protocol{}, non_neg_integer()) -> {ok, binary()} | {error, _Reason}.
299read_data(This, 0) -> {ok, <<>>};
300read_data(This, Len) when is_integer(Len), Len >= 0 ->
David Reissf32d0fb2010-08-30 22:05:00 +0000301 thrift_transport:read(This#binary_protocol.transport, Len).
David Reissfc427af2008-06-11 01:11:57 +0000302
303
304%%%% FACTORY GENERATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305
306-record(tbp_opts, {strict_read = true,
307 strict_write = true}).
308
309parse_factory_options([], Opts) ->
310 Opts;
311parse_factory_options([{strict_read, Bool} | Rest], Opts) when is_boolean(Bool) ->
312 parse_factory_options(Rest, Opts#tbp_opts{strict_read=Bool});
313parse_factory_options([{strict_write, Bool} | Rest], Opts) when is_boolean(Bool) ->
314 parse_factory_options(Rest, Opts#tbp_opts{strict_write=Bool}).
315
316
317%% returns a (fun() -> thrift_protocol())
318new_protocol_factory(TransportFactory, Options) ->
319 ParsedOpts = parse_factory_options(Options, #tbp_opts{}),
320 F = fun() ->
321 {ok, Transport} = TransportFactory(),
322 thrift_binary_protocol:new(
323 Transport,
324 [{strict_read, ParsedOpts#tbp_opts.strict_read},
325 {strict_write, ParsedOpts#tbp_opts.strict_write}])
326 end,
327 {ok, F}.
David Reiss1a2f2182008-06-11 01:14:01 +0000328