mirror of https://github.com/quasar/Quasar.git
Fixed keepalive
no more disconnects, can detect network disconnects + network cable unplugs
This commit is contained in:
parent
ee061e927d
commit
2b55051a62
|
@ -11,6 +11,7 @@
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace Core
|
namespace Core
|
||||||
{
|
{
|
||||||
|
@ -62,6 +63,8 @@ private void OnClientRead(byte[] e)
|
||||||
|
|
||||||
if (packet.GetType() == typeof(KeepAlive))
|
if (packet.GetType() == typeof(KeepAlive))
|
||||||
new KeepAliveResponse() { TimeSent = ((KeepAlive)packet).TimeSent }.Execute(this);
|
new KeepAliveResponse() { TimeSent = ((KeepAlive)packet).TimeSent }.Execute(this);
|
||||||
|
else if (packet.GetType() == typeof(KeepAliveResponse))
|
||||||
|
HandleKeepAlivePacket((KeepAliveResponse)packet, this);
|
||||||
else
|
else
|
||||||
ClientRead(this, packet);
|
ClientRead(this, packet);
|
||||||
}
|
}
|
||||||
|
@ -97,6 +100,8 @@ private void OnClientWrite(IPacket packet, long length, byte[] rawData)
|
||||||
|
|
||||||
private SocketAsyncEventArgs[] _item = new SocketAsyncEventArgs[2];
|
private SocketAsyncEventArgs[] _item = new SocketAsyncEventArgs[2];
|
||||||
|
|
||||||
|
private List<KeepAlive> _keepAlives;
|
||||||
|
|
||||||
private bool[] _processing = new bool[2];
|
private bool[] _processing = new bool[2];
|
||||||
|
|
||||||
public int BufferSize { get; set; }
|
public int BufferSize { get; set; }
|
||||||
|
@ -209,6 +214,8 @@ private void Initialize()
|
||||||
|
|
||||||
_sendQueue = new Queue<byte[]>();
|
_sendQueue = new Queue<byte[]>();
|
||||||
|
|
||||||
|
_keepAlives = new List<KeepAlive>();
|
||||||
|
|
||||||
_item[0] = new SocketAsyncEventArgs();
|
_item[0] = new SocketAsyncEventArgs();
|
||||||
_item[0].Completed += Process;
|
_item[0].Completed += Process;
|
||||||
_item[1] = new SocketAsyncEventArgs();
|
_item[1] = new SocketAsyncEventArgs();
|
||||||
|
@ -249,13 +256,20 @@ private void Process(object s, SocketAsyncEventArgs e)
|
||||||
switch (e.LastOperation)
|
switch (e.LastOperation)
|
||||||
{
|
{
|
||||||
case SocketAsyncOperation.Connect:
|
case SocketAsyncOperation.Connect:
|
||||||
|
|
||||||
_endPoint = (IPEndPoint)_handle.RemoteEndPoint;
|
_endPoint = (IPEndPoint)_handle.RemoteEndPoint;
|
||||||
|
|
||||||
Connected = true;
|
Connected = true;
|
||||||
|
|
||||||
_item[0].SetBuffer(new byte[BufferSize], 0, BufferSize);
|
_item[0].SetBuffer(new byte[BufferSize], 0, BufferSize);
|
||||||
|
|
||||||
_asyncOperation.Post(x => OnClientState((bool)x), true);
|
_asyncOperation.Post(x => OnClientState((bool)x), true);
|
||||||
|
|
||||||
|
SendKeepAlives();
|
||||||
|
|
||||||
if (!_handle.ReceiveAsync(e))
|
if (!_handle.ReceiveAsync(e))
|
||||||
Process(null, e);
|
Process(null, e);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case SocketAsyncOperation.Receive:
|
case SocketAsyncOperation.Receive:
|
||||||
if (!Connected)
|
if (!Connected)
|
||||||
|
@ -317,6 +331,8 @@ public void Disconnect()
|
||||||
_handle.Close();
|
_handle.Close();
|
||||||
if (_sendQueue != null)
|
if (_sendQueue != null)
|
||||||
_sendQueue.Clear();
|
_sendQueue.Clear();
|
||||||
|
if (_keepAlives != null)
|
||||||
|
_keepAlives.Clear();
|
||||||
|
|
||||||
_sendBuffer = new byte[0];
|
_sendBuffer = new byte[0];
|
||||||
_readBuffer = new byte[0];
|
_readBuffer = new byte[0];
|
||||||
|
@ -442,5 +458,53 @@ private void HandleRead(byte[] data, int index, int length)
|
||||||
Disconnect();
|
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
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
Thread.Sleep(15000);
|
||||||
|
}
|
||||||
|
|
||||||
|
}) { IsBackground = true }.Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void KeepAliveCallback(object state)
|
||||||
|
{
|
||||||
|
KeepAlive keepAlive = (KeepAlive)state;
|
||||||
|
|
||||||
|
if (_keepAlives.Contains(keepAlive))
|
||||||
|
{
|
||||||
|
Disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleKeepAlivePacket(KeepAliveResponse packet, Client client)
|
||||||
|
{
|
||||||
|
foreach (KeepAlive keepAlive in _keepAlives)
|
||||||
|
{
|
||||||
|
if (keepAlive.TimeSent == packet.TimeSent && keepAlive.Client == client)
|
||||||
|
{
|
||||||
|
_keepAlives.Remove(keepAlive);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ static class Program
|
||||||
{
|
{
|
||||||
public static Core.Client _Client;
|
public static Core.Client _Client;
|
||||||
static bool Reconnect = true;
|
static bool Reconnect = true;
|
||||||
static bool Connected = false;
|
static volatile bool Connected = false;
|
||||||
static Mutex AppMutex;
|
static Mutex AppMutex;
|
||||||
|
|
||||||
[STAThread]
|
[STAThread]
|
||||||
|
|
|
@ -65,6 +65,8 @@ private void OnClientRead(byte[] e)
|
||||||
|
|
||||||
if (packet.GetType() == typeof(KeepAliveResponse))
|
if (packet.GetType() == typeof(KeepAliveResponse))
|
||||||
_parentServer.HandleKeepAlivePacket((KeepAliveResponse)packet, this);
|
_parentServer.HandleKeepAlivePacket((KeepAliveResponse)packet, this);
|
||||||
|
else if (packet.GetType() == typeof(KeepAlive))
|
||||||
|
new KeepAliveResponse() { TimeSent = ((KeepAlive)packet).TimeSent }.Execute(this);
|
||||||
else
|
else
|
||||||
ClientRead(this, packet);
|
ClientRead(this, packet);
|
||||||
}
|
}
|
||||||
|
|
|
@ -245,6 +245,8 @@ public void Disconnect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_keepAlives = null;
|
||||||
|
|
||||||
Listening = false;
|
Listening = false;
|
||||||
OnServerState(false);
|
OnServerState(false);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue