Beispiel #1
0
        /// <summary>
        /// The DI constructor.
        /// </summary>
        /// <param name="aggregateTradeClient"></param>
        /// <param name="candlestickClient"></param>
        /// <param name="depthClient"></param>
        /// <param name="statisticsClient"></param>
        /// <param name="tradeClient"></param>
        /// <param name="logger"></param>
        protected BinanceJsonClientManager(
            IAggregateTradeWebSocketClient aggregateTradeClient,
            ICandlestickWebSocketClient candlestickClient,
            IDepthWebSocketClient depthClient,
            ISymbolStatisticsWebSocketClient statisticsClient,
            ITradeWebSocketClient tradeClient,
            ILogger<BinanceJsonClientManager<TStream>> logger = null)
        {
            Throw.IfNull(aggregateTradeClient, nameof(aggregateTradeClient));
            Throw.IfNull(candlestickClient, nameof(candlestickClient));
            Throw.IfNull(depthClient, nameof(depthClient));
            Throw.IfNull(statisticsClient, nameof(statisticsClient));
            Throw.IfNull(tradeClient, nameof(tradeClient));

            AggregateTradeClient = aggregateTradeClient;
            CandlestickClient = candlestickClient;
            DepthClient = depthClient;
            StatisticsClient = statisticsClient;
            TradeClient = tradeClient;
            _logger = logger;

            // Forward controller error events.
            aggregateTradeClient.Error += HandleError;
            candlestickClient.Error += HandleError;
            depthClient.Error += HandleError;
            statisticsClient.Error += HandleError;
            tradeClient.Error += HandleError;
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="client"></param>
        /// <param name="symbol"></param>
        /// <param name="callback"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static Task StreamAsync(this ISymbolStatisticsWebSocketClient client, string symbol, Action <SymbolStatisticsEventArgs> callback, CancellationToken token)
        {
            Throw.IfNull(client, nameof(client));

            client.Subscribe(symbol, callback);

            return(client.WebSocket.StreamAsync(token));
        }
 /// <summary>
 /// The DI constructor.
 /// </summary>
 /// <param name="aggregateTradeWebSocketClient"></param>
 /// <param name="candlestickWebSocketClient"></param>
 /// <param name="depthWebSocketClient"></param>
 /// <param name="symbolStatisticsWebSocketClient"></param>
 /// <param name="tradeWebSocketClient"></param>
 /// <param name="logger"></param>
 public BinanceWebSocketClientManager(
     IAggregateTradeWebSocketClient aggregateTradeWebSocketClient,
     ICandlestickWebSocketClient candlestickWebSocketClient,
     IDepthWebSocketClient depthWebSocketClient,
     ISymbolStatisticsWebSocketClient symbolStatisticsWebSocketClient,
     ITradeWebSocketClient tradeWebSocketClient,
     ILogger <BinanceWebSocketClientManager> logger = null)
     : base(aggregateTradeWebSocketClient,
            candlestickWebSocketClient,
            depthWebSocketClient,
            symbolStatisticsWebSocketClient,
            tradeWebSocketClient,
            logger)
 {
 }
        private async Task SubscribeToSymbols()
        {
            if (_symbolsSubscribeTask != null)
            {
                return;
            }

            try
            {
                try
                {
                    await _symbolsStatisticSemaphore.WaitAsync();

                    if (_symbolsSubscribeTask != null)
                    {
                        return;
                    }

                    _log.LogInformation($"Subscribe to symbols");

                    _symbolStatisticCancellationTokenSource?.Cancel();
                    _symbolStatisticCancellationTokenSource?.Dispose();

                    _symbolStatisticCancellationTokenSource = new CancellationTokenSource();

                    _symbolStatisticsWebSocketClient = _serviceProvider.GetService <ISymbolStatisticsWebSocketClient>();

                    _symbolsSubscribeTask = _symbolStatisticsWebSocketClient.SubscribeAsync(_onSymbolStatisticUpdate, _symbolStatisticCancellationTokenSource.Token);

                    SymbolsDisconnectionTimerInitialize();
                }
                finally
                {
                    _symbolsStatisticSemaphore.Release();
                }

                await _symbolsSubscribeTask;

                _symbolsSubscribeTask = null;
            }
            catch (Exception)
            {
                _symbolsSubscribeTask = null;

                OnSymbolStatisticDisconnect(true);
            }
        }
Beispiel #5
0
        private async Task SubscribeToSymbols(bool reConnect = false)
        {
            _log.LogInformation($"Subscribe to symbols");

            if (_symbolsSubscribeTask != null && !reConnect)
            {
                return;
            }

            try
            {
                _symbolStatisticCancellationTokenSource?.Cancel();
                _symbolStatisticCancellationTokenSource?.Dispose();

                _symbolStatisticCancellationTokenSource = new CancellationTokenSource();

                _symbolStatisticsWebSocketClient = _serviceProvider.GetService <ISymbolStatisticsWebSocketClient>();

                _symbolsSubscribeTask = _symbolStatisticsWebSocketClient.SubscribeAsync(_onSymbolStatisticUpdate, _symbolStatisticCancellationTokenSource.Token);

                if (!reConnect)
                {
                    SymbolsReConnectionTimerInitialize();
                }

                await _symbolsSubscribeTask;
            }
            catch (Exception ex)
            {
                _symbolsSubscribeTask = null;

                _log.LogError($"Error with symbols statistic websocket {ex.Message}", ex);

                _onSymbolStatisticError?.Invoke();
            }
        }
Beispiel #6
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="client"></param>
 /// <param name="symbol"></param>
 /// <param name="token"></param>
 /// <returns></returns>
 public static Task StreamAsync(this ISymbolStatisticsWebSocketClient client, string symbol, CancellationToken token)
 => StreamAsync(client, symbol, null, token);
Beispiel #7
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="client"></param>
 /// <param name="token"></param>
 /// <returns></returns>
 public static Task StreamAsync(this ISymbolStatisticsWebSocketClient client, CancellationToken token)
 => StreamAsync(client, (Action <SymbolStatisticsEventArgs>)null, token);
Beispiel #8
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="client"></param>
 /// <param name="symbol"></param>
 /// <returns></returns>
 public static void Subscribe(this ISymbolStatisticsWebSocketClient client, string symbol)
 => client.Subscribe(symbol, null);
Beispiel #9
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="client"></param>
 /// <returns></returns>
 public static void Subscribe(this ISymbolStatisticsWebSocketClient client)
 => client.Subscribe(null);
        /// <summary>
        ///
        /// </summary>
        /// <param name="client"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static Task StreamAsync(this ISymbolStatisticsWebSocketClient client, CancellationToken token)
        {
            Throw.IfNull(client, nameof(client));

            return(client.WebSocket.StreamAsync(token));
        }
        public KucoinWebSocketManager(
            IAggregateTradeWebSocketClient aggregateTradeClient,
            ICandlestickWebSocketClient candlestickClient,
            IDepthWebSocketClient depthClient,
            ISymbolStatisticsWebSocketClient statisticsClient,
            ITradeWebSocketClient tradeClient,
            ILogger <IKucoinWebSocketManager> logger = null)
        {
            Throw.IfNull(aggregateTradeClient, nameof(aggregateTradeClient));
            Throw.IfNull(candlestickClient, nameof(candlestickClient));
            Throw.IfNull(depthClient, nameof(depthClient));
            Throw.IfNull(statisticsClient, nameof(statisticsClient));
            Throw.IfNull(tradeClient, nameof(tradeClient));

            _logger = logger;


            _aggregateTradeClientAdapter = new AggregateTradeWebSocketClientAdapter(
                this, aggregateTradeClient, _logger,
                err => RaiseErrorEvent(_aggregateTradeClientAdapter, err,
                                       $"{nameof(IAggregateTradeWebSocketClient)}: Adapter failed."));

            _candlestickClientAdapter = new CandlestickWebSocketClientAdapter(
                this, candlestickClient, _logger,
                err => RaiseErrorEvent(_candlestickClientAdapter, err,
                                       $"{nameof(ICandlestickWebSocketClient)}: Adapter failed."));

            _depthClientAdapter = new DepthWebSocketClientAdapter(
                this, depthClient, _logger,
                err => RaiseErrorEvent(_depthClientAdapter, err,
                                       $"{nameof(IDepthWebSocketClient)}: Adapter failed."));

            _statisticsClientAdapter = new SymbolStatisticsWebSocketClientAdapter(
                this, statisticsClient, _logger,
                err => RaiseErrorEvent(_statisticsClientAdapter, err,
                                       $"{nameof(ISymbolStatisticsWebSocketClient)}: Adapter failed."));

            _tradeClientAdapter = new TradeWebSocketClientAdapter(
                this, tradeClient, _logger,
                err => RaiseErrorEvent(_tradeClientAdapter, err,
                                       $"{nameof(ITradeWebSocketClient)}: Adapter failed."));


            _controllers[aggregateTradeClient.WebSocket] =
                new WebSocketStreamController(
                    aggregateTradeClient.WebSocket,
                    err => RaiseErrorEvent(_aggregateTradeClientAdapter, err,
                                           $"{nameof(IAggregateTradeWebSocketClient)}: Controller failed."));

            _controllers[candlestickClient.WebSocket] =
                new WebSocketStreamController(
                    candlestickClient.WebSocket,
                    err => RaiseErrorEvent(_candlestickClientAdapter, err,
                                           $"{nameof(ICandlestickWebSocketClient)}: Controller failed."));

            _controllers[depthClient.WebSocket] =
                new WebSocketStreamController(
                    depthClient.WebSocket,
                    err => RaiseErrorEvent(_depthClientAdapter, err,
                                           $"{nameof(IDepthWebSocketClient)}: Controller failed."));

            _controllers[statisticsClient.WebSocket] =
                new WebSocketStreamController(
                    statisticsClient.WebSocket,
                    err => RaiseErrorEvent(_statisticsClientAdapter, err,
                                           $"{nameof(ISymbolStatisticsWebSocketClient)}: Controller failed."));

            _controllers[tradeClient.WebSocket] =
                new WebSocketStreamController(
                    tradeClient.WebSocket,
                    err => RaiseErrorEvent(_tradeClientAdapter, err,
                                           $"{nameof(ITradeWebSocketClient)}: Controller failed."));
        }
Beispiel #12
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="client"></param>
 /// <param name="symbol"></param>
 /// <param name="token"></param>
 /// <returns></returns>
 public static Task SubscribeAsync(this ISymbolStatisticsWebSocketClient client, string symbol, CancellationToken token)
 => client.SubscribeAsync(symbol, null, token);