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