blob: 6df9fdbf8b0e9cd40f1a76cdf964d9b2310798d4 [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,
16 write/2
17]).
18
19-record(binary_protocol, {transport}).
20
21
22-define(VERSION_MASK, 16#FFFF0000).
23-define(VERSION_1, 16#80010000).
24
25
26new(Transport) ->
27 thrift_protocol:new(?MODULE, #binary_protocol{transport = Transport}).
28
29
30%%%
31%%% instance methods
32%%%
33
34write(This, #protocol_message_begin{
35 name = Name,
36 type = Type,
37 seqid = Seqid}) ->
38 write(This, {i32, ?VERSION_1 bor Type}),
39 write(This, {string, Name}),
40 write(This, {i32, Seqid}),
41 ok;
42
43write(This, message_end) -> ok;
44
45write(This, #protocol_field_begin{
46 name = _Name,
47 type = Type,
48 id = Id}) ->
49 write(This, {byte, Type}),
50 write(This, {i16, Id}),
51 ok;
52
53write(This, field_stop) ->
54 write(This, {byte, ?tType_STOP}),
55 ok;
56
57write(This, field_end) -> ok;
58
59write(This, #protocol_map_begin{
60 ktype = Ktype,
61 vtype = Vtype,
62 size = Size}) ->
63 write(This, {byte, Ktype}),
64 write(This, {byte, Vtype}),
65 write(This, {i32, Size}),
66 ok;
67
68write(This, map_end) -> ok;
69
70write(This, #protocol_list_begin{
71 etype = Etype,
72 size = Size}) ->
73 write(This, {byte, Etype}),
74 write(This, {i32, Size}),
75 ok;
76
77write(This, list_end) -> ok;
78
79write(This, #protocol_set_begin{
80 etype = Etype,
81 size = Size}) ->
82 write(This, {byte, Etype}),
83 write(This, {i32, Size}),
84 ok;
85
86write(This, set_end) -> ok;
87
David Reissae756f42008-06-10 22:57:11 +000088write(This, #protocol_struct_begin{}) -> ok;
David Reissac549552008-06-10 22:56:59 +000089write(This, struct_end) -> ok;
90
91
92
93write(This, {bool, true}) -> write(This, {byte, 1});
94write(This, {bool, false}) -> write(This, {byte, 0});
95
96write(This, {byte, Byte}) ->
97 write(This, <<Byte:8/big>>);
98
99write(This, {i16, I16}) ->
100 write(This, <<I16:16/big>>);
101
102write(This, {i32, I32}) ->
103 write(This, <<I32:32/big>>);
104
105write(This, {i63, I64}) ->
106 write(This, <<I64:64/big>>);
107
108write(This, {double, Double}) ->
109 write(This, <<Double:64/big>>);
110
111write(This, {string, Str}) when is_list(Str) ->
112 write(This, {i32, length(Str)}),
113 write(This, list_to_binary(Str));
114
115write(This, Binary) when is_binary(Binary) ->
116 thrift_transport:write(This#binary_protocol.transport, Binary).
117
118%%
119
120read(This, message_begin) ->
121 case read(This, i32) of
122 {ok, Version} when Version band ?VERSION_MASK == ?VERSION_1 ->
123 Type = Version band 16#000000ff,
124 {ok, Name} = read(This, string),
125 {ok, SeqId} = read(This, i32),
126 #protocol_message_begin{name = Name,
127 type = Type,
128 seqid = SeqId};
David Reissae756f42008-06-10 22:57:11 +0000129 Err = {error, closed} -> Err;
130 Err = {error, ebadf} -> Err
David Reissac549552008-06-10 22:56:59 +0000131 end;
132
133read(This, message_end) -> ok;
134
135read(This, struct_begin) -> ok;
136read(This, struct_end) -> ok;
137
138read(This, field_begin) ->
139 case read(This, byte) of
140 {ok, Type = ?tType_STOP} ->
141 #protocol_field_begin{type = Type,
142 id = 0}; % TODO(todd) 0 or undefined?
143 {ok, Type} ->
David Reissae756f42008-06-10 22:57:11 +0000144 {ok, Id} = read(This, i16),
David Reissac549552008-06-10 22:56:59 +0000145 #protocol_field_begin{type = Type,
146 id = Id}
147 end;
148
149read(This, field_end) -> ok;
150
151read(This, map_begin) ->
152 {ok, Ktype} = read(This, byte),
153 {ok, Vtype} = read(This, byte),
154 {ok, Size} = read(This, i32),
155 #protocol_map_begin{ktype = Ktype,
156 vtype = Vtype,
157 size = Size};
158read(This, map_end) -> ok;
159
160read(This, list_begin) ->
161 {ok, Etype} = read(This, byte),
162 {ok, Size} = read(This, i32),
163 #protocol_list_begin{etype = Etype,
164 size = Size};
165read(This, list_end) -> ok;
166
167read(This, set_begin) ->
168 {ok, Etype} = read(This, byte),
169 {ok, Size} = read(This, i32),
170 #protocol_set_begin{etype = Etype,
171 size = Size};
172read(This, set_end) -> ok;
173
174read(This, field_stop) ->
175 {ok, ?tType_STOP} = read(This, byte),
176 ok;
177
178%%
179
180read(This, bool) ->
181 Byte = read(This, byte),
182 {ok, (Byte /= 0)};
183
184
185read(This, byte) ->
186 case read(This, 1) of
187 {ok, <<Val:8/integer-signed-big, _/binary>>} -> {ok, Val};
188 Else -> Else
189 end;
190
191read(This, i16) ->
192 case read(This, 2) of
193 {ok, <<Val:16/integer-signed-big, _/binary>>} -> {ok, Val};
194 Else -> Else
195 end;
196
197read(This, i32) ->
198 case read(This, 4) of
199 {ok, <<Val:32/integer-signed-big, _/binary>>} -> {ok, Val};
200 Else -> Else
201 end;
202
203read(This, i64) ->
204 case read(This, 8) of
205 {ok, <<Val:64/integer-signed-big, _/binary>>} -> {ok, Val};
206 Else -> Else
207 end;
208
209read(This, double) ->
210 case read(This, 8) of
211 {ok, <<Val:64/float-signed-big, _/binary>>} -> {ok, Val};
212 Else -> Else
213 end;
214
215read(This, string) ->
216 {ok, Sz} = read(This, i32),
217 read(This, Sz);
218
219read(This, Len) when is_integer(Len), Len >= 0 ->
220 thrift_transport:read(This#binary_protocol.transport, Len).