From 957ae2c0efd0d9d404615a41e1b69d1476440792 Mon Sep 17 00:00:00 2001 From: Abdullah Saleem Date: Mon, 6 Apr 2015 22:40:54 +0500 Subject: [PATCH] New KeepAlive Added Previous method of keep alive removed. --- Client/Config/Settings.cs | 2 +- Client/Core/Client.cs | 102 ++++++++++++++++++------------------ Server/Core/Client.cs | 18 +++---- Server/Core/Server.cs | 106 +++++++++++++++++++------------------- 4 files changed, 114 insertions(+), 114 deletions(-) diff --git a/Client/Config/Settings.cs b/Client/Config/Settings.cs index 32aeee7b..3b835209 100644 --- a/Client/Config/Settings.cs +++ b/Client/Config/Settings.cs @@ -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"; diff --git a/Client/Core/Client.cs b/Client/Core/Client.cs index 9ca8122b..237a9c58 100644 --- a/Client/Core/Client.cs +++ b/Client/Core/Client.cs @@ -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(); + //_keepAlives = new List(); 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(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(); + //} } } diff --git a/Server/Core/Client.cs b/Server/Core/Client.cs index 55c0c03a..181c85f0 100644 --- a/Server/Core/Client.cs +++ b/Server/Core/Client.cs @@ -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(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); } } diff --git a/Server/Core/Server.cs b/Server/Core/Server.cs index 23ca21d1..3fde10d1 100644 --- a/Server/Core/Server.cs +++ b/Server/Core/Server.cs @@ -66,7 +66,7 @@ private void OnClientWrite(Client c, IPacket packet, long length, byte[] rawData public bool Listening { get; private set; } - private List _keepAlives; + // private List _keepAlives; private List _clients; public Client[] Clients @@ -94,7 +94,7 @@ public void Listen(ushort port) { if (!Listening) { - _keepAlives = new List(); + // _keepAlives = new List(); _clients = new List(); @@ -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);