blob: 440da22911fad97fa2b43ec926c0a956e7c124d7 [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}) ->
35 thrift_transport:flush(Transport),
36 thrift_transport:close(Transport).
37
David Reissac549552008-06-10 22:56:59 +000038%%%
39%%% instance methods
40%%%
41
42write(This, #protocol_message_begin{
43 name = Name,
44 type = Type,
45 seqid = Seqid}) ->
46 write(This, {i32, ?VERSION_1 bor Type}),
47 write(This, {string, Name}),
48 write(This, {i32, Seqid}),
49 ok;
50
51write(This, message_end) -> ok;
52
53write(This, #protocol_field_begin{
54 name = _Name,
55 type = Type,
56 id = Id}) ->
57 write(This, {byte, Type}),
58 write(This, {i16, Id}),
59 ok;
60
61write(This, field_stop) ->
62 write(This, {byte, ?tType_STOP}),
63 ok;
64
65write(This, field_end) -> ok;
66
67write(This, #protocol_map_begin{
68 ktype = Ktype,
69 vtype = Vtype,
70 size = Size}) ->
71 write(This, {byte, Ktype}),
72 write(This, {byte, Vtype}),
73 write(This, {i32, Size}),
74 ok;
75
76write(This, map_end) -> ok;
77
78write(This, #protocol_list_begin{
79 etype = Etype,
80 size = Size}) ->
81 write(This, {byte, Etype}),
82 write(This, {i32, Size}),
83 ok;
84
85write(This, list_end) -> ok;
86
87write(This, #protocol_set_begin{
88 etype = Etype,
89 size = Size}) ->
90 write(This, {byte, Etype}),
91 write(This, {i32, Size}),
92 ok;
93
94write(This, set_end) -> ok;
95
David Reissae756f42008-06-10 22:57:11 +000096write(This, #protocol_struct_begin{}) -> ok;
David Reissac549552008-06-10 22:56:59 +000097write(This, struct_end) -> ok;
98
David Reissac549552008-06-10 22:56:59 +000099write(This, {bool, true}) -> write(This, {byte, 1});
100write(This, {bool, false}) -> write(This, {byte, 0});
101
102write(This, {byte, Byte}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000103 write(This, <<Byte:8/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000104
105write(This, {i16, I16}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000106 write(This, <<I16:16/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000107
108write(This, {i32, I32}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000109 write(This, <<I32:32/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000110
David Reiss07a725f2008-06-10 22:57:59 +0000111write(This, {i64, I64}) ->
112 write(This, <<I64:64/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000113
114write(This, {double, Double}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000115 write(This, <<Double:64/big-signed-float>>);
David Reissac549552008-06-10 22:56:59 +0000116
117write(This, {string, Str}) when is_list(Str) ->
118 write(This, {i32, length(Str)}),
119 write(This, list_to_binary(Str));
120
David Reiss225db732008-06-11 00:58:48 +0000121write(This, {string, Bin}) when is_binary(Bin) ->
122 write(This, {i32, size(Bin)}),
123 write(This, Bin);
124
David Reissac549552008-06-10 22:56:59 +0000125write(This, Binary) when is_binary(Binary) ->
126 thrift_transport:write(This#binary_protocol.transport, Binary).
127
128%%
129
130read(This, message_begin) ->
131 case read(This, i32) of
132 {ok, Version} when Version band ?VERSION_MASK == ?VERSION_1 ->
133 Type = Version band 16#000000ff,
134 {ok, Name} = read(This, string),
135 {ok, SeqId} = read(This, i32),
136 #protocol_message_begin{name = Name,
137 type = Type,
138 seqid = SeqId};
David Reissae756f42008-06-10 22:57:11 +0000139 Err = {error, closed} -> Err;
140 Err = {error, ebadf} -> Err
David Reissac549552008-06-10 22:56:59 +0000141 end;
142
143read(This, message_end) -> ok;
144
145read(This, struct_begin) -> ok;
146read(This, struct_end) -> ok;
147
148read(This, field_begin) ->
149 case read(This, byte) of
150 {ok, Type = ?tType_STOP} ->
151 #protocol_field_begin{type = Type,
152 id = 0}; % TODO(todd) 0 or undefined?
153 {ok, Type} ->
David Reissae756f42008-06-10 22:57:11 +0000154 {ok, Id} = read(This, i16),
David Reissac549552008-06-10 22:56:59 +0000155 #protocol_field_begin{type = Type,
156 id = Id}
157 end;
158
159read(This, field_end) -> ok;
160
161read(This, map_begin) ->
162 {ok, Ktype} = read(This, byte),
163 {ok, Vtype} = read(This, byte),
164 {ok, Size} = read(This, i32),
165 #protocol_map_begin{ktype = Ktype,
166 vtype = Vtype,
167 size = Size};
168read(This, map_end) -> ok;
169
170read(This, list_begin) ->
171 {ok, Etype} = read(This, byte),
172 {ok, Size} = read(This, i32),
173 #protocol_list_begin{etype = Etype,
174 size = Size};
175read(This, list_end) -> ok;
176
177read(This, set_begin) ->
178 {ok, Etype} = read(This, byte),
179 {ok, Size} = read(This, i32),
180 #protocol_set_begin{etype = Etype,
181 size = Size};
182read(This, set_end) -> ok;
183
184read(This, field_stop) ->
185 {ok, ?tType_STOP} = read(This, byte),
David Reiss225db732008-06-11 00:58:48 +0000186 ok;
David Reissac549552008-06-10 22:56:59 +0000187
188%%
189
190read(This, bool) ->
191 Byte = read(This, byte),
192 {ok, (Byte /= 0)};
193
194
195read(This, byte) ->
196 case read(This, 1) of
197 {ok, <<Val:8/integer-signed-big, _/binary>>} -> {ok, Val};
198 Else -> Else
199 end;
200
201read(This, i16) ->
202 case read(This, 2) of
203 {ok, <<Val:16/integer-signed-big, _/binary>>} -> {ok, Val};
204 Else -> Else
205 end;
206
207read(This, i32) ->
208 case read(This, 4) of
209 {ok, <<Val:32/integer-signed-big, _/binary>>} -> {ok, Val};
210 Else -> Else
211 end;
212
213read(This, i64) ->
214 case read(This, 8) of
215 {ok, <<Val:64/integer-signed-big, _/binary>>} -> {ok, Val};
216 Else -> Else
217 end;
218
219read(This, double) ->
220 case read(This, 8) of
221 {ok, <<Val:64/float-signed-big, _/binary>>} -> {ok, Val};
222 Else -> Else
223 end;
224
225read(This, string) ->
226 {ok, Sz} = read(This, i32),
David Reissd93521b2008-06-10 22:58:07 +0000227 {ok, Bin} = read(This, Sz),
228 {ok, binary_to_list(Bin)};
David Reissac549552008-06-10 22:56:59 +0000229
230read(This, Len) when is_integer(Len), Len >= 0 ->
231 thrift_transport:read(This#binary_protocol.transport, Len).