From 471247827b3f926122fffba34eab12b0e66a8c62 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Thu, 22 Oct 2020 13:13:57 +0200 Subject: [PATCH 1/5] [ADD] added host user check to join --- Server/Models/ServerCommunication.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index 1d22c27..44c92fe 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -207,6 +207,10 @@ namespace Server.Models { if (l.ID == id) { + if (l.Users.Count == 0) + { + user.Host = true; + } AddToLobby(l, user); Debug.WriteLine($"{user.Username} joined lobby with id {id}"); break; -- 2.47.2 From 0b72c4dc9b6ea4024175a7b67580af3037be2b7b Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Thu, 22 Oct 2020 13:22:52 +0200 Subject: [PATCH 2/5] [ADD] added that when you join an empty lobby you become the host --- Client/Client.cs | 6 ++++-- Client/ViewModels/ViewModel.cs | 6 +++--- Server/Models/ServerClient.cs | 5 +++-- Server/Models/ServerCommunication.cs | 4 +++- SharedClientServer/JSONConvert.cs | 11 +++++++++-- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index bc7b2b5..a86ba3e 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -9,6 +9,7 @@ using static SharedClientServer.JSONConvert; namespace Client { public delegate void LobbyCallback(int id); + public delegate void LobbyJoinCallback(bool isHost); class Client : ObservableObject { private TcpClient tcpClient; @@ -21,7 +22,7 @@ namespace Client private string username; public Callback OnSuccessfullConnect; public Callback OnLobbiesListReceived; - public Callback OnLobbyJoinSuccess; + public LobbyJoinCallback OnLobbyJoinSuccess; public Callback OnLobbiesReceivedAndWaitingForHost; public LobbyCallback OnLobbyCreated; public LobbyCallback OnLobbyLeave; @@ -124,7 +125,8 @@ namespace Client OnLobbyCreated?.Invoke(lobbyCreatedID); break; case LobbyIdentifier.JOIN_SUCCESS: - OnLobbyJoinSuccess?.Invoke(); + + OnLobbyJoinSuccess?.Invoke(JSONConvert.GetLobbyJoinIsHost(payload)); break; case LobbyIdentifier.LEAVE: int lobbyLeaveID = JSONConvert.GetLobbyID(payload); diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs index 37d4ed9..32e65d8 100644 --- a/Client/ViewModels/ViewModel.cs +++ b/Client/ViewModels/ViewModel.cs @@ -85,14 +85,14 @@ namespace Client private void joinLobby() { - // lobby die je wilt joinen verwijderen - // nieuwe binnengekregen lobby toevoegen + client.OnLobbyJoinSuccess = OnLobbyJoinSuccess; client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID)); } - private void OnLobbyJoinSuccess() + private void OnLobbyJoinSuccess(bool isHost) { + ClientData.Instance.User.Host = isHost; startGameInLobby(); } diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index e264363..05eca4f 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -175,8 +175,9 @@ namespace Server.Models break; case LobbyIdentifier.JOIN: int id = JSONConvert.GetLobbyID(payload); - ServerCommunication.INSTANCE.JoinLobby(this.User,id); - sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage()); + bool isHost; + ServerCommunication.INSTANCE.JoinLobby(this.User,id, out isHost); + sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage(isHost)); ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); break; case LobbyIdentifier.LEAVE: diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index 44c92fe..dffe07f 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -201,8 +201,9 @@ namespace Server.Models return lobby.ID; } - public void JoinLobby(User user, int id) + public void JoinLobby(User user, int id, out bool isHost) { + isHost = false; foreach (Lobby l in lobbies) { if (l.ID == id) @@ -210,6 +211,7 @@ namespace Server.Models if (l.Users.Count == 0) { user.Host = true; + isHost = true; } AddToLobby(l, user); Debug.WriteLine($"{user.Username} joined lobby with id {id}"); diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index 9c96f2d..9ab0900 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -133,9 +133,16 @@ namespace SharedClientServer return dynamicAsObject.ToObject(); } - public static byte[] ConstructLobbyJoinSuccessMessage() + public static byte[] ConstructLobbyJoinSuccessMessage(bool isHost) { - return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS}); + return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS, + host = isHost}); + } + + public static bool GetLobbyJoinIsHost(byte[] json) + { + dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); + return payload.host; } #endregion -- 2.47.2 From d3bd1418d1e4c939581f04223a0fa4c479d7821b Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Thu, 22 Oct 2020 13:55:46 +0200 Subject: [PATCH 3/5] line --- Client/ViewModels/ViewModel.cs | 2 -- Server/Models/ServerClient.cs | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs index 37d4ed9..c1c91df 100644 --- a/Client/ViewModels/ViewModel.cs +++ b/Client/ViewModels/ViewModel.cs @@ -58,12 +58,10 @@ namespace Client private void becomeHostForLobby(int id) { - Debug.WriteLine($"got host succes with data {id} "); wantToBeHost = true; wantToBeHostId = id; client.OnLobbiesReceivedAndWaitingForHost = hostLobbiesReceived; - } private void hostLobbiesReceived() diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index 9bb0b93..2c063ea 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -92,6 +92,7 @@ namespace Server.Models } catch (IOException e) { + Debug.WriteLine("[SERVERCLIENT] Client disconnected! exception was " + e.Message); tcpClient.Close(); ServerCommunication.INSTANCE.ServerClientDisconnect(this); } -- 2.47.2 From bb289865b76022002f14014d90c01a25b62fe67a Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Thu, 22 Oct 2020 16:34:31 +0200 Subject: [PATCH 4/5] [ADD] added message received ok --- Client/Client.cs | 2 ++ Server/Models/ServerClient.cs | 6 ++++++ SharedClientServer/JSONConvert.cs | 1 + 3 files changed, 9 insertions(+) diff --git a/Client/Client.cs b/Client/Client.cs index 29833b2..f67228b 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -159,12 +159,14 @@ namespace Client { Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message)); stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWriteComplete), null); + } private void OnWriteComplete(IAsyncResult ar) { Debug.WriteLine("[CLIENT] finished writing"); stream.EndWrite(ar); + stream.Flush(); } } } diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index 01b723a..3c2bc89 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -15,6 +15,7 @@ using static SharedClientServer.JSONConvert; namespace Server.Models { + public delegate void Callback(); class ServerClient : ObservableObject { private TcpClient tcpClient; @@ -24,6 +25,7 @@ namespace Server.Models private int totalBufferReceived = 0; public User User { get; set; } private ServerCommunication serverCom = ServerCommunication.INSTANCE; + private Callback OnMessageReceivedOk; /// @@ -159,6 +161,10 @@ namespace Server.Models case JSONConvert.RANDOMWORD: //Flag byte for receiving the random word. break; + case JSONConvert.MESSAGE_RECEIVED: + // we now can send a new message + OnMessageReceivedOk?.Invoke(); + break; default: Debug.WriteLine("[SERVER] Received weird identifier: " + id); break; diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index 1c7527b..551c2ff 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -18,6 +18,7 @@ namespace SharedClientServer public const byte LOBBY = 0x03; public const byte CANVAS = 0x04; public const byte RANDOMWORD = 0x05; + public const byte MESSAGE_RECEIVED = 0x06; public enum LobbyIdentifier { -- 2.47.2 From eca17cc70f33690ef980d2f0845676037104df09 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Thu, 22 Oct 2020 17:00:26 +0200 Subject: [PATCH 5/5] [FIX] handled disconnects for everything --- Client/Client.cs | 72 +++++++++++++++++++++------------- Client/ViewModels/ViewModel.cs | 4 ++ 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index a4f9916..6d5e80b 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -2,8 +2,10 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Net.Sockets; using System.Text; +using System.Windows; using static SharedClientServer.JSONConvert; namespace Client @@ -23,6 +25,7 @@ namespace Client public Callback OnLobbiesListReceived; public Callback OnLobbyJoinSuccess; public Callback OnLobbiesReceivedAndWaitingForHost; + public Callback OnServerDisconnect; public LobbyCallback OnLobbyCreated; public LobbyCallback OnLobbyLeave; private ClientData data = ClientData.Instance; @@ -39,45 +42,60 @@ namespace Client private void OnConnect(IAsyncResult ar) { Debug.Write("finished connecting to server"); - this.tcpClient.EndConnect(ar); - this.stream = tcpClient.GetStream(); - OnSuccessfullConnect?.Invoke(); - SendMessage(JSONConvert.ConstructUsernameMessage(username)); - this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null); + try + { + this.tcpClient.EndConnect(ar); + this.stream = tcpClient.GetStream(); + OnSuccessfullConnect?.Invoke(); + SendMessage(JSONConvert.ConstructUsernameMessage(username)); + this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null); + + } catch (Exception e) + { + Debug.WriteLine("Can't connect, retrying..."); + tcpClient.BeginConnect("localhost", Port, new AsyncCallback(OnConnect), null); + } } private void OnReadComplete(IAsyncResult ar) { if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected) return; - - int amountReceived = stream.EndRead(ar); - - if (totalBufferReceived + amountReceived > 1024) + try { - throw new OutOfMemoryException("buffer too small"); - } + int amountReceived = stream.EndRead(ar); - Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, amountReceived); - totalBufferReceived += amountReceived; + if (totalBufferReceived + amountReceived > 1024) + { + throw new OutOfMemoryException("buffer too small"); + } - int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); + Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, amountReceived); + totalBufferReceived += amountReceived; - while (totalBufferReceived >= expectedMessageLength) + int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); + + while (totalBufferReceived >= expectedMessageLength) + { + // we have received the complete packet + byte[] message = new byte[expectedMessageLength]; + // put the message received into the message array + Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength); + + handleData(message); + + totalBufferReceived -= expectedMessageLength; + expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); + } + + ar.AsyncWaitHandle.WaitOne(); + stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null); + } catch (IOException e) { - // we have received the complete packet - byte[] message = new byte[expectedMessageLength]; - // put the message received into the message array - Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength); - - handleData(message); - - totalBufferReceived -= expectedMessageLength; - expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); + Debug.WriteLine("[CLIENT] server not responding! got error: " + e.Message); + OnServerDisconnect?.Invoke(); } - - ar.AsyncWaitHandle.WaitOne(); - stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null); + } private void handleData(byte[] message) diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs index c1c91df..55c909b 100644 --- a/Client/ViewModels/ViewModel.cs +++ b/Client/ViewModels/ViewModel.cs @@ -35,6 +35,10 @@ namespace Client client = ClientData.Instance.Client; client.OnLobbiesListReceived = updateLobbies; client.OnLobbyLeave = leaveLobby; + client.OnServerDisconnect = () => + { + Environment.Exit(0); + }; OnHostButtonClick = new RelayCommand(hostGame); -- 2.47.2