FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Socket.RemoteEndPoint exception in OnWsDisconnected #116 · bubdm/NetCoreServer@b5f3507 · GitHub

Repository navigation

Commit b5f3507

Browse files
committed
Socket.RemoteEndPoint exception in OnWsDisconnected chronoxor#116
1 parent 8a8679d commit b5f3507

16 files changed

Lines changed: 337 additions & 318 deletions

‎.appveyor.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Specify version format
2-
version: "5.0.7.{build}"
2+
version: "5.0.9.{build}"
33

44
# Image to use
55
image: Visual Studio 2019

‎source/NetCoreServer/IWebSocket.cs‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ public interface IWebSocket
77
/// <summary>
88
/// Handle WebSocket client connecting notification
99
/// </summary>
10-
/// <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>
10+
/// <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>
1111
/// <param name="request">WebSocket upgrade HTTP request</param>
1212
void OnWsConnecting(HttpRequest request) {}
13-
1413
/// <summary>
1514
/// Handle WebSocket client connected notification
1615
/// </summary>
@@ -20,18 +19,21 @@ void OnWsConnected(HttpResponse response) {}
2019
/// <summary>
2120
/// Handle WebSocket server session validating notification
2221
/// </summary>
23-
/// <remarks>Notification is called when WebSocket client is connecting to the server.You can handle the connection and validate WebSocket upgrade HTTP request.</remarks>
22+
/// <remarks>Notification is called when WebSocket client is connecting to the server. You can handle the connection and validate WebSocket upgrade HTTP request.</remarks>
2423
/// <param name="request">WebSocket upgrade HTTP request</param>
2524
/// <param name="response">WebSocket upgrade HTTP response</param>
2625
/// <returns>return 'true' if the WebSocket update request is valid, 'false' if the WebSocket update request is not valid</returns>
2726
bool OnWsConnecting(HttpRequest request, HttpResponse response) { return true; }
28-
2927
/// <summary>
3028
/// Handle WebSocket server session connected notification
3129
/// </summary>
3230
/// <param name="request">WebSocket upgrade HTTP request</param>
3331
void OnWsConnected(HttpRequest request) {}
3432

33+
/// <summary>
34+
/// Handle WebSocket client disconnecting notification
35+
/// </summary>
36+
void OnWsDisconnecting() {}
3537
/// <summary>
3638
/// Handle WebSocket client disconnected notification
3739
/// </summary>
@@ -85,6 +87,6 @@ void OnWsError(SocketError error) {}
8587
/// Send WebSocket server upgrade response
8688
/// </summary>
8789
/// <param name="response">WebSocket upgrade HTTP response</param>
88-
void SendResponse(HttpResponse response) {}
90+
void SendUpgrade(HttpResponse response) {}
8991
}
9092
}

‎source/NetCoreServer/NetCoreServer.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5-
<Version>5.0.7.0</Version>
5+
<Version>5.0.9.0</Version>
66
<Authors>Ivan Shynkarenka</Authors>
77
<Copyright>Copyright (c) 2019-2021 Ivan Shynkarenka</Copyright>
88
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>

‎source/NetCoreServer/SslClient.cs‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,23 +178,37 @@ public virtual bool Connect()
178178
if (Socket.AddressFamily == AddressFamily.InterNetworkV6)
179179
Socket.DualMode = OptionDualMode;
180180

181+
// Call the client connecting handler
182+
OnConnecting();
183+
181184
try
182185
{
183186
// Connect to the server
184187
Socket.Connect(Endpoint);
185188
}
186189
catch (SocketException ex)
187190
{
191+
// Call the client error handler
192+
SendError(ex.SocketErrorCode);
193+
194+
// Reset event args
195+
_connectEventArg.Completed -= OnAsyncCompleted;
196+
197+
// Call the client disconnecting handler
198+
OnDisconnecting();
199+
188200
// Close the client socket
189201
Socket.Close();
202+
190203
// Dispose the client socket
191204
Socket.Dispose();
205+
192206
// Dispose event arguments
193207
_connectEventArg.Dispose();
194208

195209
// Call the client disconnected handler
196-
SendError(ex.SocketErrorCode);
197210
OnDisconnected();
211+
198212
return false;
199213
}
200214

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

248+
// Call the session handshaking handler
249+
OnHandshaking();
250+
234251
// SSL handshake
235252
_sslStream.AuthenticateAsClient(Address, Context.Certificates ?? new X509CertificateCollection(new[] { Context.Certificate }), Context.Protocols, true);
236253
}
@@ -280,6 +297,9 @@ public virtual bool Disconnect()
280297
// Reset event args
281298
_connectEventArg.Completed -= OnAsyncCompleted;
282299

