blob: f115b2f4842851a2649bcafcf30fee462dbdce69 [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
14-export([new/1,
15 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 Reissac549552008-06-10 22:56:59 +000019]).
20
21-record(binary_protocol, {transport}).
22
23
24-define(VERSION_MASK, 16#FFFF0000).
25-define(VERSION_1, 16#80010000).
26
27
28new(Transport) ->
29 thrift_protocol:new(?MODULE, #binary_protocol{transport = Transport}).
30
David Reiss90b40832008-06-10 22:58:52 +000031flush_transport(#binary_protocol{transport = Transport}) ->
32 thrift_transport:flush(Transport).
David Reissac549552008-06-10 22:56:59 +000033
David Reissc11734e2008-06-11 00:59:48 +000034close_transport(#binary_protocol{transport = Transport}) ->
David Reissc11734e2008-06-11 00:59:48 +000035 thrift_transport:close(Transport).
36
David Reissac549552008-06-10 22:56:59 +000037%%%
38%%% instance methods
39%%%
40
41write(This, #protocol_message_begin{
42 name = Name,
43 type = Type,
44 seqid = Seqid}) ->
45 write(This, {i32, ?VERSION_1 bor Type}),
46 write(This, {string, Name}),
47 write(This, {i32, Seqid}),
48 ok;
49
50write(This, message_end) -> ok;
51
52write(This, #protocol_field_begin{
53 name = _Name,
54 type = Type,
55 id = Id}) ->
56 write(This, {byte, Type}),
57 write(This, {i16, Id}),
58 ok;
59
60write(This, field_stop) ->
61 write(This, {byte, ?tType_STOP}),
62 ok;
63
64write(This, field_end) -> ok;
65
66write(This, #protocol_map_begin{
67 ktype = Ktype,
68 vtype = Vtype,
69 size = Size}) ->
70 write(This, {byte, Ktype}),
71 write(This, {byte, Vtype}),
72 write(This, {i32, Size}),
73 ok;
74
75write(This, map_end) -> ok;
76
77write(This, #protocol_list_begin{
78 etype = Etype,
79 size = Size}) ->
80 write(This, {byte, Etype}),
81 write(This, {i32, Size}),
82 ok;
83
84write(This, list_end) -> ok;
85
86write(This, #protocol_set_begin{
87 etype = Etype,
88 size = Size}) ->
89 write(This, {byte, Etype}),
90 write(This, {i32, Size}),
91 ok;
92
93write(This, set_end) -> ok;
94
David Reissae756f42008-06-10 22:57:11 +000095write(This, #protocol_struct_begin{}) -> ok;
David Reissac549552008-06-10 22:56:59 +000096write(This, struct_end) -> ok;
97
David Reissac549552008-06-10 22:56:59 +000098write(This, {bool, true}) -> write(This, {byte, 1});
99write(This, {bool, false}) -> write(This, {byte, 0});
100
101write(This, {byte, Byte}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000102 write(This, <<Byte:8/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000103
104write(This, {i16, I16}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000105 write(This, <<I16:16/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000106
107write(This, {i32, I32}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000108 write(This, <<I32:32/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000109
David Reiss07a725f2008-06-10 22:57:59 +0000110write(This, {i64, I64}) ->
111 write(This, <<I64:64/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000112
113write(This, {double, Double}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000114 write(This, <<Double:64/big-signed-float>>);
David Reissac549552008-06-10 22:56:59 +0000115
116write(This, {string, Str}) when is_list(Str) ->
117 write(This, {i32, length(Str)}),
118 write(This, list_to_binary(Str));
119
David Reiss225db732008-06-11 00:58:48 +0000120write(This, {string, Bin}) when is_binary(Bin) ->
121 write(This, {i32, size(Bin)}),
122 write(This, Bin);
123
David Reissac549552008-06-10 22:56:59 +0000124write(This, Binary) when is_binary(Binary) ->
125 thrift_transport:write(This#binary_protocol.transport, Binary).
126
127%%
128
129read(This, message_begin) ->
130 case read(This, i32) of
131 {ok, Version} when Version band ?VERSION_MASK == ?VERSION_1 ->
132 Type = Version band 16#000000ff,
133 {ok, Name} = read(This, string),
134 {ok, SeqId} = read(This, i32),
David Reiss4ec777e2008-06-11 01:01:29 +0000135 #protocol_message_begin{name = binary_to_list(Name),
136 type = Type,
David Reissac549552008-06-10 22:56:59 +0000137 seqid = SeqId};
David Reissae756f42008-06-10 22:57:11 +0000138 Err = {error, closed} -> Err;
139 Err = {error, ebadf} -> Err
David Reissac549552008-06-10 22:56:59 +0000140 end;
141
142read(This, message_end) -> ok;
143
144read(This, struct_begin) -> ok;
145read(This, struct_end) -> ok;
146
147read(This, field_begin) ->
148 case read(This, byte) of
149 {ok, Type = ?tType_STOP} ->
150 #protocol_field_begin{type = Type,
151 id = 0}; % TODO(todd) 0 or undefined?
152 {ok, Type} ->
David Reissae756f42008-06-10 22:57:11 +0000153 {ok, Id} = read(This, i16),
David Reissac549552008-06-10 22:56:59 +0000154 #protocol_field_begin{type = Type,
155 id = Id}
156 end;
157
158read(This, field_end) -> ok;
159
160read(This, map_begin) ->
161 {ok, Ktype} = read(This, byte),
162 {ok, Vtype} = read(This, byte),
163 {ok, Size} = read(This, i32),
164 #protocol_map_begin{ktype = Ktype,
165 vtype = Vtype,
166 size = Size};
167read(This, map_end) -> ok;
168
169read(This, list_begin) ->
170 {ok, Etype} = read(This, byte),
171 {ok, Size} = read(This, i32),
172 #protocol_list_begin{etype = Etype,
173 size = Size};
174read(This, list_end) -> ok;
175
176read(This, set_begin) ->
177 {ok, Etype} = read(This, byte),
178 {ok, Size} = read(This, i32),
179 #protocol_set_begin{etype = Etype,
180 size = Size};
181read(This, set_end) -> ok;
182
183read(This, field_stop) ->
184 {ok, ?tType_STOP} = read(This, byte),
David Reiss225db732008-06-11 00:58:48 +0000185 ok;
David Reissac549552008-06-10 22:56:59 +0000186
187%%
188
189read(This, bool) ->
190 Byte = read(This, byte),
191 {ok, (Byte /= 0)};
192
193
194read(This, byte) ->
195 case read(This, 1) of
196 {ok, <<Val:8/integer-signed-big, _/binary>>} -> {ok, Val};
197 Else -> Else
198 end;
199
200read(This, i16) ->
201 case read(This, 2) of
202 {ok, <<Val:16/integer-signed-big, _/binary>>} -> {ok, Val};
203 Else -> Else
204 end;
205
206read(This, i32) ->
207 case read(This, 4) of
208 {ok, <<Val:32/integer-signed-big, _/binary>>} -> {ok, Val};
209 Else -> Else
210 end;
211
212read(This, i64) ->
213 case read(This, 8) of
214 {ok, <<Val:64/integer-signed-big, _/binary>>} -> {ok, Val};
215 Else -> Else
216 end;
217
218read(This, double) ->
219 case read(This, 8) of
220 {ok, <<Val:64/float-signed-big, _/binary>>} -> {ok, Val};
221 Else -> Else
222 end;
223
David Reiss4ec777e2008-06-11 01:01:29 +0000224% returns a binary directly, call binary_to_list if necessary
David Reissac549552008-06-10 22:56:59 +0000225read(This, string) ->
226 {ok, Sz} = read(This, i32),
David Reiss4ec777e2008-06-11 01:01:29 +0000227 {ok, Bin} = read(This, Sz);
David Reissac549552008-06-10 22:56:59 +0000228
229read(This, Len) when is_integer(Len), Len >= 0 ->
230 thrift_transport:read(This#binary_protocol.transport, Len).