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, |
| 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 | |
| 26 | new(Transport) -> |
| 27 | thrift_protocol:new(?MODULE, #binary_protocol{transport = Transport}). |
| 28 | |
| 29 | |
| 30 | %%% |
| 31 | %%% instance methods |
| 32 | %%% |
| 33 | |
| 34 | write(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 | |
| 43 | write(This, message_end) -> ok; |
| 44 | |
| 45 | write(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 | |
| 53 | write(This, field_stop) -> |
| 54 | write(This, {byte, ?tType_STOP}), |
| 55 | ok; |
| 56 | |
| 57 | write(This, field_end) -> ok; |
| 58 | |
| 59 | write(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 | |
| 68 | write(This, map_end) -> ok; |
| 69 | |
| 70 | write(This, #protocol_list_begin{ |
| 71 | etype = Etype, |
| 72 | size = Size}) -> |
| 73 | write(This, {byte, Etype}), |
| 74 | write(This, {i32, Size}), |
| 75 | ok; |
| 76 | |
| 77 | write(This, list_end) -> ok; |
| 78 | |
| 79 | write(This, #protocol_set_begin{ |
| 80 | etype = Etype, |
| 81 | size = Size}) -> |
| 82 | write(This, {byte, Etype}), |
| 83 | write(This, {i32, Size}), |
| 84 | ok; |
| 85 | |
| 86 | write(This, set_end) -> ok; |
| 87 | |
| 88 | write(This, struct_begin) -> ok; |
| 89 | write(This, struct_end) -> ok; |
| 90 | |
| 91 | |
| 92 | |
| 93 | write(This, {bool, true}) -> write(This, {byte, 1}); |
| 94 | write(This, {bool, false}) -> write(This, {byte, 0}); |
| 95 | |
| 96 | write(This, {byte, Byte}) -> |
| 97 | write(This, <<Byte:8/big>>); |
| 98 | |
| 99 | write(This, {i16, I16}) -> |
| 100 | write(This, <<I16:16/big>>); |
| 101 | |
| 102 | write(This, {i32, I32}) -> |
| 103 | write(This, <<I32:32/big>>); |
| 104 | |
| 105 | write(This, {i63, I64}) -> |
| 106 | write(This, <<I64:64/big>>); |
| 107 | |
| 108 | write(This, {double, Double}) -> |
| 109 | write(This, <<Double:64/big>>); |
| 110 | |
| 111 | write(This, {string, Str}) when is_list(Str) -> |
| 112 | write(This, {i32, length(Str)}), |
| 113 | write(This, list_to_binary(Str)); |
| 114 | |
| 115 | write(This, Binary) when is_binary(Binary) -> |
| 116 | thrift_transport:write(This#binary_protocol.transport, Binary). |
| 117 | |
| 118 | %% |
| 119 | |
| 120 | read(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}; |
| 129 | Err = {error, closed} -> Err |
| 130 | end; |
| 131 | |
| 132 | read(This, message_end) -> ok; |
| 133 | |
| 134 | read(This, struct_begin) -> ok; |
| 135 | read(This, struct_end) -> ok; |
| 136 | |
| 137 | read(This, field_begin) -> |
| 138 | case read(This, byte) of |
| 139 | {ok, Type = ?tType_STOP} -> |
| 140 | #protocol_field_begin{type = Type, |
| 141 | id = 0}; % TODO(todd) 0 or undefined? |
| 142 | {ok, Type} -> |
| 143 | Id = read(This, i16), |
| 144 | #protocol_field_begin{type = Type, |
| 145 | id = Id} |
| 146 | end; |
| 147 | |
| 148 | read(This, field_end) -> ok; |
| 149 | |
| 150 | read(This, map_begin) -> |
| 151 | {ok, Ktype} = read(This, byte), |
| 152 | {ok, Vtype} = read(This, byte), |
| 153 | {ok, Size} = read(This, i32), |
| 154 | #protocol_map_begin{ktype = Ktype, |
| 155 | vtype = Vtype, |
| 156 | size = Size}; |
| 157 | read(This, map_end) -> ok; |
| 158 | |
| 159 | read(This, list_begin) -> |
| 160 | {ok, Etype} = read(This, byte), |
| 161 | {ok, Size} = read(This, i32), |
| 162 | #protocol_list_begin{etype = Etype, |
| 163 | size = Size}; |
| 164 | read(This, list_end) -> ok; |
| 165 | |
| 166 | read(This, set_begin) -> |
| 167 | {ok, Etype} = read(This, byte), |
| 168 | {ok, Size} = read(This, i32), |
| 169 | #protocol_set_begin{etype = Etype, |
| 170 | size = Size}; |
| 171 | read(This, set_end) -> ok; |
| 172 | |
| 173 | read(This, field_stop) -> |
| 174 | {ok, ?tType_STOP} = read(This, byte), |
| 175 | ok; |
| 176 | |
| 177 | %% |
| 178 | |
| 179 | read(This, bool) -> |
| 180 | Byte = read(This, byte), |
| 181 | {ok, (Byte /= 0)}; |
| 182 | |
| 183 | |
| 184 | read(This, byte) -> |
| 185 | case read(This, 1) of |
| 186 | {ok, <<Val:8/integer-signed-big, _/binary>>} -> {ok, Val}; |
| 187 | Else -> Else |
| 188 | end; |
| 189 | |
| 190 | read(This, i16) -> |
| 191 | case read(This, 2) of |
| 192 | {ok, <<Val:16/integer-signed-big, _/binary>>} -> {ok, Val}; |
| 193 | Else -> Else |
| 194 | end; |
| 195 | |
| 196 | read(This, i32) -> |
| 197 | case read(This, 4) of |
| 198 | {ok, <<Val:32/integer-signed-big, _/binary>>} -> {ok, Val}; |
| 199 | Else -> Else |
| 200 | end; |
| 201 | |
| 202 | read(This, i64) -> |
| 203 | case read(This, 8) of |
| 204 | {ok, <<Val:64/integer-signed-big, _/binary>>} -> {ok, Val}; |
| 205 | Else -> Else |
| 206 | end; |
| 207 | |
| 208 | read(This, double) -> |
| 209 | case read(This, 8) of |
| 210 | {ok, <<Val:64/float-signed-big, _/binary>>} -> {ok, Val}; |
| 211 | Else -> Else |
| 212 | end; |
| 213 | |
| 214 | read(This, string) -> |
| 215 | {ok, Sz} = read(This, i32), |
| 216 | read(This, Sz); |
| 217 | |
| 218 | read(This, Len) when is_integer(Len), Len >= 0 -> |
| 219 | thrift_transport:read(This#binary_protocol.transport, Len). |