From 13c2e42013ee42bb326c964da71ab891ed3d5c84 Mon Sep 17 00:00:00 2001 From: DJGosnell Date: Thu, 24 Aug 2023 10:42:05 -0400 Subject: [PATCH] Added disconnect reason. --- src/NexNet/Internals/NexusSession.Sending.cs | 11 +++++++---- src/NexNet/Internals/NexusSession.cs | 5 +++++ src/NexNet/Messages/DisconnectReason.cs | 7 ++++++- src/NexNet/Messages/MessageHeaderType.cs | 5 +++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/NexNet/Internals/NexusSession.Sending.cs b/src/NexNet/Internals/NexusSession.Sending.cs index f2b78893..2f6e397f 100644 --- a/src/NexNet/Internals/NexusSession.Sending.cs +++ b/src/NexNet/Internals/NexusSession.Sending.cs @@ -53,9 +53,12 @@ public async ValueTask SendMessage(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."); @@ -65,7 +68,7 @@ public async ValueTask SendMessage(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); } @@ -142,7 +145,7 @@ public async ValueTask SendHeaderWithBody(MessageType type, ReadOnlyMemory OnSent?.Invoke(); if (result.IsCanceled || result.IsCompleted) - await DisconnectCore(DisconnectReason.ProtocolError, false).ConfigureAwait(false); + await DisconnectCore(DisconnectReason.SocketClosedWhenWriting, false).ConfigureAwait(false); } /// @@ -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); } } diff --git a/src/NexNet/Internals/NexusSession.cs b/src/NexNet/Internals/NexusSession.cs index 79b6522a..8eb00fa0 100644 --- a/src/NexNet/Internals/NexusSession.cs +++ b/src/NexNet/Internals/NexusSession.cs @@ -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})"); diff --git a/src/NexNet/Messages/DisconnectReason.cs b/src/NexNet/Messages/DisconnectReason.cs index 2e3b8a59..de4ffe53 100644 --- a/src/NexNet/Messages/DisconnectReason.cs +++ b/src/NexNet/Messages/DisconnectReason.cs @@ -56,7 +56,12 @@ public enum DisconnectReason : byte ServerRestarting = MessageType.DisconnectServerRestarting, /// - /// The high water cutoff was reached on a duplex pipe. No body. + /// The high water cutoff was reached on a duplex pipe. /// NexusPipeHighWaterCutoffReached = MessageType.DisconnectNexusPipeHighWaterCutoffReached, + + /// + /// The socket was closed while attempting to write. + /// + SocketClosedWhenWriting = MessageType.DisconnectSocketClosedWhenWriting, } diff --git a/src/NexNet/Messages/MessageHeaderType.cs b/src/NexNet/Messages/MessageHeaderType.cs index 97116851..c2a7393f 100644 --- a/src/NexNet/Messages/MessageHeaderType.cs +++ b/src/NexNet/Messages/MessageHeaderType.cs @@ -69,6 +69,11 @@ public enum MessageType : byte /// DisconnectNexusPipeHighWaterCutoffReached = 31, + /// + /// The socket was closed while attempting to write. No body. + /// + DisconnectSocketClosedWhenWriting = 32, + /// /// Header for data sent to a pipe. ///