David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 1 | %%% 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 | |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 14 | -export([new/1, new/2, |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 15 | read/2, |
David Reiss | 90b4083 | 2008-06-10 22:58:52 +0000 | [diff] [blame] | 16 | write/2, |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 17 | flush_transport/1, |
David Reiss | fc427af | 2008-06-11 01:11:57 +0000 | [diff] [blame] | 18 | close_transport/1, |
| 19 | |
| 20 | new_protocol_factory/2 |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 21 | ]). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 22 | |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 23 | -record(binary_protocol, {transport, |
| 24 | strict_read=true, |
| 25 | strict_write=true |
| 26 | }). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 27 | |
| 28 | -define(VERSION_MASK, 16#FFFF0000). |
| 29 | -define(VERSION_1, 16#80010000). |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 30 | -define(TYPE_MASK, 16#000000ff). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 31 | |
| 32 | new(Transport) -> |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 33 | new(Transport, _Options = []). |
| 34 | |
| 35 | new(Transport, Options) -> |
| 36 | State = #binary_protocol{transport = Transport}, |
| 37 | State1 = parse_options(Options, State), |
| 38 | thrift_protocol:new(?MODULE, State1). |
| 39 | |
| 40 | parse_options([], State) -> |
| 41 | State; |
| 42 | parse_options([{strict_read, Bool} | Rest], State) when is_boolean(Bool) -> |
| 43 | parse_options(Rest, State#binary_protocol{strict_read=Bool}); |
| 44 | parse_options([{strict_write, Bool} | Rest], State) when is_boolean(Bool) -> |
| 45 | parse_options(Rest, State#binary_protocol{strict_write=Bool}). |
| 46 | |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 47 | |
David Reiss | 90b4083 | 2008-06-10 22:58:52 +0000 | [diff] [blame] | 48 | flush_transport(#binary_protocol{transport = Transport}) -> |
| 49 | thrift_transport:flush(Transport). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 50 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 51 | close_transport(#binary_protocol{transport = Transport}) -> |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 52 | thrift_transport:close(Transport). |
| 53 | |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 54 | %%% |
| 55 | %%% instance methods |
| 56 | %%% |
| 57 | |
| 58 | write(This, #protocol_message_begin{ |
| 59 | name = Name, |
| 60 | type = Type, |
| 61 | seqid = Seqid}) -> |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 62 | case This#binary_protocol.strict_write of |
| 63 | true -> |
| 64 | write(This, {i32, ?VERSION_1 bor Type}), |
| 65 | write(This, {string, Name}), |
| 66 | write(This, {i32, Seqid}); |
| 67 | false -> |
| 68 | write(This, {string, Name}), |
| 69 | write(This, {byte, Type}), |
| 70 | write(This, {i32, Seqid}) |
| 71 | end, |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 72 | ok; |
| 73 | |
| 74 | write(This, message_end) -> ok; |
| 75 | |
| 76 | write(This, #protocol_field_begin{ |
| 77 | name = _Name, |
| 78 | type = Type, |
| 79 | id = Id}) -> |
| 80 | write(This, {byte, Type}), |
| 81 | write(This, {i16, Id}), |
| 82 | ok; |
| 83 | |
| 84 | write(This, field_stop) -> |
| 85 | write(This, {byte, ?tType_STOP}), |
| 86 | ok; |
| 87 | |
| 88 | write(This, field_end) -> ok; |
| 89 | |
| 90 | write(This, #protocol_map_begin{ |
| 91 | ktype = Ktype, |
| 92 | vtype = Vtype, |
| 93 | size = Size}) -> |
| 94 | write(This, {byte, Ktype}), |
| 95 | write(This, {byte, Vtype}), |
| 96 | write(This, {i32, Size}), |
| 97 | ok; |
| 98 | |
| 99 | write(This, map_end) -> ok; |
| 100 | |
| 101 | write(This, #protocol_list_begin{ |
| 102 | etype = Etype, |
| 103 | size = Size}) -> |
| 104 | write(This, {byte, Etype}), |
| 105 | write(This, {i32, Size}), |
| 106 | ok; |
| 107 | |
| 108 | write(This, list_end) -> ok; |
| 109 | |
| 110 | write(This, #protocol_set_begin{ |
| 111 | etype = Etype, |
| 112 | size = Size}) -> |
| 113 | write(This, {byte, Etype}), |
| 114 | write(This, {i32, Size}), |
| 115 | ok; |
| 116 | |
| 117 | write(This, set_end) -> ok; |
| 118 | |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 119 | write(This, #protocol_struct_begin{}) -> ok; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 120 | write(This, struct_end) -> ok; |
| 121 | |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 122 | write(This, {bool, true}) -> write(This, {byte, 1}); |
| 123 | write(This, {bool, false}) -> write(This, {byte, 0}); |
| 124 | |
| 125 | write(This, {byte, Byte}) -> |
David Reiss | 07a725f | 2008-06-10 22:57:59 +0000 | [diff] [blame] | 126 | write(This, <<Byte:8/big-signed>>); |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 127 | |
| 128 | write(This, {i16, I16}) -> |
David Reiss | 07a725f | 2008-06-10 22:57:59 +0000 | [diff] [blame] | 129 | write(This, <<I16:16/big-signed>>); |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 130 | |
| 131 | write(This, {i32, I32}) -> |
David Reiss | 07a725f | 2008-06-10 22:57:59 +0000 | [diff] [blame] | 132 | write(This, <<I32:32/big-signed>>); |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 133 | |
David Reiss | 07a725f | 2008-06-10 22:57:59 +0000 | [diff] [blame] | 134 | write(This, {i64, I64}) -> |
| 135 | write(This, <<I64:64/big-signed>>); |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 136 | |
| 137 | write(This, {double, Double}) -> |
David Reiss | 07a725f | 2008-06-10 22:57:59 +0000 | [diff] [blame] | 138 | write(This, <<Double:64/big-signed-float>>); |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 139 | |
| 140 | write(This, {string, Str}) when is_list(Str) -> |
| 141 | write(This, {i32, length(Str)}), |
| 142 | write(This, list_to_binary(Str)); |
| 143 | |
David Reiss | 225db73 | 2008-06-11 00:58:48 +0000 | [diff] [blame] | 144 | write(This, {string, Bin}) when is_binary(Bin) -> |
| 145 | write(This, {i32, size(Bin)}), |
| 146 | write(This, Bin); |
| 147 | |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 148 | %% Data :: iolist() |
| 149 | write(This, Data) -> |
| 150 | thrift_transport:write(This#binary_protocol.transport, Data). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 151 | |
| 152 | %% |
| 153 | |
| 154 | read(This, message_begin) -> |
David Reiss | 9ad6a31 | 2008-06-11 01:12:45 +0000 | [diff] [blame] | 155 | case read(This, ui32) of |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 156 | {ok, Sz} when Sz band ?VERSION_MASK =:= ?VERSION_1 -> |
| 157 | %% we're at version 1 |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 158 | {ok, Name} = read(This, string), |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 159 | Type = Sz band ?TYPE_MASK, |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 160 | {ok, SeqId} = read(This, i32), |
David Reiss | 4ec777e | 2008-06-11 01:01:29 +0000 | [diff] [blame] | 161 | #protocol_message_begin{name = binary_to_list(Name), |
| 162 | type = Type, |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 163 | seqid = SeqId}; |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 164 | |
| 165 | {ok, Sz} when Sz < 0 -> |
| 166 | %% there's a version number but it's unexpected |
| 167 | {error, {bad_binary_protocol_version, Sz}}; |
| 168 | |
| 169 | {ok, Sz} when This#binary_protocol.strict_read =:= true -> |
| 170 | %% strict_read is true and there's no version header; that's an error |
| 171 | {error, no_binary_protocol_version}; |
| 172 | |
| 173 | {ok, Sz} when This#binary_protocol.strict_read =:= false -> |
| 174 | %% strict_read is false, so just read the old way |
| 175 | {ok, Name} = read(This, Sz), |
| 176 | {ok, Type} = read(This, byte), |
| 177 | {ok, SeqId} = read(This, i32), |
| 178 | #protocol_message_begin{name = binary_to_list(Name), |
| 179 | type = Type, |
| 180 | seqid = SeqId}; |
| 181 | |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 182 | Err = {error, closed} -> Err; |
David Reiss | 7956f23 | 2008-06-11 01:02:47 +0000 | [diff] [blame] | 183 | Err = {error, timeout}-> Err; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 184 | Err = {error, ebadf} -> Err |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 185 | end; |
| 186 | |
| 187 | read(This, message_end) -> ok; |
| 188 | |
| 189 | read(This, struct_begin) -> ok; |
| 190 | read(This, struct_end) -> ok; |
| 191 | |
| 192 | read(This, field_begin) -> |
| 193 | case read(This, byte) of |
| 194 | {ok, Type = ?tType_STOP} -> |
David Reiss | fe7ce1b | 2008-06-11 01:03:10 +0000 | [diff] [blame] | 195 | #protocol_field_begin{type = Type}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 196 | {ok, Type} -> |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 197 | {ok, Id} = read(This, i16), |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 198 | #protocol_field_begin{type = Type, |
| 199 | id = Id} |
| 200 | end; |
| 201 | |
| 202 | read(This, field_end) -> ok; |
| 203 | |
| 204 | read(This, map_begin) -> |
| 205 | {ok, Ktype} = read(This, byte), |
| 206 | {ok, Vtype} = read(This, byte), |
| 207 | {ok, Size} = read(This, i32), |
| 208 | #protocol_map_begin{ktype = Ktype, |
| 209 | vtype = Vtype, |
| 210 | size = Size}; |
| 211 | read(This, map_end) -> ok; |
| 212 | |
| 213 | read(This, list_begin) -> |
| 214 | {ok, Etype} = read(This, byte), |
| 215 | {ok, Size} = read(This, i32), |
| 216 | #protocol_list_begin{etype = Etype, |
| 217 | size = Size}; |
| 218 | read(This, list_end) -> ok; |
| 219 | |
| 220 | read(This, set_begin) -> |
| 221 | {ok, Etype} = read(This, byte), |
| 222 | {ok, Size} = read(This, i32), |
| 223 | #protocol_set_begin{etype = Etype, |
| 224 | size = Size}; |
| 225 | read(This, set_end) -> ok; |
| 226 | |
| 227 | read(This, field_stop) -> |
| 228 | {ok, ?tType_STOP} = read(This, byte), |
David Reiss | 225db73 | 2008-06-11 00:58:48 +0000 | [diff] [blame] | 229 | ok; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 230 | |
| 231 | %% |
| 232 | |
| 233 | read(This, bool) -> |
| 234 | Byte = read(This, byte), |
| 235 | {ok, (Byte /= 0)}; |
| 236 | |
| 237 | |
| 238 | read(This, byte) -> |
| 239 | case read(This, 1) of |
| 240 | {ok, <<Val:8/integer-signed-big, _/binary>>} -> {ok, Val}; |
| 241 | Else -> Else |
| 242 | end; |
| 243 | |
| 244 | read(This, i16) -> |
| 245 | case read(This, 2) of |
| 246 | {ok, <<Val:16/integer-signed-big, _/binary>>} -> {ok, Val}; |
| 247 | Else -> Else |
| 248 | end; |
| 249 | |
| 250 | read(This, i32) -> |
| 251 | case read(This, 4) of |
| 252 | {ok, <<Val:32/integer-signed-big, _/binary>>} -> {ok, Val}; |
| 253 | Else -> Else |
| 254 | end; |
| 255 | |
David Reiss | 9ad6a31 | 2008-06-11 01:12:45 +0000 | [diff] [blame] | 256 | %% unsigned ints aren't used by thrift itself, but it's used for the parsing |
| 257 | %% of the packet version header. Without this special function BEAM works fine |
| 258 | %% but hipe thinks it received a bad version header. |
| 259 | read(This, ui32) -> |
| 260 | case read(This, 4) of |
| 261 | {ok, <<Val:32/integer-unsigned-big, _/binary>>} -> {ok, Val}; |
| 262 | Else -> Else |
| 263 | end; |
| 264 | |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 265 | read(This, i64) -> |
| 266 | case read(This, 8) of |
| 267 | {ok, <<Val:64/integer-signed-big, _/binary>>} -> {ok, Val}; |
| 268 | Else -> Else |
| 269 | end; |
| 270 | |
| 271 | read(This, double) -> |
| 272 | case read(This, 8) of |
| 273 | {ok, <<Val:64/float-signed-big, _/binary>>} -> {ok, Val}; |
| 274 | Else -> Else |
| 275 | end; |
| 276 | |
David Reiss | 4ec777e | 2008-06-11 01:01:29 +0000 | [diff] [blame] | 277 | % returns a binary directly, call binary_to_list if necessary |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 278 | read(This, string) -> |
| 279 | {ok, Sz} = read(This, i32), |
David Reiss | 4ec777e | 2008-06-11 01:01:29 +0000 | [diff] [blame] | 280 | {ok, Bin} = read(This, Sz); |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 281 | |
David Reiss | d74b023 | 2008-06-11 01:02:55 +0000 | [diff] [blame] | 282 | read(This, 0) -> {ok, <<>>}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 283 | read(This, Len) when is_integer(Len), Len >= 0 -> |
| 284 | thrift_transport:read(This#binary_protocol.transport, Len). |
David Reiss | fc427af | 2008-06-11 01:11:57 +0000 | [diff] [blame] | 285 | |
| 286 | |
| 287 | %%%% FACTORY GENERATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 288 | |
| 289 | -record(tbp_opts, {strict_read = true, |
| 290 | strict_write = true}). |
| 291 | |
| 292 | parse_factory_options([], Opts) -> |
| 293 | Opts; |
| 294 | parse_factory_options([{strict_read, Bool} | Rest], Opts) when is_boolean(Bool) -> |
| 295 | parse_factory_options(Rest, Opts#tbp_opts{strict_read=Bool}); |
| 296 | parse_factory_options([{strict_write, Bool} | Rest], Opts) when is_boolean(Bool) -> |
| 297 | parse_factory_options(Rest, Opts#tbp_opts{strict_write=Bool}). |
| 298 | |
| 299 | |
| 300 | %% returns a (fun() -> thrift_protocol()) |
| 301 | new_protocol_factory(TransportFactory, Options) -> |
| 302 | ParsedOpts = parse_factory_options(Options, #tbp_opts{}), |
| 303 | F = fun() -> |
| 304 | {ok, Transport} = TransportFactory(), |
| 305 | thrift_binary_protocol:new( |
| 306 | Transport, |
| 307 | [{strict_read, ParsedOpts#tbp_opts.strict_read}, |
| 308 | {strict_write, ParsedOpts#tbp_opts.strict_write}]) |
| 309 | end, |
| 310 | {ok, F}. |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 311 | |