示例#1
0
        private void DedicatedSocketTask()
        {
            while (_disposeToken.IsCancellationRequested == false)
            {
                try
                {
                    _processNetworkstreamTasksAction();
                }
                catch (Exception ex)
                {
                    if (_disposeToken.IsCancellationRequested)
                    {
                        _log.WarnFormat("KafkaTcpSocket thread shutting down because of a dispose call.");
                        var disposeException = new ObjectDisposedException("Object is disposing.");
                        _sendTaskQueue.DrainAndApply(t => t.Tcp.TrySetException(disposeException));
                        _readTaskQueue.DrainAndApply(t => t.Tcp.TrySetException(disposeException));
                        return;
                    }

                    if (ex is ServerDisconnectedException)
                    {
                        if (OnServerDisconnected != null)
                        {
                            OnServerDisconnected();
                        }
                        _log.ErrorFormat(ex.Message);
                        continue;
                    }

                    _log.ErrorFormat("Exception occured in Socket handler task.  Exception: {0}", ex);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Stop all pendding task when can not establish connection in max retry,
        /// but keep trying to recove and connect to this connection.
        /// Only the broker router can dispose of it.
        /// </summary>
        /// <returns></returns>
        private async Task DedicatedSocketTask()
        {
            while (_disposeToken.IsCancellationRequested == false)
            {
                //block here until we can get connections then start loop pushing data through network stream
                var netStreamTask = GetStreamAsync();
                try
                {
                    await Task.WhenAny(_disposeTask, netStreamTask).ConfigureAwait(false);

                    if (_disposeToken.IsCancellationRequested)
                    {
                        SetDisposeExceptonToPenndingTasks();
                        if (OnServerDisconnected != null)
                        {
                            OnServerDisconnected();
                        }
                        return;
                    }
                    await netStreamTask.ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    _log.ErrorFormat("Exception occured in Socket handler task.  Exception: {0}", ex);

                    SetDisposeExceptonToPenndingTasks(new SocketException());
                    if (OnServerDisconnected != null)
                    {
                        OnServerDisconnected();
                    }
                }
                try
                {
                    var netStream = await netStreamTask.ConfigureAwait(false);
                    await ProcessNetworkstreamTasks(netStream).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    //it handel the Pennding task inside so we don't need to SetDisposeExceptonToPenndingTasks
                    _log.ErrorFormat("Exception occured in Socket handler task.  Exception: {0}", ex);
                    if (OnServerDisconnected != null)
                    {
                        OnServerDisconnected();
                    }
                }
            }
        }
示例#3
0
        public async Task SendDataAsync(byte[] data)
        {
            try
            {
                await _semaphoreSlim.WaitAsync();

                _log.DebugFormat("FakeTcpServer: writing {0} bytes.", data.Length);
                await _client.GetStream().WriteAsync(data, 0, data.Length).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _log.ErrorFormat("error:{0} stack{1}", ex.Message, ex.StackTrace);
            }
            finally
            {
                _semaphoreSlim.Release();
            }
        }
        private void SetExceptionToAllPendingTasks(Exception ex)
        {
            if (_sendTaskQueue.Count > 0)
            {
                _log.ErrorFormat("KafkaTcpSocket received an exception, cancelling all pending tasks: {0}", ex);
            }

            var wrappedException = WrappedException(ex);

            _sendTaskQueue.DrainAndApply(t => t.Tcp.TrySetException(wrappedException));
            _readTaskQueue.DrainAndApply(t => t.Tcp.TrySetException(wrappedException));
        }