1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-07-23 18:05:43 +00:00

Added copy for option objects, socket message handlers can now return whether they handled the message

This commit is contained in:
JKorf 2018-11-28 11:14:32 +01:00
parent 9648606bdc
commit 596b28dd47
3 changed files with 46 additions and 5 deletions

View File

@ -36,7 +36,7 @@ namespace CryptoExchange.Net.Objects
/// <summary>
/// The log writers
/// </summary>
public List<TextWriter> LogWriters { get; set; } = new List<TextWriter>() {new DebugTextWriter()};
public List<TextWriter> LogWriters { get; set; } = new List<TextWriter>() { new DebugTextWriter() };
}
public class ClientOptions: ExchangeOptions
@ -55,6 +55,25 @@ namespace CryptoExchange.Net.Objects
/// The time the server has to respond to a request before timing out
/// </summary>
public TimeSpan RequestTimeout { get; set; } = TimeSpan.FromSeconds(30);
public T Copy<T>() where T:ClientOptions, new()
{
var copy = new T
{
BaseAddress = BaseAddress,
LogVerbosity = LogVerbosity,
Proxy = Proxy,
LogWriters = LogWriters,
RateLimiters = RateLimiters,
RateLimitingBehaviour = RateLimitingBehaviour,
RequestTimeout = RequestTimeout
};
if (ApiCredentials != null)
copy.ApiCredentials = new ApiCredentials(ApiCredentials.Key.GetString(), ApiCredentials.Secret.GetString());
return copy;
}
}
public class SocketClientOptions: ExchangeOptions
@ -63,5 +82,22 @@ namespace CryptoExchange.Net.Objects
/// Time to wait between reconnect attempts
/// </summary>
public TimeSpan ReconnectInterval { get; set; } = TimeSpan.FromSeconds(5);
public T Copy<T>() where T : SocketClientOptions, new()
{
var copy = new T
{
BaseAddress = BaseAddress,
LogVerbosity = LogVerbosity,
Proxy = Proxy,
LogWriters = LogWriters,
ReconnectInterval = ReconnectInterval
};
if (ApiCredentials != null)
copy.ApiCredentials = new ApiCredentials(ApiCredentials.Key.GetString(), ApiCredentials.Secret.GetString());
return copy;
}
}
}

View File

@ -121,8 +121,9 @@ namespace CryptoExchange.Net
protected virtual void ProcessMessage(SocketSubscription subscription, string data)
{
log.Write(LogVerbosity.Debug, $"Socket {subscription.Socket.Id} received data: " + data);
foreach (var handler in subscription.DataHandlers)
handler(subscription, JToken.Parse(data));
foreach (var handler in subscription.MessageHandlers)
if (handler(subscription, JToken.Parse(data)))
return;
}
/// <summary>
@ -163,6 +164,7 @@ namespace CryptoExchange.Net
}
else
{
log.Write(LogVerbosity.Info, $"Socket {socket.Id} closed");
socket.Dispose();
lock (sockets)
{

View File

@ -13,7 +13,10 @@ namespace CryptoExchange.Net.Sockets
public event Action ConnectionLost;
public event Action<TimeSpan> ConnectionRestored;
public List<Action<SocketSubscription, JToken>> DataHandlers { get; set; }
/// <summary>
/// Message handlers for this subscription. Should return true if the message is handled and should not be distributed to the other handlers
/// </summary>
public List<Func<SocketSubscription, JToken, bool>> MessageHandlers { get; set; }
public List<SocketEvent> Events { get; set; }
public IWebsocket Socket { get; set; }
@ -29,7 +32,7 @@ namespace CryptoExchange.Net.Sockets
Events = new List<SocketEvent>();
waitingForEvents = new List<SocketEvent>();
DataHandlers = new List<Action<SocketSubscription, JToken>>();
MessageHandlers = new List<Func<SocketSubscription, JToken, bool>>();
Socket.OnClose += () =>
{