From 40d480e1fca5bf65872536d1b251c275f77b6e81 Mon Sep 17 00:00:00 2001 From: JKorf Date: Thu, 5 Feb 2026 21:54:16 +0100 Subject: [PATCH] Added AutoTimestamp option for socket client, fixed socket client timestamp offset bug --- .../Authentication/AuthenticationProvider.cs | 11 +++++++---- CryptoExchange.Net/Clients/BaseApiClient.cs | 5 ++++- CryptoExchange.Net/Objects/Options/ApiOptions.cs | 5 +++++ CryptoExchange.Net/Objects/Options/ExchangeOptions.cs | 4 ++++ CryptoExchange.Net/Objects/Options/RestApiOptions.cs | 5 ----- .../Objects/Options/RestExchangeOptions.cs | 5 ----- .../Objects/Options/SocketApiOptions.cs | 1 + .../Objects/Options/SocketExchangeOptions.cs | 10 ++++++++++ 8 files changed, 31 insertions(+), 15 deletions(-) diff --git a/CryptoExchange.Net/Authentication/AuthenticationProvider.cs b/CryptoExchange.Net/Authentication/AuthenticationProvider.cs index 5f73f41..efd50d9 100644 --- a/CryptoExchange.Net/Authentication/AuthenticationProvider.cs +++ b/CryptoExchange.Net/Authentication/AuthenticationProvider.cs @@ -518,11 +518,14 @@ namespace CryptoExchange.Net.Authentication /// protected DateTime GetTimestamp(SocketApiClient apiClient, bool includeOneSecondOffset = true) { - var result = TimeProvider.GetTime().Add(TimeOffsetManager.GetSocketOffset(apiClient.ClientName) ?? TimeSpan.Zero)!; - if (includeOneSecondOffset) - result = result.AddSeconds(-1); + var timestamp = TimeProvider.GetTime(); + if(apiClient.ApiOptions.AutoTimestamp ?? apiClient.ClientOptions.AutoTimestamp) + timestamp = timestamp.Add(-TimeOffsetManager.GetSocketOffset(apiClient.ClientName) ?? TimeSpan.Zero)!; - return result; + if (includeOneSecondOffset) + timestamp = timestamp.AddSeconds(-1); + + return timestamp; } /// diff --git a/CryptoExchange.Net/Clients/BaseApiClient.cs b/CryptoExchange.Net/Clients/BaseApiClient.cs index 13fe15d..266f845 100644 --- a/CryptoExchange.Net/Clients/BaseApiClient.cs +++ b/CryptoExchange.Net/Clients/BaseApiClient.cs @@ -13,7 +13,10 @@ namespace CryptoExchange.Net.Clients /// public abstract class BaseApiClient : IDisposable, IBaseApiClient { - private string? _clientName; + /// + /// Client name + /// + protected string? _clientName; /// /// Logger diff --git a/CryptoExchange.Net/Objects/Options/ApiOptions.cs b/CryptoExchange.Net/Objects/Options/ApiOptions.cs index a46fe79..ad4bb85 100644 --- a/CryptoExchange.Net/Objects/Options/ApiOptions.cs +++ b/CryptoExchange.Net/Objects/Options/ApiOptions.cs @@ -7,6 +7,11 @@ namespace CryptoExchange.Net.Objects.Options /// public class ApiOptions { + /// + /// Whether or not to automatically sync the local time with the server time + /// + public bool? AutoTimestamp { get; set; } + /// /// If true, the CallResult and DataEvent objects will also include the originally received string data in the OriginalData property. /// Note that this comes at a performance cost diff --git a/CryptoExchange.Net/Objects/Options/ExchangeOptions.cs b/CryptoExchange.Net/Objects/Options/ExchangeOptions.cs index c766a3c..8a5b139 100644 --- a/CryptoExchange.Net/Objects/Options/ExchangeOptions.cs +++ b/CryptoExchange.Net/Objects/Options/ExchangeOptions.cs @@ -8,6 +8,10 @@ namespace CryptoExchange.Net.Objects.Options /// public class ExchangeOptions { + /// + /// Whether or not to automatically sync the local time with the server time + /// + public bool AutoTimestamp { get; set; } /// /// Proxy settings /// diff --git a/CryptoExchange.Net/Objects/Options/RestApiOptions.cs b/CryptoExchange.Net/Objects/Options/RestApiOptions.cs index 244fb43..277adb8 100644 --- a/CryptoExchange.Net/Objects/Options/RestApiOptions.cs +++ b/CryptoExchange.Net/Objects/Options/RestApiOptions.cs @@ -8,11 +8,6 @@ namespace CryptoExchange.Net.Objects.Options /// public class RestApiOptions : ApiOptions { - /// - /// Whether or not to automatically sync the local time with the server time - /// - public bool? AutoTimestamp { get; set; } - /// /// How often the timestamp adjustment between client and server is recalculated. If you need a very small TimeSpan here you're probably better of syncing your server time more often /// diff --git a/CryptoExchange.Net/Objects/Options/RestExchangeOptions.cs b/CryptoExchange.Net/Objects/Options/RestExchangeOptions.cs index 86c3884..db4cd6e 100644 --- a/CryptoExchange.Net/Objects/Options/RestExchangeOptions.cs +++ b/CryptoExchange.Net/Objects/Options/RestExchangeOptions.cs @@ -8,11 +8,6 @@ namespace CryptoExchange.Net.Objects.Options /// public class RestExchangeOptions: ExchangeOptions { - /// - /// Whether or not to automatically sync the local time with the server time - /// - public bool AutoTimestamp { get; set; } - /// /// How often the timestamp adjustment between client and server is recalculated. If you need a very small TimeSpan here you're probably better of syncing your server time more often /// diff --git a/CryptoExchange.Net/Objects/Options/SocketApiOptions.cs b/CryptoExchange.Net/Objects/Options/SocketApiOptions.cs index b41f144..49098bd 100644 --- a/CryptoExchange.Net/Objects/Options/SocketApiOptions.cs +++ b/CryptoExchange.Net/Objects/Options/SocketApiOptions.cs @@ -27,6 +27,7 @@ namespace CryptoExchange.Net.Objects.Options item.ApiCredentials = ApiCredentials?.Copy(); item.OutputOriginalData = OutputOriginalData; item.SocketNoDataTimeout = SocketNoDataTimeout; + item.AutoTimestamp = AutoTimestamp; item.MaxSocketConnections = MaxSocketConnections; return item; } diff --git a/CryptoExchange.Net/Objects/Options/SocketExchangeOptions.cs b/CryptoExchange.Net/Objects/Options/SocketExchangeOptions.cs index 9500267..f8d1e7f 100644 --- a/CryptoExchange.Net/Objects/Options/SocketExchangeOptions.cs +++ b/CryptoExchange.Net/Objects/Options/SocketExchangeOptions.cs @@ -75,6 +75,15 @@ namespace CryptoExchange.Net.Objects.Options /// public int? ReceiveBufferSize { get; set; } + /// + /// ctor + /// + public SocketExchangeOptions() + { + // Enable auto timestamping by default for sockets + AutoTimestamp = true; + } + /// /// Create a copy of this options /// @@ -83,6 +92,7 @@ namespace CryptoExchange.Net.Objects.Options public T Set(T item) where T : SocketExchangeOptions, new() { item.ApiCredentials = ApiCredentials?.Copy(); + item.AutoTimestamp = AutoTimestamp; item.OutputOriginalData = OutputOriginalData; item.ReconnectPolicy = ReconnectPolicy; item.DelayAfterConnect = DelayAfterConnect;