1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-08-19 20:33:03 +00:00

Added code docs, added ContinueOnQueryResponse

This commit is contained in:
Jan Korf
2019-08-06 13:19:34 +02:00
parent 11016bc213
commit 780da53475
50 changed files with 2545 additions and 30 deletions
+4 -1
View File
@@ -13,7 +13,10 @@ using WebSocket4Net;
namespace CryptoExchange.Net.Sockets
{
public class BaseSocket: IWebsocket
/// <summary>
/// Socket implementation
/// </summary>
internal class BaseSocket: IWebsocket
{
internal static int lastStreamId;
private static readonly object streamIdLock = new object();
+83 -6
View File
@@ -11,25 +11,58 @@ using Newtonsoft.Json.Linq;
namespace CryptoExchange.Net.Sockets
{
/// <summary>
/// Socket connecting
/// </summary>
public class SocketConnection
{
/// <summary>
/// Connection lost event
/// </summary>
public event Action ConnectionLost;
/// <summary>
/// Connecting restored event
/// </summary>
public event Action<TimeSpan> ConnectionRestored;
/// <summary>
/// Connecting closed event
/// </summary>
public event Action Closed;
/// <summary>
/// The amount of handlers
/// </summary>
public int HandlerCount
{
get { lock (handlersLock)
return handlers.Count(h => h.UserSubscription); }
}
/// <summary>
/// If connection is authenticated
/// </summary>
public bool Authenticated { get; set; }
/// <summary>
/// If connection is made
/// </summary>
public bool Connected { get; private set; }
/// <summary>
/// The socket
/// </summary>
public IWebsocket Socket { get; set; }
/// <summary>
/// If should reconnect upon closing
/// </summary>
public bool ShouldReconnect { get; set; }
/// <summary>
/// Time of disconnecting
/// </summary>
public DateTime? DisconnectTime { get; set; }
/// <summary>
/// If activity is paused
/// </summary>
public bool PausedActivity { get; set; }
internal readonly List<SocketSubscription> handlers;
@@ -41,6 +74,11 @@ namespace CryptoExchange.Net.Sockets
private readonly List<PendingRequest> pendingRequests;
/// <summary>
/// New socket connection
/// </summary>
/// <param name="client">The socket client</param>
/// <param name="socket">The socket</param>
public SocketConnection(SocketClient client, IWebsocket socket)
{
log = client.log;
@@ -72,6 +110,13 @@ namespace CryptoExchange.Net.Sockets
};
}
/// <summary>
/// Add a handler
/// </summary>
/// <param name="request">The request object</param>
/// <param name="userSubscription">If it is a user subscription or a generic handler</param>
/// <param name="dataHandler">The data handler</param>
/// <returns></returns>
public SocketSubscription AddHandler(object request, bool userSubscription, Action<SocketConnection, JToken> dataHandler)
{
var handler = new SocketSubscription(null, request, userSubscription, dataHandler);
@@ -80,6 +125,14 @@ namespace CryptoExchange.Net.Sockets
return handler;
}
/// <summary>
/// Add a handler
/// </summary>
/// <param name="identifier">The identifier of the handler</param>
/// <param name="userSubscription">If it is a user subscription or a generic handler</param>
/// <param name="dataHandler">The data handler</param>
/// <returns></returns>
/// <returns></returns>
public SocketSubscription AddHandler(string identifier, bool userSubscription, Action<SocketConnection, JToken> dataHandler)
{
var handler = new SocketSubscription(identifier, null, userSubscription, dataHandler);
@@ -88,7 +141,7 @@ namespace CryptoExchange.Net.Sockets
return handler;
}
public void ProcessMessage(string data)
private void ProcessMessage(string data)
{
log.Write(LogVerbosity.Debug, $"Socket {Socket.Id} received data: " + data);
var tokenData = data.ToJToken(log);
@@ -100,7 +153,9 @@ namespace CryptoExchange.Net.Sockets
if (pendingRequest.Check(tokenData))
{
pendingRequests.Remove(pendingRequest);
return;
if (!socketClient.ContinueOnQueryResponse)
return;
break;
}
}
@@ -156,6 +211,14 @@ namespace CryptoExchange.Net.Sockets
}
}
/// <summary>
/// Send data
/// </summary>
/// <typeparam name="T">The data type</typeparam>
/// <param name="obj">The object to send</param>
/// <param name="timeout">The timeout for response</param>
/// <param name="handler">The response handler</param>
/// <returns></returns>
public virtual Task SendAndWait<T>(T obj, TimeSpan timeout, Func<JToken, bool> handler)
{
var pending = new PendingRequest(handler, timeout);
@@ -230,7 +293,7 @@ namespace CryptoExchange.Net.Sockets
if (lostTriggered)
{
lostTriggered = false;
Task.Run(() => ConnectionRestored?.Invoke(DisconnectTime.HasValue ? DateTime.UtcNow - DisconnectTime.Value : TimeSpan.FromSeconds(0)));
InvokeConnectionRestored();
}
break;
@@ -248,7 +311,12 @@ namespace CryptoExchange.Net.Sockets
}
}
public async Task<bool> ProcessReconnect()
private async void InvokeConnectionRestored()
{
await Task.Run(() => ConnectionRestored?.Invoke(DisconnectTime.HasValue ? DateTime.UtcNow - DisconnectTime.Value : TimeSpan.FromSeconds(0))).ConfigureAwait(false);
}
private async Task<bool> ProcessReconnect()
{
if (Authenticated)
{
@@ -279,6 +347,10 @@ namespace CryptoExchange.Net.Sockets
return true;
}
/// <summary>
/// Close the connection
/// </summary>
/// <returns></returns>
public async Task Close()
{
Connected = false;
@@ -290,6 +362,11 @@ namespace CryptoExchange.Net.Sockets
Socket.Dispose();
}
/// <summary>
/// Close the subscriptions
/// </summary>
/// <param name="subscription">Subscription to close</param>
/// <returns></returns>
public async Task Close(SocketSubscription subscription)
{
if (subscription.Confirmed)
@@ -308,7 +385,7 @@ namespace CryptoExchange.Net.Sockets
}
}
public class PendingRequest
internal class PendingRequest
{
public Func<JToken, bool> Handler { get; }
public JToken Result { get; private set; }
@@ -3,8 +3,14 @@ using Newtonsoft.Json.Linq;
namespace CryptoExchange.Net.Sockets
{
/// <summary>
/// Socket subscription
/// </summary>
public class SocketSubscription
{
/// <summary>
/// Exception event
/// </summary>
public event Action<Exception> Exception;
/// <summary>
@@ -12,13 +18,32 @@ namespace CryptoExchange.Net.Sockets
/// </summary>
public Action<SocketConnection, JToken> MessageHandler { get; set; }
/// <summary>
/// Request object
/// </summary>
public object Request { get; set; }
/// <summary>
/// Subscription identifier
/// </summary>
public string Identifier { get; set; }
/// <summary>
/// Is user subscription or generic
/// </summary>
public bool UserSubscription { get; set; }
/// <summary>
/// If the subscription has been confirmed
/// </summary>
public bool Confirmed { get; set; }
/// <summary>
/// ctor
/// </summary>
/// <param name="identifier"></param>
/// <param name="request"></param>
/// <param name="userSubscription"></param>
/// <param name="dataHandler"></param>
public SocketSubscription(string identifier, object request, bool userSubscription, Action<SocketConnection, JToken> dataHandler)
{
UserSubscription = userSubscription;
@@ -27,6 +52,10 @@ namespace CryptoExchange.Net.Sockets
Request = request;
}
/// <summary>
/// Invoke the exception event
/// </summary>
/// <param name="e"></param>
public void InvokeExceptionHandler(Exception e)
{
Exception?.Invoke(e);
@@ -3,6 +3,9 @@ using System.Threading.Tasks;
namespace CryptoExchange.Net.Sockets
{
/// <summary>
/// Subscription
/// </summary>
public class UpdateSubscription
{
private readonly SocketConnection connection;
@@ -40,6 +43,11 @@ namespace CryptoExchange.Net.Sockets
/// </summary>
public int Id => connection.Socket.Id;
/// <summary>
/// ctor
/// </summary>
/// <param name="connection"></param>
/// <param name="subscription"></param>
public UpdateSubscription(SocketConnection connection, SocketSubscription subscription)
{
this.connection = connection;
@@ -4,13 +4,18 @@ using CryptoExchange.Net.Logging;
namespace CryptoExchange.Net.Sockets
{
/// <summary>
/// Factory implementation
/// </summary>
public class WebsocketFactory : IWebsocketFactory
{
/// <inheritdoc />
public IWebsocket CreateWebsocket(Log log, string url)
{
return new BaseSocket(log, url);
}
/// <inheritdoc />
public IWebsocket CreateWebsocket(Log log, string url, IDictionary<string, string> cookies, IDictionary<string, string> headers)
{
return new BaseSocket(log, url, cookies, headers);