Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Await and Disconnection Fixes #25

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/NexNet/Internals/NexusSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,15 @@ private async ValueTask DisconnectCore(DisconnectReason reason, bool sendDisconn

if (_config.InternalNoLingerOnShutdown)
{
await _transportConnection.CloseAsync(false).ConfigureAwait(false);
try
{
await _transportConnection.CloseAsync(false).ConfigureAwait(false);
}
catch (Exception e)
{
Logger?.LogError(e, "Error while closing transport connection.");
}

return;
}
else
Expand All @@ -302,7 +310,15 @@ private async ValueTask DisconnectCore(DisconnectReason reason, bool sendDisconn
}

_pipeOutput = null;
await _transportConnection.CloseAsync(true);
try
{
await _transportConnection.CloseAsync(true).ConfigureAwait(false);
}
catch (Exception e)
{
Logger?.LogError(e, "Error while closing transport connection.");
}

}

// If we match a limited type of disconnects, attempt to reconnect if we are the client
Expand Down
15 changes: 8 additions & 7 deletions src/NexNet/Pipes/NexusChannelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static async ValueTask WriteAndComplete<T>(
CancellationToken cancellationToken = default)
{
var writer = await channel.GetWriterAsync();
await WriteAndComplete<T>(writer, enumerableData, chunkSize, cancellationToken);
await WriteAndComplete<T>(writer, enumerableData, chunkSize, cancellationToken).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -58,19 +58,19 @@ public static async ValueTask WriteAndComplete<T>(

foreach (var chunk in dataChunks)
{
await writer.WriteAsync(chunk, cancellationToken);
await writer.WriteAsync(chunk, cancellationToken).ConfigureAwait(false);
}
}
else
{
foreach (var data in enumerableData)
{
await writer.WriteAsync(data, cancellationToken);
await writer.WriteAsync(data, cancellationToken).ConfigureAwait(false);
}

}

await writer.CompleteAsync();
await writer.CompleteAsync().ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -107,8 +107,8 @@ public static async ValueTask<List<T>> ReadUntilComplete<T, TTo>(
int estimatedSize = 0,
CancellationToken cancellationToken = default)
{
var reader = await channel.GetReaderAsync();
return await ReadUntilComplete<T>(reader, estimatedSize, cancellationToken);
var reader = await channel.GetReaderAsync().ConfigureAwait(false);
return await ReadUntilComplete<T>(reader, estimatedSize, cancellationToken).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -153,7 +153,8 @@ public static async ValueTask<List<TTo>> ReadUntilComplete<T, TTo>(
if (reader.IsComplete && previousBufferLength == 0)
break;

var readResult = await reader.ReadAsync(converter, cancellationToken);
var readResult = await reader.ReadAsync(converter, cancellationToken)
.ConfigureAwait(false);
var resultCount = readResult.Count;
if (reader.IsComplete
&& resultCount == 0
Expand Down