Merged PR 9189: Modify WebSockets ValueTaskSource

Create the GCHandle per operation and Free it when resetting the ValueTaskSource for re-use.
This commit is contained in:
Brennan Conroy 2020-07-20 19:11:17 +00:00
parent c9f75a1f41
commit 40383cf1f9
3 changed files with 9 additions and 5 deletions

View File

@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Server.IIS.Core.IO
}; };
private readonly WebSocketsAsyncIOEngine _engine; private readonly WebSocketsAsyncIOEngine _engine;
private readonly GCHandle _thisHandle; private GCHandle _thisHandle;
private MemoryHandle _inputHandle; private MemoryHandle _inputHandle;
private IntPtr _requestHandler; private IntPtr _requestHandler;
private Memory<byte> _memory; private Memory<byte> _memory;
@ -33,11 +33,11 @@ namespace Microsoft.AspNetCore.Server.IIS.Core.IO
public WebSocketReadOperation(WebSocketsAsyncIOEngine engine) public WebSocketReadOperation(WebSocketsAsyncIOEngine engine)
{ {
_engine = engine; _engine = engine;
_thisHandle = GCHandle.Alloc(this);
} }
protected override unsafe bool InvokeOperation(out int hr, out int bytes) protected override unsafe bool InvokeOperation(out int hr, out int bytes)
{ {
_thisHandle = GCHandle.Alloc(this);
_inputHandle = _memory.Pin(); _inputHandle = _memory.Pin();
hr = NativeMethods.HttpWebsocketsReadBytes( hr = NativeMethods.HttpWebsocketsReadBytes(
@ -67,6 +67,8 @@ namespace Microsoft.AspNetCore.Server.IIS.Core.IO
{ {
base.ResetOperation(); base.ResetOperation();
_thisHandle.Free();
_memory = default; _memory = default;
_inputHandle.Dispose(); _inputHandle.Dispose();
_inputHandle = default; _inputHandle = default;

View File

@ -25,16 +25,16 @@ namespace Microsoft.AspNetCore.Server.IIS.Core.IO
}; };
private readonly WebSocketsAsyncIOEngine _engine; private readonly WebSocketsAsyncIOEngine _engine;
private readonly GCHandle _thisHandle; private GCHandle _thisHandle;
public WebSocketWriteOperation(WebSocketsAsyncIOEngine engine) public WebSocketWriteOperation(WebSocketsAsyncIOEngine engine)
{ {
_engine = engine; _engine = engine;
_thisHandle = GCHandle.Alloc(this);
} }
protected override unsafe int WriteChunks(IntPtr requestHandler, int chunkCount, HttpApiTypes.HTTP_DATA_CHUNK* dataChunks, out bool completionExpected) protected override unsafe int WriteChunks(IntPtr requestHandler, int chunkCount, HttpApiTypes.HTTP_DATA_CHUNK* dataChunks, out bool completionExpected)
{ {
_thisHandle = GCHandle.Alloc(this);
return NativeMethods.HttpWebsocketsWriteBytes(requestHandler, dataChunks, chunkCount, WriteCallback, (IntPtr)_thisHandle, out completionExpected); return NativeMethods.HttpWebsocketsWriteBytes(requestHandler, dataChunks, chunkCount, WriteCallback, (IntPtr)_thisHandle, out completionExpected);
} }
@ -42,6 +42,8 @@ namespace Microsoft.AspNetCore.Server.IIS.Core.IO
{ {
base.ResetOperation(); base.ResetOperation();
_thisHandle.Free();
_engine.ReturnOperation(this); _engine.ReturnOperation(this);
} }
} }

View File

@ -39,7 +39,7 @@ namespace Microsoft.AspNetCore.Server.IIS.Core.IO
var read = GetReadOperation(); var read = GetReadOperation();
read.Initialize(_handler, memory); read.Initialize(_handler, memory);
read.Invoke(); read.Invoke();
return new ValueTask<int>(read, 0); return new ValueTask<int>(read, 0);
} }
} }