using System; using System.Threading; namespace CryptoExchange.Net.Sockets { /// /// Socket subscription /// public class SocketSubscription { /// /// Unique subscription id /// public int Id { get; } /// /// Exception event /// public event Action? Exception; /// /// Message handlers for this subscription. Should return true if the message is handled and should not be distributed to the other handlers /// public Action MessageHandler { get; set; } /// /// The request object send when subscribing on the server. Either this or the `Identifier` property should be set /// public object? Request { get; set; } /// /// The subscription identifier, used instead of a `Request` object to identify the subscription /// public string? Identifier { get; set; } /// /// Whether this is a user subscription or an internal listener /// public bool UserSubscription { get; set; } /// /// If the subscription has been confirmed to be subscribed by the server /// public bool Confirmed { get; set; } /// /// Cancellation token registration, should be disposed when subscription is closed. Used for closing the subscription with /// a provided cancelation token /// public CancellationTokenRegistration? CancellationTokenRegistration { get; set; } private SocketSubscription(int id, object? request, string? identifier, bool userSubscription, Action dataHandler) { Id = id; UserSubscription = userSubscription; MessageHandler = dataHandler; Request = request; Identifier = identifier; } /// /// Create SocketSubscription for a subscribe request /// /// /// /// /// /// public static SocketSubscription CreateForRequest(int id, object request, bool userSubscription, Action dataHandler) { return new SocketSubscription(id, request, null, userSubscription, dataHandler); } /// /// Create SocketSubscription for an identifier /// /// /// /// /// /// public static SocketSubscription CreateForIdentifier(int id, string identifier, bool userSubscription, Action dataHandler) { return new SocketSubscription(id, null, identifier, userSubscription, dataHandler); } /// /// Invoke the exception event /// /// public void InvokeExceptionHandler(Exception e) { Exception?.Invoke(e); } } }