New KeepAlive Added

Previous method of keep alive removed.
This commit is contained in:
Abdullah Saleem 2015-04-06 22:40:54 +05:00
parent 46bd17c4fa
commit 957ae2c0ef
4 changed files with 114 additions and 114 deletions

View File

@ -9,7 +9,7 @@ public static class Settings
{
#if DEBUG
public static string VERSION = "1.0.0.0d";
public static string HOST = "localhost";
public static string HOST = "192.168.1.11";
public static ushort PORT = 4782;
public static int RECONNECTDELAY = 5000;
public static string PASSWORD = "1234";

View File

@ -116,7 +116,7 @@ public void Connect(string host, ushort port)
{
_handle.BeginReceive(this._buffer, 0, this._buffer.Length, SocketFlags.None, AsyncReceive, null);
SendKeepAlives();
//SendKeepAlives();
OnClientState(true);
}
}
@ -129,13 +129,13 @@ public void Connect(string host, ushort port)
private void Initialize()
{
_keepAlives = new List<KeepAlive>();
//_keepAlives = new List<KeepAlive>();
AddTypesToSerializer(typeof(IPacket), new Type[]
{
typeof(UnknownPacket),
typeof(KeepAlive),
typeof(KeepAliveResponse)
//typeof(KeepAlive),
//typeof(KeepAliveResponse)
});
}
@ -196,11 +196,11 @@ private void AsyncReceive(IAsyncResult result)
IPacket packet = Serializer.DeserializeWithLengthPrefix<IPacket>(deserialized,
PrefixStyle.Fixed32);
if (packet.GetType() == typeof (KeepAlive))
new KeepAliveResponse() {TimeSent = ((KeepAlive) packet).TimeSent}.Execute(this);
else if (packet.GetType() == typeof (KeepAliveResponse))
HandleKeepAlivePacket((KeepAliveResponse) packet, this);
else
//if (packet.GetType() == typeof (KeepAlive))
// new KeepAliveResponse() {TimeSent = ((KeepAlive) packet).TimeSent}.Execute(this);
//else if (packet.GetType() == typeof (KeepAliveResponse))
// HandleKeepAlivePacket((KeepAliveResponse) packet, this);
//else
OnClientRead(packet);
}
}
@ -336,52 +336,52 @@ public void AddTypesToSerializer(Type parent, params Type[] types)
AddTypeToSerializer(parent, type);
}
private void HandleKeepAlivePacket(KeepAliveResponse packet, Client client)
{
foreach (KeepAlive keepAlive in _keepAlives)
{
if (keepAlive.TimeSent == packet.TimeSent && keepAlive.Client == client)
{
_keepAlives.Remove(keepAlive);
break;
}
}
}
//private void HandleKeepAlivePacket(KeepAliveResponse packet, Client client)
//{
// foreach (KeepAlive keepAlive in _keepAlives)
// {
// if (keepAlive.TimeSent == packet.TimeSent && keepAlive.Client == client)
// {
// _keepAlives.Remove(keepAlive);
// break;
// }
// }
//}
private void KeepAliveCallback(object state)
{
KeepAlive keepAlive = (KeepAlive)state;
//private void KeepAliveCallback(object state)
//{
// KeepAlive keepAlive = (KeepAlive)state;
if (_keepAlives.Contains(keepAlive))
{
Disconnect();
}
}
// if (_keepAlives.Contains(keepAlive))
// {
// Disconnect();
// }
//}
private void SendKeepAlives()
{
new Thread(() =>
{
while (Connected)
{
try
{
KeepAlive keepAlive = new KeepAlive();
lock (_keepAlives)
{
_keepAlives.Add(keepAlive);
}
keepAlive.Execute(this);
Timer timer = new Timer(KeepAliveCallback, keepAlive, 25000, Timeout.Infinite);
}
catch
{
//private void SendKeepAlives()
//{
// new Thread(() =>
// {
// while (Connected)
// {
// try
// {
// KeepAlive keepAlive = new KeepAlive();
// lock (_keepAlives)
// {
// _keepAlives.Add(keepAlive);
// }
// keepAlive.Execute(this);
// Timer timer = new Timer(KeepAliveCallback, keepAlive, 25000, Timeout.Infinite);
// }
// catch
// {
}
Thread.Sleep(15000);
}
// }
// Thread.Sleep(15000);
// }
}) { IsBackground = true }.Start();
}
// }) { IsBackground = true }.Start();
//}
}
}

View File

