1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-08-19 12:22:56 +00:00

Socket client update, added headers to rest result

This commit is contained in:
Jan Korf
2019-04-30 11:58:39 +02:00
parent c489b4e9aa
commit 9008c4ed2c
16 changed files with 446 additions and 321 deletions
+6 -6
View File
@@ -114,7 +114,7 @@ namespace CryptoExchange.Net.Sockets
handle?.Invoke(data);
}
protected void CheckTimeout()
protected async Task CheckTimeout()
{
while (true)
{
@@ -131,7 +131,7 @@ namespace CryptoExchange.Net.Sockets
}
}
Thread.Sleep(500);
await Task.Delay(500).ConfigureAwait(false);
}
}
@@ -184,7 +184,7 @@ namespace CryptoExchange.Net.Sockets
socket.Send(data);
}
public virtual async Task<bool> Connect()
public virtual Task<bool> Connect()
{
if (socket == null)
{
@@ -211,7 +211,7 @@ namespace CryptoExchange.Net.Sockets
socket.DataReceived += (o, s) => HandleByteData(s.Data);
}
return await Task.Run(() =>
return Task.Run(() =>
{
bool connected;
lock (socketLock)
@@ -247,7 +247,7 @@ namespace CryptoExchange.Net.Sockets
{
log?.Write(LogVerbosity.Debug, $"Socket {Id} connected");
if ((timeoutTask == null || timeoutTask.IsCompleted) && Timeout != default(TimeSpan))
timeoutTask = Task.Run(() => CheckTimeout());
timeoutTask = Task.Run(CheckTimeout);
}
else
log?.Write(LogVerbosity.Debug, $"Socket {Id} connection failed, state: " + socket.State);
@@ -257,7 +257,7 @@ namespace CryptoExchange.Net.Sockets
socket.Close();
return connected;
}).ConfigureAwait(false);
});
}
public virtual void SetProxy(string host, int port)
+11 -16
View File
@@ -32,7 +32,7 @@ namespace CryptoExchange.Net.Sockets
public DateTime? DisconnectTime { get; set; }
public bool PausedActivity { get; set; }
private readonly List<SocketSubscription> handlers;
internal readonly List<SocketSubscription> handlers;
private readonly object handlersLock = new object();
private bool lostTriggered;
@@ -41,9 +41,9 @@ namespace CryptoExchange.Net.Sockets
private readonly List<PendingRequest> pendingRequests;
public SocketConnection(SocketClient client, Log log, IWebsocket socket)
public SocketConnection(SocketClient client, IWebsocket socket)
{
this.log = log;
log = client.log;
socketClient = client;
pendingRequests = new List<PendingRequest>();
@@ -51,7 +51,7 @@ namespace CryptoExchange.Net.Sockets
handlers = new List<SocketSubscription>();
Socket = socket;
Socket.Timeout = client.SocketTimeout;
Socket.Timeout = client.SocketNoDataTimeout;
Socket.OnMessage += ProcessMessage;
Socket.OnClose += () =>
{
@@ -160,15 +160,12 @@ namespace CryptoExchange.Net.Sockets
}
}
public virtual async Task SendAndWait<T>(T obj, TimeSpan timeout, Func<JToken, bool> handler)
public virtual Task SendAndWait<T>(T obj, TimeSpan timeout, Func<JToken, bool> handler)
{
var pending = new PendingRequest(handler, timeout);
pendingRequests.Add(pending);
await Task.Run(() =>
{
Send(obj);
pending.Event.WaitOne(timeout);
}).ConfigureAwait(false);
Send(obj);
return pending.Event.WaitOneAsync(timeout);
}
/// <summary>
@@ -209,7 +206,7 @@ namespace CryptoExchange.Net.Sockets
{
while (ShouldReconnect)
{
Thread.Sleep(socketClient.ReconnectInterval);
await Task.Delay(socketClient.ReconnectInterval).ConfigureAwait(false);
if (!ShouldReconnect)
{
// Should reconnect changed to false while waiting to reconnect
@@ -282,11 +279,9 @@ namespace CryptoExchange.Net.Sockets
{
Connected = false;
ShouldReconnect = false;
lock (socketClient.socketLock)
{
if (socketClient.sockets.Contains(this))
socketClient.sockets.Remove(this);
}
if (socketClient.sockets.ContainsKey(Socket.Id))
socketClient.sockets.TryRemove(Socket.Id, out _);
await Socket.Close().ConfigureAwait(false);
Socket.Dispose();