コード例 #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);
            }
        }
コード例 #2
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="request">Specific subscription details.</param>
        public void Subscribe(SubscriptionRequest request)
        {
            var topic                  = request.SubscriptionType.ToString().FirstToLower() + (string.IsNullOrEmpty(request.Symbol) ? string.Empty : ":" + request.Symbol);
            var respReceived           = new ManualResetEvent(false);
            SubscribeResponse?response = null;

            void ResultReceived(object sender, ResponseEventArgs <SubscribeResponse> e)
            {
                if (e.Response.Subscribe == topic)
                {
                    response = e.Response;
                    respReceived.Set();
                }
            }

            this.SubscribeResponseReceived += ResultReceived;
            //_bitmexApiSocketProxy.OperationResultReceived += ResultReceived;
            _logger.LogInformation($"Subscribing on {topic}...");
            _bitmexApiSocketProxy.Send(request);
            var waitReuslt = respReceived.WaitOne(SocketMessageResponseTimeout);

            this.SubscribeResponseReceived -= ResultReceived;
            //_bitmexApiSocketProxy.OperationResultReceived -= ResultReceived;
            if (!waitReuslt)
            {
                throw new BitmexSocketSubscriptionException("Subscription failed: timeout waiting subscription response");
            }

            if (response?.Success ?? false)
            {
                _logger.LogInformation($"Successfully subscribed on {topic} ");
            }
            else
            {
                var error = response?.Error ?? "Unknown error.";
                _logger.LogError($"Failed to subscribe on {topic} {error} ");
                throw new BitmexSocketSubscriptionException(error, new[] { topic });
            }
        }
コード例 #3
0
        private bool Authorize()
        {
            var nonce        = _nonceProvider.GetNonce();
            var respReceived = new ManualResetEvent(false);
            var data         = new string[0];
            var error        = string.Empty;
            OperationResultEventHandler resultReceived = args =>
            {
                if (args.OperationType == OperationType.authKey)
                {
                    _isAuthorized = args.Result;
                    error         = args.Error;
                    data          = args.Args;
                    respReceived.Set();
                }
            };

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

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

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

            if (!IsAuthorized)
            {
                throw new BitmexSocketAuthorizationException(error, data);
            }

            return(IsAuthorized);
        }