@ -58,8 +58,8 @@ public enum ReceiveType
Payload
}
public const uint KEEP_ALIVE_TIME = 5000;
public const uint KEEP_ALIVE_INTERVAL = 5000;
public const uint KEEP_ALIVE_TIME = 1000;
public const uint KEEP_ALIVE_INTERVAL = 1000;
public const int HEADER_SIZE = 4;
public const int MAX_PACKET_SIZE = (1024 * 1024) * 1; //1MB
@ -119,8 +119,8 @@ private void Initialize()
AddTypesToSerializer(typeof(IPacket), new Type[]
{
typeof(UnknownPacket),
typeof(KeepAlive),
typeof(KeepAliveResponse)
//typeof(KeepAlive),
//typeof(KeepAliveResponse)
});
}
@ -183,11 +183,11 @@ private void AsyncReceive(IAsyncResult result)
IPacket packet = Serializer.DeserializeWithLengthPrefix<IPacket>(deserialized,
PrefixStyle.Fixed32);
if (packet.GetType() == typeof (KeepAlive))
new KeepAliveResponse() {TimeSent = ((KeepAlive) packet).TimeSent}.Execute(this);
else if (packet.GetType() == typeof (KeepAliveResponse))
_parentServer.HandleKeepAlivePacket((KeepAliveResponse) packet, this); // HERE
else
//if (packet.GetType() == typeof (KeepAlive))
// new KeepAliveResponse() {TimeSent = ((KeepAlive) packet).TimeSent}.Execute(this);
//else if (packet.GetType() == typeof (KeepAliveResponse))
// _parentServer.HandleKeepAlivePacket((KeepAliveResponse) packet, this); // HERE
//else
OnClientRead(packet);
}
}

View File

@ -66,7 +66,7 @@ private void OnClientWrite(Client c, IPacket packet, long length, byte[] rawData
public bool Listening { get; private set; }
private List<KeepAlive> _keepAlives;
// private List<KeepAlive> _keepAlives;
private List<Client> _clients;
public Client[] Clients
@ -94,7 +94,7 @@ public void Listen(ushort port)
{
if (!Listening)
{
_keepAlives = new List<KeepAlive>();
// _keepAlives = new List<KeepAlive>();
_clients = new List<Client>();
@ -111,7 +111,7 @@ public void Listen(ushort port)
OnServerState(true);
SendKeepAlives();
// SendKeepAlives();
if (!_handle.AcceptAsync(_item))
Process(null, _item);
@ -173,60 +173,60 @@ private void Process(object s, SocketAsyncEventArgs e)
}
}
private void SendKeepAlives()
{
new Thread(() =>
{
while (true)
{
try
{
foreach (Client client in Clients)
{
KeepAlive keepAlive = new KeepAlive();
lock (_keepAlives)
{
_keepAlives.Add(keepAlive);
}
keepAlive.Execute(client);
Timer timer = new Timer(KeepAliveCallback, keepAlive, 15000, Timeout.Infinite);
}
}
catch
{
//private void SendKeepAlives()
//{
// new Thread(() =>
// {
// while (true)
// {
// try
// {
// foreach (Client client in Clients)
// {
// KeepAlive keepAlive = new KeepAlive();
// lock (_keepAlives)
// {
// _keepAlives.Add(keepAlive);
// }
// keepAlive.Execute(client);
// Timer timer = new Timer(KeepAliveCallback, keepAlive, 15000, Timeout.Infinite);
// }
// }
// catch
// {
}
Thread.Sleep(10000);
}
// }
// Thread.Sleep(10000);
// }
}) { IsBackground = true }.Start();
}
// }) { IsBackground = true }.Start();
//}
private void KeepAliveCallback(object state)
{
KeepAlive keepAlive = (KeepAlive)state;
//private void KeepAliveCallback(object state)
//{
// KeepAlive keepAlive = (KeepAlive)state;
if (_keepAlives != null)
{
if (_keepAlives.Contains(keepAlive))
{
keepAlive.Client.Disconnect();
_keepAlives.Remove(keepAlive);
}
}
}
// if (_keepAlives != null)
// {
// if (_keepAlives.Contains(keepAlive))
// {
// keepAlive.Client.Disconnect();
// _keepAlives.Remove(keepAlive);
// }
// }
//}
internal void HandleKeepAlivePacket(KeepAliveResponse packet, Client client)
{
foreach (KeepAlive keepAlive in _keepAlives)
{
if (keepAlive.TimeSent == packet.TimeSent && keepAlive.Client == client)
{
_keepAlives.Remove(keepAlive);
break;
}
}
}
//internal void HandleKeepAlivePacket(KeepAliveResponse packet, Client client)
//{
// foreach (KeepAlive keepAlive in _keepAlives)
// {
// if (keepAlive.TimeSent == packet.TimeSent && keepAlive.Client == client)
// {
// _keepAlives.Remove(keepAlive);
// break;
// }
// }
//}
public void Disconnect()
{
@ -252,7 +252,7 @@ public void Disconnect()
}
}
_keepAlives = null;
// _keepAlives = null;
Listening = false;
OnServerState(false);