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
10 # http://www.apache.org/licenses/LICENSE-2.0
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
20 from TProtocol import *
21 from struct import pack, unpack
24 class TBinaryProtocol(TProtocolBase):
25 """Binary implementation of the Thrift protocol driver."""
27 # 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.
31 # VERSION_MASK = 0xffff0000
34 # VERSION_1 = 0x80010000
35 VERSION_1 = -2147418112
37 TYPE_MASK = 0x000000ff
39 def __init__(self, trans, strictRead=False, strictWrite=True):
40 TProtocolBase.__init__(self, trans)
41 self.strictRead = strictRead
42 self.strictWrite = strictWrite
44 def writeMessageBegin(self, name, type, seqid):
46 self.writeI32(TBinaryProtocol.VERSION_1 | type)
47 self.writeString(name)
50 self.writeString(name)
54 def writeMessageEnd(self):
57 def writeStructBegin(self, name):
60 def writeStructEnd(self):
63 def writeFieldBegin(self, name, type, id):
67 def writeFieldEnd(self):
70 def writeFieldStop(self):
71 self.writeByte(TType.STOP)
73 def writeMapBegin(self, ktype, vtype, size):
78 def writeMapEnd(self):
81 def writeListBegin(self, etype, size):
85 def writeListEnd(self):
88 def writeSetBegin(self, etype, size):
92 def writeSetEnd(self):
95 def writeBool(self, bool):
101 def writeByte(self, byte):
102 buff = pack("!b", byte)
103 self.trans.write(buff)
105 def writeI16(self, i16):
106 buff = pack("!h", i16)
107 self.trans.write(buff)
109 def writeI32(self, i32):
110 buff = pack("!i", i32)
111 self.trans.write(buff)
113 def writeI64(self, i64):
114 buff = pack("!q", i64)
115 self.trans.write(buff)
117 def writeDouble(self, dub):
118 buff = pack("!d", dub)
119 self.trans.write(buff)
121 def writeString(self, str):
122 self.writeI32(len(str))
123 self.trans.write(str)
125 def readMessageBegin(self):
128 version = sz & TBinaryProtocol.VERSION_MASK
129 if version != TBinaryProtocol.VERSION_1:
130 raise TProtocolException(
131 type=TProtocolException.BAD_VERSION,
132 message='Bad version in readMessageBegin: %d' % (sz))
133 type = sz & TBinaryProtocol.TYPE_MASK
134 name = self.readString()
135 seqid = self.readI32()
138 raise TProtocolException(type=TProtocolException.BAD_VERSION,
139 message='No protocol version header')
140 name = self.trans.readAll(sz)
141 type = self.readByte()
142 seqid = self.readI32()
143 return (name, type, seqid)
145 def readMessageEnd(self):
148 def readStructBegin(self):
151 def readStructEnd(self):
154 def readFieldBegin(self):
155 type = self.readByte()
156 if type == TType.STOP:
157 return (None, type, 0)
159 return (None, type, id)
161 def readFieldEnd(self):
164 def readMapBegin(self):
165 ktype = self.readByte()
166 vtype = self.readByte()
167 size = self.readI32()
168 return (ktype, vtype, size)
170 def readMapEnd(self):
173 def readListBegin(self):
174 etype = self.readByte()
175 size = self.readI32()
178 def readListEnd(self):
181 def readSetBegin(self):
182 etype = self.readByte()
183 size = self.readI32()
186 def readSetEnd(self):
190 byte = self.readByte()
196 buff = self.trans.readAll(1)
197 val, = unpack('!b', buff)
201 buff = self.trans.readAll(2)
202 val, = unpack('!h', buff)
206 buff = self.trans.readAll(4)
207 val, = unpack('!i', buff)
211 buff = self.trans.readAll(8)
212 val, = unpack('!q', buff)
215 def readDouble(self):
216 buff = self.trans.readAll(8)
217 val, = unpack('!d', buff)
220 def readString(self):
222 str = self.trans.readAll(len)
226 class TBinaryProtocolFactory:
227 def __init__(self, strictRead=False, strictWrite=True):
228 self.strictRead = strictRead
229 self.strictWrite = strictWrite
231 def getProtocol(self, trans):
232 prot = TBinaryProtocol(trans, self.strictRead, self.strictWrite)
236 class TBinaryProtocolAccelerated(TBinaryProtocol):
237 """C-Accelerated version of TBinaryProtocol.
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
244 reason. (TODO(dreiss): Make this happen sanely in more cases.)
246 In order to take advantage of the C module, just use
247 TBinaryProtocolAccelerated instead of TBinaryProtocol.
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.
258 class TBinaryProtocolAcceleratedFactory:
259 def getProtocol(self, trans):
260 return TBinaryProtocolAccelerated(trans)