Cache remote endpoint to prevent ObjectDisposedException

This commit is contained in:
Oliver Booth 2023-01-23 18:00:43 +00:00
parent ca55005c30
commit b8df2c50c4
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
3 changed files with 8 additions and 2 deletions

View File

@ -17,6 +17,7 @@ public abstract class BaseClientNode : Node
{ {
private readonly ObjectIDGenerator _callbackIdGenerator = new(); private readonly ObjectIDGenerator _callbackIdGenerator = new();
private readonly ConcurrentDictionary<int, List<TaskCompletionSource<Packet>>> _packetCompletionSources = new(); private readonly ConcurrentDictionary<int, List<TaskCompletionSource<Packet>>> _packetCompletionSources = new();
private EndPoint? _remoteEP;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="BaseClientNode" /> class. /// Initializes a new instance of the <see cref="BaseClientNode" /> class.
@ -36,8 +37,11 @@ public abstract class BaseClientNode : Node
/// </summary> /// </summary>
/// <value>The <see cref="EndPoint" /> with which the client is communicating.</value> /// <value>The <see cref="EndPoint" /> with which the client is communicating.</value>
/// <exception cref="SocketException">An error occurred when attempting to access the socket.</exception> /// <exception cref="SocketException">An error occurred when attempting to access the socket.</exception>
/// <exception cref="ObjectDisposedException"><see cref="Node.BaseSocket" /> has been closed.</exception> public EndPoint RemoteEndPoint
public EndPoint RemoteEndPoint => BaseSocket.RemoteEndPoint; {
get => _remoteEP ??= BaseSocket.RemoteEndPoint;
internal set => _remoteEP = value;
}
/// <summary> /// <summary>
/// Gets the session ID of the client. /// Gets the session ID of the client.

View File

@ -91,6 +91,7 @@ public sealed class ProtocolClient : BaseClientNode
} }
IsConnected = true; IsConnected = true;
RemoteEndPoint = BaseSocket.RemoteEndPoint;
State = ClientState.Handshaking; State = ClientState.Handshaking;
var handshakeRequest = new HandshakeRequestPacket(ProtocolVersion); var handshakeRequest = new HandshakeRequestPacket(ProtocolVersion);

View File

@ -16,6 +16,7 @@ public sealed partial class ProtocolListener
BaseSocket = socket ?? throw new ArgumentNullException(nameof(socket)); BaseSocket = socket ?? throw new ArgumentNullException(nameof(socket));
SessionId = Guid.NewGuid(); SessionId = Guid.NewGuid();
IsConnected = true; IsConnected = true;
RemoteEndPoint = socket.RemoteEndPoint;
} }
/// <summary> /// <summary>