blob: 6fdd08c268e97fe057c15d0402ef79107d70f422 [file] [log] [blame]
Mark Slee89e2bb82007-03-01 00:20:36 +00001#
David Reissea2cba82009-03-30 21:35:00 +00002# 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#
Mark Slee89e2bb82007-03-01 00:20:36 +000019
Mark Sleecde2b612006-09-03 21:13:07 +000020from TProtocol import *
21from struct import pack, unpack
22
Mark Sleecde2b612006-09-03 21:13:07 +000023
Bryan Duxbury69720412012-01-03 17:32:30 +000024class TBinaryProtocol(TProtocolBase):
Mark Sleecde2b612006-09-03 21:13:07 +000025 """Binary implementation of the Thrift protocol driver."""
26
Mark Slee9b36ef32007-10-02 04:44:48 +000027 # NastyHaxx. Python 2.4+ on 32-bit machines forces hex constants to be
28 # positive, converting this into a long. If we hardcode the int value
29 # instead it'll stay in 32 bit-land.
30
31 # VERSION_MASK = 0xffff0000
32 VERSION_MASK = -65536
33
34 # VERSION_1 = 0x80010000
35 VERSION_1 = -2147418112
36
37 TYPE_MASK = 0x000000ff
Mark Slee808454e2007-06-20 21:51:57 +000038
39 def __init__(self, trans, strictRead=False, strictWrite=True):
Aditya Agarwal5c468192007-02-06 01:14:33 +000040 TProtocolBase.__init__(self, trans)
Mark Slee808454e2007-06-20 21:51:57 +000041 self.strictRead = strictRead
42 self.strictWrite = strictWrite
Mark Sleecde2b612006-09-03 21:13:07 +000043
Mark Slee4ac459f2006-10-25 21:39:01 +000044 def writeMessageBegin(self, name, type, seqid):
Mark Slee808454e2007-06-20 21:51:57 +000045 if self.strictWrite:
Mark Slee552410c2007-06-22 01:03:55 +000046 self.writeI32(TBinaryProtocol.VERSION_1 | type)
Mark Slee808454e2007-06-20 21:51:57 +000047 self.writeString(name)
48 self.writeI32(seqid)
49 else:
50 self.writeString(name)
51 self.writeByte(type)
52 self.writeI32(seqid)
Mark Slee4ac459f2006-10-25 21:39:01 +000053
54 def writeMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000055 pass
56
Mark Slee4ac459f2006-10-25 21:39:01 +000057 def writeStructBegin(self, name):
Mark Sleecde2b612006-09-03 21:13:07 +000058 pass
59
Mark Slee4ac459f2006-10-25 21:39:01 +000060 def writeStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000061 pass
62
Mark Slee4ac459f2006-10-25 21:39:01 +000063 def writeFieldBegin(self, name, type, id):
64 self.writeByte(type)
65 self.writeI16(id)
Mark Sleecde2b612006-09-03 21:13:07 +000066
Mark Slee4ac459f2006-10-25 21:39:01 +000067 def writeFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000068 pass
69
Mark Slee4ac459f2006-10-25 21:39:01 +000070 def writeFieldStop(self):
Bryan Duxbury69720412012-01-03 17:32:30 +000071 self.writeByte(TType.STOP)
Mark Sleecde2b612006-09-03 21:13:07 +000072
Mark Slee4ac459f2006-10-25 21:39:01 +000073 def writeMapBegin(self, ktype, vtype, size):
74 self.writeByte(ktype)
75 self.writeByte(vtype)
76 self.writeI32(size)
Mark Sleecde2b612006-09-03 21:13:07 +000077
Mark Slee4ac459f2006-10-25 21:39:01 +000078 def writeMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000079 pass
80
Mark Slee4ac459f2006-10-25 21:39:01 +000081 def writeListBegin(self, etype, size):
82 self.writeByte(etype)
83 self.writeI32(size)
Mark Sleecde2b612006-09-03 21:13:07 +000084
Mark Slee4ac459f2006-10-25 21:39:01 +000085 def writeListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000086 pass
87
Mark Slee4ac459f2006-10-25 21:39:01 +000088 def writeSetBegin(self, etype, size):
89 self.writeByte(etype)
90 self.writeI32(size)
Mark Sleecde2b612006-09-03 21:13:07 +000091
Mark Slee4ac459f2006-10-25 21:39:01 +000092 def writeSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000093 pass
94
Mark Slee4ac459f2006-10-25 21:39:01 +000095 def writeBool(self, bool):
Mark Sleecde2b612006-09-03 21:13:07 +000096 if bool:
Mark Slee4ac459f2006-10-25 21:39:01 +000097 self.writeByte(1)
Mark Sleecde2b612006-09-03 21:13:07 +000098 else:
Mark Slee4ac459f2006-10-25 21:39:01 +000099 self.writeByte(0)
David Reiss382fc302007-08-25 18:01:30 +0000100
Mark Slee4ac459f2006-10-25 21:39:01 +0000101 def writeByte(self, byte):
Mark Sleecde2b612006-09-03 21:13:07 +0000102 buff = pack("!b", byte)
Aditya Agarwal5c468192007-02-06 01:14:33 +0000103 self.trans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +0000104
Mark Slee4ac459f2006-10-25 21:39:01 +0000105 def writeI16(self, i16):
Mark Sleecde2b612006-09-03 21:13:07 +0000106 buff = pack("!h", i16)
Aditya Agarwal5c468192007-02-06 01:14:33 +0000107 self.trans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +0000108
Mark Slee4ac459f2006-10-25 21:39:01 +0000109 def writeI32(self, i32):
Mark Sleecde2b612006-09-03 21:13:07 +0000110 buff = pack("!i", i32)
Aditya Agarwal5c468192007-02-06 01:14:33 +0000111 self.trans.write(buff)
David Reiss382fc302007-08-25 18:01:30 +0000112
Mark Slee4ac459f2006-10-25 21:39:01 +0000113 def writeI64(self, i64):
Mark Sleec9676562006-09-05 17:34:52 +0000114 buff = pack("!q", i64)
Aditya Agarwal5c468192007-02-06 01:14:33 +0000115 self.trans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +0000116
Mark Slee4ac459f2006-10-25 21:39:01 +0000117 def writeDouble(self, dub):
Mark Sleec98d0502006-09-06 02:42:25 +0000118 buff = pack("!d", dub)
Aditya Agarwal5c468192007-02-06 01:14:33 +0000119 self.trans.write(buff)
Mark Sleec98d0502006-09-06 02:42:25 +0000120
Mark Slee4ac459f2006-10-25 21:39:01 +0000121 def writeString(self, str):
122 self.writeI32(len(str))
Aditya Agarwal5c468192007-02-06 01:14:33 +0000123 self.trans.write(str)
Mark Sleecde2b612006-09-03 21:13:07 +0000124
Mark Slee4ac459f2006-10-25 21:39:01 +0000125 def readMessageBegin(self):
Mark Slee808454e2007-06-20 21:51:57 +0000126 sz = self.readI32()
127 if sz < 0:
Mark Slee552410c2007-06-22 01:03:55 +0000128 version = sz & TBinaryProtocol.VERSION_MASK
129 if version != TBinaryProtocol.VERSION_1:
Bryan Duxbury69720412012-01-03 17:32:30 +0000130 raise TProtocolException(
131 type=TProtocolException.BAD_VERSION,
132 message='Bad version in readMessageBegin: %d' % (sz))
Mark Slee9b36ef32007-10-02 04:44:48 +0000133 type = sz & TBinaryProtocol.TYPE_MASK
Mark Slee808454e2007-06-20 21:51:57 +0000134 name = self.readString()
135 seqid = self.readI32()
136 else:
137 if self.strictRead:
Bryan Duxbury69720412012-01-03 17:32:30 +0000138 raise TProtocolException(type=TProtocolException.BAD_VERSION,
139 message='No protocol version header')
Mark Slee808454e2007-06-20 21:51:57 +0000140 name = self.trans.readAll(sz)
141 type = self.readByte()
142 seqid = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000143 return (name, type, seqid)
144
Mark Slee4ac459f2006-10-25 21:39:01 +0000145 def readMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000146 pass
147
Mark Slee4ac459f2006-10-25 21:39:01 +0000148 def readStructBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000149 pass
150
Mark Slee4ac459f2006-10-25 21:39:01 +0000151 def readStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000152 pass
153
Mark Slee4ac459f2006-10-25 21:39:01 +0000154 def readFieldBegin(self):
155 type = self.readByte()
Mark Sleecde2b612006-09-03 21:13:07 +0000156 if type == TType.STOP:
157 return (None, type, 0)
Mark Slee4ac459f2006-10-25 21:39:01 +0000158 id = self.readI16()
Mark Sleecde2b612006-09-03 21:13:07 +0000159 return (None, type, id)
160
Mark Slee4ac459f2006-10-25 21:39:01 +0000161 def readFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000162 pass
163
Mark Slee4ac459f2006-10-25 21:39:01 +0000164 def readMapBegin(self):
165 ktype = self.readByte()
166 vtype = self.readByte()
167 size = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000168 return (ktype, vtype, size)
169
Mark Slee4ac459f2006-10-25 21:39:01 +0000170 def readMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000171 pass
172
Mark Slee4ac459f2006-10-25 21:39:01 +0000173 def readListBegin(self):
174 etype = self.readByte()
175 size = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000176 return (etype, size)
177
Mark Slee4ac459f2006-10-25 21:39:01 +0000178 def readListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000179 pass
180
Mark Slee4ac459f2006-10-25 21:39:01 +0000181 def readSetBegin(self):
182 etype = self.readByte()
183 size = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000184 return (etype, size)
185
Mark Slee4ac459f2006-10-25 21:39:01 +0000186 def readSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000187 pass
188
Mark Slee4ac459f2006-10-25 21:39:01 +0000189 def readBool(self):
190 byte = self.readByte()
Mark Sleecde2b612006-09-03 21:13:07 +0000191 if byte == 0:
192 return False
193 return True
194
Mark Slee4ac459f2006-10-25 21:39:01 +0000195 def readByte(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000196 buff = self.trans.readAll(1)
Mark Sleecde2b612006-09-03 21:13:07 +0000197 val, = unpack('!b', buff)
198 return val
199
Mark Slee4ac459f2006-10-25 21:39:01 +0000200 def readI16(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000201 buff = self.trans.readAll(2)
Mark Sleecde2b612006-09-03 21:13:07 +0000202 val, = unpack('!h', buff)
203 return val
204
Mark Slee4ac459f2006-10-25 21:39:01 +0000205 def readI32(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000206 buff = self.trans.readAll(4)
Mark Sleecde2b612006-09-03 21:13:07 +0000207 val, = unpack('!i', buff)
208 return val
209
Mark Slee4ac459f2006-10-25 21:39:01 +0000210 def readI64(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000211 buff = self.trans.readAll(8)
Mark Sleec9676562006-09-05 17:34:52 +0000212 val, = unpack('!q', buff)
Mark Sleecde2b612006-09-03 21:13:07 +0000213 return val
214
Mark Slee4ac459f2006-10-25 21:39:01 +0000215 def readDouble(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000216 buff = self.trans.readAll(8)
Mark Sleec98d0502006-09-06 02:42:25 +0000217 val, = unpack('!d', buff)
218 return val
219
Mark Slee4ac459f2006-10-25 21:39:01 +0000220 def readString(self):
221 len = self.readI32()
Aditya Agarwal5c468192007-02-06 01:14:33 +0000222 str = self.trans.readAll(len)
Mark Sleecde2b612006-09-03 21:13:07 +0000223 return str
Mark Slee4ac459f2006-10-25 21:39:01 +0000224
David Reiss382fc302007-08-25 18:01:30 +0000225
Mark Slee4ac459f2006-10-25 21:39:01 +0000226class TBinaryProtocolFactory:
Mark Slee808454e2007-06-20 21:51:57 +0000227 def __init__(self, strictRead=False, strictWrite=True):
228 self.strictRead = strictRead
229 self.strictWrite = strictWrite
230
Aditya Agarwal5c468192007-02-06 01:14:33 +0000231 def getProtocol(self, trans):
Mark Slee808454e2007-06-20 21:51:57 +0000232 prot = TBinaryProtocol(trans, self.strictRead, self.strictWrite)
Aditya Agarwal5c468192007-02-06 01:14:33 +0000233 return prot
David Reiss382fc302007-08-25 18:01:30 +0000234
235
236class TBinaryProtocolAccelerated(TBinaryProtocol):
David Reiss382fc302007-08-25 18:01:30 +0000237 """C-Accelerated version of TBinaryProtocol.
238
239 This class does not override any of TBinaryProtocol's methods,
240 but the generated code recognizes it directly and will call into
241 our C module to do the encoding, bypassing this object entirely.
242 We inherit from TBinaryProtocol so that the normal TBinaryProtocol
243 encoding can happen if the fastbinary module doesn't work for some
David Reiss5db3e922007-08-30 23:07:45 +0000244 reason. (TODO(dreiss): Make this happen sanely in more cases.)
David Reiss382fc302007-08-25 18:01:30 +0000245
246 In order to take advantage of the C module, just use
247 TBinaryProtocolAccelerated instead of TBinaryProtocol.
248
249 NOTE: This code was contributed by an external developer.
250 The internal Thrift team has reviewed and tested it,
251 but we cannot guarantee that it is production-ready.
252 Please feel free to report bugs and/or success stories
253 to the public mailing list.
254 """
David Reiss382fc302007-08-25 18:01:30 +0000255 pass
256
257
258class TBinaryProtocolAcceleratedFactory:
259 def getProtocol(self, trans):
260 return TBinaryProtocolAccelerated(trans)