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