blob: 633cfba026167b8fb5591bfd31191554ef531111 [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,
17 flush_transport/1
David Reissac549552008-06-10 22:56:59 +000018]).
19
20-record(binary_protocol, {transport}).
21
22
23-define(VERSION_MASK, 16#FFFF0000).
24-define(VERSION_1, 16#80010000).
25
26
27new(Transport) ->
28 thrift_protocol:new(?MODULE, #binary_protocol{transport = Transport}).
29
David Reiss90b40832008-06-10 22:58:52 +000030flush_transport(#binary_protocol{transport = Transport}) ->
31 thrift_transport:flush(Transport).
David Reissac549552008-06-10 22:56:59 +000032
33%%%
34%%% instance methods
35%%%
36
37write(This, #protocol_message_begin{
38 name = Name,
39 type = Type,
40 seqid = Seqid}) ->
41 write(This, {i32, ?VERSION_1 bor Type}),
42 write(This, {string, Name}),
43 write(This, {i32, Seqid}),
44 ok;
45
46write(This, message_end) -> ok;
47
48write(This, #protocol_field_begin{
49 name = _Name,
50 type = Type,
51 id = Id}) ->
52 write(This, {byte, Type}),
53 write(This, {i16, Id}),
54 ok;
55
56write(This, field_stop) ->
57 write(This, {byte, ?tType_STOP}),
58 ok;
59
60write(This, field_end) -> ok;
61
62write(This, #protocol_map_begin{
63 ktype = Ktype,
64 vtype = Vtype,
65 size = Size}) ->
66 write(This, {byte, Ktype}),
67 write(This, {byte, Vtype}),
68 write(This, {i32, Size}),
69 ok;
70
71write(This, map_end) -> ok;
72
73write(This, #protocol_list_begin{
74 etype = Etype,
75 size = Size}) ->
76 write(This, {byte, Etype}),
77 write(This, {i32, Size}),
78 ok;
79
80write(This, list_end) -> ok;
81
82write(This, #protocol_set_begin{
83 etype = Etype,
84 size = Size}) ->
85 write(This, {byte, Etype}),
86 write(This, {i32, Size}),
87 ok;
88
89write(This, set_end) -> ok;
90
David Reissae756f42008-06-10 22:57:11 +000091write(This, #protocol_struct_begin{}) -> ok;
David Reissac549552008-06-10 22:56:59 +000092write(This, struct_end) -> ok;
93
David Reissac549552008-06-10 22:56:59 +000094write(This, {bool, true}) -> write(This, {byte, 1});
95write(This, {bool, false}) -> write(This, {byte, 0});
96
97write(This, {byte, Byte}) ->
David Reiss07a725f2008-06-10 22:57:59 +000098 write(This, <<Byte:8/big-signed>>);
David Reissac549552008-06-10 22:56:59 +000099
100write(This, {i16, I16}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000101 write(This, <<I16:16/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000102
103write(This, {i32, I32}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000104 write(This, <<I32:32/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000105
David Reiss07a725f2008-06-10 22:57:59 +0000106write(This, {i64, I64}) ->
107 write(This, <<I64:64/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000108
109write(This, {double, Double}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000110 write(This, <<Double:64/big-signed-float>>);
David Reissac549552008-06-10 22:56:59 +0000111
112write(This, {string, Str}) when is_list(Str) ->
113 write(This, {i32, length(Str)}),
114 write(This, list_to_binary(Str));
115
David Reiss225db732008-06-11 00:58:48 +0000116write(This, {string, Bin}) when is_binary(Bin) ->
117 write(This, {i32, size(Bin)}),
118 write(This, Bin);
119
David Reissac549552008-06-10 22:56:59 +0000120write(This, Binary) when is_binary(Binary) ->
121 thrift_transport:write(This#binary_protocol.transport, Binary).
122
123%%
124
125read(This, message_begin) ->
126 case read(This, i32) of
127 {ok, Version} when Version band ?VERSION_MASK == ?VERSION_1 ->
128 Type = Version band 16#000000ff,
129 {ok, Name} = read(This, string),
130 {ok, SeqId} = read(This, i32),
131 #protocol_message_begin{name = Name,
132 type = Type,
133 seqid = SeqId};
David Reissae756f42008-06-10 22:57:11 +0000134 Err = {error, closed} -> Err;
135 Err = {error, ebadf} -> Err
David Reissac549552008-06-10 22:56:59 +0000136 end;
137
138read(This, message_end) -> ok;
139
140read(This, struct_begin) -> ok;
141read(This, struct_end) -> ok;
142
143read(This, field_begin) ->
144 case read(This, byte) of
145 {ok, Type = ?tType_STOP} ->
146 #protocol_field_begin{type = Type,
147 id = 0}; % TODO(todd) 0 or undefined?
148 {ok, Type} ->
David Reissae756f42008-06-10 22:57:11 +0000149 {ok, Id} = read(This, i16),
David Reissac549552008-06-10 22:56:59 +0000150 #protocol_field_begin{type = Type,
151 id = Id}
152 end;
153
154read(This, field_end) -> ok;
155
156read(This, map_begin) ->
157 {ok, Ktype} = read(This, byte),
158 {ok, Vtype} = read(This, byte),
159 {ok, Size} = read(This, i32),
160 #protocol_map_begin{ktype = Ktype,
161 vtype = Vtype,
162 size = Size};
163read(This, map_end) -> ok;
164
165read(This, list_begin) ->
166 {ok, Etype} = read(This, byte),
167 {ok, Size} = read(This, i32),
168 #protocol_list_begin{etype = Etype,
169 size = Size};
170read(This, list_end) -> ok;
171
172read(This, set_begin) ->
173 {ok, Etype} = read(This, byte),
174 {ok, Size} = read(This, i32),
175 #protocol_set_begin{etype = Etype,
176 size = Size};
177read(This, set_end) -> ok;
178
179read(This, field_stop) ->
180 {ok, ?tType_STOP} = read(This, byte),
David Reiss225db732008-06-11 00:58:48 +0000181 ok;
David Reissac549552008-06-10 22:56:59 +0000182
183%%
184
185read(This, bool) ->
186 Byte = read(This, byte),
187 {ok, (Byte /= 0)};
188
189
190read(This, byte) ->
191 case read(This, 1) of
192 {ok, <<Val:8/integer-signed-big, _/binary>>} -> {ok, Val};
193 Else -> Else
194 end;
195
196read(This, i16) ->
197 case read(This, 2) of
198 {ok, <<Val:16/integer-signed-big, _/binary>>} -> {ok, Val};
199 Else -> Else
200 end;
201
202read(This, i32) ->
203 case read(This, 4) of
204 {ok, <<Val:32/integer-signed-big, _/binary>>} -> {ok, Val};
205 Else -> Else
206 end;
207
208read(This, i64) ->
209 case read(This, 8) of
210 {ok, <<Val:64/integer-signed-big, _/binary>>} -> {ok, Val};
211 Else -> Else
212 end;
213
214read(This, double) ->
215 case read(This, 8) of
216 {ok, <<Val:64/float-signed-big, _/binary>>} -> {ok, Val};
217 Else -> Else
218 end;
219
220read(This, string) ->
221 {ok, Sz} = read(This, i32),
David Reissd93521b2008-06-10 22:58:07 +0000222 {ok, Bin} = read(This, Sz),
223 {ok, binary_to_list(Bin)};
David Reissac549552008-06-10 22:56:59 +0000224
225read(This, Len) when is_integer(Len), Len >= 0 ->
226 thrift_transport:read(This#binary_protocol.transport, Len).