More clean up of LibuvConnection (#1743)

* More clean up of LibuvConnection
- Use C# 7
- Use Buffer<T>.Pin to get access to the underlying
pointer instead of using TryGetPointer.
This commit is contained in:
David Fowler 2017-04-23 20:45:03 -07:00 committed by GitHub
parent db44f5b672
commit 71d2abed06
1 changed files with 9 additions and 10 deletions

View File

@ -5,6 +5,7 @@ using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Kestrel.Internal.System.Buffers;
using Microsoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Networking;
@ -26,13 +27,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
private IConnectionContext _connectionContext;
private WritableBuffer? _currentWritableBuffer;
private BufferHandle _bufferHandle;
public LibuvConnection(ListenerContext context, UvStreamHandle socket) : base(context)
{
_socket = socket;
var tcpHandle = _socket as UvTcpHandle;
if (tcpHandle != null)
if (_socket is UvTcpHandle tcpHandle)
{
RemoteEndPoint = tcpHandle.GetPeerIPEndPoint();
LocalEndPoint = tcpHandle.GetSockIPEndPoint();
@ -110,13 +111,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
Debug.Assert(_currentWritableBuffer == null);
var currentWritableBuffer = Input.Alloc(MinAllocBufferSize);
_currentWritableBuffer = currentWritableBuffer;
void* dataPtr;
var tryGetPointer = currentWritableBuffer.Buffer.TryGetPointer(out dataPtr);
Debug.Assert(tryGetPointer);
return handle.Libuv.buf_init(
(IntPtr)dataPtr,
currentWritableBuffer.Buffer.Length);
_bufferHandle = currentWritableBuffer.Buffer.Pin();
return handle.Libuv.buf_init((IntPtr)_bufferHandle.PinnedPointer, currentWritableBuffer.Buffer.Length);
}
private static void ReadCallback(UvStreamHandle handle, int status, object state)
@ -149,8 +147,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
WritableBufferAwaitable? flushTask = null;
if (errorDone)
{
Exception uvError;
handle.Libuv.Check(status, out uvError);
handle.Libuv.Check(status, out var uvError);
// Log connection resets at a lower (Debug) level.
if (status == LibuvConstants.ECONNRESET)
@ -176,6 +173,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
}
_currentWritableBuffer = null;
_bufferHandle.Free();
if (flushTask?.IsCompleted == false)
{
Log.ConnectionPause(ConnectionId);