1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-07 16:06:15 +00:00

Added basic converters, added IDisposable to socket, added trycatch to result checking

This commit is contained in:
JKorf 2018-03-23 14:25:09 +01:00
parent f22562cd86
commit 343af73d50
7 changed files with 118 additions and 11 deletions

View File

@ -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<T>: JsonConverter
{

View File

@ -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;
}
}
}

View File

@ -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));
}
}
}

View File

@ -7,7 +7,7 @@
<PropertyGroup>
<PackageId>CryptoExchange.Net</PackageId>
<Authors>JKorf</Authors>
<PackageVersion>0.0.14</PackageVersion>
<PackageVersion>0.0.15</PackageVersion>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageProjectUrl>https://github.com/JKorf/CryptoExchange.Net</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/JKorf/CryptoExchange.Net/blob/master/LICENSE</PackageLicenseUrl>

View File

@ -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);
}
}

View File

@ -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<Action> handlers)
{
foreach (var handle in new List<Action>(handlers))
handle();
handle?.Invoke();
}
protected void Handle<T>(List<Action<T>> handlers, T data)
{
foreach (var handle in new List<Action<T>>(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();
}
}
}

View File

@ -4,7 +4,7 @@ using System.Threading.Tasks;
namespace CryptoExchange.Net.Interfaces
{
public interface IWebsocket
public interface IWebsocket: IDisposable
{
void SetEnabledSslProtocols(SslProtocols protocols);