/// <summary> /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() and /// </summary> private static void AssertReadVarint(byte[] data, ulong value) { CodedInputStream input = CodedInputStream.CreateInstance(data); Assert.AreEqual((uint)value, input.ReadRawVarint32()); input = CodedInputStream.CreateInstance(data); Assert.AreEqual(value, input.ReadRawVarint64()); Assert.IsTrue(input.IsAtEnd); // Try different block sizes. for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2) { input = CodedInputStream.CreateInstance(new SmallBlockInputStream(data, bufferSize)); Assert.AreEqual((uint)value, input.ReadRawVarint32()); input = CodedInputStream.CreateInstance(new SmallBlockInputStream(data, bufferSize)); Assert.AreEqual(value, input.ReadRawVarint64()); Assert.IsTrue(input.IsAtEnd); } // Try reading directly from a MemoryStream. We want to verify that it // doesn't read past the end of the input, so write an extra byte - this // lets us test the position at the end. MemoryStream memoryStream = new MemoryStream(); memoryStream.Write(data, 0, data.Length); memoryStream.WriteByte(0); memoryStream.Position = 0; Assert.AreEqual((uint)value, CodedInputStream.ReadRawVarint32(memoryStream)); Assert.AreEqual(data.Length, memoryStream.Position); }
/// <summary> /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() and /// expects them to fail with an InvalidProtocolBufferException whose /// description matches the given one. /// </summary> private static void AssertReadVarintFailure(InvalidProtocolBufferException expected, byte[] data) { CodedInputStream input = CodedInputStream.CreateInstance(data); try { input.ReadRawVarint32(); Assert.Fail("Should have thrown an exception."); } catch (InvalidProtocolBufferException e) { Assert.AreEqual(expected.Message, e.Message); } input = CodedInputStream.CreateInstance(data); try { input.ReadRawVarint64(); Assert.Fail("Should have thrown an exception."); } catch (InvalidProtocolBufferException e) { Assert.AreEqual(expected.Message, e.Message); } // Make sure we get the same error when reading directly from a Stream. try { CodedInputStream.ReadRawVarint32(new MemoryStream(data)); Assert.Fail("Should have thrown an exception."); } catch (InvalidProtocolBufferException e) { Assert.AreEqual(expected.Message, e.Message); } }
/// <summary> /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() and /// </summary> private static void AssertReadVarint(byte[] data, ulong value) { CodedInputStream input = CodedInputStream.CreateInstance(data); Assert.AreEqual((uint)value, input.ReadRawVarint32()); input = CodedInputStream.CreateInstance(data); Assert.AreEqual(value, input.ReadRawVarint64()); // Try different block sizes. for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2) { input = CodedInputStream.CreateInstance(new SmallBlockInputStream(data, bufferSize)); Assert.AreEqual((uint)value, input.ReadRawVarint32()); input = CodedInputStream.CreateInstance(new SmallBlockInputStream(data, bufferSize)); Assert.AreEqual(value, input.ReadRawVarint64()); } }
public Header(CodedInputStream stream) { var serviceId = stream.ReadRawByte(); var methodId = stream.ReadRawVarint32(); var requestId = stream.ReadRawByte() | (stream.ReadRawByte() << 8); if (serviceId != 0xfe) this.Unknown = stream.ReadRawVarint64(); var payloadLength = stream.ReadRawVarint32(); this.SetData(serviceId, methodId, requestId, payloadLength); }
public PacketIn(CodedInputStream stream) { this._stream = stream; this.ServiceId = stream.ReadRawByte(); this.MethodId = stream.ReadRawVarint32(); this.RequestId = stream.ReadRawByte() | (stream.ReadRawByte() << 8); this.ObjectId = 0UL; if (this.ServiceId != 0xfe) this.ObjectId = stream.ReadRawVarint64(); }
public BNetHeader(CodedInputStream stream) { var serviceId = stream.ReadRawByte(); var methodId = stream.ReadRawVarint32(); var requestId = stream.ReadRawByte() | (stream.ReadRawByte() << 8); var objectId = 0UL; if (serviceId != 0xfe) objectId = stream.ReadRawVarint64(); var payloadLength = stream.ReadRawVarint32(); this.SetData(serviceId, methodId, requestId, payloadLength, objectId); }
public ClientPacket(CodedInputStream stream) { m_stream = stream; // Read header m_service = m_stream.ReadRawByte(); m_method = m_stream.ReadInt32(); m_requestId = m_stream.ReadInt16(); m_listenerId = 0; Console.WriteLine("IN: service {0}, method {1:X}, requestId {2}, listenerId {3}", m_service, m_method, m_requestId, m_listenerId); if (m_service != 0xFE) m_listenerId = m_stream.ReadRawVarint64(); }
/// <summary> /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() and /// expects them to fail with an InvalidProtocolBufferException whose /// description matches the given one. /// </summary> private static void AssertReadVarintFailure(InvalidProtocolBufferException expected, byte[] data) { CodedInputStream input = CodedInputStream.CreateInstance(data); var exception = Assert.Throws <InvalidProtocolBufferException>(() => input.ReadRawVarint32()); Assert.AreEqual(expected.Message, exception.Message); input = CodedInputStream.CreateInstance(data); exception = Assert.Throws <InvalidProtocolBufferException>(() => input.ReadRawVarint64()); Assert.AreEqual(expected.Message, exception.Message); // Make sure we get the same error when reading directly from a Stream. exception = Assert.Throws <InvalidProtocolBufferException>(() => CodedInputStream.ReadRawVarint32(new MemoryStream(data))); Assert.AreEqual(expected.Message, exception.Message); }
/// <summary> /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() and /// expects them to fail with an InvalidProtocolBufferException whose /// description matches the given one. /// </summary> private void AssertReadVarintFailure(InvalidProtocolBufferException expected, byte[] data) { CodedInputStream input = CodedInputStream.CreateInstance(data); try { input.ReadRawVarint32(); Assert.Fail("Should have thrown an exception."); } catch (InvalidProtocolBufferException e) { Assert.AreEqual(expected.Message, e.Message); } input = CodedInputStream.CreateInstance(data); try { input.ReadRawVarint64(); Assert.Fail("Should have thrown an exception."); } catch (InvalidProtocolBufferException e) { Assert.AreEqual(expected.Message, e.Message); } }