diff --git a/CryptoExchange.Net/ExchangeClient.cs b/CryptoExchange.Net/ExchangeClient.cs index fb61e78..0714e14 100644 --- a/CryptoExchange.Net/ExchangeClient.cs +++ b/CryptoExchange.Net/ExchangeClient.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; +using System.Net.NetworkInformation; +using System.Net.Sockets; using System.Reflection; using System.Threading.Tasks; using CryptoExchange.Net.Attributes; @@ -21,6 +23,7 @@ namespace CryptoExchange.Net { public IRequestFactory RequestFactory { get; set; } = new RequestFactory(); + protected string baseAddress; protected Log log; protected ApiProxy apiProxy; protected RateLimitingBehaviour rateLimitBehaviour; @@ -49,6 +52,7 @@ namespace CryptoExchange.Net log.UpdateWriters(exchangeOptions.LogWriters); log.Level = exchangeOptions.LogVerbosity; + baseAddress = exchangeOptions.BaseAddress; apiProxy = exchangeOptions.Proxy; if (apiProxy != null) log.Write(LogVerbosity.Info, $"Setting api proxy to {exchangeOptions.Proxy.Host}:{exchangeOptions.Proxy.Port}"); @@ -86,6 +90,41 @@ namespace CryptoExchange.Net authProvider = authentictationProvider; } + /// + /// Ping to see if the server is reachable + /// + /// The roundtrip time of the ping request + public CallResult Ping() => PingAsync().Result; + + /// + /// Ping to see if the server is reachable + /// + /// The roundtrip time of the ping request + public async Task> 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(0, new CantConnectError() { Message = "Ping failed: " + ((SocketException)e.InnerException).SocketErrorCode }); + else + return new CallResult(0, new CantConnectError() { Message = "Ping failed: " + e.InnerException.Message }); + } + return new CallResult(0, new CantConnectError() { Message = "Ping failed: " + e.Message }); + } + if (reply.Status == IPStatus.Success) + return new CallResult(reply.RoundtripTime, null); + return new CallResult(0, new CantConnectError() { Message = "Ping failed: " + reply.Status }); + } + protected virtual async Task> ExecuteRequest(Uri uri, string method = "GET", Dictionary parameters = null, bool signed = false) where T : class { log.Write(LogVerbosity.Debug, $"Creating request for " + uri); diff --git a/CryptoExchange.Net/Objects/ExchangeOptions.cs b/CryptoExchange.Net/Objects/ExchangeOptions.cs index 8fc75d0..67d3159 100644 --- a/CryptoExchange.Net/Objects/ExchangeOptions.cs +++ b/CryptoExchange.Net/Objects/ExchangeOptions.cs @@ -18,6 +18,11 @@ namespace CryptoExchange.Net /// public ApiCredentials ApiCredentials { get; set; } + /// + /// The base address of the client + /// + public string BaseAddress { get; set; } + /// /// Proxy to use ///