diff --git a/CryptoExchange.Net/BaseConverter.cs b/CryptoExchange.Net/Converters/BaseConverter.cs similarity index 96% rename from CryptoExchange.Net/BaseConverter.cs rename to CryptoExchange.Net/Converters/BaseConverter.cs index 690211d..e61f28c 100644 --- a/CryptoExchange.Net/BaseConverter.cs +++ b/CryptoExchange.Net/Converters/BaseConverter.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; -namespace CryptoExchange.Net +namespace CryptoExchange.Net.Converters { public abstract class BaseConverter: JsonConverter { diff --git a/CryptoExchange.Net/Converters/KrakenArrayConverter.cs b/CryptoExchange.Net/Converters/KrakenArrayConverter.cs new file mode 100644 index 0000000..1718c95 --- /dev/null +++ b/CryptoExchange.Net/Converters/KrakenArrayConverter.cs @@ -0,0 +1,65 @@ +using System; +using System.Reflection; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace CryptoExchange.Net.Converters +{ + public class ArrayConverter : JsonConverter + { + public override bool CanConvert(Type objectType) + { + throw new NotImplementedException(); + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + var result = Activator.CreateInstance(objectType); + var arr = JArray.Load(reader); + foreach (var property in objectType.GetProperties()) + { + var attribute = + (ArrayPropertyAttribute)property.GetCustomAttribute(typeof(ArrayPropertyAttribute)); + if (attribute == null) + continue; + + if (attribute.Index >= arr.Count) + continue; + + object value; + var converterAttribute = (JsonConverterAttribute)property.GetCustomAttribute(typeof(JsonConverterAttribute)); + if (converterAttribute != null) + value = arr[attribute.Index].ToObject(property.PropertyType, new JsonSerializer() { Converters = { (JsonConverter)Activator.CreateInstance(converterAttribute.ConverterType) } }); + else + value = arr[attribute.Index]; + + if (value != null && property.PropertyType.IsInstanceOfType(value)) + property.SetValue(result, value); + else + { + if (value is JToken) + if (((JToken)value).Type == JTokenType.Null) + value = null; + + property.SetValue(result, value == null ? null : Convert.ChangeType(value, property.PropertyType)); + } + } + return result; + } + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + throw new NotImplementedException(); + } + } + + public class ArrayPropertyAttribute: Attribute + { + public int Index { get; } + + public ArrayPropertyAttribute(int index) + { + Index = index; + } + } +} diff --git a/CryptoExchange.Net/Converters/TimestampConverter.cs b/CryptoExchange.Net/Converters/TimestampConverter.cs new file mode 100644 index 0000000..b3358eb --- /dev/null +++ b/CryptoExchange.Net/Converters/TimestampConverter.cs @@ -0,0 +1,24 @@ +using System; +using Newtonsoft.Json; + +namespace CryptoExchange.Net.Converters +{ + public class TimestampConverter : JsonConverter + { + public override bool CanConvert(Type objectType) + { + return objectType == typeof(DateTime); + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + var t = long.Parse(reader.Value.ToString()); + return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(t); + } + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + writer.WriteValue(Math.Round((((DateTime)value) - new DateTime(1970, 1, 1)).TotalMilliseconds)); + } + } +} diff --git a/CryptoExchange.Net/CryptoExchange.Net.csproj b/CryptoExchange.Net/CryptoExchange.Net.csproj index d360a74..283ab1d 100644 --- a/CryptoExchange.Net/CryptoExchange.Net.csproj +++ b/CryptoExchange.Net/CryptoExchange.Net.csproj @@ -7,7 +7,7 @@ CryptoExchange.Net JKorf - 0.0.14 + 0.0.15 false https://github.com/JKorf/CryptoExchange.Net https://github.com/JKorf/CryptoExchange.Net/blob/master/LICENSE diff --git a/CryptoExchange.Net/ExchangeClient.cs b/CryptoExchange.Net/ExchangeClient.cs index d7a6b6c..42893c8 100644 --- a/CryptoExchange.Net/ExchangeClient.cs +++ b/CryptoExchange.Net/ExchangeClient.cs @@ -180,13 +180,20 @@ namespace CryptoExchange.Net var obj = JToken.Parse(data); if (checkObject && log.Level == LogVerbosity.Debug) { - if (obj is JObject o) - CheckObject(typeof(T), o); - else + try { - var ary = (JArray) obj; - if (ary.HasValues && ary[0] is JObject jObject) - CheckObject(typeof(T).GetElementType(), jObject); + if (obj is JObject o) + CheckObject(typeof(T), o); + else + { + var ary = (JArray) obj; + if (ary.HasValues && ary[0] is JObject jObject) + CheckObject(typeof(T).GetElementType(), jObject); + } + } + catch (Exception e) + { + log.Write(LogVerbosity.Debug, "Failed to check response data: " + e.Message); } } diff --git a/CryptoExchange.Net/Implementation/BaseSocket.cs b/CryptoExchange.Net/Implementation/BaseSocket.cs index 1e8e6e6..ba9e0de 100644 --- a/CryptoExchange.Net/Implementation/BaseSocket.cs +++ b/CryptoExchange.Net/Implementation/BaseSocket.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Net; using System.Security.Authentication; @@ -76,13 +77,13 @@ namespace CryptoExchange.Net.Implementation protected static void Handle(List handlers) { foreach (var handle in new List(handlers)) - handle(); + handle?.Invoke(); } protected void Handle(List> handlers, T data) { foreach (var handle in new List>(handlers)) - handle(data); + handle?.Invoke(data); } public async Task Close() @@ -131,5 +132,15 @@ namespace CryptoExchange.Net.Implementation ? new HttpConnectProxy(new IPEndPoint(address, port)) : new HttpConnectProxy(new DnsEndPoint(host, port)); } + + public void Dispose() + { + socket?.Dispose(); + + errorhandlers.Clear(); + openhandlers.Clear(); + closehandlers.Clear(); + messagehandlers.Clear(); + } } } diff --git a/CryptoExchange.Net/Interfaces/IWebsocket.cs b/CryptoExchange.Net/Interfaces/IWebsocket.cs index 9b5ac84..fe4518a 100644 --- a/CryptoExchange.Net/Interfaces/IWebsocket.cs +++ b/CryptoExchange.Net/Interfaces/IWebsocket.cs @@ -4,7 +4,7 @@ using System.Threading.Tasks; namespace CryptoExchange.Net.Interfaces { - public interface IWebsocket + public interface IWebsocket: IDisposable { void SetEnabledSslProtocols(SslProtocols protocols);