Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ public override void SetLength(long value)

public override void Write(byte[] buffer, int offset, int count)
{
var inputBuffer = _socketInput.IncomingStart(count);

Buffer.BlockCopy(buffer, offset, inputBuffer.Data.Array, inputBuffer.Data.Offset, count);

_socketInput.IncomingComplete(count, error: null);
_socketInput.IncomingData(buffer, offset, count);
}

public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken token)
Expand All @@ -90,7 +86,7 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
protected override void Dispose(bool disposing)
{
// Close _socketInput with a fake zero-length write that will result in a zero-length read.
_socketInput.IncomingComplete(0, error: null);
_socketInput.IncomingData(null, 0, 0);
base.Dispose(disposing);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public void ProducingComplete(MemoryPoolIterator2 end)

var returnBlock = block;
block = block.Next;
returnBlock.Pool?.Return(returnBlock);
returnBlock.Pool.Return(returnBlock);
}

_outputStream.Write(end.Block.Array, end.Block.Data.Offset, end.Index - end.Block.Data.Offset);
end.Block.Pool?.Return(end.Block);
end.Block.Pool.Return(end.Block);
}
}
}
6 changes: 3 additions & 3 deletions src/Microsoft.AspNet.Server.Kestrel/Http/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ private static Libuv.uv_buf_t AllocCallback(UvStreamHandle handle, int suggested

private Libuv.uv_buf_t OnAlloc(UvStreamHandle handle, int suggestedSize)
{
var result = _rawSocketInput.IncomingStart(2048);
var result = _rawSocketInput.IncomingRawStart();

return handle.Libuv.buf_init(
result.DataPtr,
result.Data.Count);
result.Pin + result.End,
result.Data.Offset + result.Data.Count - result.End);
}

