1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-08-28 19:21:32 +00:00

Added no data timeout in socket

This commit is contained in:
JKorf 2018-12-04 15:33:13 +01:00
parent 0f594bf8f4
commit f215f3c53f
2 changed files with 30 additions and 1 deletions

View File

@ -23,6 +23,7 @@ namespace CryptoExchange.Net.Interfaces
bool PingConnection { get; set; }
TimeSpan PingInterval { get; set; }
SslProtocols SSLProtocols { get; set; }
TimeSpan Timeout { get; set; }
Task<bool> Connect();
void Send(string data);
Task Close();

View File

@ -39,6 +39,10 @@ namespace CryptoExchange.Net.Sockets
public SslProtocols SSLProtocols { get; set; } = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;
public Func<byte[], string> DataInterpreter { get; set; }
public DateTime LastActionTime { get; private set; }
public TimeSpan Timeout { get; set; }
private Task timeoutTask;
public bool PingConnection
{
get => socket.EnableAutoSendPing;
@ -101,18 +105,38 @@ namespace CryptoExchange.Net.Sockets
remove => openHandlers.Remove(value);
}
protected static void Handle(List<Action> handlers)
protected void Handle(List<Action> handlers)
{
LastActionTime = DateTime.UtcNow;
foreach (var handle in new List<Action>(handlers))
handle?.Invoke();
}
protected void Handle<T>(List<Action<T>> handlers, T data)
{
LastActionTime = DateTime.UtcNow;
foreach (var handle in new List<Action<T>>(handlers))
handle?.Invoke(data);
}
protected void CheckTimeout()
{
while (true)
{
if (socket == null || socket.State != WebSocketState.Open)
return;
if (DateTime.UtcNow - LastActionTime > Timeout)
{
log.Write(LogVerbosity.Warning, $"No data received for {Timeout}, reconnecting socket");
Close().Wait();
return;
}
Thread.Sleep(500);
}
}
public virtual async Task Close()
{
await Task.Run(() =>
@ -202,7 +226,11 @@ namespace CryptoExchange.Net.Sockets
}
connected = socket.State == WebSocketState.Open;
if (connected)
{
log?.Write(LogVerbosity.Debug, $"Socket {Id} connected");
if ((timeoutTask == null || timeoutTask.IsCompleted) && Timeout != default(TimeSpan))
timeoutTask = Task.Run(() => CheckTimeout());
}
else
log?.Write(LogVerbosity.Debug, $"Socket {Id} connection failed, state: " + socket.State);
}