From 3b667b3f0cb59287fe5411979e6ae4285a9a294c Mon Sep 17 00:00:00 2001 From: Dogukan Date: Thu, 22 Oct 2020 16:11:19 +0200 Subject: [PATCH] [ADDITION] tried to fix the async methods. --- Client/Client.cs | 6 ++++ Server/Models/ServerClient.cs | 44 ++++++++++++++++++++-------- Server/Models/ServerCommunication.cs | 2 +- SharedClientServer/JSONConvert.cs | 1 + 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 25f07c1..29833b2 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -48,6 +48,9 @@ namespace Client 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) @@ -140,6 +143,9 @@ namespace Client case JSONConvert.RANDOMWORD: //Flag byte for receiving the random word. + int lobbyId = JSONConvert.GetLobbyID(payload); + + if(data.Lobby?.ID == lobbyId) ViewModels.ViewModelGame.HandleRandomWord(JSONConvert.GetRandomWord(payload)); break; default: diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index 9d56daa..ac535ed 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -9,6 +9,8 @@ using System.Diagnostics; using System.IO; using System.Net.Sockets; using System.Text; +using System.Threading; +using System.Threading.Tasks; using static SharedClientServer.JSONConvert; namespace Server.Models @@ -22,7 +24,7 @@ namespace Server.Models private int totalBufferReceived = 0; public User User { get; set; } private ServerCommunication serverCom = ServerCommunication.INSTANCE; - + /// /// Constructor that creates a new serverclient object with the given tcp client. @@ -95,8 +97,8 @@ namespace Server.Models tcpClient.Close(); ServerCommunication.INSTANCE.ServerClientDisconnect(this); } - - + + } /// @@ -108,21 +110,21 @@ namespace Server.Models Debug.WriteLine($"Got message : {Encoding.ASCII.GetString(message)}"); byte id = message[4]; byte[] payload = new byte[message.Length - 5]; - Array.Copy(message,5,payload,0,message.Length-5); + Array.Copy(message, 5, payload, 0, message.Length - 5); Debug.WriteLine("[SERVERCLIENT] GOT STRING" + Encoding.ASCII.GetString(payload)); - switch(id) + switch (id) { - + case JSONConvert.LOGIN: // json log in username data string uName = JSONConvert.GetUsernameLogin(payload); - + if (uName != null) { User = new User(uName); User.Username = uName; Debug.WriteLine("[SERVERCLIENT] set username to " + uName); - + } break; case JSONConvert.MESSAGE: @@ -145,7 +147,7 @@ namespace Server.Models case JSONConvert.LOBBY: // lobby data LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload); - handleLobbyMessage(payload,l); + handleLobbyMessage(payload, l); break; case JSONConvert.CANVAS: Debug.WriteLine("GOT A MESSAGE FROM THE CLIENT ABOUT THE CANVAS!!!"); @@ -179,15 +181,17 @@ namespace Server.Models break; case LobbyIdentifier.JOIN: int id = JSONConvert.GetLobbyID(payload); - ServerCommunication.INSTANCE.JoinLobby(this.User,id); + ServerCommunication.INSTANCE.JoinLobby(this.User, id); sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage()); + ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); - serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(RANDOMWORD, new + Task.Run(SendLobbyData); + + serverCom.sendToAll(JSONConvert.GetMessageToSend(JSONConvert.RANDOMWORD, new { + id = serverCom.GetLobbyForUser(User).ID, word = JSONConvert.SendRandomWord("WordsForGame.json") })); - - ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); break; case LobbyIdentifier.LEAVE: id = JSONConvert.GetLobbyID(payload); @@ -198,6 +202,20 @@ namespace Server.Models } } + private async void SendLobbyData() + { + string result = await WaitForData(); + if(result == "bruh momento") + { + } + } + + private async Task WaitForData() + { + await Task.Delay(1000); + return "bruh momento"; + } + /// /// sends a message to the tcp client /// diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index 4440096..c1ff0ee 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -90,7 +90,7 @@ namespace Server.Models /// send a message to all tcp clients in the list /// /// the message to send - public void sendToAll(byte[] message) + public async void sendToAll(byte[] message) { foreach (ServerClient sc in serverClients) { diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index c0f84af..1c7527b 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -184,6 +184,7 @@ namespace SharedClientServer int index = random.Next(0, 24); Debug.WriteLine($"[SERVERCLIENT] Sending random words {words}"); + return words.words[index]; }