Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 1 | %%% Copyright (c) 2007- Facebook |
| 2 | %%% Distributed under the Thrift Software License |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 3 | %%% |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 4 | %%% See accompanying file LICENSE or visit the Thrift site at: |
| 5 | %%% http://developers.facebook.com/thrift/ |
| 6 | |
| 7 | -module(tBinaryProtocol). |
| 8 | |
| 9 | -include("oop.hrl"). |
| 10 | |
| 11 | -include("thrift.hrl"). |
| 12 | -include("protocol/tProtocolException.hrl"). |
| 13 | -include("protocol/tBinaryProtocol.hrl"). |
| 14 | |
| 15 | -behavior(oop). |
| 16 | |
| 17 | -export([attr/4, super/0, inspect/1]). |
| 18 | |
| 19 | -export([ |
| 20 | new/1, |
| 21 | |
| 22 | writeMessageBegin/4, |
| 23 | writeFieldBegin/4, writeFieldStop/1, |
| 24 | writeMapBegin/4, |
| 25 | writeListBegin/3, |
| 26 | writeSetBegin/3, |
| 27 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 28 | writeBool/2, writeByte/2, writeI16/2, writeI32/2, |
| 29 | writeI64/2, writeDouble/2, writeString/2, |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 30 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 31 | readMessageBegin/1, |
| 32 | readFieldBegin/1, |
| 33 | readMapBegin/1, |
| 34 | readListBegin/1, |
| 35 | readSetBegin/1, |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 36 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 37 | readBool/1, readByte/1, readI16/1, readI32/1, |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 38 | readI64/1, readDouble/1, readString/1 |
| 39 | ]). |
| 40 | |
| 41 | %%% |
| 42 | %%% define attributes |
| 43 | %%% 'super' is required unless ?MODULE is a base class |
| 44 | %%% |
| 45 | |
| 46 | ?DEFINE_ATTR(super). |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 47 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 48 | %%% |
| 49 | %%% behavior callbacks |
| 50 | %%% |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 51 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 52 | %%% super() -> SuperModule = atom() |
| 53 | %%% | none |
| 54 | |
| 55 | super() -> |
| 56 | tProtocol. |
| 57 | |
| 58 | %%% inspect(This) -> string() |
| 59 | |
| 60 | inspect(_This) -> |
| 61 | "". |
| 62 | |
| 63 | %%% |
| 64 | %%% class methods |
| 65 | %%% |
| 66 | |
| 67 | new(Trans) -> |
| 68 | Super = (super()):new(Trans), |
| 69 | #?MODULE{super=Super}. |
| 70 | |
| 71 | %%% |
| 72 | %%% instance methods |
| 73 | %%% |
| 74 | |
| 75 | writeMessageBegin(This, Name, Type, Seqid) -> |
| 76 | ?L1(writeI32, ?VERSION_1 bor Type), |
| 77 | ?L1(writeString, Name), |
| 78 | ?L1(writeI32, Seqid), |
| 79 | ok. |
| 80 | |
| 81 | writeFieldBegin(This, _Name, Type, Id) -> |
| 82 | ?L1(writeByte, Type), |
| 83 | ?L1(writeI16, Id), |
| 84 | ok. |
| 85 | |
| 86 | writeFieldStop(This) -> |
| 87 | ?L1(writeByte, ?tType_STOP), |
| 88 | ok. |
| 89 | |
| 90 | writeMapBegin(This, Ktype, Vtype, Size) -> |
| 91 | ?L1(writeByte, Ktype), |
| 92 | ?L1(writeByte, Vtype), |
| 93 | ?L1(writeI32, Size), |
| 94 | ok. |
| 95 | |
| 96 | writeListBegin(This, Etype, Size) -> |
| 97 | ?L1(writeByte, Etype), |
| 98 | ?L1(writeI32, Size), |
| 99 | ok. |
| 100 | |
| 101 | writeSetBegin(This, Etype, Size) -> |
| 102 | ?L1(writeByte, Etype), |
| 103 | ?L1(writeI32, Size), |
| 104 | ok. |
| 105 | |
| 106 | % |
| 107 | |
Christopher Piro | aa93451 | 2008-01-30 01:39:01 +0000 | [diff] [blame] | 108 | writeBool(This, true) -> |
| 109 | ?L1(writeByte, 1); |
| 110 | writeBool(This, false) -> |
| 111 | ?L1(writeByte, 0). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 112 | |
Christopher Piro | aa93451 | 2008-01-30 01:39:01 +0000 | [diff] [blame] | 113 | writeByte(This, Byte) when is_integer(Byte) -> |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 114 | Trans = oop:get(This, trans), |
Christopher Piro | aa93451 | 2008-01-30 01:39:01 +0000 | [diff] [blame] | 115 | ?R1(Trans, effectful_write, <<Byte:8/big>>). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 116 | |
Christopher Piro | aa93451 | 2008-01-30 01:39:01 +0000 | [diff] [blame] | 117 | writeI16(This, I16) when is_integer(I16) -> |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 118 | Trans = oop:get(This, trans), |
Christopher Piro | aa93451 | 2008-01-30 01:39:01 +0000 | [diff] [blame] | 119 | ?R1(Trans, effectful_write, <<I16:16/big>>). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 120 | |
Christopher Piro | aa93451 | 2008-01-30 01:39:01 +0000 | [diff] [blame] | 121 | writeI32(This, I32) when is_integer(I32) -> |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 122 | Trans = oop:get(This, trans), |
Christopher Piro | aa93451 | 2008-01-30 01:39:01 +0000 | [diff] [blame] | 123 | ?R1(Trans, effectful_write, <<I32:32/big>>). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 124 | |
Christopher Piro | aa93451 | 2008-01-30 01:39:01 +0000 | [diff] [blame] | 125 | writeI64(This, I64) when is_integer(I64) -> |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 126 | Trans = oop:get(This, trans), |
Christopher Piro | aa93451 | 2008-01-30 01:39:01 +0000 | [diff] [blame] | 127 | ?R1(Trans, effectful_write, <<I64:64/big>>). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 128 | |
Christopher Piro | aa93451 | 2008-01-30 01:39:01 +0000 | [diff] [blame] | 129 | writeDouble(This, Double) when is_float(Double) -> |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 130 | Trans = oop:get(This, trans), |
eletuchy | 7d2ab9f | 2008-02-27 19:56:52 +0000 | [diff] [blame] | 131 | ?R1(Trans, effectful_write, <<Double:64/float-big>>). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 132 | |
Christopher Piro | aa93451 | 2008-01-30 01:39:01 +0000 | [diff] [blame] | 133 | writeString(This, Str) when is_list(Str) -> % [char()] or iolist() |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 134 | Trans = oop:get(This, trans), |
Christopher Piro | aa93451 | 2008-01-30 01:39:01 +0000 | [diff] [blame] | 135 | Data = list_to_binary(Str), |
| 136 | ?L1(writeI32, size(Data)), |
| 137 | ?R1(Trans, effectful_write, Data); |
| 138 | |
| 139 | writeString(This, Binary) when is_binary(Binary) -> |
| 140 | Trans = oop:get(This, trans), |
| 141 | ?L1(writeI32, size(Binary)), |
| 142 | ?R1(Trans, effectful_write, Binary). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 143 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 144 | %% |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 145 | |
| 146 | readMessageBegin(This) -> |
| 147 | Version = ?L0(readI32), |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 148 | if |
| 149 | (Version band ?VERSION_MASK) /= ?VERSION_1 -> |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 150 | tException:throw(tProtocolException, [?tProtocolException_BAD_VERSION, "Missing version identifier"]); |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 151 | true -> ok |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 152 | end, |
| 153 | Type = Version band 16#000000ff, |
| 154 | Name = ?L0(readString), |
| 155 | Seqid = ?L0(readI32), |
| 156 | { Name, Type, Seqid }. |
| 157 | |
| 158 | readFieldBegin(This) -> |
| 159 | Type = ?L0(readByte), |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 160 | case Type of |
| 161 | ?tType_STOP -> |
| 162 | { nil, Type, 0 }; % WATCH |
| 163 | _ -> |
| 164 | Id = ?L0(readI16), |
| 165 | { nil, Type, Id } |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 166 | end. |
| 167 | |
| 168 | readMapBegin(This) -> |
| 169 | Ktype = ?L0(readByte), |
| 170 | Vtype = ?L0(readByte), |
| 171 | Size = ?L0(readI32), |
| 172 | { Ktype, Vtype, Size }. |
| 173 | |
| 174 | readListBegin(This) -> |
| 175 | Etype = ?L0(readByte), |
| 176 | Size = ?L0(readI32), |
| 177 | { Etype, Size }. |
| 178 | |
| 179 | readSetBegin(This) -> |
| 180 | Etype = ?L0(readByte), |
| 181 | Size = ?L0(readI32), |
| 182 | { Etype, Size }. |
| 183 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 184 | %% |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 185 | |
| 186 | readBool(This) -> |
| 187 | Byte = ?L0(readByte), |
| 188 | (Byte /= 0). |
| 189 | |
| 190 | readByte(This) -> |
| 191 | Trans = oop:get(This, trans), |
| 192 | <<Val:8/integer-signed-big, _/binary>> = ?R1(Trans, readAll, 1), |
| 193 | Val. |
| 194 | |
| 195 | readI16(This) -> |
| 196 | Trans = oop:get(This, trans), |
| 197 | <<Val:16/integer-signed-big, _/binary>> = ?R1(Trans, readAll, 2), |
| 198 | Val. |
| 199 | |
| 200 | readI32(This) -> |
| 201 | Trans = oop:get(This, trans), |
| 202 | <<Val:32/integer-signed-big, _/binary>> = ?R1(Trans, readAll, 4), |
| 203 | Val. |
| 204 | |
| 205 | readI64(This) -> |
| 206 | Trans = oop:get(This, trans), |
| 207 | <<Val:64/integer-signed-big, _/binary>> = ?R1(Trans, readAll, 8), |
| 208 | Val. |
| 209 | |
| 210 | readDouble(This) -> |
| 211 | Trans = oop:get(This, trans), |
| 212 | <<Val:64/float-signed-big, _/binary>> = ?R1(Trans, readAll, 8), |
| 213 | Val. |
| 214 | |
| 215 | readString(This) -> |
| 216 | Trans = oop:get(This, trans), |
| 217 | Sz = ?L0(readI32), |
| 218 | binary_to_list(?R1(Trans, readAll, Sz)). |