blob: 9755ada718d93d802ae133ff3cee074748c84b27 [file] [log] [blame]
Mark Slee89e2bb82007-03-01 00:20:36 +00001#!/usr/bin/env python
2#
3# Copyright (c) 2006- Facebook
4# Distributed under the Thrift Software License
5#
6# See accompanying file LICENSE or visit the Thrift site at:
7# http://developers.facebook.com/thrift/
8
Mark Sleecde2b612006-09-03 21:13:07 +00009from TProtocol import *
10from struct import pack, unpack
11
12class TBinaryProtocol(TProtocolBase):
13
14 """Binary implementation of the Thrift protocol driver."""
15
Mark Slee808454e2007-06-20 21:51:57 +000016 VERSION_MASK = 0xffff0000
17 VERSION_1 = 0x80010000
18
19 def __init__(self, trans, strictRead=False, strictWrite=True):
Aditya Agarwal5c468192007-02-06 01:14:33 +000020 TProtocolBase.__init__(self, trans)
Mark Slee808454e2007-06-20 21:51:57 +000021 self.strictRead = strictRead
22 self.strictWrite = strictWrite
Mark Sleecde2b612006-09-03 21:13:07 +000023
Mark Slee4ac459f2006-10-25 21:39:01 +000024 def writeMessageBegin(self, name, type, seqid):
Mark Slee808454e2007-06-20 21:51:57 +000025 if self.strictWrite:
Mark Slee552410c2007-06-22 01:03:55 +000026 self.writeI32(TBinaryProtocol.VERSION_1 | type)
Mark Slee808454e2007-06-20 21:51:57 +000027 self.writeString(name)
28 self.writeI32(seqid)
29 else:
30 self.writeString(name)
31 self.writeByte(type)
32 self.writeI32(seqid)
Mark Slee4ac459f2006-10-25 21:39:01 +000033
34 def writeMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000035 pass
36
Mark Slee4ac459f2006-10-25 21:39:01 +000037 def writeStructBegin(self, name):
Mark Sleecde2b612006-09-03 21:13:07 +000038 pass
39
Mark Slee4ac459f2006-10-25 21:39:01 +000040 def writeStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000041 pass
42
Mark Slee4ac459f2006-10-25 21:39:01 +000043 def writeFieldBegin(self, name, type, id):
44 self.writeByte(type)
45 self.writeI16(id)
Mark Sleecde2b612006-09-03 21:13:07 +000046
Mark Slee4ac459f2006-10-25 21:39:01 +000047 def writeFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000048 pass
49
Mark Slee4ac459f2006-10-25 21:39:01 +000050 def writeFieldStop(self):
51 self.writeByte(TType.STOP);
Mark Sleecde2b612006-09-03 21:13:07 +000052
Mark Slee4ac459f2006-10-25 21:39:01 +000053 def writeMapBegin(self, ktype, vtype, size):
54 self.writeByte(ktype)
55 self.writeByte(vtype)
56 self.writeI32(size)
Mark Sleecde2b612006-09-03 21:13:07 +000057
Mark Slee4ac459f2006-10-25 21:39:01 +000058 def writeMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000059 pass
60
Mark Slee4ac459f2006-10-25 21:39:01 +000061 def writeListBegin(self, etype, size):
62 self.writeByte(etype)
63 self.writeI32(size)
Mark Sleecde2b612006-09-03 21:13:07 +000064
Mark Slee4ac459f2006-10-25 21:39:01 +000065 def writeListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000066 pass
67
Mark Slee4ac459f2006-10-25 21:39:01 +000068 def writeSetBegin(self, etype, size):
69 self.writeByte(etype)
70 self.writeI32(size)
Mark Sleecde2b612006-09-03 21:13:07 +000071
Mark Slee4ac459f2006-10-25 21:39:01 +000072 def writeSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000073 pass
74
Mark Slee4ac459f2006-10-25 21:39:01 +000075 def writeBool(self, bool):
Mark Sleecde2b612006-09-03 21:13:07 +000076 if bool:
Mark Slee4ac459f2006-10-25 21:39:01 +000077 self.writeByte(1)
Mark Sleecde2b612006-09-03 21:13:07 +000078 else:
Mark Slee4ac459f2006-10-25 21:39:01 +000079 self.writeByte(0)
David Reiss382fc302007-08-25 18:01:30 +000080
Mark Slee4ac459f2006-10-25 21:39:01 +000081 def writeByte(self, byte):
Mark Sleecde2b612006-09-03 21:13:07 +000082 buff = pack("!b", byte)
Aditya Agarwal5c468192007-02-06 01:14:33 +000083 self.trans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +000084
Mark Slee4ac459f2006-10-25 21:39:01 +000085 def writeI16(self, i16):
Mark Sleecde2b612006-09-03 21:13:07 +000086 buff = pack("!h", i16)
Aditya Agarwal5c468192007-02-06 01:14:33 +000087 self.trans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +000088
Mark Slee4ac459f2006-10-25 21:39:01 +000089 def writeI32(self, i32):
Mark Sleecde2b612006-09-03 21:13:07 +000090 buff = pack("!i", i32)
Aditya Agarwal5c468192007-02-06 01:14:33 +000091 self.trans.write(buff)
David Reiss382fc302007-08-25 18:01:30 +000092
Mark Slee4ac459f2006-10-25 21:39:01 +000093 def writeI64(self, i64):
Mark Sleec9676562006-09-05 17:34:52 +000094 buff = pack("!q", i64)
Aditya Agarwal5c468192007-02-06 01:14:33 +000095 self.trans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +000096
Mark Slee4ac459f2006-10-25 21:39:01 +000097 def writeDouble(self, dub):
Mark Sleec98d0502006-09-06 02:42:25 +000098 buff = pack("!d", dub)
Aditya Agarwal5c468192007-02-06 01:14:33 +000099 self.trans.write(buff)
Mark Sleec98d0502006-09-06 02:42:25 +0000100
Mark Slee4ac459f2006-10-25 21:39:01 +0000101 def writeString(self, str):
102 self.writeI32(len(str))
Aditya Agarwal5c468192007-02-06 01:14:33 +0000103 self.trans.write(str)
Mark Sleecde2b612006-09-03 21:13:07 +0000104
Mark Slee4ac459f2006-10-25 21:39:01 +0000105 def readMessageBegin(self):
Mark Slee808454e2007-06-20 21:51:57 +0000106 sz = self.readI32()
107 if sz < 0:
Mark Slee552410c2007-06-22 01:03:55 +0000108 version = sz & TBinaryProtocol.VERSION_MASK
109 if version != TBinaryProtocol.VERSION_1:
Mark Slee808454e2007-06-20 21:51:57 +0000110 raise TProtocolException(TProtocolException.BAD_VERSION, 'Bad version in readMessageBegin: %d' % (sz))
111 type = version & 0x000000ff
112 name = self.readString()
113 seqid = self.readI32()
114 else:
115 if self.strictRead:
116 raise TProtocolException(TProtocolException.BAD_VERSION, 'No protocol version header')
117 name = self.trans.readAll(sz)
118 type = self.readByte()
119 seqid = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000120 return (name, type, seqid)
121
Mark Slee4ac459f2006-10-25 21:39:01 +0000122 def readMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000123 pass
124
Mark Slee4ac459f2006-10-25 21:39:01 +0000125 def readStructBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000126 pass
127
Mark Slee4ac459f2006-10-25 21:39:01 +0000128 def readStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000129 pass
130
Mark Slee4ac459f2006-10-25 21:39:01 +0000131 def readFieldBegin(self):
132 type = self.readByte()
Mark Sleecde2b612006-09-03 21:13:07 +0000133 if type == TType.STOP:
134 return (None, type, 0)
Mark Slee4ac459f2006-10-25 21:39:01 +0000135 id = self.readI16()
Mark Sleecde2b612006-09-03 21:13:07 +0000136 return (None, type, id)
137
Mark Slee4ac459f2006-10-25 21:39:01 +0000138 def readFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000139 pass
140
Mark Slee4ac459f2006-10-25 21:39:01 +0000141 def readMapBegin(self):
142 ktype = self.readByte()
143 vtype = self.readByte()
144 size = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000145 return (ktype, vtype, size)
146
Mark Slee4ac459f2006-10-25 21:39:01 +0000147 def readMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000148 pass
149
Mark Slee4ac459f2006-10-25 21:39:01 +0000150 def readListBegin(self):
151 etype = self.readByte()
152 size = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000153 return (etype, size)
154
Mark Slee4ac459f2006-10-25 21:39:01 +0000155 def readListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000156 pass
157
Mark Slee4ac459f2006-10-25 21:39:01 +0000158 def readSetBegin(self):
159 etype = self.readByte()
160 size = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000161 return (etype, size)
162
Mark Slee4ac459f2006-10-25 21:39:01 +0000163 def readSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000164 pass
165
Mark Slee4ac459f2006-10-25 21:39:01 +0000166 def readBool(self):
167 byte = self.readByte()
Mark Sleecde2b612006-09-03 21:13:07 +0000168 if byte == 0:
169 return False
170 return True
171
Mark Slee4ac459f2006-10-25 21:39:01 +0000172 def readByte(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000173 buff = self.trans.readAll(1)
Mark Sleecde2b612006-09-03 21:13:07 +0000174 val, = unpack('!b', buff)
175 return val
176
Mark Slee4ac459f2006-10-25 21:39:01 +0000177 def readI16(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000178 buff = self.trans.readAll(2)
Mark Sleecde2b612006-09-03 21:13:07 +0000179 val, = unpack('!h', buff)
180 return val
181
Mark Slee4ac459f2006-10-25 21:39:01 +0000182 def readI32(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000183 buff = self.trans.readAll(4)
Mark Sleecde2b612006-09-03 21:13:07 +0000184 val, = unpack('!i', buff)
185 return val
186
Mark Slee4ac459f2006-10-25 21:39:01 +0000187 def readI64(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000188 buff = self.trans.readAll(8)
Mark Sleec9676562006-09-05 17:34:52 +0000189 val, = unpack('!q', buff)
Mark Sleecde2b612006-09-03 21:13:07 +0000190 return val
191
Mark Slee4ac459f2006-10-25 21:39:01 +0000192 def readDouble(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000193 buff = self.trans.readAll(8)
Mark Sleec98d0502006-09-06 02:42:25 +0000194 val, = unpack('!d', buff)
195 return val
196
Mark Slee4ac459f2006-10-25 21:39:01 +0000197 def readString(self):
198 len = self.readI32()
Aditya Agarwal5c468192007-02-06 01:14:33 +0000199 str = self.trans.readAll(len)
Mark Sleecde2b612006-09-03 21:13:07 +0000200 return str
Mark Slee4ac459f2006-10-25 21:39:01 +0000201
David Reiss382fc302007-08-25 18:01:30 +0000202
Mark Slee4ac459f2006-10-25 21:39:01 +0000203class TBinaryProtocolFactory:
Mark Slee808454e2007-06-20 21:51:57 +0000204 def __init__(self, strictRead=False, strictWrite=True):
205 self.strictRead = strictRead
206 self.strictWrite = strictWrite
207
Aditya Agarwal5c468192007-02-06 01:14:33 +0000208 def getProtocol(self, trans):
Mark Slee808454e2007-06-20 21:51:57 +0000209 prot = TBinaryProtocol(trans, self.strictRead, self.strictWrite)
Aditya Agarwal5c468192007-02-06 01:14:33 +0000210 return prot
David Reiss382fc302007-08-25 18:01:30 +0000211
212
213class TBinaryProtocolAccelerated(TBinaryProtocol):
214
215 """C-Accelerated version of TBinaryProtocol.
216
217 This class does not override any of TBinaryProtocol's methods,
218 but the generated code recognizes it directly and will call into
219 our C module to do the encoding, bypassing this object entirely.
220 We inherit from TBinaryProtocol so that the normal TBinaryProtocol
221 encoding can happen if the fastbinary module doesn't work for some
David Reiss5db3e922007-08-30 23:07:45 +0000222 reason. (TODO(dreiss): Make this happen sanely in more cases.)
David Reiss382fc302007-08-25 18:01:30 +0000223
224 In order to take advantage of the C module, just use
225 TBinaryProtocolAccelerated instead of TBinaryProtocol.
226
227 NOTE: This code was contributed by an external developer.
228 The internal Thrift team has reviewed and tested it,
229 but we cannot guarantee that it is production-ready.
230 Please feel free to report bugs and/or success stories
231 to the public mailing list.
232 """
233
234 pass
235
236
237class TBinaryProtocolAcceleratedFactory:
238 def getProtocol(self, trans):
239 return TBinaryProtocolAccelerated(trans)