mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-07-25 18:57:07 +00:00
Added some extra checks
This commit is contained in:
parent
4ec4159455
commit
4e4ce047b4
@ -68,7 +68,10 @@ namespace CryptoExchange.Net.Authentication
|
|||||||
using (var reader = new StreamReader(inputStream, Encoding.ASCII, false, 512, true))
|
using (var reader = new StreamReader(inputStream, Encoding.ASCII, false, 512, true))
|
||||||
{
|
{
|
||||||
var stringData = reader.ReadToEnd();
|
var stringData = reader.ReadToEnd();
|
||||||
var jsonData = JToken.Parse(stringData);
|
var jsonData = stringData.ToJToken();
|
||||||
|
if(jsonData == null)
|
||||||
|
throw new ArgumentException("Input stream not valid json data");
|
||||||
|
|
||||||
var key = TryGetValue(jsonData, identifierKey ?? "apiKey");
|
var key = TryGetValue(jsonData, identifierKey ?? "apiKey");
|
||||||
var secret = TryGetValue(jsonData, identifierSecret ?? "apiSecret");
|
var secret = TryGetValue(jsonData, identifierSecret ?? "apiSecret");
|
||||||
|
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Security;
|
using System.Security;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using CryptoExchange.Net.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
namespace CryptoExchange.Net
|
namespace CryptoExchange.Net
|
||||||
{
|
{
|
||||||
@ -127,5 +131,30 @@ namespace CryptoExchange.Net
|
|||||||
{
|
{
|
||||||
return handle.WaitOneAsync((int)timeout.TotalMilliseconds, CancellationToken.None);
|
return handle.WaitOneAsync((int)timeout.TotalMilliseconds, CancellationToken.None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static JToken ToJToken(this string stringData, Log log = null)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(stringData))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return JToken.Parse(stringData);
|
||||||
|
}
|
||||||
|
catch (JsonReaderException jre)
|
||||||
|
{
|
||||||
|
var info = $"Deserialize JsonReaderException: {jre.Message}, Path: {jre.Path}, LineNumber: {jre.LineNumber}, LinePosition: {jre.LinePosition}. Data: {stringData}";
|
||||||
|
log?.Write(LogVerbosity.Error, info);
|
||||||
|
if (log == null) Debug.WriteLine(LogVerbosity.Error, info);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (JsonSerializationException jse)
|
||||||
|
{
|
||||||
|
var info = $"Deserialize JsonSerializationException: {jse.Message}. Data: {stringData}";
|
||||||
|
log?.Write(LogVerbosity.Error, info);
|
||||||
|
if (log == null) Debug.WriteLine(LogVerbosity.Error, info);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,8 +75,32 @@ namespace CryptoExchange.Net.Sockets
|
|||||||
|
|
||||||
private void HandleByteData(byte[] data)
|
private void HandleByteData(byte[] data)
|
||||||
{
|
{
|
||||||
var message = DataInterpreterBytes(data);
|
if (DataInterpreterBytes == null)
|
||||||
Handle(messageHandlers, message);
|
throw new Exception("Byte interpreter not set while receiving byte data");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var message = DataInterpreterBytes(data);
|
||||||
|
Handle(messageHandlers, message);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
log.Write(LogVerbosity.Error, $"{Id} Something went wrong while processing a byte message from the socket: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleStringData(string data)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (DataInterpreterString != null)
|
||||||
|
data = DataInterpreterString(data);
|
||||||
|
Handle(messageHandlers, data);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
log.Write(LogVerbosity.Error, $"{Id} Something went wrong while processing a string message from the socket: {ex}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public event Action OnClose
|
public event Action OnClose
|
||||||
@ -201,13 +225,7 @@ namespace CryptoExchange.Net.Sockets
|
|||||||
socket.Opened += (o, s) => Handle(openHandlers);
|
socket.Opened += (o, s) => Handle(openHandlers);
|
||||||
socket.Closed += (o, s) => Handle(closeHandlers);
|
socket.Closed += (o, s) => Handle(closeHandlers);
|
||||||
socket.Error += (o, s) => Handle(errorHandlers, s.Exception);
|
socket.Error += (o, s) => Handle(errorHandlers, s.Exception);
|
||||||
socket.MessageReceived += (o, s) =>
|
socket.MessageReceived += (o, s) => HandleStringData(s.Message);
|
||||||
{
|
|
||||||
string data = s.Message;
|
|
||||||
if (DataInterpreterString != null)
|
|
||||||
data = DataInterpreterString(data);
|
|
||||||
Handle(messageHandlers, data);
|
|
||||||
};
|
|
||||||
socket.DataReceived += (o, s) => HandleByteData(s.Data);
|
socket.DataReceived += (o, s) => HandleByteData(s.Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,9 +91,10 @@ namespace CryptoExchange.Net.Sockets
|
|||||||
public void ProcessMessage(string data)
|
public void ProcessMessage(string data)
|
||||||
{
|
{
|
||||||
log.Write(LogVerbosity.Debug, $"Socket {Socket.Id} received data: " + data);
|
log.Write(LogVerbosity.Debug, $"Socket {Socket.Id} received data: " + data);
|
||||||
|
var tokenData = data.ToJToken(log);
|
||||||
var tokenData = JToken.Parse(data);
|
if (tokenData == null)
|
||||||
|
return;
|
||||||
|
|
||||||
foreach (var pendingRequest in pendingRequests.ToList())
|
foreach (var pendingRequest in pendingRequests.ToList())
|
||||||
{
|
{
|
||||||
if (pendingRequest.Check(tokenData))
|
if (pendingRequest.Check(tokenData))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user