Skip to content

Commit

Permalink
Socket.RemoteEndPoint exception in OnWsDisconnected #116
Browse files Browse the repository at this point in the history
  • Loading branch information
chronoxor committed Feb 21, 2021
1 parent 8a8679d commit b5f3507
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 318 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Specify version format
version: "5.0.7.{build}"
version: "5.0.9.{build}"

# Image to use
image: Visual Studio 2019
Expand Down
12 changes: 7 additions & 5 deletions source/NetCoreServer/IWebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ public interface IWebSocket
/// <summary>
/// Handle WebSocket client connecting notification
/// </summary>
/// <remarks>Notification is called when WebSocket client is connecting to the server.You can handle the connection and change WebSocket upgrade HTTP request by providing your own headers.</remarks>
/// <remarks>Notification is called when WebSocket client is connecting to the server. You can handle the connection and change WebSocket upgrade HTTP request by providing your own headers.</remarks>
/// <param name="request">WebSocket upgrade HTTP request</param>
void OnWsConnecting(HttpRequest request) {}

/// <summary>
/// Handle WebSocket client connected notification
/// </summary>
Expand All @@ -20,18 +19,21 @@ void OnWsConnected(HttpResponse response) {}
/// <summary>
/// Handle WebSocket server session validating notification
/// </summary>
/// <remarks>Notification is called when WebSocket client is connecting to the server.You can handle the connection and validate WebSocket upgrade HTTP request.</remarks>
/// <remarks>Notification is called when WebSocket client is connecting to the server. You can handle the connection and validate WebSocket upgrade HTTP request.</remarks>
/// <param name="request">WebSocket upgrade HTTP request</param>
/// <param name="response">WebSocket upgrade HTTP response</param>
/// <returns>return 'true' if the WebSocket update request is valid, 'false' if the WebSocket update request is not valid</returns>
bool OnWsConnecting(HttpRequest request, HttpResponse response) { return true; }

/// <summary>
/// Handle WebSocket server session connected notification
/// </summary>
/// <param name="request">WebSocket upgrade HTTP request</param>
void OnWsConnected(HttpRequest request) {}

/// <summary>
/// Handle WebSocket client disconnecting notification
/// </summary>
void OnWsDisconnecting() {}
/// <summary>
/// Handle WebSocket client disconnected notification
/// </summary>
Expand Down Expand Up @@ -85,6 +87,6 @@ void OnWsError(SocketError error) {}
/// Send WebSocket server upgrade response
/// </summary>
/// <param name="response">WebSocket upgrade HTTP response</param>
void SendResponse(HttpResponse response) {}
void SendUpgrade(HttpResponse response) {}
}
}
2 changes: 1 addition & 1 deletion source/NetCoreServer/NetCoreServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>5.0.7.0</Version>
<Version>5.0.9.0</Version>
<Authors>Ivan Shynkarenka</Authors>
<Copyright>Copyright (c) 2019-2021 Ivan Shynkarenka</Copyright>
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>
Expand Down
47 changes: 45 additions & 2 deletions source/NetCoreServer/SslClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,23 +178,37 @@ public virtual bool Connect()
if (Socket.AddressFamily == AddressFamily.InterNetworkV6)
Socket.DualMode = OptionDualMode;

// Call the client connecting handler
OnConnecting();

try
{
// Connect to the server
Socket.Connect(Endpoint);
}
catch (SocketException ex)
{
// Call the client error handler
SendError(ex.SocketErrorCode);

// Reset event args
_connectEventArg.Completed -= OnAsyncCompleted;

// Call the client disconnecting handler
OnDisconnecting();

// Close the client socket
Socket.Close();

// Dispose the client socket
Socket.Dispose();

// Dispose event arguments
_connectEventArg.Dispose();

// Call the client disconnected handler
SendError(ex.SocketErrorCode);
OnDisconnected();

return false;
}

Expand Down Expand Up @@ -231,6 +245,9 @@ public virtual bool Connect()
_sslStreamId = Guid.NewGuid();
_sslStream = (Context.CertificateValidationCallback != null) ? new SslStream(new NetworkStream(Socket, false), false, Context.CertificateValidationCallback) : new SslStream(new NetworkStream(Socket, false), false);

// Call the session handshaking handler
OnHandshaking();

