| 30 |
ashish |
1 |
%%
|
|
|
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 |
%%
|
|
|
19 |
|
|
|
20 |
-module(thrift_binary_protocol).
|
|
|
21 |
|
|
|
22 |
-behavior(thrift_protocol).
|
|
|
23 |
|
|
|
24 |
-include("thrift_constants.hrl").
|
|
|
25 |
-include("thrift_protocol.hrl").
|
|
|
26 |
|
|
|
27 |
-export([new/1, new/2,
|
|
|
28 |
read/2,
|
|
|
29 |
write/2,
|
|
|
30 |
flush_transport/1,
|
|
|
31 |
close_transport/1,
|
|
|
32 |
|
|
|
33 |
new_protocol_factory/2
|
|
|
34 |
]).
|
|
|
35 |
|
|
|
36 |
-record(binary_protocol, {transport,
|
|
|
37 |
strict_read=true,
|
|
|
38 |
strict_write=true
|
|
|
39 |
}).
|
|
|
40 |
|
|
|
41 |
-define(VERSION_MASK, 16#FFFF0000).
|
|
|
42 |
-define(VERSION_1, 16#80010000).
|
|
|
43 |
-define(TYPE_MASK, 16#000000ff).
|
|
|
44 |
|
|
|
45 |
new(Transport) ->
|
|
|
46 |
new(Transport, _Options = []).
|
|
|
47 |
|
|
|
48 |
new(Transport, Options) ->
|
|
|
49 |
State = #binary_protocol{transport = Transport},
|
|
|
50 |
State1 = parse_options(Options, State),
|
|
|
51 |
thrift_protocol:new(?MODULE, State1).
|
|
|
52 |
|
|
|
53 |
parse_options([], State) ->
|
|
|
54 |
State;
|
|
|
55 |
parse_options([{strict_read, Bool} | Rest], State) when is_boolean(Bool) ->
|
|
|
56 |
parse_options(Rest, State#binary_protocol{strict_read=Bool});
|
|
|
57 |
parse_options([{strict_write, Bool} | Rest], State) when is_boolean(Bool) ->
|
|
|
58 |
parse_options(Rest, State#binary_protocol{strict_write=Bool}).
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
flush_transport(#binary_protocol{transport = Transport}) ->
|
|
|
62 |
thrift_transport:flush(Transport).
|
|
|
63 |
|
|
|
64 |
close_transport(#binary_protocol{transport = Transport}) ->
|
|
|
65 |
thrift_transport:close(Transport).
|
|
|
66 |
|
|
|
67 |
%%%
|
|
|
68 |
%%% instance methods
|
|
|
69 |
%%%
|
|
|
70 |
|
|
|
71 |
write(This, #protocol_message_begin{
|
|
|
72 |
name = Name,
|
|
|
73 |
type = Type,
|
|
|
74 |
seqid = Seqid}) ->
|
|
|
75 |
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,
|
|
|
85 |
ok;
|
|
|
86 |
|
|
|
87 |
write(This, message_end) -> ok;
|
|
|
88 |
|
|
|
89 |
write(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 |
|
|
|
97 |
write(This, field_stop) ->
|
|
|
98 |
write(This, {byte, ?tType_STOP}),
|
|
|
99 |
ok;
|
|
|
100 |
|
|
|
101 |
write(This, field_end) -> ok;
|
|
|
102 |
|
|
|
103 |
write(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 |
|
|
|
112 |
write(This, map_end) -> ok;
|
|
|
113 |
|
|
|
114 |
write(This, #protocol_list_begin{
|
|
|
115 |
etype = Etype,
|
|
|
116 |
size = Size}) ->
|
|
|
117 |
write(This, {byte, Etype}),
|
|
|
118 |
write(This, {i32, Size}),
|
|
|
119 |
ok;
|
|
|
120 |
|
|
|
121 |
write(This, list_end) -> ok;
|
|
|
122 |
|
|
|
123 |
write(This, #protocol_set_begin{
|
|
|
124 |
etype = Etype,
|
|
|
125 |
size = Size}) ->
|
|
|
126 |
write(This, {byte, Etype}),
|
|
|
127 |
write(This, {i32, Size}),
|
|
|
128 |
ok;
|
|
|
129 |
|
|
|
130 |
write(This, set_end) -> ok;
|
|
|
131 |
|
|
|
132 |
write(This, #protocol_struct_begin{}) -> ok;
|
|
|
133 |
write(This, struct_end) -> ok;
|
|
|
134 |
|
|
|
135 |
write(This, {bool, true}) -> write(This, {byte, 1});
|
|
|
136 |
write(This, {bool, false}) -> write(This, {byte, 0});
|
|
|
137 |
|
|
|
138 |
write(This, {byte, Byte}) ->
|
|
|
139 |
write(This, <<Byte:8/big-signed>>);
|
|
|
140 |
|
|
|
141 |
write(This, {i16, I16}) ->
|
|
|
142 |
write(This, <<I16:16/big-signed>>);
|
|
|
143 |
|
|
|
144 |
write(This, {i32, I32}) ->
|
|
|
145 |
write(This, <<I32:32/big-signed>>);
|
|
|
146 |
|
|
|
147 |
write(This, {i64, I64}) ->
|
|
|
148 |
write(This, <<I64:64/big-signed>>);
|
|
|
149 |
|
|
|
150 |
write(This, {double, Double}) ->
|
|
|
151 |
write(This, <<Double:64/big-signed-float>>);
|
|
|
152 |
|
|
|
153 |
write(This, {string, Str}) when is_list(Str) ->
|
|
|
154 |
write(This, {i32, length(Str)}),
|
|
|
155 |
write(This, list_to_binary(Str));
|
|
|
156 |
|
|
|
157 |
write(This, {string, Bin}) when is_binary(Bin) ->
|
|
|
158 |
write(This, {i32, size(Bin)}),
|
|
|
159 |
write(This, Bin);
|
|
|
160 |
|
|
|
161 |
%% Data :: iolist()
|
|
|
162 |
write(This, Data) ->
|
|
|
163 |
thrift_transport:write(This#binary_protocol.transport, Data).
|
|
|
164 |
|
|
|
165 |
%%
|
|
|
166 |
|
|
|
167 |
read(This, message_begin) ->
|
|
|
168 |
case read(This, ui32) of
|
|
|
169 |
{ok, Sz} when Sz band ?VERSION_MASK =:= ?VERSION_1 ->
|
|
|
170 |
%% we're at version 1
|
|
|
171 |
{ok, Name} = read(This, string),
|
|
|
172 |
Type = Sz band ?TYPE_MASK,
|
|
|
173 |
{ok, SeqId} = read(This, i32),
|
|
|
174 |
#protocol_message_begin{name = binary_to_list(Name),
|
|
|
175 |
type = Type,
|
|
|
176 |
seqid = SeqId};
|
|
|
177 |
|
|
|
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 |
|
|
|
195 |
Err = {error, closed} -> Err;
|
|
|
196 |
Err = {error, timeout}-> Err;
|
|
|
197 |
Err = {error, ebadf} -> Err
|
|
|
198 |
end;
|
|
|
199 |
|
|
|
200 |
read(This, message_end) -> ok;
|
|
|
201 |
|
|
|
202 |
read(This, struct_begin) -> ok;
|
|
|
203 |
read(This, struct_end) -> ok;
|
|
|
204 |
|
|
|
205 |
read(This, field_begin) ->
|
|
|
206 |
case read(This, byte) of
|
|
|
207 |
{ok, Type = ?tType_STOP} ->
|
|
|
208 |
#protocol_field_begin{type = Type};
|
|
|
209 |
{ok, Type} ->
|
|
|
210 |
{ok, Id} = read(This, i16),
|
|
|
211 |
#protocol_field_begin{type = Type,
|
|
|
212 |
id = Id}
|
|
|
213 |
end;
|
|
|
214 |
|
|
|
215 |
read(This, field_end) -> ok;
|
|
|
216 |
|
|
|
217 |
read(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};
|
|
|
224 |
read(This, map_end) -> ok;
|
|
|
225 |
|
|
|
226 |
read(This, list_begin) ->
|
|
|
227 |
{ok, Etype} = read(This, byte),
|
|
|
228 |
{ok, Size} = read(This, i32),
|
|
|
229 |
#protocol_list_begin{etype = Etype,
|
|
|
230 |
size = Size};
|
|
|
231 |
read(This, list_end) -> ok;
|
|
|
232 |
|
|
|
233 |
read(This, set_begin) ->
|
|
|
234 |
{ok, Etype} = read(This, byte),
|
|
|
235 |
{ok, Size} = read(This, i32),
|
|
|
236 |
#protocol_set_begin{etype = Etype,
|
|
|
237 |
size = Size};
|
|
|
238 |
read(This, set_end) -> ok;
|
|
|
239 |
|
|
|
240 |
read(This, field_stop) ->
|
|
|
241 |
{ok, ?tType_STOP} = read(This, byte),
|
|
|
242 |
ok;
|
|
|
243 |
|
|
|
244 |
%%
|
|
|
245 |
|
|
|
246 |
read(This, bool) ->
|
|
|
247 |
case read(This, byte) of
|
|
|
248 |
{ok, Byte} -> {ok, Byte /= 0};
|
|
|
249 |
Else -> Else
|
|
|
250 |
end;
|
|
|
251 |
|
|
|
252 |
read(This, byte) ->
|
|
|
253 |
case read(This, 1) of
|
|
|
254 |
{ok, <<Val:8/integer-signed-big, _/binary>>} -> {ok, Val};
|
|
|
255 |
Else -> Else
|
|
|
256 |
end;
|
|
|
257 |
|
|
|
258 |
read(This, i16) ->
|
|
|
259 |
case read(This, 2) of
|
|
|
260 |
{ok, <<Val:16/integer-signed-big, _/binary>>} -> {ok, Val};
|
|
|
261 |
Else -> Else
|
|
|
262 |
end;
|
|
|
263 |
|
|
|
264 |
read(This, i32) ->
|
|
|
265 |
case read(This, 4) of
|
|
|
266 |
{ok, <<Val:32/integer-signed-big, _/binary>>} -> {ok, Val};
|
|
|
267 |
Else -> Else
|
|
|
268 |
end;
|
|
|
269 |
|
|
|
270 |
%% 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.
|
|
|
273 |
read(This, ui32) ->
|
|
|
274 |
case read(This, 4) of
|
|
|
275 |
{ok, <<Val:32/integer-unsigned-big, _/binary>>} -> {ok, Val};
|
|
|
276 |
Else -> Else
|
|
|
277 |
end;
|
|
|
278 |
|
|
|
279 |
read(This, i64) ->
|
|
|
280 |
case read(This, 8) of
|
|
|
281 |
{ok, <<Val:64/integer-signed-big, _/binary>>} -> {ok, Val};
|
|
|
282 |
Else -> Else
|
|
|
283 |
end;
|
|
|
284 |
|
|
|
285 |
read(This, double) ->
|
|
|
286 |
case read(This, 8) of
|
|
|
287 |
{ok, <<Val:64/float-signed-big, _/binary>>} -> {ok, Val};
|
|
|
288 |
Else -> Else
|
|
|
289 |
end;
|
|
|
290 |
|
|
|
291 |
% returns a binary directly, call binary_to_list if necessary
|
|
|
292 |
read(This, string) ->
|
|
|
293 |
{ok, Sz} = read(This, i32),
|
|
|
294 |
{ok, Bin} = read(This, Sz);
|
|
|
295 |
|
|
|
296 |
read(This, 0) -> {ok, <<>>};
|
|
|
297 |
read(This, Len) when is_integer(Len), Len >= 0 ->
|
|
|
298 |
thrift_transport:read(This#binary_protocol.transport, Len).
|
|
|
299 |
|
|
|
300 |
|
|
|
301 |
%%%% FACTORY GENERATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
302 |
|
|
|
303 |
-record(tbp_opts, {strict_read = true,
|
|
|
304 |
strict_write = true}).
|
|
|
305 |
|
|
|
306 |
parse_factory_options([], Opts) ->
|
|
|
307 |
Opts;
|
|
|
308 |
parse_factory_options([{strict_read, Bool} | Rest], Opts) when is_boolean(Bool) ->
|
|
|
309 |
parse_factory_options(Rest, Opts#tbp_opts{strict_read=Bool});
|
|
|
310 |
parse_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())
|
|
|
315 |
new_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}.
|
|
|
325 |
|