Kevin Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame] | 1 | /** |
| 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 |
| 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. |
Todd Lipcon | 53ae9f3 | 2009-12-07 00:42:38 +0000 | [diff] [blame] | 18 | * |
| 19 | * Contains some contributions under the Thrift Software License. |
| 20 | * Please see doc/old-thrift-license.txt in the Thrift distribution for |
| 21 | * details. |
Kevin Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame] | 22 | */ |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 23 | |
| 24 | using System; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 25 | using System.Text; |
| 26 | using Thrift.Transport; |
| 27 | |
| 28 | namespace Thrift.Protocol |
| 29 | { |
| 30 | public class TBinaryProtocol : TProtocol |
| 31 | { |
| 32 | protected const uint VERSION_MASK = 0xffff0000; |
| 33 | protected const uint VERSION_1 = 0x80010000; |
| 34 | |
| 35 | protected bool strictRead_ = false; |
| 36 | protected bool strictWrite_ = true; |
| 37 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 38 | #region BinaryProtocol Factory |
| 39 | /** |
| 40 | * Factory |
| 41 | */ |
| 42 | public class Factory : TProtocolFactory { |
| 43 | |
| 44 | protected bool strictRead_ = false; |
| 45 | protected bool strictWrite_ = true; |
| 46 | |
| 47 | public Factory() |
| 48 | :this(false, true) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | public Factory(bool strictRead, bool strictWrite) |
| 53 | { |
| 54 | strictRead_ = strictRead; |
| 55 | strictWrite_ = strictWrite; |
| 56 | } |
| 57 | |
| 58 | public TProtocol GetProtocol(TTransport trans) { |
| 59 | return new TBinaryProtocol(trans, strictRead_, strictWrite_); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | #endregion |
| 64 | |
| 65 | public TBinaryProtocol(TTransport trans) |
| 66 | : this(trans, false, true) |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | public TBinaryProtocol(TTransport trans, bool strictRead, bool strictWrite) |
| 71 | :base(trans) |
| 72 | { |
| 73 | strictRead_ = strictRead; |
| 74 | strictWrite_ = strictWrite; |
| 75 | } |
| 76 | |
| 77 | #region Write Methods |
| 78 | |
| 79 | public override void WriteMessageBegin(TMessage message) |
| 80 | { |
| 81 | if (strictWrite_) |
| 82 | { |
| 83 | uint version = VERSION_1 | (uint)(message.Type); |
| 84 | WriteI32((int)version); |
| 85 | WriteString(message.Name); |
| 86 | WriteI32(message.SeqID); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | WriteString(message.Name); |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 91 | WriteByte((sbyte)message.Type); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 92 | WriteI32(message.SeqID); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public override void WriteMessageEnd() |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | public override void WriteStructBegin(TStruct struc) |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | public override void WriteStructEnd() |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | public override void WriteFieldBegin(TField field) |
| 109 | { |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 110 | WriteByte((sbyte)field.Type); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 111 | WriteI16(field.ID); |
| 112 | } |
| 113 | |
| 114 | public override void WriteFieldEnd() |
| 115 | { |
| 116 | } |
| 117 | |
| 118 | public override void WriteFieldStop() |
| 119 | { |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 120 | WriteByte((sbyte)TType.Stop); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | public override void WriteMapBegin(TMap map) |
| 124 | { |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 125 | WriteByte((sbyte)map.KeyType); |
| 126 | WriteByte((sbyte)map.ValueType); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 127 | WriteI32(map.Count); |
| 128 | } |
| 129 | |
| 130 | public override void WriteMapEnd() |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | public override void WriteListBegin(TList list) |
| 135 | { |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 136 | WriteByte((sbyte)list.ElementType); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 137 | WriteI32(list.Count); |
| 138 | } |
| 139 | |
| 140 | public override void WriteListEnd() |
| 141 | { |
| 142 | } |
| 143 | |
| 144 | public override void WriteSetBegin(TSet set) |
| 145 | { |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 146 | WriteByte((sbyte)set.ElementType); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 147 | WriteI32(set.Count); |
| 148 | } |
| 149 | |
| 150 | public override void WriteSetEnd() |
| 151 | { |
| 152 | } |
| 153 | |
| 154 | public override void WriteBool(bool b) |
| 155 | { |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 156 | WriteByte(b ? (sbyte)1 : (sbyte)0); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | private byte[] bout = new byte[1]; |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 160 | public override void WriteByte(sbyte b) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 161 | { |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 162 | bout[0] = (byte)b; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 163 | trans.Write(bout, 0, 1); |
| 164 | } |
| 165 | |
| 166 | private byte[] i16out = new byte[2]; |
| 167 | public override void WriteI16(short s) |
| 168 | { |
| 169 | i16out[0] = (byte)(0xff & (s >> 8)); |
| 170 | i16out[1] = (byte)(0xff & s); |
| 171 | trans.Write(i16out, 0, 2); |
| 172 | } |
| 173 | |
| 174 | private byte[] i32out = new byte[4]; |
| 175 | public override void WriteI32(int i32) |
| 176 | { |
| 177 | i32out[0] = (byte)(0xff & (i32 >> 24)); |
| 178 | i32out[1] = (byte)(0xff & (i32 >> 16)); |
| 179 | i32out[2] = (byte)(0xff & (i32 >> 8)); |
| 180 | i32out[3] = (byte)(0xff & i32); |
| 181 | trans.Write(i32out, 0, 4); |
| 182 | } |
| 183 | |
| 184 | private byte[] i64out = new byte[8]; |
| 185 | public override void WriteI64(long i64) |
| 186 | { |
| 187 | i64out[0] = (byte)(0xff & (i64 >> 56)); |
| 188 | i64out[1] = (byte)(0xff & (i64 >> 48)); |
| 189 | i64out[2] = (byte)(0xff & (i64 >> 40)); |
| 190 | i64out[3] = (byte)(0xff & (i64 >> 32)); |
| 191 | i64out[4] = (byte)(0xff & (i64 >> 24)); |
| 192 | i64out[5] = (byte)(0xff & (i64 >> 16)); |
| 193 | i64out[6] = (byte)(0xff & (i64 >> 8)); |
| 194 | i64out[7] = (byte)(0xff & i64); |
| 195 | trans.Write(i64out, 0, 8); |
| 196 | } |
| 197 | |
| 198 | public override void WriteDouble(double d) |
| 199 | { |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 200 | #if !SILVERLIGHT |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 201 | WriteI64(BitConverter.DoubleToInt64Bits(d)); |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 202 | #else |
| 203 | var bytes = BitConverter.GetBytes(d); |
| 204 | WriteI64(BitConverter.ToInt64(bytes, 0)); |
| 205 | #endif |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 206 | } |
| 207 | |
David Reiss | cba5727 | 2008-02-06 22:09:44 +0000 | [diff] [blame] | 208 | public override void WriteBinary(byte[] b) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 209 | { |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 210 | WriteI32(b.Length); |
| 211 | trans.Write(b, 0, b.Length); |
| 212 | } |
| 213 | |
| 214 | #endregion |
| 215 | |
| 216 | #region ReadMethods |
| 217 | |
| 218 | public override TMessage ReadMessageBegin() |
| 219 | { |
| 220 | TMessage message = new TMessage(); |
| 221 | int size = ReadI32(); |
| 222 | if (size < 0) |
| 223 | { |
| 224 | uint version = (uint)size & VERSION_MASK; |
| 225 | if (version != VERSION_1) |
| 226 | { |
| 227 | throw new TProtocolException(TProtocolException.BAD_VERSION, "Bad version in ReadMessageBegin: " + version); |
| 228 | } |
| 229 | message.Type = (TMessageType)(size & 0x000000ff); |
| 230 | message.Name = ReadString(); |
| 231 | message.SeqID = ReadI32(); |
| 232 | } |
| 233 | else |
| 234 | { |
| 235 | if (strictRead_) |
| 236 | { |
| 237 | throw new TProtocolException(TProtocolException.BAD_VERSION, "Missing version in readMessageBegin, old client?"); |
| 238 | } |
| 239 | message.Name = ReadStringBody(size); |
| 240 | message.Type = (TMessageType)ReadByte(); |
| 241 | message.SeqID = ReadI32(); |
| 242 | } |
| 243 | return message; |
| 244 | } |
| 245 | |
| 246 | public override void ReadMessageEnd() |
| 247 | { |
| 248 | } |
| 249 | |
| 250 | public override TStruct ReadStructBegin() |
| 251 | { |
| 252 | return new TStruct(); |
| 253 | } |
| 254 | |
| 255 | public override void ReadStructEnd() |
| 256 | { |
| 257 | } |
| 258 | |
| 259 | public override TField ReadFieldBegin() |
| 260 | { |
| 261 | TField field = new TField(); |
| 262 | field.Type = (TType)ReadByte(); |
| 263 | |
| 264 | if (field.Type != TType.Stop) |
| 265 | { |
| 266 | field.ID = ReadI16(); |
| 267 | } |
| 268 | |
| 269 | return field; |
| 270 | } |
| 271 | |
| 272 | public override void ReadFieldEnd() |
| 273 | { |
| 274 | } |
| 275 | |
| 276 | public override TMap ReadMapBegin() |
| 277 | { |
| 278 | TMap map = new TMap(); |
| 279 | map.KeyType = (TType)ReadByte(); |
| 280 | map.ValueType = (TType)ReadByte(); |
| 281 | map.Count = ReadI32(); |
| 282 | |
| 283 | return map; |
| 284 | } |
| 285 | |
| 286 | public override void ReadMapEnd() |
| 287 | { |
| 288 | } |
| 289 | |
| 290 | public override TList ReadListBegin() |
| 291 | { |
| 292 | TList list = new TList(); |
| 293 | list.ElementType = (TType)ReadByte(); |
| 294 | list.Count = ReadI32(); |
| 295 | |
| 296 | return list; |
| 297 | } |
| 298 | |
| 299 | public override void ReadListEnd() |
| 300 | { |
| 301 | } |
| 302 | |
| 303 | public override TSet ReadSetBegin() |
| 304 | { |
| 305 | TSet set = new TSet(); |
| 306 | set.ElementType = (TType)ReadByte(); |
| 307 | set.Count = ReadI32(); |
| 308 | |
| 309 | return set; |
| 310 | } |
| 311 | |
| 312 | public override void ReadSetEnd() |
| 313 | { |
| 314 | } |
| 315 | |
| 316 | public override bool ReadBool() |
| 317 | { |
| 318 | return ReadByte() == 1; |
| 319 | } |
| 320 | |
| 321 | private byte[] bin = new byte[1]; |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 322 | public override sbyte ReadByte() |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 323 | { |
| 324 | ReadAll(bin, 0, 1); |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 325 | return (sbyte)bin[0]; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | private byte[] i16in = new byte[2]; |
| 329 | public override short ReadI16() |
| 330 | { |
| 331 | ReadAll(i16in, 0, 2); |
| 332 | return (short)(((i16in[0] & 0xff) << 8) | ((i16in[1] & 0xff))); |
| 333 | } |
| 334 | |
| 335 | private byte[] i32in = new byte[4]; |
| 336 | public override int ReadI32() |
| 337 | { |
| 338 | ReadAll(i32in, 0, 4); |
| 339 | return (int)(((i32in[0] & 0xff) << 24) | ((i32in[1] & 0xff) << 16) | ((i32in[2] & 0xff) << 8) | ((i32in[3] & 0xff))); |
Jens Geyer | 1c99e70 | 2014-03-17 22:50:39 +0200 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | #pragma warning disable 675 |
| 343 | |
Jens Geyer | ee353e6 | 2013-07-06 09:28:49 +0200 | [diff] [blame] | 344 | private byte[] i64in = new byte[8]; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 345 | public override long ReadI64() |
| 346 | { |
Jens Geyer | 1c99e70 | 2014-03-17 22:50:39 +0200 | [diff] [blame] | 347 | ReadAll(i64in, 0, 8); |
Jake Farrell | 98f9377 | 2012-10-20 16:47:51 +0000 | [diff] [blame] | 348 | unchecked { |
| 349 | return (long)( |
| 350 | ((long)(i64in[0] & 0xff) << 56) | |
| 351 | ((long)(i64in[1] & 0xff) << 48) | |
| 352 | ((long)(i64in[2] & 0xff) << 40) | |
| 353 | ((long)(i64in[3] & 0xff) << 32) | |
| 354 | ((long)(i64in[4] & 0xff) << 24) | |
| 355 | ((long)(i64in[5] & 0xff) << 16) | |
| 356 | ((long)(i64in[6] & 0xff) << 8) | |
| 357 | ((long)(i64in[7] & 0xff))); |
| 358 | } |
Jens Geyer | 1c99e70 | 2014-03-17 22:50:39 +0200 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | #pragma warning restore 675 |
| 362 | |
Jens Geyer | ee353e6 | 2013-07-06 09:28:49 +0200 | [diff] [blame] | 363 | public override double ReadDouble() |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 364 | { |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 365 | #if !SILVERLIGHT |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 366 | return BitConverter.Int64BitsToDouble(ReadI64()); |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 367 | #else |
| 368 | var value = ReadI64(); |
| 369 | var bytes = BitConverter.GetBytes(value); |
| 370 | return BitConverter.ToDouble(bytes, 0); |
| 371 | #endif |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 372 | } |
| 373 | |
David Reiss | cba5727 | 2008-02-06 22:09:44 +0000 | [diff] [blame] | 374 | public override byte[] ReadBinary() |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 375 | { |
| 376 | int size = ReadI32(); |
David Reiss | cba5727 | 2008-02-06 22:09:44 +0000 | [diff] [blame] | 377 | byte[] buf = new byte[size]; |
| 378 | trans.ReadAll(buf, 0, size); |
| 379 | return buf; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 380 | } |
David Reiss | cba5727 | 2008-02-06 22:09:44 +0000 | [diff] [blame] | 381 | private string ReadStringBody(int size) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 382 | { |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 383 | byte[] buf = new byte[size]; |
| 384 | trans.ReadAll(buf, 0, size); |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 385 | return Encoding.UTF8.GetString(buf, 0, buf.Length); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | private int ReadAll(byte[] buf, int off, int len) |
| 389 | { |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 390 | return trans.ReadAll(buf, off, len); |
| 391 | } |
| 392 | |
| 393 | #endregion |
| 394 | } |
| 395 | } |