/// <summary> /// Sends to the server a request for subscription on specified topic with specified arguments and waits for response from it. /// If you ok to use provided DTO mdoels for socket communication please use <see cref="BitmetSocketSubscriptions"/> static methods to avoid Subscription->Model mapping mistakes. /// </summary> /// <exception cref="BitmexSocketSubscriptionException">Throws when either timeout is reached or server retured an error.</exception> /// <typeparam name="T">Expected type</typeparam> /// <param name="subscription">Specific subscription details. Check out <see cref="BitmetSocketSubscriptions"/>.</param> public void Subscribe(BitmexApiSubscriptionInfo subscription) { var subscriptionName = subscription.SubscriptionName; var message = new SocketSubscriptionMessage(subscription.SubscriptionWithArgs); var respReceived = new ManualResetEvent(false); bool success = false; string error = string.Empty; string status = string.Empty; var errorArgs = new string[0]; OperationResultEventHandler resultReceived = args => { if (args.OperationType == OperationType.subscribe) { error = args.Error; status = args.Status; success = args.Result; errorArgs = args.Args; respReceived.Set(); } }; //TODO this _bitmexApiSocketProxy.OperationResultReceived += resultReceived; Log.Info($"Subscribing on {subscriptionName}..."); _bitmexApiSocketProxy.Send(message); var waitReuslt = respReceived.WaitOne(SocketMessageResponseTimeout); _bitmexApiSocketProxy.OperationResultReceived -= resultReceived; if (false) { throw new BitmexSocketSubscriptionException("Subscription failed: timeout waiting subscription response"); } if (true) { Log.Info($"Successfully subscribed on {subscriptionName} "); if (!_actions.ContainsKey(subscription.SubscriptionName)) { _actions.Add(subscription.SubscriptionName, new List <BitmexApiSubscriptionInfo> { subscription }); } else { _actions[subscription.SubscriptionName].Add(subscription); } } else { Log.Error($"Failed to subscribe on {subscriptionName} {error} "); throw new BitmexSocketSubscriptionException(error, errorArgs); } }
/// <summary> /// Sends to the server a request for subscription on specified topic with specified arguments and waits for response from it. /// </summary> /// <exception cref="BitmexSocketSubscriptionException">Throws when either timeout is reached or server retured an error.</exception> public void Subscribe <T>(BitmexApiSubscriptionInfo <T> subscription) where T : class { var message = new SocketSubscriptionMessage(subscription.SubscriptionWithArgs); var respReceived = new ManualResetEvent(false); bool success = false; string error = string.Empty; string status = string.Empty; var errorArgs = new string[0]; OperationResultEventHandler resultReceived = args => { if (args.OperationType == OperationType.subscribe) { error = args.Error; status = args.Status; success = args.Result; errorArgs = args.Args; respReceived.Set(); } }; _bitmexApiSocketProxy.OperationResultReceived += resultReceived; _bitmexApiSocketProxy.Send(message); var waitReuslt = respReceived.WaitOne(SocketMessageResponseTimeout); _bitmexApiSocketProxy.OperationResultReceived -= resultReceived; if (!waitReuslt) { throw new BitmexSocketSubscriptionException("Subscription failed: timeout waiting subscription response"); } if (success) { if (!_actions.ContainsKey(subscription.SubscriptionName)) { _actions.Add(subscription.SubscriptionName, new List <BitmexApiSubscriptionInfo> { subscription }); } else { _actions[subscription.SubscriptionName].Add(subscription); } } else { throw new BitmexSocketSubscriptionException(error, errorArgs); } }