blob: 860f4611946746998b6ef1059363f2406fcf5efb [file] [log] [blame]
Mark Sleecde2b612006-09-03 21:13:07 +00001from TProtocol import *
2from struct import pack, unpack
3
4class TBinaryProtocol(TProtocolBase):
5
6 """Binary implementation of the Thrift protocol driver."""
7
8 def writeMessageBegin(self, otrans, name, type, seqid):
9 self.writeString(otrans, name)
10 self.writeByte(otrans, type)
11 self.writeI32(otrans, seqid)
12
13 def writeMessageEnd(self, otrans):
14 pass
15
16 def writeStructBegin(self, otrans, name):
17 pass
18
19 def writeStructEnd(self, otrans):
20 pass
21
22 def writeFieldBegin(self, otrans, name, type, id):
23 self.writeByte(otrans, type)
24 self.writeI16(otrans, id)
25
26 def writeFieldEnd(self, otrans):
27 pass
28
29 def writeFieldStop(self, otrans):
30 self.writeByte(otrans, TType.STOP);
31
32 def writeMapBegin(self, otrans, ktype, vtype, size):
33 self.writeByte(otrans, ktype)
34 self.writeByte(otrans, vtype)
35 self.writeI32(otrans, size)
36
37 def writeMapEnd(self, otrans):
38 pass
39
40 def writeListBegin(self, otrans, etype, size):
41 self.writeByte(otrans, etype)
42 self.writeI32(otrans, size)
43
44 def writeListEnd(self, otrans):
45 pass
46
47 def writeSetBegin(self, otrans, etype, size):
48 self.writeByte(otrans, etype)
49 self.writeI32(otrans, size)
50
51 def writeSetEnd(self, otrans):
52 pass
53
54 def writeBool(self, otrans, bool):
55 if bool:
56 self.writeByte(otrans, 1)
57 else:
58 self.writeByte(otrans, 0)
59
60 def writeByte(self, otrans, byte):
61 buff = pack("!b", byte)
62 otrans.write(buff)
63
64 def writeI16(self, otrans, i16):
65 buff = pack("!h", i16)
66 otrans.write(buff)
67
68 def writeI32(self, otrans, i32):
69 buff = pack("!i", i32)
70 otrans.write(buff)
71
72 def writeI64(self, otrans, i64):
Mark Sleec9676562006-09-05 17:34:52 +000073 buff = pack("!q", i64)
Mark Sleecde2b612006-09-03 21:13:07 +000074 otrans.write(buff)
75
76 def writeString(self, otrans, str):
77 self.writeI32(otrans, len(str))
78 otrans.write(str)
79
80 def readMessageBegin(self, itrans):
81 name = self.readString(itrans)
82 type = self.readByte(itrans)
83 seqid = self.readI32(itrans)
84 return (name, type, seqid)
85
86 def readMessageEnd(self, itrans):
87 pass
88
89 def readStructBegin(self, itrans):
90 pass
91
92 def readStructEnd(self, itrans):
93 pass
94
95 def readFieldBegin(self, itrans):
96 type = self.readByte(itrans)
97 if type == TType.STOP:
98 return (None, type, 0)
99 id = self.readI16(itrans)
100 return (None, type, id)
101
102 def readFieldEnd(self, itrans):
103 pass
104
105 def readMapBegin(self, itrans):
106 ktype = self.readByte(itrans)
107 vtype = self.readByte(itrans)
108 size = self.readI32(itrans)
109 return (ktype, vtype, size)
110
111 def readMapEnd(self, itrans):
112 pass
113
114 def readListBegin(self, itrans):
115 etype = self.readByte(itrans)
116 size = self.readI32(itrans)
117 return (etype, size)
118
119 def readListEnd(self, itrans):
120 pass
121
122 def readSetBegin(self, itrans):
123 etype = self.readByte(itrans)
124 size = self.readI32(itrans)
125 return (etype, size)
126
127 def readSetEnd(self, itrans):
128 pass
129
130 def readBool(self, itrans):
131 byte = self.readByte(itrans)
132 if byte == 0:
133 return False
134 return True
135
136 def readByte(self, itrans):
137 buff = itrans.readAll(1)
138 val, = unpack('!b', buff)
139 return val
140
141 def readI16(self, itrans):
142 buff = itrans.readAll(2)
143 val, = unpack('!h', buff)
144 return val
145
146 def readI32(self, itrans):
147 buff = itrans.readAll(4)
148 val, = unpack('!i', buff)
149 return val
150
151 def readI64(self, itrans):
152 buff = itrans.readAll(8)
Mark Sleec9676562006-09-05 17:34:52 +0000153 val, = unpack('!q', buff)
Mark Sleecde2b612006-09-03 21:13:07 +0000154 return val
155
156 def readString(self, itrans):
157 len = self.readI32(itrans)
158 str = itrans.readAll(len)
159 return str