From f2e549db68902d8b1bf870ae7a4a29112c9e9606 Mon Sep 17 00:00:00 2001 From: Kerry Jiang Date: Fri, 30 Aug 2024 23:01:49 -0700 Subject: [PATCH] tried to fix the issue cannot receive on unity3d --- src/SuperSocket.Connection/PipeConnection.cs | 4 +- .../TcpPipeConnection.cs | 5 ++- .../UdpPipeConnection.cs | 41 +++++++++++++++---- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/SuperSocket.Connection/PipeConnection.cs b/src/SuperSocket.Connection/PipeConnection.cs index e6dc7823f..10550697c 100644 --- a/src/SuperSocket.Connection/PipeConnection.cs +++ b/src/SuperSocket.Connection/PipeConnection.cs @@ -183,9 +183,9 @@ protected async ValueTask ProcessOutputRead(PipeReader reader) protected abstract ValueTask SendOverIOAsync(ReadOnlySequence buffer, CancellationToken cancellationToken); - protected internal ArraySegment GetArrayByMemory(ReadOnlyMemory memory) + protected internal ArraySegment GetArrayByMemory(ReadOnlyMemory memory) { - if (!MemoryMarshal.TryGetArray(memory, out var result)) + if (!MemoryMarshal.TryGetArray(memory, out var result)) { throw new InvalidOperationException("Buffer backed by array was expected"); } diff --git a/src/SuperSocket.Connection/TcpPipeConnection.cs b/src/SuperSocket.Connection/TcpPipeConnection.cs index 5d22aadda..2bb406852 100644 --- a/src/SuperSocket.Connection/TcpPipeConnection.cs +++ b/src/SuperSocket.Connection/TcpPipeConnection.cs @@ -31,13 +31,14 @@ protected override void OnClosed() protected override async ValueTask FillPipeWithDataAsync(Memory memory, CancellationToken cancellationToken) { - return await ReceiveAsync(_socket, memory, SocketFlags.None, cancellationToken); + return await ReceiveAsync(_socket, memory, SocketFlags.None, cancellationToken) + .ConfigureAwait(false); } private async ValueTask ReceiveAsync(Socket socket, Memory memory, SocketFlags socketFlags, CancellationToken cancellationToken) { return await socket - .ReceiveAsync(GetArrayByMemory((ReadOnlyMemory)memory), socketFlags, cancellationToken) + .ReceiveAsync(GetArrayByMemory(memory), socketFlags, cancellationToken) .ConfigureAwait(false); } diff --git a/src/SuperSocket.Connection/UdpPipeConnection.cs b/src/SuperSocket.Connection/UdpPipeConnection.cs index 56b242643..9bb84385c 100644 --- a/src/SuperSocket.Connection/UdpPipeConnection.cs +++ b/src/SuperSocket.Connection/UdpPipeConnection.cs @@ -50,7 +50,9 @@ protected override async ValueTask SendOverIOAsync(ReadOnlySequence b foreach (var piece in buffer) { - total += await _socket.SendToAsync(GetArrayByMemory(piece), SocketFlags.None, RemoteEndPoint); + total += await _socket + .SendToAsync(GetArrayByMemory(piece), SocketFlags.None, RemoteEndPoint) + .ConfigureAwait(false); } return total; @@ -62,7 +64,10 @@ protected override async ValueTask SendOverIOAsync(ReadOnlySequence b try { MergeBuffer(ref buffer, destBuffer); - return await _socket.SendToAsync(new ArraySegment(destBuffer, 0, (int)buffer.Length), SocketFlags.None, RemoteEndPoint); + + return await _socket + .SendToAsync(new ArraySegment(destBuffer, 0, (int)buffer.Length), SocketFlags.None, RemoteEndPoint) + .ConfigureAwait(false); } finally { @@ -96,28 +101,43 @@ public override async ValueTask SendAsync(ReadOnlyMemory buffer, Cancellat { if (_enableSendingPipe) { - await base.SendAsync(buffer, cancellationToken); + await base + .SendAsync(buffer, cancellationToken) + .ConfigureAwait(false); return; } - await SendOverIOAsync(new ReadOnlySequence(buffer), cancellationToken); + await SendOverIOAsync(new ReadOnlySequence(buffer), cancellationToken) + .ConfigureAwait(false); } public override async ValueTask SendAsync(IPackageEncoder packageEncoder, TPackage package, CancellationToken cancellationToken) { if (_enableSendingPipe) { - await base.SendAsync(packageEncoder, package, cancellationToken); + await base + .SendAsync(packageEncoder, package, cancellationToken) + .ConfigureAwait(false); + return; } try { - await SendLock.WaitAsync(cancellationToken); + await SendLock + .WaitAsync(cancellationToken) + .ConfigureAwait(false); + var writer = OutputWriter; + WritePackageWithEncoder(writer, packageEncoder, package); - await writer.FlushAsync(cancellationToken); - await ProcessOutputRead(Output.Reader); + + await writer + .FlushAsync(cancellationToken) + .ConfigureAwait(false); + + await ProcessOutputRead(Output.Reader) + .ConfigureAwait(false); } finally { @@ -129,7 +149,10 @@ public override async ValueTask SendAsync(Action write, Cancellation { if (_enableSendingPipe) { - await base.SendAsync(write, cancellationToken); + await base + .SendAsync(write, cancellationToken) + .ConfigureAwait(false); + return; }