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