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 | |
David Reiss | 6d0be72 | 2010-08-30 22:05:09 +0000 | [diff] [blame] | 22 | -behaviour(thrift_protocol). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 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 | 5e6637b | 2010-08-30 22:05:18 +0000 | [diff] [blame] | 40 | -type state() :: #binary_protocol{}. |
David Reiss | cf7f397 | 2010-08-30 22:05:55 +0000 | [diff] [blame^] | 41 | -include("thrift_protocol_behaviour.hrl"). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 42 | |
| 43 | -define(VERSION_MASK, 16#FFFF0000). |
| 44 | -define(VERSION_1, 16#80010000). |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 45 | -define(TYPE_MASK, 16#000000ff). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 46 | |
| 47 | new(Transport) -> |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 48 | new(Transport, _Options = []). |
| 49 | |
| 50 | new(Transport, Options) -> |
| 51 | State = #binary_protocol{transport = Transport}, |
| 52 | State1 = parse_options(Options, State), |
| 53 | thrift_protocol:new(?MODULE, State1). |
| 54 | |
| 55 | parse_options([], State) -> |
| 56 | State; |
| 57 | parse_options([{strict_read, Bool} | Rest], State) when is_boolean(Bool) -> |
| 58 | parse_options(Rest, State#binary_protocol{strict_read=Bool}); |
| 59 | parse_options([{strict_write, Bool} | Rest], State) when is_boolean(Bool) -> |
| 60 | parse_options(Rest, State#binary_protocol{strict_write=Bool}). |
| 61 | |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 62 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 63 | flush_transport(This = #binary_protocol{transport = Transport}) -> |
David Reiss | 639e1cf | 2010-08-30 22:05:43 +0000 | [diff] [blame] | 64 | {NewTransport, Result} = thrift_transport:flush(Transport), |
| 65 | {This#binary_protocol{transport = NewTransport}, Result}. |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 66 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 67 | close_transport(This = #binary_protocol{transport = Transport}) -> |
David Reiss | 639e1cf | 2010-08-30 22:05:43 +0000 | [diff] [blame] | 68 | {NewTransport, Result} = thrift_transport:close(Transport), |
| 69 | {This#binary_protocol{transport = NewTransport}, Result}. |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 70 | |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 71 | %%% |
| 72 | %%% instance methods |
| 73 | %%% |
| 74 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 75 | write(This0, #protocol_message_begin{ |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 76 | name = Name, |
| 77 | type = Type, |
| 78 | seqid = Seqid}) -> |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 79 | case This0#binary_protocol.strict_write of |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 80 | true -> |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 81 | {This1, ok} = write(This0, {i32, ?VERSION_1 bor Type}), |
| 82 | {This2, ok} = write(This1, {string, Name}), |
| 83 | {This3, ok} = write(This2, {i32, Seqid}), |
| 84 | {This3, ok}; |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 85 | false -> |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 86 | {This1, ok} = write(This0, {string, Name}), |
| 87 | {This2, ok} = write(This1, {byte, Type}), |
| 88 | {This3, ok} = write(This2, {i32, Seqid}), |
| 89 | {This3, ok} |
| 90 | end; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 91 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 92 | write(This, message_end) -> {This, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 93 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 94 | write(This0, #protocol_field_begin{ |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 95 | name = _Name, |
| 96 | type = Type, |
| 97 | id = Id}) -> |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 98 | {This1, ok} = write(This0, {byte, Type}), |
| 99 | {This2, ok} = write(This1, {i16, Id}), |
| 100 | {This2, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 101 | |
| 102 | write(This, field_stop) -> |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 103 | write(This, {byte, ?tType_STOP}); |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 104 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 105 | write(This, field_end) -> {This, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 106 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 107 | write(This0, #protocol_map_begin{ |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 108 | ktype = Ktype, |
| 109 | vtype = Vtype, |
| 110 | size = Size}) -> |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 111 | {This1, ok} = write(This0, {byte, Ktype}), |
| 112 | {This2, ok} = write(This1, {byte, Vtype}), |
| 113 | {This3, ok} = write(This2, {i32, Size}), |
| 114 | {This3, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 115 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 116 | write(This, map_end) -> {This, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 117 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 118 | write(This0, #protocol_list_begin{ |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 119 | etype = Etype, |
| 120 | size = Size}) -> |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 121 | {This1, ok} = write(This0, {byte, Etype}), |
| 122 | {This2, ok} = write(This1, {i32, Size}), |
| 123 | {This2, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 124 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 125 | write(This, list_end) -> {This, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 126 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 127 | write(This0, #protocol_set_begin{ |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 128 | etype = Etype, |
| 129 | size = Size}) -> |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 130 | {This1, ok} = write(This0, {byte, Etype}), |
| 131 | {This2, ok} = write(This1, {i32, Size}), |
| 132 | {This2, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 133 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 134 | write(This, set_end) -> {This, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 135 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 136 | write(This, #protocol_struct_begin{}) -> {This, ok}; |
| 137 | write(This, struct_end) -> {This, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 138 | |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 139 | write(This, {bool, true}) -> write(This, {byte, 1}); |
| 140 | write(This, {bool, false}) -> write(This, {byte, 0}); |
| 141 | |
| 142 | write(This, {byte, Byte}) -> |
David Reiss | 07a725f | 2008-06-10 22:57:59 +0000 | [diff] [blame] | 143 | write(This, <<Byte:8/big-signed>>); |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 144 | |
| 145 | write(This, {i16, I16}) -> |
David Reiss | 07a725f | 2008-06-10 22:57:59 +0000 | [diff] [blame] | 146 | write(This, <<I16:16/big-signed>>); |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 147 | |
| 148 | write(This, {i32, I32}) -> |
David Reiss | 07a725f | 2008-06-10 22:57:59 +0000 | [diff] [blame] | 149 | write(This, <<I32:32/big-signed>>); |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 150 | |
David Reiss | 07a725f | 2008-06-10 22:57:59 +0000 | [diff] [blame] | 151 | write(This, {i64, I64}) -> |
| 152 | write(This, <<I64:64/big-signed>>); |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 153 | |
| 154 | write(This, {double, Double}) -> |
David Reiss | 07a725f | 2008-06-10 22:57:59 +0000 | [diff] [blame] | 155 | write(This, <<Double:64/big-signed-float>>); |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 156 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 157 | write(This0, {string, Str}) when is_list(Str) -> |
| 158 | {This1, ok} = write(This0, {i32, length(Str)}), |
| 159 | {This2, ok} = write(This1, list_to_binary(Str)), |
| 160 | {This2, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 161 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 162 | write(This0, {string, Bin}) when is_binary(Bin) -> |
| 163 | {This1, ok} = write(This0, {i32, size(Bin)}), |
| 164 | {This2, ok} = write(This1, Bin), |
| 165 | {This2, ok}; |
David Reiss | 225db73 | 2008-06-11 00:58:48 +0000 | [diff] [blame] | 166 | |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 167 | %% Data :: iolist() |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 168 | write(This = #binary_protocol{transport = Trans}, Data) -> |
David Reiss | 639e1cf | 2010-08-30 22:05:43 +0000 | [diff] [blame] | 169 | {NewTransport, Result} = thrift_transport:write(Trans, Data), |
| 170 | {This#binary_protocol{transport = NewTransport}, Result}. |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 171 | |
| 172 | %% |
| 173 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 174 | read(This0, message_begin) -> |
| 175 | {This1, Initial} = read(This0, ui32), |
| 176 | case Initial of |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 177 | {ok, Sz} when Sz band ?VERSION_MASK =:= ?VERSION_1 -> |
| 178 | %% we're at version 1 |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 179 | {This2, {ok, Name}} = read(This1, string), |
| 180 | {This3, {ok, SeqId}} = read(This2, i32), |
| 181 | Type = Sz band ?TYPE_MASK, |
| 182 | {This3, #protocol_message_begin{name = binary_to_list(Name), |
| 183 | type = Type, |
| 184 | seqid = SeqId}}; |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 185 | |
| 186 | {ok, Sz} when Sz < 0 -> |
| 187 | %% there's a version number but it's unexpected |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 188 | {This1, {error, {bad_binary_protocol_version, Sz}}}; |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 189 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 190 | {ok, Sz} when This1#binary_protocol.strict_read =:= true -> |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 191 | %% strict_read is true and there's no version header; that's an error |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 192 | {This1, {error, no_binary_protocol_version}}; |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 193 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 194 | {ok, Sz} when This1#binary_protocol.strict_read =:= false -> |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 195 | %% strict_read is false, so just read the old way |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 196 | {This2, {ok, Name}} = read_data(This1, Sz), |
| 197 | {This3, {ok, Type}} = read(This2, byte), |
| 198 | {This4, {ok, SeqId}} = read(This3, i32), |
| 199 | {This4, #protocol_message_begin{name = binary_to_list(Name), |
| 200 | type = Type, |
| 201 | seqid = SeqId}}; |
David Reiss | 914ebb4 | 2008-06-11 01:01:48 +0000 | [diff] [blame] | 202 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 203 | Else -> |
| 204 | {This1, Else} |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 205 | end; |
| 206 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 207 | read(This, message_end) -> {This, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 208 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 209 | read(This, struct_begin) -> {This, ok}; |
| 210 | read(This, struct_end) -> {This, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 211 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 212 | read(This0, field_begin) -> |
| 213 | {This1, Result} = read(This0, byte), |
| 214 | case Result of |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 215 | {ok, Type = ?tType_STOP} -> |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 216 | {This1, #protocol_field_begin{type = Type}}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 217 | {ok, Type} -> |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 218 | {This2, {ok, Id}} = read(This1, i16), |
| 219 | {This2, #protocol_field_begin{type = Type, |
| 220 | id = Id}} |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 221 | end; |
| 222 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 223 | read(This, field_end) -> {This, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 224 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 225 | read(This0, map_begin) -> |
| 226 | {This1, {ok, Ktype}} = read(This0, byte), |
| 227 | {This2, {ok, Vtype}} = read(This1, byte), |
| 228 | {This3, {ok, Size}} = read(This2, i32), |
| 229 | {This3, #protocol_map_begin{ktype = Ktype, |
| 230 | vtype = Vtype, |
| 231 | size = Size}}; |
| 232 | read(This, map_end) -> {This, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 233 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 234 | read(This0, list_begin) -> |
| 235 | {This1, {ok, Etype}} = read(This0, byte), |
| 236 | {This2, {ok, Size}} = read(This1, i32), |
| 237 | {This2, #protocol_list_begin{etype = Etype, |
| 238 | size = Size}}; |
| 239 | read(This, list_end) -> {This, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 240 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 241 | read(This0, set_begin) -> |
| 242 | {This1, {ok, Etype}} = read(This0, byte), |
| 243 | {This2, {ok, Size}} = read(This1, i32), |
| 244 | {This2, #protocol_set_begin{etype = Etype, |
| 245 | size = Size}}; |
| 246 | read(This, set_end) -> {This, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 247 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 248 | read(This0, field_stop) -> |
| 249 | {This1, {ok, ?tType_STOP}} = read(This0, byte), |
| 250 | {This1, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 251 | |
| 252 | %% |
| 253 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 254 | read(This0, bool) -> |
| 255 | {This1, Result} = read(This0, byte), |
| 256 | case Result of |
| 257 | {ok, Byte} -> {This1, {ok, Byte /= 0}}; |
| 258 | Else -> {This1, Else} |
Kevin Clark | 9a863ee | 2009-03-24 16:04:36 +0000 | [diff] [blame] | 259 | end; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 260 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 261 | read(This0, byte) -> |
| 262 | {This1, Bytes} = read_data(This0, 1), |
| 263 | case Bytes of |
| 264 | {ok, <<Val:8/integer-signed-big, _/binary>>} -> {This1, {ok, Val}}; |
| 265 | Else -> {This1, Else} |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 266 | end; |
| 267 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 268 | read(This0, i16) -> |
| 269 | {This1, Bytes} = read_data(This0, 2), |
| 270 | case Bytes of |
| 271 | {ok, <<Val:16/integer-signed-big, _/binary>>} -> {This1, {ok, Val}}; |
| 272 | Else -> {This1, Else} |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 273 | end; |
| 274 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 275 | read(This0, i32) -> |
| 276 | {This1, Bytes} = read_data(This0, 4), |
| 277 | case Bytes of |
| 278 | {ok, <<Val:32/integer-signed-big, _/binary>>} -> {This1, {ok, Val}}; |
| 279 | Else -> {This1, Else} |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 280 | end; |
| 281 | |
David Reiss | 9ad6a31 | 2008-06-11 01:12:45 +0000 | [diff] [blame] | 282 | %% unsigned ints aren't used by thrift itself, but it's used for the parsing |
| 283 | %% of the packet version header. Without this special function BEAM works fine |
| 284 | %% but hipe thinks it received a bad version header. |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 285 | read(This0, ui32) -> |
| 286 | {This1, Bytes} = read_data(This0, 4), |
| 287 | case Bytes of |
| 288 | {ok, <<Val:32/integer-unsigned-big, _/binary>>} -> {This1, {ok, Val}}; |
| 289 | Else -> {This1, Else} |
David Reiss | 9ad6a31 | 2008-06-11 01:12:45 +0000 | [diff] [blame] | 290 | end; |
| 291 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 292 | read(This0, i64) -> |
| 293 | {This1, Bytes} = read_data(This0, 8), |
| 294 | case Bytes of |
| 295 | {ok, <<Val:64/integer-signed-big, _/binary>>} -> {This1, {ok, Val}}; |
| 296 | Else -> {This1, Else} |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 297 | end; |
| 298 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 299 | read(This0, double) -> |
| 300 | {This1, Bytes} = read_data(This0, 8), |
| 301 | case Bytes of |
| 302 | {ok, <<Val:64/float-signed-big, _/binary>>} -> {This1, {ok, Val}}; |
| 303 | Else -> {This1, Else} |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 304 | end; |
| 305 | |
David Reiss | 4ec777e | 2008-06-11 01:01:29 +0000 | [diff] [blame] | 306 | % returns a binary directly, call binary_to_list if necessary |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 307 | read(This0, string) -> |
| 308 | {This1, {ok, Sz}} = read(This0, i32), |
| 309 | read_data(This1, Sz). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 310 | |
David Reiss | 63f6126 | 2010-08-30 22:05:34 +0000 | [diff] [blame] | 311 | -spec read_data(#binary_protocol{}, non_neg_integer()) -> |
| 312 | {#binary_protocol{}, {ok, binary()} | {error, _Reason}}. |
| 313 | read_data(This, 0) -> {This, {ok, <<>>}}; |
| 314 | read_data(This = #binary_protocol{transport = Trans}, Len) when is_integer(Len) andalso Len > 0 -> |
David Reiss | 639e1cf | 2010-08-30 22:05:43 +0000 | [diff] [blame] | 315 | {NewTransport, Result} = thrift_transport:read(Trans, Len), |
| 316 | {This#binary_protocol{transport = NewTransport}, Result}. |
David Reiss | fc427af | 2008-06-11 01:11:57 +0000 | [diff] [blame] | 317 | |
| 318 | |
| 319 | %%%% FACTORY GENERATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 320 | |
| 321 | -record(tbp_opts, {strict_read = true, |
| 322 | strict_write = true}). |
| 323 | |
| 324 | parse_factory_options([], Opts) -> |
| 325 | Opts; |
| 326 | parse_factory_options([{strict_read, Bool} | Rest], Opts) when is_boolean(Bool) -> |
| 327 | parse_factory_options(Rest, Opts#tbp_opts{strict_read=Bool}); |
| 328 | parse_factory_options([{strict_write, Bool} | Rest], Opts) when is_boolean(Bool) -> |
| 329 | parse_factory_options(Rest, Opts#tbp_opts{strict_write=Bool}). |
| 330 | |
| 331 | |
| 332 | %% returns a (fun() -> thrift_protocol()) |
| 333 | new_protocol_factory(TransportFactory, Options) -> |
| 334 | ParsedOpts = parse_factory_options(Options, #tbp_opts{}), |
| 335 | F = fun() -> |
| 336 | {ok, Transport} = TransportFactory(), |
| 337 | thrift_binary_protocol:new( |
| 338 | Transport, |
| 339 | [{strict_read, ParsedOpts#tbp_opts.strict_read}, |
| 340 | {strict_write, ParsedOpts#tbp_opts.strict_write}]) |
| 341 | end, |
| 342 | {ok, F}. |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 343 | |