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