1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-10 17:36:19 +00:00

Added ping method to exchange client

This commit is contained in:
Jan Korf 2018-08-12 15:47:07 +02:00
parent e035a34316
commit 25cf530d89
2 changed files with 44 additions and 0 deletions

View File

@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using CryptoExchange.Net.Attributes; using CryptoExchange.Net.Attributes;
@ -21,6 +23,7 @@ namespace CryptoExchange.Net
{ {
public IRequestFactory RequestFactory { get; set; } = new RequestFactory(); public IRequestFactory RequestFactory { get; set; } = new RequestFactory();
protected string baseAddress;
protected Log log; protected Log log;
protected ApiProxy apiProxy; protected ApiProxy apiProxy;
protected RateLimitingBehaviour rateLimitBehaviour; protected RateLimitingBehaviour rateLimitBehaviour;
@ -49,6 +52,7 @@ namespace CryptoExchange.Net
log.UpdateWriters(exchangeOptions.LogWriters); log.UpdateWriters(exchangeOptions.LogWriters);
log.Level = exchangeOptions.LogVerbosity; log.Level = exchangeOptions.LogVerbosity;
baseAddress = exchangeOptions.BaseAddress;
apiProxy = exchangeOptions.Proxy; apiProxy = exchangeOptions.Proxy;
if (apiProxy != null) if (apiProxy != null)
log.Write(LogVerbosity.Info, $"Setting api proxy to {exchangeOptions.Proxy.Host}:{exchangeOptions.Proxy.Port}"); log.Write(LogVerbosity.Info, $"Setting api proxy to {exchangeOptions.Proxy.Host}:{exchangeOptions.Proxy.Port}");
@ -86,6 +90,41 @@ namespace CryptoExchange.Net
authProvider = authentictationProvider; authProvider = authentictationProvider;
} }
/// <summary>
/// Ping to see if the server is reachable
/// </summary>
/// <returns>The roundtrip time of the ping request</returns>
public CallResult<long> Ping() => PingAsync().Result;
/// <summary>
/// Ping to see if the server is reachable
/// </summary>
/// <returns>The roundtrip time of the ping request</returns>
public async Task<CallResult<long>> PingAsync()
{
var ping = new Ping();
var uri = new Uri(baseAddress);
PingReply reply = null;
try
{
reply = await ping.SendPingAsync(uri.Host);
}
catch(PingException e)
{
if(e.InnerException != null)
{
if (e.InnerException is SocketException)
return new CallResult<long>(0, new CantConnectError() { Message = "Ping failed: " + ((SocketException)e.InnerException).SocketErrorCode });
else
return new CallResult<long>(0, new CantConnectError() { Message = "Ping failed: " + e.InnerException.Message });
}
return new CallResult<long>(0, new CantConnectError() { Message = "Ping failed: " + e.Message });
}
if (reply.Status == IPStatus.Success)
return new CallResult<long>(reply.RoundtripTime, null);
return new CallResult<long>(0, new CantConnectError() { Message = "Ping failed: " + reply.Status });
}
protected virtual async Task<CallResult<T>> ExecuteRequest<T>(Uri uri, string method = "GET", Dictionary<string, object> parameters = null, bool signed = false) where T : class protected virtual async Task<CallResult<T>> ExecuteRequest<T>(Uri uri, string method = "GET", Dictionary<string, object> parameters = null, bool signed = false) where T : class
{ {
log.Write(LogVerbosity.Debug, $"Creating request for " + uri); log.Write(LogVerbosity.Debug, $"Creating request for " + uri);

View File

@ -18,6 +18,11 @@ namespace CryptoExchange.Net
/// </summary> /// </summary>
public ApiCredentials ApiCredentials { get; set; } public ApiCredentials ApiCredentials { get; set; }
/// <summary>
/// The base address of the client
/// </summary>
public string BaseAddress { get; set; }
/// <summary> /// <summary>
/// Proxy to use /// Proxy to use
/// </summary> /// </summary>