예제 #1
0
        private Task <byte[]> EnqueueReadTask(int readSize, CancellationToken cancellationToken)
        {
            var readTask = new SocketPayloadReadTask(readSize, cancellationToken);

            _readTaskQueue.Add(readTask);
            return(readTask.Tcp.Task);
        }
예제 #2
0
        private async Task ProcessReadTaskAsync(NetworkStream netStream, SocketPayloadReadTask readTask)
        {
            using (readTask)
            {
                try
                {
                    if (UseStatisticsTracker())
                    {
                        StatisticsTracker.IncrementGauge(StatisticGauge.ActiveReadOperation);
                    }
                    var readSize = readTask.ReadSize;
                    var result = new List<byte>(readSize);
                    var bytesReceived = 0;

                    while (bytesReceived < readSize)
                    {
                        readSize = readSize - bytesReceived;
                        var buffer = new byte[readSize];

                        if (OnReadFromSocketAttempt != null) OnReadFromSocketAttempt(readSize);

                        bytesReceived = await netStream.ReadAsync(buffer, 0, readSize, readTask.CancellationToken).ConfigureAwait(false);

                        if (OnBytesReceived != null) OnBytesReceived(bytesReceived);

                        if (bytesReceived <= 0)
                        {
                            using (_client)
                            {
                                _client = null;
                                if (_disposeToken.IsCancellationRequested) { return; }

                                throw new BrokerConnectionException(string.Format("Lost connection to server: {0}", _endpoint), _endpoint);
                            }
                        }

                        result.AddRange(buffer.Take(bytesReceived));
                    }

                    readTask.Tcp.TrySetResult(result.ToArray());
                }
                catch (Exception ex)
                {
                    if (_disposeToken.IsCancellationRequested)
                    {
                        var exception = new ObjectDisposedException(string.Format("Object is disposing (KafkaTcpSocket for endpoint: {0})", Endpoint));
                        readTask.Tcp.TrySetException(exception);
                        throw exception;
                    }

                    if (ex is BrokerConnectionException)
                    {
                        readTask.Tcp.TrySetException(ex);
                        if (_disposeToken.IsCancellationRequested) return;
                        throw;
                    }

                    //if an exception made us lose a connection throw disconnected exception
                    if (_client == null || _client.Connected == false)
                    {
                        var exception = new BrokerConnectionException(string.Format("Lost connection to server: {0}", _endpoint), _endpoint);
                        readTask.Tcp.TrySetException(exception);
                        throw exception;
                    }

                    readTask.Tcp.TrySetException(ex);
                    if (_disposeToken.IsCancellationRequested) return;

                    throw;
                }
                finally
                {
                    if (UseStatisticsTracker())
                    {
                        StatisticsTracker.DecrementGauge(StatisticGauge.ActiveReadOperation);
                    }
                }
            }
        }
예제 #3
0
 private Task<byte[]> EnqueueReadTask(int readSize, CancellationToken cancellationToken)
 {
     var readTask = new SocketPayloadReadTask(readSize, cancellationToken);
     _readTaskQueue.Add(readTask);
     return readTask.Tcp.Task;
 }
예제 #4
0
        private async Task ProcessReadTaskAsync(NetworkStream netStream, SocketPayloadReadTask readTask)
        {
            using (readTask)
            {
                try
                {
                    if (UseStatisticsTracker())
                    {
                        StatisticsTracker.IncrementGauge(StatisticGauge.ActiveReadOperation);
                    }
                    var readSize      = readTask.ReadSize;
                    var result        = new List <byte>(readSize);
                    var bytesReceived = 0;

                    while (bytesReceived < readSize)
                    {
                        readSize = readSize - bytesReceived;
                        var buffer = new byte[readSize];

                        if (OnReadFromSocketAttempt != null)
                        {
                            OnReadFromSocketAttempt(readSize);
                        }

                        bytesReceived = await netStream.ReadAsync(buffer, 0, readSize, readTask.CancellationToken).ConfigureAwait(false);

                        if (OnBytesReceived != null)
                        {
                            OnBytesReceived(bytesReceived);
                        }

                        if (bytesReceived <= 0)
                        {
                            using (_client)
                            {
                                _client = null;
                                if (_disposeToken.IsCancellationRequested)
                                {
                                    return;
                                }

                                throw new BrokerConnectionException(string.Format("Lost connection to server: {0}", _endpoint), _endpoint);
                            }
                        }

                        result.AddRange(buffer.Take(bytesReceived));
                    }

                    readTask.Tcp.TrySetResult(result.ToArray());
                }
                catch (Exception ex)
                {
                    if (_disposeToken.IsCancellationRequested)
                    {
                        var exception = new ObjectDisposedException(string.Format("Object is disposing (KafkaTcpSocket for endpoint: {0})", Endpoint));
                        readTask.Tcp.TrySetException(exception);
                        throw exception;
                    }

                    if (ex is BrokerConnectionException)
                    {
                        readTask.Tcp.TrySetException(ex);
                        if (_disposeToken.IsCancellationRequested)
                        {
                            return;
                        }
                        throw;
                    }

                    //if an exception made us lose a connection throw disconnected exception
                    if (_client == null || _client.Connected == false)
                    {
                        var exception = new BrokerConnectionException(string.Format("Lost connection to server: {0}", _endpoint), _endpoint);
                        readTask.Tcp.TrySetException(exception);
                        throw exception;
                    }

                    readTask.Tcp.TrySetException(ex);
                    if (_disposeToken.IsCancellationRequested)
                    {
                        return;
                    }

                    throw;
                }
                finally
                {
                    if (UseStatisticsTracker())
                    {
                        StatisticsTracker.DecrementGauge(StatisticGauge.ActiveReadOperation);
                    }
                }
            }
        }