public void seeking_past_end_of_stream_throws_an_argumentexception() { BufferPoolStream stream = new BufferPoolStream(BufferPool); stream.Write(new byte[500], 0, 500); stream.Seek(501, SeekOrigin.Begin); }
public void position_is_incremented() { BufferPoolStream stream = new BufferPoolStream(BufferPool); stream.Write(new byte[500], 0, 500); Assert.AreEqual(500, stream.Position); }
public void seeking_past_end_of_stream_throws_an_argumentexception() { BufferPoolStream stream = new BufferPoolStream(BufferPool); stream.Write(new byte[500], 0, 500); Assert.Throws <ArgumentOutOfRangeException>(() => { stream.Seek(501, SeekOrigin.Begin); }); }
public void from_begin_sets_relative_to_beginning() { BufferPoolStream stream = new BufferPoolStream(BufferPool); stream.Write(new byte[500], 0, 500); stream.Seek(22, SeekOrigin.Begin); Assert.AreEqual(22, stream.Position); }
public void from_end_sets_relative_to_end() { BufferPoolStream stream = new BufferPoolStream(BufferPool); stream.Write(new byte[500], 0, 500); stream.Seek(-100, SeekOrigin.End); Assert.AreEqual(400, stream.Position); }
public void from_current_sets_relative_to_current() { BufferPoolStream stream = new BufferPoolStream(BufferPool); stream.Write(new byte[500], 0, 500); stream.Seek(-2, SeekOrigin.Current); stream.Seek(1, SeekOrigin.Current); Assert.AreEqual(499, stream.Position); }
public void position_is_incremented() { BufferPoolStream stream = new BufferPoolStream(BufferPool); stream.Write(new byte[500], 0, 500); stream.Seek(0, SeekOrigin.Begin); Assert.AreEqual(0, stream.Position); int read = stream.Read(new byte[50], 0, 50); Assert.AreEqual(50, stream.Position); }
public void position_is_incremented() { BufferPoolStream stream = new BufferPoolStream(BufferPool); stream.Write(new byte[500], 0, 500); stream.Seek(0, SeekOrigin.Begin); Assert.AreEqual(0, stream.Position); stream.Read(new byte[50], 0, 50); Assert.AreEqual(50, stream.Position); }
public void a_read_past_the_end_of_the_stream_returns_zero() { BufferPoolStream stream = new BufferPoolStream(BufferPool); stream.Write(new byte[500], 0, 500); stream.Position = 0; int read = stream.Read(new byte[500], 0, 500); Assert.AreEqual(500, read); read = stream.Read(new byte[500], 0, 500); Assert.AreEqual(0, read); }
public void reading_from_the_stream_with_StreamCopyTo_returns_all_data() { BufferPoolStream stream = new BufferPoolStream(BufferPool); int size = 20123; stream.Write(new byte[size], 0, size); stream.Position = 0; var destination = new MemoryStream(); stream.CopyTo(destination); Assert.AreEqual(destination.Length, size); }
public BufferPool ToBufferPool(byte[] message) { if (message == null) { throw new ArgumentNullException("message"); } var bufferPool = new BufferPool(_initialBuffers, _bufferManager); var stream = new BufferPoolStream(bufferPool); stream.Write(message, 0, message.Length); return(bufferPool); }
/// <summary> /// Handle an message /// </summary> /// <param name="context">Context unique for this handler instance</param><param name="message">Message to process</param> /// <remarks> /// All messages that can't be handled MUST be send up the chain using <see cref="M:Griffin.Networking.IPipelineHandlerContext.SendUpstream(Griffin.Networking.IPipelineMessage)"/>. /// </remarks> public void HandleUpstream(IPipelineHandlerContext context, IPipelineMessage message) { var headerMsg = message as ReceivedHeader; if (headerMsg != null) { HandleHeader(context, message, headerMsg); return; } if (_packetType == null) { context.SendUpstream(message); return; } var msg = message as Received; if (msg == null) { context.SendUpstream(message); return; } var bytesToRead = Math.Min(msg.BufferSlice.RemainingLength, _bytesLeft); _stream.Write(msg.BufferSlice.Buffer, msg.BufferSlice.Position, bytesToRead); msg.BufferSlice.Position += bytesToRead; if (_stream.Length == _header.ContentLength) { _stream.Position = 0; var packet = _decoder.Decode(_packetType, _stream); _stream.SetLength(0); context.SendUpstream(new ReceivedPacket(packet)); Clear(); return; } // There are remaining received bytes. context.SendUpstream(msg); }
/// <summary> /// Handle an message /// </summary> /// <param name="context">Context unique for this handler instance</param> /// <param name="message">Message to process</param> public void HandleUpstream(IPipelineHandlerContext context, IPipelineMessage message) { var headerMsg = message as ReceivedHeader; if (headerMsg != null) { _header = headerMsg.Header; if (_header.Length > 65535) { var error = new ErrorResponse("-9999", new RpcError { Code = RpcErrorCode.InvalidRequest, Message = "Support requests which is at most 655355 bytes.", }); context.SendDownstream(new SendResponse(error)); } return; } var received = message as Received; if (received != null) { var count = Math.Min(received.BufferSlice.RemainingLength, _header.Length); _stream.Write(received.BufferSlice.Buffer, received.BufferSlice.Position, count); received.BufferSlice.Position += count; if (_stream.Length == _header.Length) { _stream.Position = 0; var request = DeserializeRequest(_stream); context.SendUpstream(new ReceivedRequest(request)); } return; } context.SendUpstream(message); }