blob: 0605dc1efc9d80ac73c22db7a354ffa069d8d4e0 [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
94
95
96write(This, {bool, true}) -> write(This, {byte, 1});
97write(This, {bool, false}) -> write(This, {byte, 0});
98
99write(This, {byte, Byte}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000100 write(This, <<Byte:8/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000101
102write(This, {i16, I16}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000103 write(This, <<I16:16/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000104
105write(This, {i32, I32}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000106 write(This, <<I32:32/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000107
David Reiss07a725f2008-06-10 22:57:59 +0000108write(This, {i64, I64}) ->
109 write(This, <<I64:64/big-signed>>);
David Reissac549552008-06-10 22:56:59 +0000110
111write(This, {double, Double}) ->
David Reiss07a725f2008-06-10 22:57:59 +0000112 write(This, <<Double:64/big-signed-float>>);
David Reissac549552008-06-10 22:56:59 +0000113
114write(This, {string, Str}) when is_list(Str) ->
115 write(This, {i32, length(Str)}),
116 write(This, list_to_binary(Str));
117
118write(This, Binary) when is_binary(Binary) ->
119 thrift_transport:write(This#binary_protocol.transport, Binary).
120
121%%
122
123read(This, message_begin) ->
124 case read(This, i32) of
125 {ok, Version} when Version band ?VERSION_MASK == ?VERSION_1 ->
126 Type = Version band 16#000000ff,
127 {ok, Name} = read(This, string),
128 {ok, SeqId} = read(This, i32),
129 #protocol_message_begin{name = Name,
130 type = Type,
131 seqid = SeqId};
David Reissae756f42008-06-10 22:57:11 +0000132 Err = {error, closed} -> Err;
133 Err = {error, ebadf} -> Err
David Reissac549552008-06-10 22:56:59 +0000134 end;
135
136read(This, message_end) -> ok;
137
138read(This, struct_begin) -> ok;
139read(This, struct_end) -> ok;
140
141read(This, field_begin) ->
142 case read(This, byte) of
143 {ok, Type = ?tType_STOP} ->
144 #protocol_field_begin{type = Type,
145 id = 0}; % TODO(todd) 0 or undefined?
146 {ok, Type} ->
David Reissae756f42008-06-10 22:57:11 +0000147 {ok, Id} = read(This, i16),
David Reissac549552008-06-10 22:56:59 +0000148 #protocol_field_begin{type = Type,
149 id = Id}
150 end;
151
152read(This, field_end) -> ok;
153
154read(This, map_begin) ->
155 {ok, Ktype} = read(This, byte),
156 {ok, Vtype} = read(This, byte),
157 {ok, Size} = read(This, i32),
158 #protocol_map_begin{ktype = Ktype,
159 vtype = Vtype,
160 size = Size};
161read(This, map_end) -> ok;
162
163read(This, list_begin) ->
164 {ok, Etype} = read(This, byte),
165 {ok, Size} = read(This, i32),
166 #protocol_list_begin{etype = Etype,
167 size = Size};
168read(This, list_end) -> ok;
169
170read(This, set_begin) ->
171 {ok, Etype} = read(This, byte),
172 {ok, Size} = read(This, i32),
173 #protocol_set_begin{etype = Etype,
174 size = Size};
175read(This, set_end) -> ok;
176
177read(This, field_stop) ->
178 {ok, ?tType_STOP} = read(This, byte),
179 ok;
180
181%%
182
183read(This, bool) ->
184 Byte = read(This, byte),
185 {ok, (Byte /= 0)};
186
187
188read(This, byte) ->
189 case read(This, 1) of
190 {ok, <<Val:8/integer-signed-big, _/binary>>} -> {ok, Val};
191 Else -> Else
192 end;
193
194read(This, i16) ->
195 case read(This, 2) of
196 {ok, <<Val:16/integer-signed-big, _/binary>>} -> {ok, Val};
197 Else -> Else
198 end;
199
200read(This, i32) ->
201 case read(This, 4) of
202 {ok, <<Val:32/integer-signed-big, _/binary>>} -> {ok, Val};
203 Else -> Else
204 end;
205
206read(This, i64) ->
207 case read(This, 8) of
208 {ok, <<Val:64/integer-signed-big, _/binary>>} -> {ok, Val};
209 Else -> Else
210 end;
211
212read(This, double) ->
213 case read(This, 8) of
214 {ok, <<Val:64/float-signed-big, _/binary>>} -> {ok, Val};
215 Else -> Else
216 end;
217
218read(This, string) ->
219 {ok, Sz} = read(This, i32),
David Reissd93521b2008-06-10 22:58:07 +0000220 {ok, Bin} = read(This, Sz),
221 {ok, binary_to_list(Bin)};
David Reissac549552008-06-10 22:56:59 +0000222
223read(This, Len) when is_integer(Len), Len >= 0 ->
224 thrift_transport:read(This#binary_protocol.transport, Len).