300+
// Call the client disconnecting handler
301+
OnDisconnecting();
302+
283303
try
284304
{
285305
try
@@ -374,8 +394,13 @@ public virtual bool ConnectAsync()
374394
if (Socket.AddressFamily == AddressFamily.InterNetworkV6)
375395
Socket.DualMode = OptionDualMode;
376396

377-
// Async connect to the server
397+
// Update the connecting flag
378398
IsConnecting = true;
399+
400+
// Call the client connecting handler
401+
OnConnecting();
402+
403+
// Async connect to the server
379404
if (!Socket.ConnectAsync(_connectEventArg))
380405
ProcessConnect(_connectEventArg);
381406

@@ -690,6 +715,9 @@ private void ClearBuffers()
690715
/// </summary>
691716
private void OnAsyncCompleted(object sender, SocketAsyncEventArgs e)
692717
{
718+
if (IsSocketDisposed)
719+
return;
720+
693721
// Determine which type of operation just completed and call the associated handler
694722
switch (e.LastOperation)
695723
{
@@ -741,6 +769,9 @@ private void ProcessConnect(SocketAsyncEventArgs e)
741769
_sslStreamId = Guid.NewGuid();
742770
_sslStream = (Context.CertificateValidationCallback != null) ? new SslStream(new NetworkStream(Socket, false), false, Context.CertificateValidationCallback) : new SslStream(new NetworkStream(Socket, false), false);
743771

772+
// Call the session handshaking handler
773+
OnHandshaking();
774+
744775
// Begin the SSL handshake
745776
IsHandshaking = true;
746777
_sslStream.BeginAuthenticateAsClient(Address, Context.Certificates ?? new X509CertificateCollection(new[] { Context.Certificate }), Context.Protocols, true, ProcessHandshake, _sslStreamId);
@@ -905,15 +936,27 @@ private void ProcessSend(IAsyncResult result)
905936

906937
#region Session handlers
907938

939+
/// <summary>
940+
/// Handle client connecting notification
941+
/// </summary>
942+
protected virtual void OnConnecting() {}
908943
/// <summary>
909944
/// Handle client connected notification
910945
/// </summary>
911946
protected virtual void OnConnected() {}
912947
/// <summary>
948+
/// Handle client handshaking notification
949+
/// </summary>
950+
protected virtual void OnHandshaking() {}
951+
/// <summary>
913952
/// Handle client handshaked notification
914953
/// </summary>
915954
protected virtual void OnHandshaked() {}
916955
/// <summary>
956+
/// Handle client disconnecting notification
957+
/// </summary>
958+
protected virtual void OnDisconnecting() {}
959+
/// <summary>
917960
/// Handle client disconnected notification
918961
/// </summary>
919962
protected virtual void OnDisconnected() {}

‎source/NetCoreServer/SslServer.cs‎

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ public virtual bool Start()
188188
_acceptorSocket.Bind(Endpoint);
189189
// Refresh the endpoint property based on the actual endpoint created
190190
Endpoint = (IPEndPoint)_acceptorSocket.LocalEndPoint;
191+
192+
// Call the server starting handler
193+
OnStarting();
194+
191195
// Start listen to the acceptor socket with the given accepting backlog size
192196
_acceptorSocket.Listen(OptionAcceptorBacklog);
193197

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

228-
// Close the acceptor socket
229-
_acceptorSocket.Close();
232+
// Call the server stopping handler
233+
OnStopping();
230234

231-
// Dispose the acceptor socket
232-
_acceptorSocket.Dispose();
235+
try
236+
{
237+
// Close the acceptor socket
238+
_acceptorSocket.Close();
233239

234-
// Dispose event arguments
235-
_acceptorEventArg.Dispose();
240+
// Dispose the acceptor socket
241+
_acceptorSocket.Dispose();
236242

237-
// Update the acceptor socket disposed flag
238-
IsSocketDisposed = true;
243+
// Dispose event arguments
244+
_acceptorEventArg.Dispose();
245+
246+
// Update the acceptor socket disposed flag
247+
IsSocketDisposed = true;
248+
}
249+
catch (ObjectDisposedException) {}
239250

240251
// Disconnect all sessions
241252
DisconnectAll();
@@ -311,6 +322,9 @@ private void ProcessAccept(SocketAsyncEventArgs e)
311322
/// </summary>
312323
private void OnAsyncCompleted(object sender, SocketAsyncEventArgs e)
313324
{
325+
if (IsSocketDisposed)
326+
return;
327+
314328
ProcessAccept(e);
315329
}
316330

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

423437
#region Server handlers
424438

439+
/// <summary>
440+
/// Handle server starting notification
441+
/// </summary>
442+
protected virtual void OnStarting() {}
425443
/// <summary>
426444
/// Handle server started notification
427445
/// </summary>
428446
protected virtual void OnStarted() {}
429447
/// <summary>
448+
/// Handle server stopping notification
449+
/// </summary>
450+
protected virtual void OnStopping() {}
451+
/// <summary>
430452
/// Handle server stopped notification
431453
/// </summary>
432454
protected virtual void OnStopped() {}
433455

456+
/// <summary>
457+
/// Handle session connecting notification
458+
/// </summary>
459+
/// <param name="session">Connecting session</param>
460+
protected virtual void OnConnecting(SslSession session) {}
434461
/// <summary>
435462
/// Handle session connected notification
436463
/// </summary>
437464
/// <param name="session">Connected session</param>
438465
protected virtual void OnConnected(SslSession session) {}
439466
/// <summary>
467+
/// Handle session handshaking notification
468+
/// </summary>
469+
/// <param name="session">Handshaking session</param>
470+
protected virtual void OnHandshaking(SslSession session) {}
471+
/// <summary>
440472
/// Handle session handshaked notification
441473
/// </summary>
442474
/// <param name="session">Handshaked session</param>
443475
protected virtual void OnHandshaked(SslSession session) {}
444476
/// <summary>
477+
/// Handle session disconnecting notification
478+
/// </summary>
479+
/// <param name="session">Disconnecting session</param>
480+
protected virtual void OnDisconnecting(SslSession session) {}
481+
/// <summary>
445482
/// Handle session disconnected notification
446483
/// </summary>
447484
/// <param name="session">Disconnected session</param>
@@ -453,8 +490,11 @@ protected virtual void OnDisconnected(SslSession session) {}
453490
/// <param name="error">Socket error code</param>
454491
protected virtual void OnError(SocketError error) {}
455492

493+
internal void OnConnectingInternal(SslSession session) { OnConnecting(session); }
456494
internal void OnConnectedInternal(SslSession session) { OnConnected(session); }
495+
internal void OnHandshakingInternal(SslSession session) { OnHandshaking(session); }
457496
internal void OnHandshakedInternal(SslSession session) { OnHandshaked(session); }
497+
internal void OnDisconnectingInternal(SslSession session) { OnDisconnecting(session); }
458498
internal void OnDisconnectedInternal(SslSession session) { OnDisconnected(session); }
459499

460500
#endregion

‎source/NetCoreServer/SslSession.cs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ internal void Connect(Socket socket)
114114
BytesSent = 0;
115115
BytesReceived = 0;
116116

117+
// Call the session connecting handler
118+
OnConnecting();
119+
120+
// Call the session connecting handler in the server
121+
Server.OnConnectingInternal(this);
122+
117123
// Update the connected flag
118124
IsConnected = true;
119125

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

138+
// Call the session handshaking handler
139+
OnHandshaking();
140+
141+
// Call the session handshaking handler in the server
142+
Server.OnHandshakingInternal(this);
143+
132144
// Begin the SSL handshake
133145
_sslStream.BeginAuthenticateAsServer(Server.Context.Certificate, Server.Context.ClientCertificateRequired, Server.Context.Protocols, false, ProcessHandshake, _sslStreamId);
134146
}
@@ -154,6 +166,12 @@ public virtual bool Disconnect()
154166
// Update the disconnecting flag
155167
_disconnecting = true;
156168

169+
// Call the session disconnecting handler
170+
OnDisconnecting();
171+
172+
// Call the session disconnecting handler in the server
173+
Server.OnDisconnectingInternal(this);
174+
157175
try
158176
{
159177
try
@@ -645,15 +663,27 @@ private void ProcessSend(IAsyncResult result)
645663

646664
#region Session handlers
647665

666+
/// <summary>
667+
/// Handle client connecting notification
668+
/// </summary>
669+
protected virtual void OnConnecting() {}
648670
/// <summary>
649671
/// Handle client connected notification
650672
/// </summary>
651673
protected virtual void OnConnected() {}
652674
/// <summary>
675+
/// Handle client handshaking notification
676+
/// </summary>
677+
protected virtual void OnHandshaking() {}
678+
/// <summary>
653679
/// Handle client handshaked notification
654680
/// </summary>
655681
protected virtual void OnHandshaked() {}
656682
/// <summary>
683+
/// Handle client disconnecting notification
684+
/// </summary>
685+
protected virtual void OnDisconnecting() {}
686+
/// <summary>
657687
/// Handle client disconnected notification
658688
/// </summary>
659689
protected virtual void OnDisconnected() {}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL