Decouple connection objects from the server (#2535)

- Making progress towards being able to use the connection objects on the client side.
This commit is contained in:
David Fowler 2018-05-01 12:01:24 -07:00 committed by GitHub
parent c7027784b6
commit b23d9b7679
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 43 deletions

View File

@ -14,7 +14,7 @@ using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
{ {
public partial class LibuvConnection : LibuvConnectionContext public partial class LibuvConnection : TransportConnection
{ {
private static readonly int MinAllocBufferSize = KestrelMemoryPool.MinimumSegmentSize / 2; private static readonly int MinAllocBufferSize = KestrelMemoryPool.MinimumSegmentSize / 2;
@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
private MemoryHandle _bufferHandle; private MemoryHandle _bufferHandle;
public LibuvConnection(ListenerContext context, UvStreamHandle socket) : base(context) public LibuvConnection(UvStreamHandle socket, ILibuvTrace log, LibuvThread thread)
{ {
_socket = socket; _socket = socket;
@ -43,20 +43,22 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
LocalAddress = localEndPoint.Address; LocalAddress = localEndPoint.Address;
LocalPort = localEndPoint.Port; LocalPort = localEndPoint.Port;
} }
Log = log;
Thread = thread;
} }
public LibuvOutputConsumer OutputConsumer { get; set; } public LibuvOutputConsumer OutputConsumer { get; set; }
private ILibuvTrace Log { get; }
private ILibuvTrace Log => ListenerContext.TransportContext.Log; private LibuvThread Thread { get; }
private IConnectionDispatcher ConnectionDispatcher => ListenerContext.TransportContext.ConnectionDispatcher; public override MemoryPool<byte> MemoryPool => Thread.MemoryPool;
private LibuvThread Thread => ListenerContext.Thread; public override PipeScheduler InputWriterScheduler => Thread;
public override PipeScheduler OutputReaderScheduler => Thread;
public async Task Start() public async Task Start()
{ {
try try
{ {
ConnectionDispatcher.OnConnection(this);
OutputConsumer = new LibuvOutputConsumer(Output, Thread, _socket, ConnectionId, Log); OutputConsumer = new LibuvOutputConsumer(Output, Thread, _socket, ConnectionId, Log);
StartReading(); StartReading();

View File

@ -1,24 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Buffers;
using System.IO.Pipelines;
using System.Threading;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal;
namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
{
public class LibuvConnectionContext : TransportConnection
{
public LibuvConnectionContext(ListenerContext context)
{
ListenerContext = context;
}
public ListenerContext ListenerContext { get; set; }
public override MemoryPool<byte> MemoryPool => ListenerContext.Thread.MemoryPool;
public override PipeScheduler InputWriterScheduler => ListenerContext.Thread;
public override PipeScheduler OutputReaderScheduler => ListenerContext.Thread;
}
}

View File

@ -93,7 +93,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
try try
{ {
pipe.Init(Thread.Loop, Thread.QueueCloseHandle, false); pipe.Init(Thread.Loop, Thread.QueueCloseHandle, false);
if (!useFileHandle) if (!useFileHandle)
{ {
pipe.Bind(EndPointInformation.SocketPath); pipe.Bind(EndPointInformation.SocketPath);
@ -181,7 +181,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
protected virtual void DispatchConnection(UvStreamHandle socket) protected virtual void DispatchConnection(UvStreamHandle socket)
{ {
var connection = new LibuvConnection(this, socket); var connection = new LibuvConnection(socket, Log, Thread);
TransportContext.ConnectionDispatcher.OnConnection(connection);
_ = connection.Start(); _ = connection.Start();
} }

View File

@ -161,7 +161,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
try try
{ {
var connection = new LibuvConnection(this, acceptSocket); var connection = new LibuvConnection(acceptSocket, Log, Thread);
TransportContext.ConnectionDispatcher.OnConnection(connection);
_ = connection.Start(); _ = connection.Start();
} }
catch (UvException ex) catch (UvException ex)

View File

@ -60,13 +60,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
public override PipeScheduler InputWriterScheduler => _scheduler; public override PipeScheduler InputWriterScheduler => _scheduler;
public override PipeScheduler OutputReaderScheduler => _scheduler; public override PipeScheduler OutputReaderScheduler => _scheduler;
public async Task StartAsync(IConnectionDispatcher connectionDispatcher) public async Task StartAsync()
{ {
Exception sendError = null; Exception sendError = null;
try try
{ {
connectionDispatcher.OnConnection(this);
// Spawn send and receive logic // Spawn send and receive logic
Task receiveTask = DoReceive(); Task receiveTask = DoReceive();
Task<Exception> sendTask = DoSend(); Task<Exception> sendTask = DoSend();

View File

@ -157,7 +157,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
acceptSocket.NoDelay = _endPointInformation.NoDelay; acceptSocket.NoDelay = _endPointInformation.NoDelay;
var connection = new SocketConnection(acceptSocket, _memoryPool, _schedulers[schedulerIndex], _trace); var connection = new SocketConnection(acceptSocket, _memoryPool, _schedulers[schedulerIndex], _trace);
_ = connection.StartAsync(_dispatcher);
_dispatcher.OnConnection(connection);
_ = connection.StartAsync();
} }
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.ConnectionReset) catch (SocketException ex) when (ex.SocketErrorCode == SocketError.ConnectionReset)
{ {

View File

@ -35,7 +35,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
Thread = thread Thread = thread
}; };
var socket = new MockSocket(mockLibuv, Thread.CurrentThread.ManagedThreadId, transportContext.Log); var socket = new MockSocket(mockLibuv, Thread.CurrentThread.ManagedThreadId, transportContext.Log);
var connection = new LibuvConnection(listenerContext, socket); var connection = new LibuvConnection(socket, listenerContext.TransportContext.Log, thread);
listenerContext.TransportContext.ConnectionDispatcher.OnConnection(connection);
connectionTask = connection.Start(); connectionTask = connection.Start();
mockLibuv.AllocCallback(socket.InternalGetHandle(), 2048, out var ignored); mockLibuv.AllocCallback(socket.InternalGetHandle(), 2048, out var ignored);
@ -89,7 +90,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
Thread = thread Thread = thread
}; };
var socket = new MockSocket(mockLibuv, Thread.CurrentThread.ManagedThreadId, transportContext.Log); var socket = new MockSocket(mockLibuv, Thread.CurrentThread.ManagedThreadId, transportContext.Log);
var connection = new LibuvConnection(listenerContext, socket); var connection = new LibuvConnection(socket, listenerContext.TransportContext.Log, thread);
listenerContext.TransportContext.ConnectionDispatcher.OnConnection(connection);
connectionTask = connection.Start(); connectionTask = connection.Start();
mockLibuv.AllocCallback(socket.InternalGetHandle(), 2048, out var ignored); mockLibuv.AllocCallback(socket.InternalGetHandle(), 2048, out var ignored);
@ -157,7 +159,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
Thread = thread Thread = thread
}; };
var socket = new MockSocket(mockLibuv, Thread.CurrentThread.ManagedThreadId, transportContext.Log); var socket = new MockSocket(mockLibuv, Thread.CurrentThread.ManagedThreadId, transportContext.Log);
var connection = new LibuvConnection(listenerContext, socket); var connection = new LibuvConnection(socket, listenerContext.TransportContext.Log, thread);
listenerContext.TransportContext.ConnectionDispatcher.OnConnection(connection);
connectionTask = connection.Start(); connectionTask = connection.Start();
mockLibuv.AllocCallback(socket.InternalGetHandle(), 2048, out var ignored); mockLibuv.AllocCallback(socket.InternalGetHandle(), 2048, out var ignored);
@ -223,7 +226,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
Thread = thread Thread = thread
}; };
var socket = new MockSocket(mockLibuv, Thread.CurrentThread.ManagedThreadId, transportContext.Log); var socket = new MockSocket(mockLibuv, Thread.CurrentThread.ManagedThreadId, transportContext.Log);
var connection = new LibuvConnection(listenerContext, socket); var connection = new LibuvConnection(socket, listenerContext.TransportContext.Log, thread);
listenerContext.TransportContext.ConnectionDispatcher.OnConnection(connection);
connectionTask = connection.Start(); connectionTask = connection.Start();
var ignored = new LibuvFunctions.uv_buf_t(); var ignored = new LibuvFunctions.uv_buf_t();