Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 1 | %%% Copyright (c) 2007- Facebook |
| 2 | %%% Distributed under the Thrift Software License |
| 3 | %%% |
| 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 | |
| 28 | writeBool/2, writeByte/2, writeI16/2, writeI32/2, |
| 29 | writeI64/2, writeDouble/2, writeString/2, |
| 30 | |
| 31 | readMessageBegin/1, |
| 32 | readFieldBegin/1, |
| 33 | readMapBegin/1, |
| 34 | readListBegin/1, |
| 35 | readSetBegin/1, |
| 36 | |
| 37 | readBool/1, readByte/1, readI16/1, readI32/1, |
| 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). |
| 47 | |
| 48 | %%% |
| 49 | %%% behavior callbacks |
| 50 | %%% |
| 51 | |
| 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 | |
| 108 | writeBool(This, Bool) -> |
| 109 | case Bool of |
| 110 | true -> ?L1(writeByte, 1); |
| 111 | false -> ?L1(writeByte, 0) |
| 112 | end. |
| 113 | |
| 114 | writeByte(This, Byte) -> |
| 115 | Trans = oop:get(This, trans), |
Christopher Piro | b6dcd2b | 2007-10-13 01:11:46 +0000 | [diff] [blame^] | 116 | ?R1(Trans, effectful_write, binary_to_list(<<Byte:8/big>>)). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 117 | |
| 118 | writeI16(This, I16) -> |
| 119 | Trans = oop:get(This, trans), |
Christopher Piro | b6dcd2b | 2007-10-13 01:11:46 +0000 | [diff] [blame^] | 120 | ?R1(Trans, effectful_write, binary_to_list(<<I16:16/big>>)). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 121 | |
| 122 | writeI32(This, I32) -> |
| 123 | Trans = oop:get(This, trans), |
Christopher Piro | b6dcd2b | 2007-10-13 01:11:46 +0000 | [diff] [blame^] | 124 | ?R1(Trans, effectful_write, binary_to_list(<<I32:32/big>>)). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 125 | |
| 126 | writeI64(This, I64) -> |
| 127 | Trans = oop:get(This, trans), |
Christopher Piro | b6dcd2b | 2007-10-13 01:11:46 +0000 | [diff] [blame^] | 128 | ?R1(Trans, effectful_write, binary_to_list(<<I64:64/big>>)). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 129 | |
| 130 | writeDouble(This, Double) -> |
| 131 | Trans = oop:get(This, trans), |
Christopher Piro | b6dcd2b | 2007-10-13 01:11:46 +0000 | [diff] [blame^] | 132 | ?R1(Trans, effectful_write, binary_to_list(<<Double:64/big>>)). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 133 | |
| 134 | writeString(This, Str) -> |
| 135 | Trans = oop:get(This, trans), |
| 136 | ?L1(writeI32, length(Str)), |
Christopher Piro | b6dcd2b | 2007-10-13 01:11:46 +0000 | [diff] [blame^] | 137 | ?R1(Trans, effectful_write, Str). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 138 | |
| 139 | % |
| 140 | |
| 141 | readMessageBegin(This) -> |
| 142 | Version = ?L0(readI32), |
| 143 | if |
| 144 | (Version band ?VERSION_MASK) /= ?VERSION_1 -> |
| 145 | throw(tProtocolException:new(?tProtocolException_BAD_VERSION, |
| 146 | "Missing version identifier")); |
| 147 | true -> ok |
| 148 | end, |
| 149 | Type = Version band 16#000000ff, |
| 150 | Name = ?L0(readString), |
| 151 | Seqid = ?L0(readI32), |
| 152 | { Name, Type, Seqid }. |
| 153 | |
| 154 | readFieldBegin(This) -> |
| 155 | Type = ?L0(readByte), |
| 156 | case Type of |
| 157 | ?tType_STOP -> |
| 158 | { nil, Type, 0 }; % WATCH |
| 159 | _ -> |
| 160 | Id = ?L0(readI16), |
| 161 | { nil, Type, Id } |
| 162 | end. |
| 163 | |
| 164 | readMapBegin(This) -> |
| 165 | Ktype = ?L0(readByte), |
| 166 | Vtype = ?L0(readByte), |
| 167 | Size = ?L0(readI32), |
| 168 | { Ktype, Vtype, Size }. |
| 169 | |
| 170 | readListBegin(This) -> |
| 171 | Etype = ?L0(readByte), |
| 172 | Size = ?L0(readI32), |
| 173 | { Etype, Size }. |
| 174 | |
| 175 | readSetBegin(This) -> |
| 176 | Etype = ?L0(readByte), |
| 177 | Size = ?L0(readI32), |
| 178 | { Etype, Size }. |
| 179 | |
| 180 | % |
| 181 | |
| 182 | readBool(This) -> |
| 183 | Byte = ?L0(readByte), |
| 184 | (Byte /= 0). |
| 185 | |
| 186 | readByte(This) -> |
| 187 | Trans = oop:get(This, trans), |
| 188 | <<Val:8/integer-signed-big, _/binary>> = ?R1(Trans, readAll, 1), |
| 189 | Val. |
| 190 | |
| 191 | readI16(This) -> |
| 192 | Trans = oop:get(This, trans), |
| 193 | <<Val:16/integer-signed-big, _/binary>> = ?R1(Trans, readAll, 2), |
| 194 | Val. |
| 195 | |
| 196 | readI32(This) -> |
| 197 | Trans = oop:get(This, trans), |
| 198 | <<Val:32/integer-signed-big, _/binary>> = ?R1(Trans, readAll, 4), |
| 199 | Val. |
| 200 | |
| 201 | readI64(This) -> |
| 202 | Trans = oop:get(This, trans), |
| 203 | <<Val:64/integer-signed-big, _/binary>> = ?R1(Trans, readAll, 8), |
| 204 | Val. |
| 205 | |
| 206 | readDouble(This) -> |
| 207 | Trans = oop:get(This, trans), |
| 208 | <<Val:64/float-signed-big, _/binary>> = ?R1(Trans, readAll, 8), |
| 209 | Val. |
| 210 | |
| 211 | readString(This) -> |
| 212 | Trans = oop:get(This, trans), |
| 213 | Sz = ?L0(readI32), |
| 214 | binary_to_list(?R1(Trans, readAll, Sz)). |