Refactor FrameTests and rename SocketInput SocketOutput properties (#1229)

This commit is contained in:
Pavel Krymets 2016-11-22 16:36:36 -08:00 committed by GitHub
parent 2bf5a966cd
commit e55c62444b
10 changed files with 330 additions and 1120 deletions

View File

@ -56,8 +56,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
_bufferSizeControl = new BufferSizeControl(ServerOptions.Limits.MaxRequestBufferSize.Value, this, Thread);
}
SocketInput = new SocketInput(Thread.Memory, ThreadPool, _bufferSizeControl);
SocketOutput = new SocketOutput(Thread, _socket, this, ConnectionId, Log, ThreadPool);
Input = new SocketInput(Thread.Memory, ThreadPool, _bufferSizeControl);
Output = new SocketOutput(Thread, _socket, this, ConnectionId, Log, ThreadPool);
var tcpHandle = _socket as UvTcpHandle;
if (tcpHandle != null)
@ -95,7 +95,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
}
else
{
_libuvStream = new LibuvStream(SocketInput, SocketOutput);
_libuvStream = new LibuvStream(Input, Output);
_filterContext = new ConnectionFilterContext
{
@ -136,7 +136,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
public Task StopAsync()
{
_frame.StopAsync();
_frame.SocketInput.CompleteAwaiting();
_frame.Input.CompleteAwaiting();
return _socketClosedTcs.Task;
}
@ -169,7 +169,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
}
}, this);
SocketInput.Dispose();
Input.Dispose();
_socketClosedTcs.TrySetResult(null);
}
@ -197,8 +197,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
{
_filteredStreamAdapter = new FilteredStreamAdapter(ConnectionId, _filterContext.Connection, Thread.Memory, Log, ThreadPool, _bufferSizeControl);
_frame.SocketInput = _filteredStreamAdapter.SocketInput;
_frame.SocketOutput = _filteredStreamAdapter.SocketOutput;
_frame.Input = _filteredStreamAdapter.SocketInput;
_frame.Output = _filteredStreamAdapter.SocketOutput;
_readInputTask = _filteredStreamAdapter.ReadInputAsync();
}
@ -214,7 +214,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
private Libuv.uv_buf_t OnAlloc(UvStreamHandle handle, int suggestedSize)
{
var result = SocketInput.IncomingStart();
var result = Input.IncomingStart();
return handle.Libuv.buf_init(
result.DataArrayPtr + result.End,
@ -234,7 +234,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
// there is no data to be read right now.
// See the note at http://docs.libuv.org/en/v1.x/stream.html#c.uv_read_cb.
// We need to clean up whatever was allocated by OnAlloc.
SocketInput.IncomingDeferred();
Input.IncomingDeferred();
return;
}
@ -276,7 +276,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
error = new IOException(uvError.Message, uvError);
}
SocketInput.IncomingComplete(readCount, error);
Input.IncomingComplete(readCount, error);
if (errorDone)
{
@ -302,7 +302,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
// ReadStart() can throw a UvException in some cases (e.g. socket is no longer connected).
// This should be treated the same as OnRead() seeing a "normalDone" condition.
Log.ConnectionReadFin(ConnectionId);
SocketInput.IncomingComplete(0, null);
Input.IncomingComplete(0, null);
}
}
@ -316,7 +316,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
case ProduceEndType.SocketShutdown:
case ProduceEndType.SocketDisconnect:
Log.ConnectionDisconnect(ConnectionId);
((SocketOutput)SocketOutput).End(endType);
((SocketOutput)Output).End(endType);
break;
}
}

View File

@ -20,9 +20,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
public ListenerContext ListenerContext { get; set; }
public SocketInput SocketInput { get; set; }
public SocketInput Input { get; set; }
public ISocketOutput SocketOutput { get; set; }
public ISocketOutput Output { get; set; }
public IConnectionControl ConnectionControl { get; set; }

View File

