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