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:
parent
0f594bf8f4
commit
f215f3c53f
@ -23,6 +23,7 @@ namespace CryptoExchange.Net.Interfaces
|
|||||||
bool PingConnection { get; set; }
|
bool PingConnection { get; set; }
|
||||||
TimeSpan PingInterval { get; set; }
|
TimeSpan PingInterval { get; set; }
|
||||||
SslProtocols SSLProtocols { get; set; }
|
SslProtocols SSLProtocols { get; set; }
|
||||||
|
TimeSpan Timeout { get; set; }
|
||||||
Task<bool> Connect();
|
Task<bool> Connect();
|
||||||
void Send(string data);
|
void Send(string data);
|
||||||
Task Close();
|
Task Close();
|
||||||
|
@ -39,6 +39,10 @@ namespace CryptoExchange.Net.Sockets
|
|||||||
public SslProtocols SSLProtocols { get; set; } = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;
|
public SslProtocols SSLProtocols { get; set; } = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;
|
||||||
public Func<byte[], string> DataInterpreter { get; set; }
|
public Func<byte[], string> DataInterpreter { get; set; }
|
||||||
|
|
||||||
|
public DateTime LastActionTime { get; private set; }
|
||||||
|
public TimeSpan Timeout { get; set; }
|
||||||
|
private Task timeoutTask;
|
||||||
|
|
||||||
public bool PingConnection
|
public bool PingConnection
|
||||||
{
|
{
|
||||||
get => socket.EnableAutoSendPing;
|
get => socket.EnableAutoSendPing;
|
||||||
@ -101,18 +105,38 @@ namespace CryptoExchange.Net.Sockets
|
|||||||
remove => openHandlers.Remove(value);
|
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))
|
foreach (var handle in new List<Action>(handlers))
|
||||||
handle?.Invoke();
|
handle?.Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void Handle<T>(List<Action<T>> handlers, T data)
|
protected void Handle<T>(List<Action<T>> handlers, T data)
|
||||||
{
|
{
|
||||||
|
LastActionTime = DateTime.UtcNow;
|
||||||
foreach (var handle in new List<Action<T>>(handlers))
|
foreach (var handle in new List<Action<T>>(handlers))
|
||||||
handle?.Invoke(data);
|
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()
|
public virtual async Task Close()
|
||||||
{
|
{
|
||||||
await Task.Run(() =>
|
await Task.Run(() =>
|
||||||
@ -202,7 +226,11 @@ namespace CryptoExchange.Net.Sockets
|
|||||||
}
|
}
|
||||||
connected = socket.State == WebSocketState.Open;
|
connected = socket.State == WebSocketState.Open;
|
||||||
if (connected)
|
if (connected)
|
||||||
|
{
|
||||||
log?.Write(LogVerbosity.Debug, $"Socket {Id} connected");
|
log?.Write(LogVerbosity.Debug, $"Socket {Id} connected");
|
||||||
|
if ((timeoutTask == null || timeoutTask.IsCompleted) && Timeout != default(TimeSpan))
|
||||||
|
timeoutTask = Task.Run(() => CheckTimeout());
|
||||||
|
}
|
||||||
else
|
else
|
||||||
log?.Write(LogVerbosity.Debug, $"Socket {Id} connection failed, state: " + socket.State);
|
log?.Write(LogVerbosity.Debug, $"Socket {Id} connection failed, state: " + socket.State);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user