Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 1 | #!/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 Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 9 | from TProtocol import * |
| 10 | from struct import pack, unpack |
| 11 | |
| 12 | class TBinaryProtocol(TProtocolBase): |
| 13 | |
| 14 | """Binary implementation of the Thrift protocol driver.""" |
| 15 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 16 | VERSION_MASK = 0xffff0000 |
| 17 | VERSION_1 = 0x80010000 |
| 18 | |
| 19 | def __init__(self, trans, strictRead=False, strictWrite=True): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 20 | TProtocolBase.__init__(self, trans) |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 21 | self.strictRead = strictRead |
| 22 | self.strictWrite = strictWrite |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 23 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 24 | def writeMessageBegin(self, name, type, seqid): |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 25 | if self.strictWrite: |
Mark Slee | 552410c | 2007-06-22 01:03:55 +0000 | [diff] [blame] | 26 | self.writeI32(TBinaryProtocol.VERSION_1 | type) |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 27 | self.writeString(name) |
| 28 | self.writeI32(seqid) |
| 29 | else: |
| 30 | self.writeString(name) |
| 31 | self.writeByte(type) |
| 32 | self.writeI32(seqid) |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 33 | |
| 34 | def writeMessageEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 35 | pass |
| 36 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 37 | def writeStructBegin(self, name): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 38 | pass |
| 39 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 40 | def writeStructEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 41 | pass |
| 42 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 43 | def writeFieldBegin(self, name, type, id): |
| 44 | self.writeByte(type) |
| 45 | self.writeI16(id) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 46 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 47 | def writeFieldEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 48 | pass |
| 49 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 50 | def writeFieldStop(self): |
| 51 | self.writeByte(TType.STOP); |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 52 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 53 | def writeMapBegin(self, ktype, vtype, size): |
| 54 | self.writeByte(ktype) |
| 55 | self.writeByte(vtype) |
| 56 | self.writeI32(size) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 57 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 58 | def writeMapEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 59 | pass |
| 60 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 61 | def writeListBegin(self, etype, size): |
| 62 | self.writeByte(etype) |
| 63 | self.writeI32(size) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 64 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 65 | def writeListEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 66 | pass |
| 67 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 68 | def writeSetBegin(self, etype, size): |
| 69 | self.writeByte(etype) |
| 70 | self.writeI32(size) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 71 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 72 | def writeSetEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 73 | pass |
| 74 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 75 | def writeBool(self, bool): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 76 | if bool: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 77 | self.writeByte(1) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 78 | else: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 79 | self.writeByte(0) |
David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 80 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 81 | def writeByte(self, byte): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 82 | buff = pack("!b", byte) |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 83 | self.trans.write(buff) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 84 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 85 | def writeI16(self, i16): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 86 | buff = pack("!h", i16) |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 87 | self.trans.write(buff) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 88 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 89 | def writeI32(self, i32): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 90 | buff = pack("!i", i32) |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 91 | self.trans.write(buff) |
David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 92 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 93 | def writeI64(self, i64): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 94 | buff = pack("!q", i64) |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 95 | self.trans.write(buff) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 96 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 97 | def writeDouble(self, dub): |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 98 | buff = pack("!d", dub) |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 99 | self.trans.write(buff) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 100 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 101 | def writeString(self, str): |
| 102 | self.writeI32(len(str)) |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 103 | self.trans.write(str) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 104 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 105 | def readMessageBegin(self): |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 106 | sz = self.readI32() |
| 107 | if sz < 0: |
Mark Slee | 552410c | 2007-06-22 01:03:55 +0000 | [diff] [blame] | 108 | version = sz & TBinaryProtocol.VERSION_MASK |
| 109 | if version != TBinaryProtocol.VERSION_1: |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 110 | 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 Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 120 | return (name, type, seqid) |
| 121 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 122 | def readMessageEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 123 | pass |
| 124 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 125 | def readStructBegin(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 126 | pass |
| 127 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 128 | def readStructEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 129 | pass |
| 130 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 131 | def readFieldBegin(self): |
| 132 | type = self.readByte() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 133 | if type == TType.STOP: |
| 134 | return (None, type, 0) |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 135 | id = self.readI16() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 136 | return (None, type, id) |
| 137 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 138 | def readFieldEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 139 | pass |
| 140 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 141 | def readMapBegin(self): |
| 142 | ktype = self.readByte() |
| 143 | vtype = self.readByte() |
| 144 | size = self.readI32() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 145 | return (ktype, vtype, size) |
| 146 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 147 | def readMapEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 148 | pass |
| 149 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 150 | def readListBegin(self): |
| 151 | etype = self.readByte() |
| 152 | size = self.readI32() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 153 | return (etype, size) |
| 154 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 155 | def readListEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 156 | pass |
| 157 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 158 | def readSetBegin(self): |
| 159 | etype = self.readByte() |
| 160 | size = self.readI32() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 161 | return (etype, size) |
| 162 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 163 | def readSetEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 164 | pass |
| 165 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 166 | def readBool(self): |
| 167 | byte = self.readByte() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 168 | if byte == 0: |
| 169 | return False |
| 170 | return True |
| 171 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 172 | def readByte(self): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 173 | buff = self.trans.readAll(1) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 174 | val, = unpack('!b', buff) |
| 175 | return val |
| 176 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 177 | def readI16(self): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 178 | buff = self.trans.readAll(2) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 179 | val, = unpack('!h', buff) |
| 180 | return val |
| 181 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 182 | def readI32(self): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 183 | buff = self.trans.readAll(4) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 184 | val, = unpack('!i', buff) |
| 185 | return val |
| 186 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 187 | def readI64(self): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 188 | buff = self.trans.readAll(8) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 189 | val, = unpack('!q', buff) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 190 | return val |
| 191 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 192 | def readDouble(self): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 193 | buff = self.trans.readAll(8) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 194 | val, = unpack('!d', buff) |
| 195 | return val |
| 196 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 197 | def readString(self): |
| 198 | len = self.readI32() |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 199 | str = self.trans.readAll(len) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 200 | return str |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 201 | |
David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 202 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 203 | class TBinaryProtocolFactory: |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 204 | def __init__(self, strictRead=False, strictWrite=True): |
| 205 | self.strictRead = strictRead |
| 206 | self.strictWrite = strictWrite |
| 207 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 208 | def getProtocol(self, trans): |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 209 | prot = TBinaryProtocol(trans, self.strictRead, self.strictWrite) |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 210 | return prot |
David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 211 | |
| 212 | |
| 213 | class 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 Reiss | 5db3e92 | 2007-08-30 23:07:45 +0000 | [diff] [blame^] | 222 | reason. (TODO(dreiss): Make this happen sanely in more cases.) |
David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 223 | |
| 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 | |
| 237 | class TBinaryProtocolAcceleratedFactory: |
| 238 | def getProtocol(self, trans): |
| 239 | return TBinaryProtocolAccelerated(trans) |