Skip to content

Commit

Permalink
tried to fix the issue cannot receive on unity3d
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Aug 31, 2024
1 parent 32311c5 commit f2e549d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/SuperSocket.Connection/PipeConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ protected async ValueTask<bool> ProcessOutputRead(PipeReader reader)

protected abstract ValueTask<int> SendOverIOAsync(ReadOnlySequence<byte> buffer, CancellationToken cancellationToken);

protected internal ArraySegment<T> GetArrayByMemory<T>(ReadOnlyMemory<T> memory)
protected internal ArraySegment<byte> GetArrayByMemory(ReadOnlyMemory<byte> memory)
{
if (!MemoryMarshal.TryGetArray(memory, out var result))
if (!MemoryMarshal.TryGetArray<byte>(memory, out var result))
{
throw new InvalidOperationException("Buffer backed by array was expected");
}
Expand Down
5 changes: 3 additions & 2 deletions src/SuperSocket.Connection/TcpPipeConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ protected override void OnClosed()

protected override async ValueTask<int> FillPipeWithDataAsync(Memory<byte> memory, CancellationToken cancellationToken)
{
return await ReceiveAsync(_socket, memory, SocketFlags.None, cancellationToken);
return await ReceiveAsync(_socket, memory, SocketFlags.None, cancellationToken)
.ConfigureAwait(false);
}

private async ValueTask<int> ReceiveAsync(Socket socket, Memory<byte> memory, SocketFlags socketFlags, CancellationToken cancellationToken)
{
return await socket
.ReceiveAsync(GetArrayByMemory((ReadOnlyMemory<byte>)memory), socketFlags, cancellationToken)
.ReceiveAsync(GetArrayByMemory(memory), socketFlags, cancellationToken)
.ConfigureAwait(false);
}

Expand Down
41 changes: 32 additions & 9 deletions src/SuperSocket.Connection/UdpPipeConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ protected override async ValueTask<int> SendOverIOAsync(ReadOnlySequence<byte> b

foreach (var piece in buffer)
{
total += await _socket.SendToAsync(GetArrayByMemory<byte>(piece), SocketFlags.None, RemoteEndPoint);
total += await _socket
.SendToAsync(GetArrayByMemory(piece), SocketFlags.None, RemoteEndPoint)
.ConfigureAwait(false);
}

return total;
Expand All @@ -62,7 +64,10 @@ protected override async ValueTask<int> SendOverIOAsync(ReadOnlySequence<byte> b
try
{
MergeBuffer(ref buffer, destBuffer);
return await _socket.SendToAsync(new ArraySegment<byte>(destBuffer, 0, (int)buffer.Length), SocketFlags.None, RemoteEndPoint);

return await _socket
.SendToAsync(new ArraySegment<byte>(destBuffer, 0, (int)buffer.Length), SocketFlags.None, RemoteEndPoint)
.ConfigureAwait(false);
}
finally
{
Expand Down Expand Up @@ -96,28 +101,43 @@ public override async ValueTask SendAsync(ReadOnlyMemory<byte> buffer, Cancellat
{
if (_enableSendingPipe)
{
await base.SendAsync(buffer, cancellationToken);
await base
.SendAsync(buffer, cancellationToken)
.ConfigureAwait(false);
return;
}

await SendOverIOAsync(new ReadOnlySequence<byte>(buffer), cancellationToken);
await SendOverIOAsync(new ReadOnlySequence<byte>(buffer), cancellationToken)
.ConfigureAwait(false);
}

public override async ValueTask SendAsync<TPackage>(IPackageEncoder<TPackage> 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<TPackage>(writer, packageEncoder, package);
await writer.FlushAsync(cancellationToken);
await ProcessOutputRead(Output.Reader);

await writer
.FlushAsync(cancellationToken)
.ConfigureAwait(false);

await ProcessOutputRead(Output.Reader)
.ConfigureAwait(false);
}
finally
{
Expand All @@ -129,7 +149,10 @@ public override async ValueTask SendAsync(Action<PipeWriter> write, Cancellation
{
if (_enableSendingPipe)
{
await base.SendAsync(write, cancellationToken);
await base
.SendAsync(write, cancellationToken)
.ConfigureAwait(false);

return;
}

Expand Down

0 comments on commit f2e549d

Please sign in to comment.