exception when message cannot be send

This commit is contained in:
SCM6WE
2021-09-21 10:56:54 +02:00
parent 697a7ad506
commit 8097b9f2cf
5 changed files with 59 additions and 31 deletions

View File

@@ -21,8 +21,6 @@ namespace PolyChat.Models
this.Ip = Ip;
}
override
public string ToString()
{

View File

@@ -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();
}
/*
/// <summary>
/// Sends Message with new name
/// </summary>
@@ -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<ChatMessage>(BitConverter.ToString(Data[0].ToObject<byte[]>()));
//TODO: send message to GUI
});
connection.On(SendCode.Command.ToString(), (Data) =>
{
Console.WriteLine("Command recieved!" + Data[0]);

View File

@@ -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))
{
}
}
}

View File

@@ -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
//=================================================================================
//=========================================================================================================================================================================================
/// <summary>
/// connects self to server with given ip
@@ -43,6 +44,26 @@ namespace PolyChat.Models
this.getClient(ip).sendMessage(SendCode.Message, msg);
}
/// <summary>
/// returns own ip adress
/// </summary>
/// <returns></returns>
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;
}
/*
/// <summary>
/// changes name of self and sends new name to all chats
/// </summary>
@@ -55,10 +76,11 @@ namespace PolyChat.Models
cl.sendNameChange(SendCode.NameChange, newName);
}
}
*/
//=================================================================================
//=========================================================================================================================================================================================
//INTERNAL METHODS
//=================================================================================
//=========================================================================================================================================================================================
/// <summary>
/// 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;
}
}
}

View File

@@ -124,6 +124,7 @@
</Compile>
<Compile Include="Models\ChatMessage.cs" />
<Compile Include="Models\ChatPartner.cs" />
<Compile Include="Models\Exceptions\MessageTimedOutException.cs" />
<Compile Include="Models\NetworkingController.cs" />
<Compile Include="Models\Client.cs" />
<Compile Include="Models\ClientHandler.cs" />