// SSL handshake
_sslStream.AuthenticateAsClient(Address, Context.Certificates ?? new X509CertificateCollection(new[] { Context.Certificate }), Context.Protocols, true);
}
Expand Down Expand Up @@ -280,6 +297,9 @@ public virtual bool Disconnect()
// Reset event args
_connectEventArg.Completed -= OnAsyncCompleted;

// Call the client disconnecting handler
OnDisconnecting();

try
{
try
Expand Down Expand Up @@ -374,8 +394,13 @@ public virtual bool ConnectAsync()
if (Socket.AddressFamily == AddressFamily.InterNetworkV6)
Socket.DualMode = OptionDualMode;

// Async connect to the server
// Update the connecting flag
IsConnecting = true;

// Call the client connecting handler
OnConnecting();

// Async connect to the server
if (!Socket.ConnectAsync(_connectEventArg))
ProcessConnect(_connectEventArg);

Expand Down Expand Up @@ -690,6 +715,9 @@ private void ClearBuffers()
/// </summary>
private void OnAsyncCompleted(object sender, SocketAsyncEventArgs e)
{
if (IsSocketDisposed)
return;

// Determine which type of operation just completed and call the associated handler
switch (e.LastOperation)
{
Expand Down Expand Up @@ -741,6 +769,9 @@ private void ProcessConnect(SocketAsyncEventArgs e)
_sslStreamId = Guid.NewGuid();
_sslStream = (Context.CertificateValidationCallback != null) ? new SslStream(new NetworkStream(Socket, false), false, Context.CertificateValidationCallback) : new SslStream(new NetworkStream(Socket, false), false);

// Call the session handshaking handler
OnHandshaking();

// Begin the SSL handshake
IsHandshaking = true;
_sslStream.BeginAuthenticateAsClient(Address, Context.Certificates ?? new X509CertificateCollection(new[] { Context.Certificate }), Context.Protocols, true, ProcessHandshake, _sslStreamId);
Expand Down Expand Up @@ -905,15 +936,27 @@ private void ProcessSend(IAsyncResult result)

#region Session handlers

/// <summary>
/// Handle client connecting notification
/// </summary>
protected virtual void OnConnecting() {}
/// <summary>
/// Handle client connected notification
/// </summary>
protected virtual void OnConnected() {}
/// <summary>
/// Handle client handshaking notification
/// </summary>
protected virtual void OnHandshaking() {}
/// <summary>
/// Handle client handshaked notification
/// </summary>
protected virtual void OnHandshaked() {}
/// <summary>
/// Handle client disconnecting notification
/// </summary>
protected virtual void OnDisconnecting() {}
/// <summary>
/// Handle client disconnected notification
/// </summary>
protected virtual void OnDisconnected() {}
Expand Down
56 changes: 48 additions & 8 deletions source/NetCoreServer/SslServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ public virtual bool Start()
_acceptorSocket.Bind(Endpoint);
// Refresh the endpoint property based on the actual endpoint created
Endpoint = (IPEndPoint)_acceptorSocket.LocalEndPoint;

// Call the server starting handler
OnStarting();

// Start listen to the acceptor socket with the given accepting backlog size
_acceptorSocket.Listen(OptionAcceptorBacklog);

Expand Down Expand Up @@ -225,17 +229,24 @@ public virtual bool Stop()
// Reset acceptor event arg
_acceptorEventArg.Completed -= OnAsyncCompleted;

// Close the acceptor socket
_acceptorSocket.Close();
// Call the server stopping handler
OnStopping();

// Dispose the acceptor socket
_acceptorSocket.Dispose();
try
{
// Close the acceptor socket
_acceptorSocket.Close();

// Dispose event arguments
_acceptorEventArg.Dispose();
// Dispose the acceptor socket
_acceptorSocket.Dispose();

// Update the acceptor socket disposed flag
IsSocketDisposed = true;
// Dispose event arguments
_acceptorEventArg.Dispose();

// Update the acceptor socket disposed flag
IsSocketDisposed = true;
}
catch (ObjectDisposedException) {}

// Disconnect all sessions
DisconnectAll();
Expand Down Expand Up @@ -311,6 +322,9 @@ private void ProcessAccept(SocketAsyncEventArgs e)
/// </summary>
private void OnAsyncCompleted(object sender, SocketAsyncEventArgs e)
{
if (IsSocketDisposed)
return;

ProcessAccept(e);
}

Expand Down Expand Up @@ -422,26 +436,49 @@ public virtual bool Multicast(byte[] buffer, long offset, long size)

#region Server handlers

/// <summary>
/// Handle server starting notification
/// </summary>
protected virtual void OnStarting() {}
/// <summary>
/// Handle server started notification
/// </summary>
protected virtual void OnStarted() {}
/// <summary>
/// Handle server stopping notification
/// </summary>
protected virtual void OnStopping() {}
/// <summary>
/// Handle server stopped notification
/// </summary>
protected virtual void OnStopped() {}

/// <summary>
/// Handle session connecting notification
/// </summary>
/// <param name="session">Connecting session</param>
protected virtual void OnConnecting(SslSession session) {}
/// <summary>
/// Handle session connected notification
/// </summary>
/// <param name="session">Connected session</param>
protected virtual void OnConnected(SslSession session) {}
/// <summary>
/// Handle session handshaking notification
/// </summary>
/// <param name="session">Handshaking session</param>
protected virtual void OnHandshaking(SslSession session) {}
/// <summary>
/// Handle session handshaked notification
/// </summary>
/// <param name="session">Handshaked session</param>
protected virtual void OnHandshaked(SslSession session) {}
/// <summary>
/// Handle session disconnecting notification
/// </summary>
/// <param name="session">Disconnecting session</param>
protected virtual void OnDisconnecting(SslSession session) {}
/// <summary>
/// Handle session disconnected notification
/// </summary>
/// <param name="session">Disconnected session</param>
Expand All @@ -453,8 +490,11 @@ protected virtual void OnDisconnected(SslSession session) {}
/// <param name="error">Socket error code</param>
protected virtual void OnError(SocketError error) {}

internal void OnConnectingInternal(SslSession session) { OnConnecting(session); }
internal void OnConnectedInternal(SslSession session) { OnConnected(session); }
internal void OnHandshakingInternal(SslSession session) { OnHandshaking(session); }
internal void OnHandshakedInternal(SslSession session) { OnHandshaked(session); }
internal void OnDisconnectingInternal(SslSession session) { OnDisconnecting(session); }
internal void OnDisconnectedInternal(SslSession session) { OnDisconnected(session); }

#endregion
Expand Down
30 changes: 30 additions & 0 deletions source/NetCoreServer/SslSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ internal void Connect(Socket socket)
BytesSent = 0;
BytesReceived = 0;

// Call the session connecting handler
OnConnecting();

// Call the session connecting handler in the server
Server.OnConnectingInternal(this);

// Update the connected flag
IsConnected = true;

Expand All @@ -129,6 +135,12 @@ internal void Connect(Socket socket)
_sslStreamId = Guid.NewGuid();
_sslStream = (Server.Context.CertificateValidationCallback != null) ? new SslStream(new NetworkStream(Socket, false), false, Server.Context.CertificateValidationCallback) : new SslStream(new NetworkStream(Socket, false), false);

// Call the session handshaking handler
OnHandshaking();

// Call the session handshaking handler in the server
Server.OnHandshakingInternal(this);

// Begin the SSL handshake
_sslStream.BeginAuthenticateAsServer(Server.Context.Certificate, Server.Context.ClientCertificateRequired, Server.Context.Protocols, false, ProcessHandshake, _sslStreamId);
}
Expand All @@ -154,6 +166,12 @@ public virtual bool Disconnect()
// Update the disconnecting flag
_disconnecting = true;

// Call the session disconnecting handler
OnDisconnecting();

// Call the session disconnecting handler in the server
Server.OnDisconnectingInternal(this);

try
{
try
Expand Down Expand Up @@ -645,15 +663,27 @@ private void ProcessSend(IAsyncResult result)

#region Session handlers

/// <summary>
/// Handle client connecting notification
/// </summary>
protected virtual void OnConnecting() {}
/// <summary>
/// Handle client connected notification
/// </summary>
protected virtual void OnConnected() {}
/// <summary>
/// Handle client handshaking notification
/// </summary>
protected virtual void OnHandshaking() {}
/// <summary>
/// Handle client handshaked notification
/// </summary>
protected virtual void OnHandshaked() {}
/// <summary>
/// Handle client disconnecting notification
/// </summary>
protected virtual void OnDisconnecting() {}
/// <summary>
/// Handle client disconnected notification
/// </summary>
protected virtual void OnDisconnected() {}
Expand Down
Loading

0 comments on commit b5f3507

Please sign in to comment.