From ee9f3fbe3a34ea0f9b941bcdddf7ed2cb2e81da3 Mon Sep 17 00:00:00 2001 From: edmand46 Date: Thu, 19 Oct 2023 20:20:40 +0300 Subject: [PATCH] :bug: crash on abnormal disconnecting from websocket server --- .dockerignore | 25 ++++++++++++ .gitignore | 1 + README.md | 2 +- .../Sources/WebSocketServer.cs | 39 ++++++++++++------- Ragon.Server/Sources/IRagonServer.cs | 3 ++ .../Sources/Plugin/Web/RagonWebHookPlugin.cs | 6 +-- Ragon.Server/Sources/RagonServer.cs | 2 +- global.json | 7 ++++ 8 files changed, 66 insertions(+), 19 deletions(-) create mode 100644 .dockerignore create mode 100644 global.json diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..cd967fc --- /dev/null +++ b/.dockerignore @@ -0,0 +1,25 @@ +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.idea +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/.gitignore b/.gitignore index dfc25ee..e4b6e9b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .DS_Store .idea .vs +.vscode obj bin *.user diff --git a/README.md b/README.md index 715fa89..c824fb7 100755 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -

+

diff --git a/Ragon.Server.WebSocketServer/Sources/WebSocketServer.cs b/Ragon.Server.WebSocketServer/Sources/WebSocketServer.cs index e2195f9..8ab2a46 100644 --- a/Ragon.Server.WebSocketServer/Sources/WebSocketServer.cs +++ b/Ragon.Server.WebSocketServer/Sources/WebSocketServer.cs @@ -47,22 +47,32 @@ public class WebSocketServer : INetworkServer { while (!cancellationToken.IsCancellationRequested) { - var context = await _httpListener.GetContextAsync(); - if (!context.Request.IsWebSocketRequest) + WebSocketConnection connection = null!; + try { - context.Response.StatusCode = 200; - context.Response.ContentLength64 = 0; - context.Response.Close(); - continue; + var context = await _httpListener.GetContextAsync(); + if (!context.Request.IsWebSocketRequest) + { + context.Response.StatusCode = 200; + context.Response.ContentLength64 = 0; + context.Response.Close(); + continue; + } + + var webSocketContext = await context.AcceptWebSocketAsync(null); + var webSocket = webSocketContext.WebSocket; + var peerId = _sequencer.Pop(); + + connection = new WebSocketConnection(webSocket, peerId); } - - var webSocketContext = await context.AcceptWebSocketAsync(null); - var webSocket = webSocketContext.WebSocket; - - var peerId = _sequencer.Pop(); - var connection = new WebSocketConnection(webSocket, peerId); - - _connections[peerId] = connection; + catch (Exception ex) + { + _logger.Warn(ex); + continue; + } + + _connections[connection.Id] = connection; + StartListen(connection, cancellationToken); } } @@ -97,6 +107,7 @@ public class WebSocketServer : INetworkServer _sequencer.Push(connection.Id); _activeConnections.Remove(connection); + _networkListener.OnDisconnected(connection); } diff --git a/Ragon.Server/Sources/IRagonServer.cs b/Ragon.Server/Sources/IRagonServer.cs index cffc960..967de96 100644 --- a/Ragon.Server/Sources/IRagonServer.cs +++ b/Ragon.Server/Sources/IRagonServer.cs @@ -14,6 +14,8 @@ * limitations under the License. */ +using Ragon.Protocol; +using Ragon.Server.Handler; using Ragon.Server.IO; using Ragon.Server.Lobby; @@ -21,6 +23,7 @@ namespace Ragon.Server; public interface IRagonServer { + BaseOperation ResolveHandler(RagonOperation operation); RagonLobbyPlayer? GetPlayerByConnection(INetworkConnection connection); RagonLobbyPlayer? GetPlayerById(string id); } \ No newline at end of file diff --git a/Ragon.Server/Sources/Plugin/Web/RagonWebHookPlugin.cs b/Ragon.Server/Sources/Plugin/Web/RagonWebHookPlugin.cs index 5dabada..37048a6 100644 --- a/Ragon.Server/Sources/Plugin/Web/RagonWebHookPlugin.cs +++ b/Ragon.Server/Sources/Plugin/Web/RagonWebHookPlugin.cs @@ -28,10 +28,10 @@ public class RagonWebHookPlugin { private Dictionary _webHooks; - private RagonServer _server; + private IRagonServer _server; private HttpClient _httpClient; - public RagonWebHookPlugin(RagonServer server, RagonServerConfiguration configuration) + public RagonWebHookPlugin(IRagonServer server, RagonServerConfiguration configuration) { _webHooks = new Dictionary(configuration.WebHooks); _httpClient = new HttpClient(); @@ -46,7 +46,7 @@ public class RagonWebHookPlugin var executor = context.Executor; executor.Run(async () => { - var authorizationOperation = (AuthorizationOperation) _server.ResolveOperation(RagonOperation.AUTHORIZE); + var authorizationOperation = (AuthorizationOperation) _server.ResolveHandler(RagonOperation.AUTHORIZE); var response = await _httpClient.PostAsync(new Uri(value), httpContent); if (response.StatusCode != HttpStatusCode.OK) { diff --git a/Ragon.Server/Sources/RagonServer.cs b/Ragon.Server/Sources/RagonServer.cs index 164cd47..a5b1a54 100644 --- a/Ragon.Server/Sources/RagonServer.cs +++ b/Ragon.Server/Sources/RagonServer.cs @@ -223,7 +223,7 @@ public class RagonServer : IRagonServer, INetworkListener _server.Broadcast(sendData, NetworkChannel.UNRELIABLE); } - public BaseOperation ResolveOperation(RagonOperation operation) + public BaseOperation ResolveHandler(RagonOperation operation) { return _handlers[(byte)operation]; } diff --git a/global.json b/global.json new file mode 100644 index 0000000..36e1a9e --- /dev/null +++ b/global.json @@ -0,0 +1,7 @@ +{ + "sdk": { + "version": "7.0.0", + "rollForward": "latestMajor", + "allowPrerelease": false + } +} \ No newline at end of file