| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8a8679d commit b5f3507
16 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,5 @@ | |||
| 1 | 1 | # Specify version format | |
| 2 | - version: "5.0.7.{build}" | ||
| 2 | + version: "5.0.9.{build}" | ||
| 3 | 3 | ||
| 4 | 4 | # Image to use | |
| 5 | 5 | image: Visual Studio 2019 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,10 +7,9 @@ public interface IWebSocket | |||
| 7 | 7 | /// <summary> | |
| 8 | 8 | /// Handle WebSocket client connecting notification | |
| 9 | 9 | /// </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> | ||
| 11 | 11 | /// <param name="request">WebSocket upgrade HTTP request</param> | |
| 12 | 12 | void OnWsConnecting(HttpRequest request) {} | |
| 13 | - | ||
| 14 | 13 | /// <summary> | |
| 15 | 14 | /// Handle WebSocket client connected notification | |
| 16 | 15 | /// </summary> | |
@@ -20,18 +19,21 @@ void OnWsConnected(HttpResponse response) {} | |||
| 20 | 19 | /// <summary> | |
| 21 | 20 | /// Handle WebSocket server session validating notification | |
| 22 | 21 | /// </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> | ||
| 24 | 23 | /// <param name="request">WebSocket upgrade HTTP request</param> | |
| 25 | 24 | /// <param name="response">WebSocket upgrade HTTP response</param> | |
| 26 | 25 | /// <returns>return 'true' if the WebSocket update request is valid, 'false' if the WebSocket update request is not valid</returns> | |
| 27 | 26 | bool OnWsConnecting(HttpRequest request, HttpResponse response) { return true; } | |
| 28 | - | ||
| 29 | 27 | /// <summary> | |
| 30 | 28 | /// Handle WebSocket server session connected notification | |
| 31 | 29 | /// </summary> | |
| 32 | 30 | /// <param name="request">WebSocket upgrade HTTP request</param> | |
| 33 | 31 | void OnWsConnected(HttpRequest request) {} | |
| 34 | 32 | ||
| 33 | + /// <summary> | ||
| 34 | + /// Handle WebSocket client disconnecting notification | ||
| 35 | + /// </summary> | ||
| 36 | + void OnWsDisconnecting() {} | ||
| 35 | 37 | /// <summary> | |
| 36 | 38 | /// Handle WebSocket client disconnected notification | |
| 37 | 39 | /// </summary> | |
@@ -85,6 +87,6 @@ void OnWsError(SocketError error) {} | |||
| 85 | 87 | /// Send WebSocket server upgrade response | |
| 86 | 88 | /// </summary> | |
| 87 | 89 | /// <param name="response">WebSocket upgrade HTTP response</param> | |
| 88 | - void SendResponse(HttpResponse response) {} | ||
| 90 | + void SendUpgrade(HttpResponse response) {} | ||
| 89 | 91 | } | |
| 90 | 92 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | 3 | <PropertyGroup> | |
| 4 | 4 | <TargetFramework>net5.0</TargetFramework> | |
| 5 | - <Version>5.0.7.0</Version> | ||
| 5 | + <Version>5.0.9.0</Version> | ||
| 6 | 6 | <Authors>Ivan Shynkarenka</Authors> | |
| 7 | 7 | <Copyright>Copyright (c) 2019-2021 Ivan Shynkarenka</Copyright> | |
| 8 | 8 | <RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -178,23 +178,37 @@ public virtual bool Connect() | |||
| 178 | 178 | if (Socket.AddressFamily == AddressFamily.InterNetworkV6) | |
| 179 | 179 | Socket.DualMode = OptionDualMode; | |
| 180 | 180 | ||
| 181 | + // Call the client connecting handler | ||
| 182 | + OnConnecting(); | ||
| 183 | + | ||
| 181 | 184 | try | |
| 182 | 185 | { | |
| 183 | 186 | // Connect to the server | |
| 184 | 187 | Socket.Connect(Endpoint); | |
| 185 | 188 | } | |
| 186 | 189 | catch (SocketException ex) | |
| 187 | 190 | { | |
| 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 | + | ||
| 188 | 200 | // Close the client socket | |
| 189 | 201 | Socket.Close(); | |
| 202 | + | ||
| 190 | 203 | // Dispose the client socket | |
| 191 | 204 | Socket.Dispose(); | |
| 205 | + | ||
| 192 | 206 | // Dispose event arguments | |
| 193 | 207 | _connectEventArg.Dispose(); | |
| 194 | 208 | ||
| 195 | 209 | // Call the client disconnected handler | |
| 196 | - SendError(ex.SocketErrorCode); | ||
| 197 | 210 | OnDisconnected(); | |
| 211 | + | ||
| 198 | 212 | return false; | |
| 199 | 213 | } | |
| 200 | 214 | ||
@@ -231,6 +245,9 @@ public virtual bool Connect() | |||
| 231 | 245 | _sslStreamId = Guid.NewGuid(); | |
| 232 | 246 | _sslStream = (Context.CertificateValidationCallback != null) ? new SslStream(new NetworkStream(Socket, false), false, Context.CertificateValidationCallback) : new SslStream(new NetworkStream(Socket, false), false); | |
| 233 | 247 | ||
| 248 | + // Call the session handshaking handler | ||
| 249 | + OnHandshaking(); | ||
| 250 | + | ||
| 234 | 251 | // SSL handshake | |
| 235 | 252 | _sslStream.AuthenticateAsClient(Address, Context.Certificates ?? new X509CertificateCollection(new[] { Context.Certificate }), Context.Protocols, true); | |
| 236 | 253 | } | |
@@ -280,6 +297,9 @@ public virtual bool Disconnect() | |||
| 280 | 297 | // Reset event args | |
| 281 | 298 | _connectEventArg.Completed -= OnAsyncCompleted; | |
| 282 | 299 | ||
| 300 | + // Call the client disconnecting handler | ||
| 301 | + OnDisconnecting(); | ||
| 302 | + | ||
| 283 | 303 | try | |
| 284 | 304 | { | |
| 285 | 305 | try | |
@@ -374,8 +394,13 @@ public virtual bool ConnectAsync() | |||
| 374 | 394 | if (Socket.AddressFamily == AddressFamily.InterNetworkV6) | |
| 375 | 395 | Socket.DualMode = OptionDualMode; | |
| 376 | 396 | ||
| 377 | - // Async connect to the server | ||
| 397 | + // Update the connecting flag | ||
| 378 | 398 | IsConnecting = true; | |
| 399 | + | ||
| 400 | + // Call the client connecting handler | ||
| 401 | + OnConnecting(); | ||
| 402 | + | ||
| 403 | + // Async connect to the server | ||
| 379 | 404 | if (!Socket.ConnectAsync(_connectEventArg)) | |
| 380 | 405 | ProcessConnect(_connectEventArg); | |
| 381 | 406 | ||
@@ -690,6 +715,9 @@ private void ClearBuffers() | |||
| 690 | 715 | /// </summary> | |
| 691 | 716 | private void OnAsyncCompleted(object sender, SocketAsyncEventArgs e) | |
| 692 | 717 | { | |
| 718 | + if (IsSocketDisposed) | ||
| 719 | + return; | ||
| 720 | + | ||
| 693 | 721 | // Determine which type of operation just completed and call the associated handler | |
| 694 | 722 | switch (e.LastOperation) | |
| 695 | 723 | { | |
@@ -741,6 +769,9 @@ private void ProcessConnect(SocketAsyncEventArgs e) | |||
| 741 | 769 | _sslStreamId = Guid.NewGuid(); | |
| 742 | 770 | _sslStream = (Context.CertificateValidationCallback != null) ? new SslStream(new NetworkStream(Socket, false), false, Context.CertificateValidationCallback) : new SslStream(new NetworkStream(Socket, false), false); | |
| 743 | 771 | ||
| 772 | + // Call the session handshaking handler | ||
| 773 | + OnHandshaking(); | ||
| 774 | + | ||
| 744 | 775 | // Begin the SSL handshake | |
| 745 | 776 | IsHandshaking = true; | |
| 746 | 777 | _sslStream.BeginAuthenticateAsClient(Address, Context.Certificates ?? new X509CertificateCollection(new[] { Context.Certificate }), Context.Protocols, true, ProcessHandshake, _sslStreamId); | |
@@ -905,15 +936,27 @@ private void ProcessSend(IAsyncResult result) | |||
| 905 | 936 | ||
| 906 | 937 | #region Session handlers | |
| 907 | 938 | ||
| 939 | + /// <summary> | ||
| 940 | + /// Handle client connecting notification | ||
| 941 | + /// </summary> | ||
| 942 | + protected virtual void OnConnecting() {} | ||
| 908 | 943 | /// <summary> | |
| 909 | 944 | /// Handle client connected notification | |
| 910 | 945 | /// </summary> | |
| 911 | 946 | protected virtual void OnConnected() {} | |
| 912 | 947 | /// <summary> | |
| 948 | + /// Handle client handshaking notification | ||
| 949 | + /// </summary> | ||
| 950 | + protected virtual void OnHandshaking() {} | ||
| 951 | + /// <summary> | ||
| 913 | 952 | /// Handle client handshaked notification | |
| 914 | 953 | /// </summary> | |
| 915 | 954 | protected virtual void OnHandshaked() {} | |
| 916 | 955 | /// <summary> | |
| 956 | + /// Handle client disconnecting notification | ||
| 957 | + /// </summary> | ||
| 958 | + protected virtual void OnDisconnecting() {} | ||
| 959 | + /// <summary> | ||
| 917 | 960 | /// Handle client disconnected notification | |
| 918 | 961 | /// </summary> | |
| 919 | 962 | protected virtual void OnDisconnected() {} | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -188,6 +188,10 @@ public virtual bool Start() | |||
| 188 | 188 | _acceptorSocket.Bind(Endpoint); | |
| 189 | 189 | // Refresh the endpoint property based on the actual endpoint created | |
| 190 | 190 | Endpoint = (IPEndPoint)_acceptorSocket.LocalEndPoint; | |
| 191 | + | ||
| 192 | + // Call the server starting handler | ||
| 193 | + OnStarting(); | ||
| 194 | + | ||
| 191 | 195 | // Start listen to the acceptor socket with the given accepting backlog size | |
| 192 | 196 | _acceptorSocket.Listen(OptionAcceptorBacklog); | |
| 193 | 197 | ||
@@ -225,17 +229,24 @@ public virtual bool Stop() | |||
| 225 | 229 | // Reset acceptor event arg | |
| 226 | 230 | _acceptorEventArg.Completed -= OnAsyncCompleted; | |
| 227 | 231 | ||
| 228 | - // Close the acceptor socket | ||
| 229 | - _acceptorSocket.Close(); | ||
| 232 | + // Call the server stopping handler | ||
| 233 | + OnStopping(); | ||
| 230 | 234 | ||
| 231 | - // Dispose the acceptor socket | ||
| 232 | - _acceptorSocket.Dispose(); | ||
| 235 | + try | ||
| 236 | + { | ||
| 237 | + // Close the acceptor socket | ||
| 238 | + _acceptorSocket.Close(); | ||
| 233 | 239 | ||
| 234 | - // Dispose event arguments | ||
| 235 | - _acceptorEventArg.Dispose(); | ||
| 240 | + // Dispose the acceptor socket | ||
| 241 | + _acceptorSocket.Dispose(); | ||
| 236 | 242 | ||
| 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) {} | ||
| 239 | 250 | ||
| 240 | 251 | // Disconnect all sessions | |
| 241 | 252 | DisconnectAll(); | |
@@ -311,6 +322,9 @@ private void ProcessAccept(SocketAsyncEventArgs e) | |||
| 311 | 322 | /// </summary> | |
| 312 | 323 | private void OnAsyncCompleted(object sender, SocketAsyncEventArgs e) | |
| 313 | 324 | { | |
| 325 | + if (IsSocketDisposed) | ||
| 326 | + return; | ||
| 327 | + | ||
| 314 | 328 | ProcessAccept(e); | |
| 315 | 329 | } | |
| 316 | 330 | ||
@@ -422,26 +436,49 @@ public virtual bool Multicast(byte[] buffer, long offset, long size) | |||
| 422 | 436 | ||
| 423 | 437 | #region Server handlers | |
| 424 | 438 | ||
| 439 | + /// <summary> | ||
| 440 | + /// Handle server starting notification | ||
| 441 | + /// </summary> | ||
| 442 | + protected virtual void OnStarting() {} | ||
| 425 | 443 | /// <summary> | |
| 426 | 444 | /// Handle server started notification | |
| 427 | 445 | /// </summary> | |
| 428 | 446 | protected virtual void OnStarted() {} | |
| 429 | 447 | /// <summary> | |
| 448 | + /// Handle server stopping notification | ||
| 449 | + /// </summary> | ||
| 450 | + protected virtual void OnStopping() {} | ||
| 451 | + /// <summary> | ||
| 430 | 452 | /// Handle server stopped notification | |
| 431 | 453 | /// </summary> | |
| 432 | 454 | protected virtual void OnStopped() {} | |
| 433 | 455 | ||
| 456 | + /// <summary> | ||
| 457 | + /// Handle session connecting notification | ||
| 458 | + /// </summary> | ||
| 459 | + /// <param name="session">Connecting session</param> | ||
| 460 | + protected virtual void OnConnecting(SslSession session) {} | ||
| 434 | 461 | /// <summary> | |
| 435 | 462 | /// Handle session connected notification | |
| 436 | 463 | /// </summary> | |
| 437 | 464 | /// <param name="session">Connected session</param> | |
| 438 | 465 | protected virtual void OnConnected(SslSession session) {} | |
| 439 | 466 | /// <summary> | |
| 467 | + /// Handle session handshaking notification | ||
| 468 | + /// </summary> | ||
| 469 | + /// <param name="session">Handshaking session</param> | ||
| 470 | + protected virtual void OnHandshaking(SslSession session) {} | ||
| 471 | + /// <summary> | ||
| 440 | 472 | /// Handle session handshaked notification | |
| 441 | 473 | /// </summary> | |
| 442 | 474 | /// <param name="session">Handshaked session</param> | |
| 443 | 475 | protected virtual void OnHandshaked(SslSession session) {} | |
| 444 | 476 | /// <summary> | |
| 477 | + /// Handle session disconnecting notification | ||
| 478 | + /// </summary> | ||
| 479 | + /// <param name="session">Disconnecting session</param> | ||
| 480 | + protected virtual void OnDisconnecting(SslSession session) {} | ||
| 481 | + /// <summary> | ||
| 445 | 482 | /// Handle session disconnected notification | |
| 446 | 483 | /// </summary> | |
| 447 | 484 | /// <param name="session">Disconnected session</param> | |
@@ -453,8 +490,11 @@ protected virtual void OnDisconnected(SslSession session) {} | |||
| 453 | 490 | /// <param name="error">Socket error code</param> | |
| 454 | 491 | protected virtual void OnError(SocketError error) {} | |
| 455 | 492 | ||
| 493 | + internal void OnConnectingInternal(SslSession session) { OnConnecting(session); } | ||
| 456 | 494 | internal void OnConnectedInternal(SslSession session) { OnConnected(session); } | |
| 495 | + internal void OnHandshakingInternal(SslSession session) { OnHandshaking(session); } | ||
| 457 | 496 | internal void OnHandshakedInternal(SslSession session) { OnHandshaked(session); } | |
| 497 | + internal void OnDisconnectingInternal(SslSession session) { OnDisconnecting(session); } | ||
| 458 | 498 | internal void OnDisconnectedInternal(SslSession session) { OnDisconnected(session); } | |
| 459 | 499 | ||
| 460 | 500 | #endregion | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -114,6 +114,12 @@ internal void Connect(Socket socket) | |||
| 114 | 114 | BytesSent = 0; | |
| 115 | 115 | BytesReceived = 0; | |
| 116 | 116 | ||
| 117 | + // Call the session connecting handler | ||
| 118 | + OnConnecting(); | ||
| 119 | + | ||
| 120 | + // Call the session connecting handler in the server | ||
| 121 | + Server.OnConnectingInternal(this); | ||
| 122 | + | ||
| 117 | 123 | // Update the connected flag | |
| 118 | 124 | IsConnected = true; | |
| 119 | 125 | ||
@@ -129,6 +135,12 @@ internal void Connect(Socket socket) | |||
| 129 | 135 | _sslStreamId = Guid.NewGuid(); | |
| 130 | 136 | _sslStream = (Server.Context.CertificateValidationCallback != null) ? new SslStream(new NetworkStream(Socket, false), false, Server.Context.CertificateValidationCallback) : new SslStream(new NetworkStream(Socket, false), false); | |
| 131 | 137 | ||
| 138 | + // Call the session handshaking handler | ||
| 139 | + OnHandshaking(); | ||
| 140 | + | ||
| 141 | + // Call the session handshaking handler in the server | ||
| 142 | + Server.OnHandshakingInternal(this); | ||
| 143 | + | ||
| 132 | 144 | // Begin the SSL handshake | |
| 133 | 145 | _sslStream.BeginAuthenticateAsServer(Server.Context.Certificate, Server.Context.ClientCertificateRequired, Server.Context.Protocols, false, ProcessHandshake, _sslStreamId); | |
| 134 | 146 | } | |
@@ -154,6 +166,12 @@ public virtual bool Disconnect() | |||
| 154 | 166 | // Update the disconnecting flag | |
| 155 | 167 | _disconnecting = true; | |
| 156 | 168 | ||
| 169 | + // Call the session disconnecting handler | ||
| 170 | + OnDisconnecting(); | ||
| 171 | + | ||
| 172 | + // Call the session disconnecting handler in the server | ||
| 173 | + Server.OnDisconnectingInternal(this); | ||
| 174 | + | ||
| 157 | 175 | try | |
| 158 | 176 | { | |
| 159 | 177 | try | |
@@ -645,15 +663,27 @@ private void ProcessSend(IAsyncResult result) | |||
| 645 | 663 | ||
| 646 | 664 | #region Session handlers | |
| 647 | 665 | ||
| 666 | + /// <summary> | ||
| 667 | + /// Handle client connecting notification | ||
| 668 | + /// </summary> | ||
| 669 | + protected virtual void OnConnecting() {} | ||
| 648 | 670 | /// <summary> | |
| 649 | 671 | /// Handle client connected notification | |
| 650 | 672 | /// </summary> | |
| 651 | 673 | protected virtual void OnConnected() {} | |
| 652 | 674 | /// <summary> | |
| 675 | + /// Handle client handshaking notification | ||
| 676 | + /// </summary> | ||
| 677 | + protected virtual void OnHandshaking() {} | ||
| 678 | + /// <summary> | ||
| 653 | 679 | /// Handle client handshaked notification | |
| 654 | 680 | /// </summary> | |
| 655 | 681 | protected virtual void OnHandshaked() {} | |
| 656 | 682 | /// <summary> | |
| 683 | + /// Handle client disconnecting notification | ||
| 684 | + /// </summary> | ||
| 685 | + protected virtual void OnDisconnecting() {} | ||
| 686 | + /// <summary> | ||
| 657 | 687 | /// Handle client disconnected notification | |
| 658 | 688 | /// </summary> | |
| 659 | 689 | protected virtual void OnDisconnected() {} | |
| Back | FazBrowse Home | New Git URL |
0 commit comments