@ -82,8 +82,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
public Frame(ConnectionContext context)
{
ConnectionContext = context;
SocketInput = context.SocketInput;
SocketOutput = context.SocketOutput;
Input = context.Input;
Output = context.Output;
ServerOptions = context.ListenerContext.ServiceContext.ServerOptions;
@ -95,8 +95,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
}
public ConnectionContext ConnectionContext { get; }
public SocketInput SocketInput { get; set; }
public ISocketOutput SocketOutput { get; set; }
public SocketInput Input { get; set; }
public ISocketOutput Output { get; set; }
public Action<IFeatureCollection> PrepareRequest
{
get
@ -531,13 +531,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
public void Flush()
{
ProduceStartAndFireOnStarting().GetAwaiter().GetResult();
SocketOutput.Flush();
Output.Flush();
}
public async Task FlushAsync(CancellationToken cancellationToken)
{
await ProduceStartAndFireOnStarting();
await SocketOutput.FlushAsync(cancellationToken);
await Output.FlushAsync(cancellationToken);
}
public void Write(ArraySegment<byte> data)
@ -564,7 +564,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
}
else
{
SocketOutput.Write(data);
Output.Write(data);
}
}
else
@ -599,7 +599,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
}
else
{
return SocketOutput.WriteAsync(data, cancellationToken: cancellationToken);
return Output.WriteAsync(data, cancellationToken: cancellationToken);
}
}
else
@ -631,7 +631,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
}
else
{
await SocketOutput.WriteAsync(data, cancellationToken: cancellationToken);
await Output.WriteAsync(data, cancellationToken: cancellationToken);
}
}
else
@ -675,17 +675,17 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
private void WriteChunked(ArraySegment<byte> data)
{
SocketOutput.Write(data, chunk: true);
Output.Write(data, chunk: true);
}
private Task WriteChunkedAsync(ArraySegment<byte> data, CancellationToken cancellationToken)
{
return SocketOutput.WriteAsync(data, chunk: true, cancellationToken: cancellationToken);
return Output.WriteAsync(data, chunk: true, cancellationToken: cancellationToken);
}
private Task WriteChunkedResponseSuffix()
{
return SocketOutput.WriteAsync(_endChunkedResponseBytes);
return Output.WriteAsync(_endChunkedResponseBytes);
}
private static ArraySegment<byte> CreateAsciiByteArraySegment(string text)
@ -706,7 +706,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
RequestHeaders.TryGetValue("Expect", out expect) &&
(expect.FirstOrDefault() ?? "").Equals("100-continue", StringComparison.OrdinalIgnoreCase))
{
SocketOutput.Write(_continueBytes);
Output.Write(_continueBytes);
}
}
@ -809,7 +809,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
ProduceStart(appCompleted: true);
// Force flush
await SocketOutput.FlushAsync();
await Output.FlushAsync();
await WriteSuffix();
}
@ -856,7 +856,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
var hasTransferEncoding = responseHeaders.HasTransferEncoding;
var transferCoding = FrameHeaders.GetFinalTransferCoding(responseHeaders.HeaderTransferEncoding);
var end = SocketOutput.ProducingStart();
var end = Output.ProducingStart();
if (_keepAlive && hasConnection)
{
@ -944,7 +944,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
responseHeaders.CopyTo(ref end);
end.CopyFrom(_bytesEndHeaders, 0, _bytesEndHeaders.Length);
SocketOutput.ProducingComplete(end);
Output.ProducingComplete(end);
}
public RequestLineStatus TakeStartLine(SocketInput input)

View File

@ -41,20 +41,20 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
while (!_requestProcessingStopping)
{
requestLineStatus = TakeStartLine(SocketInput);
requestLineStatus = TakeStartLine(Input);
if (requestLineStatus == RequestLineStatus.Done)
{
break;
}
if (SocketInput.CheckFinOrThrow())
if (Input.CheckFinOrThrow())
{
// We need to attempt to consume start lines and headers even after
// SocketInput.RemoteIntakeFin is set to true to ensure we don't close a
// connection without giving the application a chance to respond to a request
// sent immediately before the a FIN from the client.
requestLineStatus = TakeStartLine(SocketInput);
requestLineStatus = TakeStartLine(Input);
if (requestLineStatus == RequestLineStatus.Empty)
{
@ -69,20 +69,20 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
break;
}
await SocketInput;
await Input;
}
InitializeHeaders();
while (!_requestProcessingStopping && !TakeMessageHeaders(SocketInput, FrameRequestHeaders))
while (!_requestProcessingStopping && !TakeMessageHeaders(Input, FrameRequestHeaders))
{
if (SocketInput.CheckFinOrThrow())
if (Input.CheckFinOrThrow())
{
// We need to attempt to consume start lines and headers even after
// SocketInput.RemoteIntakeFin is set to true to ensure we don't close a
// connection without giving the application a chance to respond to a request
// sent immediately before the a FIN from the client.
if (!TakeMessageHeaders(SocketInput, FrameRequestHeaders))
if (!TakeMessageHeaders(Input, FrameRequestHeaders))
{
RejectRequest(RequestRejectionReason.MalformedRequestInvalidHeaders);
}
@ -90,7 +90,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
break;
}
await SocketInput;
await Input;
}
if (!_requestProcessingStopping)

View File

@ -213,9 +213,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
private void ConsumedBytes(int count)
{
var scan = _context.SocketInput.ConsumingStart();
var scan = _context.Input.ConsumingStart();
scan.Skip(count);
_context.SocketInput.ConsumingComplete(scan, scan);
_context.Input.ConsumingComplete(scan, scan);
OnConsumedBytes(count);
}
@ -305,7 +305,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
protected override ValueTask<ArraySegment<byte>> PeekAsync(CancellationToken cancellationToken)
{
return _context.SocketInput.PeekAsync();
return _context.Input.PeekAsync();
}
}
@ -330,7 +330,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
return new ValueTask<ArraySegment<byte>>();
}
var task = _context.SocketInput.PeekAsync();
var task = _context.Input.PeekAsync();
if (task.IsCompleted)
{
@ -402,7 +402,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
: base(context)
{
RequestKeepAlive = keepAlive;
_input = _context.SocketInput;
_input = _context.Input;
_requestHeaders = headers;
}

View File

@ -172,7 +172,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
SocketInput = new SocketInput(MemoryPool, ThreadPool);
var connectionContext = new MockConnection(new KestrelServerOptions());
connectionContext.SocketInput = SocketInput;
connectionContext.Input = SocketInput;
Frame = new Frame<object>(application: null, context: connectionContext);
}

View File

@ -48,7 +48,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
Libuv.uv_buf_t ignored;
mockLibuv.AllocCallback(socket.InternalGetHandle(), 2048, out ignored);
mockLibuv.ReadCallback(socket.InternalGetHandle(), 0, ref ignored);
Assert.False(connection.SocketInput.CheckFinOrThrow());
Assert.False(connection.Input.CheckFinOrThrow());
}, null);
connection.ConnectionControl.End(ProduceEndType.SocketDisconnect);

File diff suppressed because it is too large Load Diff

View File

@ -279,7 +279,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
// The block returned by IncomingStart always has at least 2048 available bytes,
// so no need to bounds check in this test.
var socketInput = input.FrameContext.SocketInput;
var socketInput = input.FrameContext.Input;
var bytes = Encoding.ASCII.GetBytes(data[0]);
var block = socketInput.IncomingStart();
Buffer.BlockCopy(bytes, 0, block.Array, block.End, bytes.Length);

View File

@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
FrameContext.ConnectionContext.ListenerContext.ServiceContext.Log = trace;
_memoryPool = new MemoryPool();
FrameContext.SocketInput = new SocketInput(_memoryPool, ltp);
FrameContext.Input = new SocketInput(_memoryPool, ltp);
}
public Frame FrameContext { get; set; }
@ -49,10 +49,10 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
public void Add(string text, bool fin = false)
{
var data = System.Text.Encoding.ASCII.GetBytes(text);
FrameContext.SocketInput.IncomingData(data, 0, data.Length);
FrameContext.Input.IncomingData(data, 0, data.Length);
if (fin)
{
FrameContext.SocketInput.IncomingFin();
FrameContext.Input.IncomingFin();
}
}
@ -116,7 +116,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
public void Dispose()
{
FrameContext.SocketInput.Dispose();
FrameContext.Input.Dispose();
_memoryPool.Dispose();
}
}