Exemplo n.º 1
0
        /// <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);
            }
        }
Exemplo n.º 2
0
        public async Task UnsubscribeAsync(BitmexApiSubscriptionInfo subscription)
        {
            var subscriptionName = subscription.SubscriptionName;
            var message          = new SocketUnsubscriptionMessage(subscription.SubscriptionWithArgs);

            using (var semafore = new SemaphoreSlim(0, 1))
            {
                bool   success   = false;
                string error     = string.Empty;
                string status    = string.Empty;
                var    errorArgs = new string[0];
                OperationResultEventHandler resultReceived = args =>
                {
                    if (args.OperationType == OperationType.unsubscribe)
                    {
                        error     = args.Error;
                        status    = args.Status;
                        success   = args.Result;
                        errorArgs = args.Args;
                        semafore.Release(1);
                    }
                };
                _bitmexApiSocketProxy.OperationResultReceived += resultReceived;
                Log.Info($"Unsubscribing on {subscriptionName}...");
                _bitmexApiSocketProxy.Send(message);
                var waitReuslt = await semafore.WaitAsync(SocketMessageResponseTimeout);

                _bitmexApiSocketProxy.OperationResultReceived -= resultReceived;
                if (!waitReuslt)
                {
                    throw new BitmexSocketSubscriptionException("Unsubscription failed: timeout waiting unsubscription response");
                }

                if (success)
                {
                    Log.Info($"Successfully unsubscribed on {subscriptionName} ");
                    if (_actions.ContainsKey(subscription.SubscriptionName))
                    {
                        if (_actions[subscription.SubscriptionName].Contains(subscription))
                        {
                            _actions[subscription.SubscriptionName].Remove(subscription);
                        }
                    }
                }
                else
                {
                    Log.Error($"Failed to unsubscribe 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);
            }
        }
Exemplo n.º 4
0
        private bool Authorize()
        {
            var expiresTime  = _expiresTimeProvider.Get();
            var respReceived = new ManualResetEvent(false);
            var data         = new string[0];
            var error        = string.Empty;
            OperationResultEventHandler resultReceived = args =>
            {
                if (args.OperationType == OperationType.authKeyExpires)
                {
                    _isAuthorized = args.Result;
                    error         = args.Error;
                    data          = args.Args;
                    respReceived.Set();
                }
            };

            var signatureString = _signatureProvider.CreateSignature(_bitmexAuthorization.Secret, $"GET/realtime{expiresTime}");
            var message         = new SocketAuthorizationMessage(_bitmexAuthorization.Key, expiresTime, signatureString);

            _bitmexApiSocketProxy.OperationResultReceived += resultReceived;
            Log.Info("Authorizing...");
            _bitmexApiSocketProxy.Send(message);
            var waitResult = respReceived.WaitOne(SocketMessageResponseTimeout);

            _bitmexApiSocketProxy.OperationResultReceived -= resultReceived;
            if (!waitResult)
            {
                Log.Error("Timeout waiting authorization response");
                throw new BitmexSocketAuthorizationException("Authorization Failed: timeout waiting authorization response");
            }

            if (!IsAuthorized)
            {
                Log.Error($"Not authorized {error}");
                throw new BitmexSocketAuthorizationException(error, data);
            }

            Log.Info("Authorized successfully...");
            return(IsAuthorized);
        }