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