Skip to content

Commit

Permalink
Added disconnect reason.
Browse files Browse the repository at this point in the history
  • Loading branch information
DJGosnell committed Aug 24, 2023
1 parent 67d102e commit 13c2e42
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/NexNet/Internals/NexusSession.Sending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ public async ValueTask SendMessage<TMessage>(TMessage body, CancellationToken ca

var length = (int)_bufferWriter.Length;
var buffer = _bufferWriter.GetBuffer();

// Only used for debugging
_config.InternalOnSend?.Invoke(this, buffer.ToArray());

buffer.CopyTo(_pipeOutput.GetSpan(length));
_bufferWriter.Deallocate(buffer);
_bufferWriter.Reset();
_pipeOutput.Advance(length);

Logger?.LogTrace($"Sending {TMessage.Type} header and body with {length} total bytes.");
Expand All @@ -65,7 +68,7 @@ public async ValueTask SendMessage<TMessage>(TMessage body, CancellationToken ca
OnSent?.Invoke();

if (result.IsCanceled || result.IsCompleted)
await DisconnectCore(DisconnectReason.ProtocolError, false).ConfigureAwait(false);
await DisconnectCore(DisconnectReason.SocketClosedWhenWriting, false).ConfigureAwait(false);
}


Expand Down Expand Up @@ -142,7 +145,7 @@ public async ValueTask SendHeaderWithBody(MessageType type, ReadOnlyMemory<byte>
OnSent?.Invoke();

if (result.IsCanceled || result.IsCompleted)
await DisconnectCore(DisconnectReason.ProtocolError, false).ConfigureAwait(false);
await DisconnectCore(DisconnectReason.SocketClosedWhenWriting, false).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -212,6 +215,6 @@ private async ValueTask SendHeaderCore(MessageType type, bool force, Cancellatio
OnSent?.Invoke();

if (result.IsCanceled || result.IsCompleted)
await DisconnectCore(DisconnectReason.ProtocolError, false).ConfigureAwait(false);
await DisconnectCore(DisconnectReason.SocketClosedWhenWriting, false).ConfigureAwait(false);
}
}
5 changes: 5 additions & 0 deletions src/NexNet/Internals/NexusSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ private async ValueTask DisconnectCore(DisconnectReason reason, bool sendDisconn
if (state == (int)ConnectionState.Disconnecting || state == (int)ConnectionState.Disconnected)
return;

if (reason == DisconnectReason.ProtocolError)
{

}

_registeredDisconnectReason = reason;

Logger?.LogTrace($"DisconnectCore({reason}, {sendDisconnect})");
Expand Down
7 changes: 6 additions & 1 deletion src/NexNet/Messages/DisconnectReason.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ public enum DisconnectReason : byte
ServerRestarting = MessageType.DisconnectServerRestarting,

/// <summary>
/// The high water cutoff was reached on a duplex pipe. No body.
/// The high water cutoff was reached on a duplex pipe.
/// </summary>
NexusPipeHighWaterCutoffReached = MessageType.DisconnectNexusPipeHighWaterCutoffReached,

/// <summary>
/// The socket was closed while attempting to write.
/// </summary>
SocketClosedWhenWriting = MessageType.DisconnectSocketClosedWhenWriting,
}
5 changes: 5 additions & 0 deletions src/NexNet/Messages/MessageHeaderType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public enum MessageType : byte
/// </summary>
DisconnectNexusPipeHighWaterCutoffReached = 31,

/// <summary>
/// The socket was closed while attempting to write. No body.
/// </summary>
DisconnectSocketClosedWhenWriting = 32,

/// <summary>
/// Header for data sent to a pipe.
/// </summary>
Expand Down

0 comments on commit 13c2e42

Please sign in to comment.