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