From 8097b9f2cf6d615e4a6e1b3341ac7235eb8bbca2 Mon Sep 17 00:00:00 2001 From: SCM6WE Date: Tue, 21 Sep 2021 10:56:54 +0200 Subject: [PATCH] exception when message cannot be send --- PolyChat/Models/ChatMessage.cs | 2 - PolyChat/Models/Client.cs | 18 ++++++- .../Exceptions/MessageTimedOutException.cs | 16 ++++++ PolyChat/Models/NetworkingController.cs | 53 +++++++++---------- PolyChat/PolyChat.csproj | 1 + 5 files changed, 59 insertions(+), 31 deletions(-) create mode 100644 PolyChat/Models/Exceptions/MessageTimedOutException.cs diff --git a/PolyChat/Models/ChatMessage.cs b/PolyChat/Models/ChatMessage.cs index 96ad8ff..60cb6d0 100644 --- a/PolyChat/Models/ChatMessage.cs +++ b/PolyChat/Models/ChatMessage.cs @@ -21,8 +21,6 @@ namespace PolyChat.Models this.Ip = Ip; } - - override public string ToString() { diff --git a/PolyChat/Models/Client.cs b/PolyChat/Models/Client.cs index e2df56e..748965f 100644 --- a/PolyChat/Models/Client.cs +++ b/PolyChat/Models/Client.cs @@ -12,6 +12,7 @@ using Json.Net; using System.Net; using SocketIOSharp.Common.Packet; using System.Threading; +using PolyChat.Models.Exceptions; namespace PolyChat.Models { @@ -49,11 +50,22 @@ namespace PolyChat.Models //convert msg String petJson = JsonNet.Serialize(msg); - //send msg + //wait if not connected and send msg + int i=0; + int sleeptimer = 2000; + while(!this.connected) + { + Thread.Sleep(sleeptimer); + i++; + if(i>=10) + { + throw new MessageTimedOutException(i*sleeptimer); + } + } connection.Emit(code.ToString(), petJson); }).Start(); } - + /* /// /// Sends Message with new name /// @@ -74,6 +86,7 @@ namespace PolyChat.Models connection.Emit(code.ToString(), petJson); }).Start(); } + */ //================================================================================== //EventHandeling @@ -91,6 +104,7 @@ namespace PolyChat.Models ChatMessage pet = JsonNet.Deserialize(BitConverter.ToString(Data[0].ToObject())); //TODO: send message to GUI }); + connection.On(SendCode.Command.ToString(), (Data) => { Console.WriteLine("Command recieved!" + Data[0]); diff --git a/PolyChat/Models/Exceptions/MessageTimedOutException.cs b/PolyChat/Models/Exceptions/MessageTimedOutException.cs new file mode 100644 index 0000000..77e7355 --- /dev/null +++ b/PolyChat/Models/Exceptions/MessageTimedOutException.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PolyChat.Models.Exceptions +{ + public class MessageTimedOutException : Exception + { + public MessageTimedOutException(int seconds) : base(String.Format("After {0} seconds of trying to send the message it has timed out", seconds)) + { + + } + } +} diff --git a/PolyChat/Models/NetworkingController.cs b/PolyChat/Models/NetworkingController.cs index 90183ba..0b09d2f 100644 --- a/PolyChat/Models/NetworkingController.cs +++ b/PolyChat/Models/NetworkingController.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using SocketIOSharp.Client; using EngineIOSharp.Common.Enum; using System.Net; +using PolyChat.Models.Exceptions; namespace PolyChat.Models { @@ -20,7 +21,7 @@ namespace PolyChat.Models } //EXTERNAL METHODS - //================================================================================= + //========================================================================================================================================================================================= /// /// connects self to server with given ip @@ -43,6 +44,26 @@ namespace PolyChat.Models this.getClient(ip).sendMessage(SendCode.Message, msg); } + /// + /// returns own ip adress + /// + /// + public IPAddress getIP() + { + IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName()); + IPAddress[] addrList = ipEntry.AddressList; + + for (short i = 0; i < addrList.Length; i++) + { + if (addrList[i].ToString().Substring(0, 3).Equals("10.")) + { + return addrList[i]; + } + } + return null; + } + + /* /// /// changes name of self and sends new name to all chats /// @@ -55,11 +76,12 @@ namespace PolyChat.Models cl.sendNameChange(SendCode.NameChange, newName); } } + */ - //================================================================================= + //========================================================================================================================================================================================= //INTERNAL METHODS - //================================================================================= - + //========================================================================================================================================================================================= + /// /// returns client that fits to ip adress /// @@ -76,28 +98,5 @@ namespace PolyChat.Models } return null; } - - private IPAddress getIP() - { - IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName()); - IPAddress[] addrList = ipEntry.AddressList; - - for (short i = 0; i < addrList.Length; i++) - { - if (addrList[i].ToString().Substring(0, 3).Equals("10.")) - { - return addrList[i]; - //get ip as byte array - /* - byte[] ba = System.Text.Encoding.ASCII.GetBytes(addrList[i].ToString()); - foreach (var item in ba) - { - Console.Write(item.ToString() + ","); - } - */ - } - } - return null; - } } } diff --git a/PolyChat/PolyChat.csproj b/PolyChat/PolyChat.csproj index d4c68d8..851d98e 100644 --- a/PolyChat/PolyChat.csproj +++ b/PolyChat/PolyChat.csproj @@ -124,6 +124,7 @@ +