private static void ReadCallback(UvStreamHandle handle, int status, object state)
Expand Down
43 changes: 25 additions & 18 deletions src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -472,33 +473,34 @@ public async Task WriteAsyncAwaited(ArraySegment<byte> data, CancellationToken c

private void WriteChunked(ArraySegment<byte> data)
{
SocketOutput.Write(BeginChunkBytes(data.Count), immediate: false);
var tenByteBuffer = ArrayPool<byte>.Shared.Rent(10);
SocketOutput.Write(BeginChunkBytes(data.Count, tenByteBuffer), immediate: false);
ArrayPool<byte>.Shared.Return(tenByteBuffer);
SocketOutput.Write(data, immediate: false);
SocketOutput.Write(_endChunkBytes, immediate: true);
}

private async Task WriteChunkedAsync(ArraySegment<byte> data, CancellationToken cancellationToken)
{
await SocketOutput.WriteAsync(BeginChunkBytes(data.Count), immediate: false, cancellationToken: cancellationToken);
var tenByteBuffer = ArrayPool<byte>.Shared.Rent(10);
await SocketOutput.WriteAsync(BeginChunkBytes(data.Count, tenByteBuffer), immediate: false, cancellationToken: cancellationToken);
ArrayPool<byte>.Shared.Return(tenByteBuffer);
await SocketOutput.WriteAsync(data, immediate: false, cancellationToken: cancellationToken);
await SocketOutput.WriteAsync(_endChunkBytes, immediate: true, cancellationToken: cancellationToken);
}

public static ArraySegment<byte> BeginChunkBytes(int dataCount)
public static ArraySegment<byte> BeginChunkBytes(int dataCount, byte[] tenByteBuffer)
{
var bytes = new byte[10]
{
_hex[((dataCount >> 0x1c) & 0x0f)],
_hex[((dataCount >> 0x18) & 0x0f)],
_hex[((dataCount >> 0x14) & 0x0f)],
_hex[((dataCount >> 0x10) & 0x0f)],
_hex[((dataCount >> 0x0c) & 0x0f)],
_hex[((dataCount >> 0x08) & 0x0f)],
_hex[((dataCount >> 0x04) & 0x0f)],
_hex[((dataCount >> 0x00) & 0x0f)],
(byte)'\r',
(byte)'\n',
};
tenByteBuffer[0] = _hex[((dataCount >> 0x1c) & 0x0f)];
tenByteBuffer[1] = _hex[((dataCount >> 0x18) & 0x0f)];
tenByteBuffer[2] = _hex[((dataCount >> 0x14) & 0x0f)];
tenByteBuffer[3] = _hex[((dataCount >> 0x10) & 0x0f)];
tenByteBuffer[4] = _hex[((dataCount >> 0x0c) & 0x0f)];
tenByteBuffer[5] = _hex[((dataCount >> 0x08) & 0x0f)];
tenByteBuffer[6] = _hex[((dataCount >> 0x04) & 0x0f)];
tenByteBuffer[7] = _hex[((dataCount >> 0x00) & 0x0f)];
tenByteBuffer[8] = (byte)'\r';
tenByteBuffer[9] = (byte)'\n';

// Determine the most-significant non-zero nibble
int total, shift;
Expand All @@ -510,7 +512,7 @@ public static ArraySegment<byte> BeginChunkBytes(int dataCount)
total |= (dataCount > 0x000f) ? 0x04 : 0x00;

var offset = 7 - (total >> 2);
return new ArraySegment<byte>(bytes, offset, 10 - offset);
return new ArraySegment<byte>(tenByteBuffer, offset, 10 - offset);
}

private void WriteChunkedResponseSuffix()
Expand Down Expand Up @@ -922,7 +924,8 @@ public static bool TakeMessageHeaders(SocketInput input, FrameRequestHeaders req
continue;
}

var name = beginName.GetArraySegment(endName);
byte[] rentedBuffer;
var name = beginName.GetArraySegment(endName, out rentedBuffer);
var value = beginValue.GetAsciiString(endValue);
if (wrapping)
{
Expand All @@ -931,6 +934,10 @@ public static bool TakeMessageHeaders(SocketInput input, FrameRequestHeaders req

consumed = scan;
requestHeaders.Append(name.Array, name.Offset, name.Count, value);
if (rentedBuffer != null)
{
ArrayPool<byte>.Shared.Return(rentedBuffer);
}
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Server.Kestrel/Http/ISocketOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface ISocketOutput

/// <summary>
/// Returns an iterator pointing to the tail of the response buffer. Response data can be appended
/// manually or by using <see cref="MemoryPoolIterator2.CopyFrom(ArraySegment{byte})"/>.
/// manually or by using <see cref="MemoryPoolIterator2.CopyFrom(ArraySegment{byte[], int, int})"/>.
/// Be careful to ensure all appended blocks are backed by a <see cref="MemoryPoolSlab2"/>.
/// </summary>
MemoryPoolIterator2 ProducingStart();
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.AspNet.Server.Kestrel/Http/ListenerPrimary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract public class ListenerPrimary : Listener

// this message is passed to write2 because it must be non-zero-length,
// but it has no other functional significance
private readonly ArraySegment<ArraySegment<byte>> _dummyMessage = new ArraySegment<ArraySegment<byte>>(new[] { new ArraySegment<byte>(new byte[] { 1, 2, 3, 4 }) });
private readonly ArraySegment<byte> _dummyMessage = new ArraySegment<byte>(new byte[] { 1, 2, 3, 4 });

protected ListenerPrimary(ServiceContext serviceContext) : base(serviceContext)
{
Expand Down Expand Up @@ -86,7 +86,7 @@ protected override void DispatchConnection(UvStreamHandle socket)
else
{
var dispatchPipe = _dispatchPipes[index];
var write = new UvWriteReq(Log);
var write = new UvWrite2Req(Log);
write.Init(Thread.Loop);
write.Write2(
dispatchPipe,
Expand Down
Loading