From e8a72e164fb85e0cbbdb9fa8a9ce50a70859f06f Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 21 Oct 2020 22:41:39 +0200 Subject: [PATCH 1/2] add try catch --- Server/Models/ServerClient.cs | 81 +++++++++++++++------------- Server/Models/ServerCommunication.cs | 6 +++ 2 files changed, 51 insertions(+), 36 deletions(-) diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index 843db9c..69875ba 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -6,6 +6,7 @@ using SharedClientServer; using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Net.Sockets; using System.Text; using static SharedClientServer.JSONConvert; @@ -45,48 +46,56 @@ namespace Server.Models if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected) return; - - int bytesReceived = this.stream.EndRead(ar); - - if (totalBufferReceived + bytesReceived > 1024) + try { - throw new OutOfMemoryException("buffer is too small!"); - } + int bytesReceived = this.stream.EndRead(ar); - // copy the received bytes into the buffer - Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, bytesReceived); - // add the bytes we received to the total amount - totalBufferReceived += bytesReceived; - - // calculate the expected length of the message - int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); - - while (totalBufferReceived >= expectedMessageLength) - { - // we have received the full packet - byte[] message = new byte[expectedMessageLength]; - // copy the total buffer contents into the message array so we can pass it to the handleIncomingMessage method - Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength); - HandleIncomingMessage(message); - - // move the contents of the totalbuffer to the start of the array - Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength)); - - // remove the length of the expected message from the total buffer - totalBufferReceived -= expectedMessageLength; - // and set the new expected length to the rest that is still in the buffer - expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); - - if (expectedMessageLength == 0) + if (totalBufferReceived + bytesReceived > 1024) { - break; - } + throw new OutOfMemoryException("buffer is too small!"); + } + // copy the received bytes into the buffer + Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, bytesReceived); + // add the bytes we received to the total amount + totalBufferReceived += bytesReceived; + + // calculate the expected length of the message + int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); + + while (totalBufferReceived >= expectedMessageLength) + { + // we have received the full packet + byte[] message = new byte[expectedMessageLength]; + // copy the total buffer contents into the message array so we can pass it to the handleIncomingMessage method + Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength); + HandleIncomingMessage(message); + + // move the contents of the totalbuffer to the start of the array + Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength)); + + // remove the length of the expected message from the total buffer + totalBufferReceived -= expectedMessageLength; + // and set the new expected length to the rest that is still in the buffer + expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); + + if (expectedMessageLength == 0) + { + break; + } + + + } + // start reading for a new message + stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); } - // start reading for a new message - stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); - + catch (IOException e) + { + tcpClient.Close(); + ServerCommunication.INSTANCE.ServerClientDisconnect(this); + } + } diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index c461aa1..0447bd2 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -97,6 +97,12 @@ namespace Server.Models } } + public void ServerClientDisconnect(ServerClient serverClient) + { + // remove from serverclientsinlobbies + // send leave message + } + public void SendToAllExcept(string username, byte[] message) { foreach (ServerClient sc in serverClients) From c150bf3611c08e2ad05410f430d6c9305b79b017 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 21 Oct 2020 22:54:37 +0200 Subject: [PATCH 2/2] [FIX] fixed handling client disconnect --- Server/Models/ServerCommunication.cs | 27 +++++++++++++++++++++++++-- Server/ViewModels/MainViewModel.cs | 4 ++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index 0447bd2..1d22c27 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -16,6 +16,7 @@ namespace Server.Models public bool Started = false; public List lobbies; private Dictionary> serverClientsInlobbies; + internal Action DisconnectClientAction; public Action newClientAction; @@ -99,8 +100,22 @@ namespace Server.Models public void ServerClientDisconnect(ServerClient serverClient) { - // remove from serverclientsinlobbies - // send leave message + Debug.WriteLine("[SERVERCOMM] handling disconnect"); + DisconnectClientAction?.Invoke(); + int id = -1; + foreach (Lobby l in serverClientsInlobbies.Keys) + { + if (serverClientsInlobbies[l].Contains(serverClient)) + { + id = l.ID; + }break; + } + + if (id != -1) + { + LeaveLobby(serverClient.User, id); + SendToAllExcept(serverClient, JSONConvert.ConstructLobbyLeaveMessage(id)); + } } public void SendToAllExcept(string username, byte[] message) @@ -111,6 +126,14 @@ namespace Server.Models } } + public void SendToAllExcept(ServerClient sc, byte[] message) + { + foreach (ServerClient s in serverClients) + { + if (s != sc) s.sendMessage(message); + } + } + public void SendToLobby(Lobby lobby, byte[] message) { foreach (Lobby l in lobbies) diff --git a/Server/ViewModels/MainViewModel.cs b/Server/ViewModels/MainViewModel.cs index ad06638..5d153b8 100644 --- a/Server/ViewModels/MainViewModel.cs +++ b/Server/ViewModels/MainViewModel.cs @@ -33,6 +33,10 @@ namespace Server.ViewModels { InformationModel.ClientsConnected++; }; + serverCommunication.DisconnectClientAction = () => + { + InformationModel.ClientsConnected--; + }; //BitmapImage onlineImg = new BitmapImage(new Uri(@"/img/online.png",UriKind.Relative)); //BitmapImage offlineImg = new BitmapImage(new Uri(@"/img/offline.png", UriKind.Relative));