public TcpChannel(IPipeline pipeline, BufferPool pool) { _pipeline = pipeline; Pipeline.SetChannel(this); _readBuffer = pool.PopSlice(); _stream = new PeekableMemoryStream(_readBuffer.Buffer, _readBuffer.StartOffset, _readBuffer.Capacity); }
/// <summary> /// Initializes a new instance of the <see cref="BodyDecoder"/> class. /// </summary> /// <param name="decoderService">The decoder service.</param> /// <param name="bufferSize">Buffer size of each buffer in the pool. Read the remarks at <see cref="BodyDecoder"/></param> /// <param name="sizeLimit">Maximum size of the body in bytes. Larger content will generate a <see cref="HttpStatusCode.RequestEntityTooLarge"/> response which will /// be sent back to the client.</param> public BodyDecoder(IBodyDecoder decoderService, int bufferSize, int sizeLimit) { if (decoderService == null) throw new ArgumentNullException("decoderService"); _decoderService = decoderService; _bufferSize = bufferSize; _sizeLimit = sizeLimit; _bufferPool = new BufferPool(_bufferSize, 10, 1000); }
/// <summary> /// Initializes a new instance of the <see cref="BufferPoolStream"/> class. /// </summary> /// <param name="pool">The pool.</param> /// <param name="slice">The slice.</param> public BufferPoolStream(BufferPool pool, BufferSlice slice) : base(slice.Buffer, slice.StartOffset, slice.Capacity, true, true) { _slize = slice; _pool = pool; SetLength(slice.Count); Position = slice.Position; }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> /// <filterpriority>2</filterpriority> public void Dispose() { if (_pool == null) return; _pool.Push(Buffer); _pool = null; }
/// <summary> /// Initializes a new instance of the <see cref="ContentDecoder"/> class. /// </summary> /// <param name="mapper">Used to determine which packets to deserialize.</param> /// <param name="bufferPool">Used to store packet bytes before deserialization.</param> /// <param name="decoder">Used to deserialize the backet bytes..</param> public ContentDecoder(ContentMapper mapper, BufferPool bufferPool, IContentDecoder decoder) { if (mapper == null) throw new ArgumentNullException("mapper"); if (bufferPool == null) throw new ArgumentNullException("bufferPool"); if (decoder == null) throw new ArgumentNullException("decoder"); _mapper = mapper; _bufferPool = bufferPool; _decoder = decoder; }
/// <summary> /// Initializes a new instance of the <see cref="TcpChannel"/> class. /// </summary> /// <param name="pipeline">The pipeline used to send messages upstream.</param> /// <param name="pool">Buffer pool.</param> public TcpChannel(IPipeline pipeline, BufferPool pool) { _pipeline = pipeline; _pool = pool; Pipeline.SetChannel(this); if (pool == null) _readBuffer = new BufferSlice(new byte[65535], 0, 65535, 0); else _readBuffer = pool.PopSlice(); _stream = new PeekableMemoryStream(_readBuffer.Buffer, _readBuffer.StartOffset, _readBuffer.Capacity); }
public void FillBuffers() { LogManager.Assign(new SimpleSystemDebugLogManager()); var pool = new BufferPool(100, 2, 2); _target = new TcpServerClientChannel(_pipeline, pool); _target.AssignSocket(_sockets.Client); _target.StartChannel(); var sb = new StringBuilder(); for (int i = 0; i < 1000; i++) { sb.Append(i.ToString()); sb.Append("|"); if ((i % 25) == 0) sb.AppendLine(); } var sendBuffer = Encoding.ASCII.GetBytes(sb.ToString()); var sent = 0; while (sent < sendBuffer.Length) { sent += _sockets.Server.Send(sendBuffer, sent, sendBuffer.Length - sent, SocketFlags.None); } Assert.True(_pipeline.WaitOnUpstream<Received>(TimeSpan.FromMilliseconds(1000)), "Incoming message timeout"); var receivedBuffer = new byte[sendBuffer.Length]; var stream= new MemoryStream(receivedBuffer); foreach (var msg in _pipeline.UpstreamMessages) { var m = msg as Received; if (m == null) continue; stream.Write(m.BufferSlice.Buffer, m.BufferSlice.CurrentOffset, m.BufferSlice.RemainingLength); } stream.Flush(); Assert.Equal(sendBuffer.Length, receivedBuffer.Length); for (int i = 0; i < sendBuffer.Length; i++) { if (sendBuffer[i] != receivedBuffer[i]) throw new InvalidOperationException("First difference at " + i); } }
public TcpServerClientChannel(IPipeline pipeline, BufferPool pool) : base(pipeline, pool) { }
public BufferPoolStream(BufferPool pool) : base(pool.Pop()) { _pool = pool; }
/// <summary> /// Initializes a new instance of the <see cref="ReusableBufferSlice"/> class. /// </summary> /// <param name="pool">Pool that the buffer should be released to.</param> /// <param name="buffer">The buffer.</param> /// <param name="startOffset">Offset in buffer where the slice starts.</param> /// <param name="capacity">Number of bytes allocated for this slice.</param> /// <param name="count">Number of bytes written to the buffer (if any)</param> public ReusableBufferSlice(BufferPool pool, byte[] buffer, int startOffset, int capacity, int count) : base(buffer, startOffset, capacity, count) { _pool = pool; }