Пример #1
0
        private async Task ProcessSentTasksAsync(NetworkStream netStream, SocketPayloadSendTask sendTask)
        {
            if (sendTask == null)
            {
                return;
            }

            using (sendTask)
            {
                var failed = false;
                var sw     = Stopwatch.StartNew();
                try
                {
                    sw.Restart();
                    if (UseStatisticsTracker())
                    {
                        StatisticsTracker.IncrementGauge(StatisticGauge.ActiveWriteOperation);
                    }
                    if (OnWriteToSocketAttempt != null)
                    {
                        OnWriteToSocketAttempt(sendTask.Payload);
                    }
                    _log.DebugFormat("Sending data for CorrelationId:{0} Connection:{1}", sendTask.Payload.CorrelationId, Endpoint);
                    await netStream.WriteAsync(sendTask.Payload.Buffer, 0, sendTask.Payload.Buffer.Length).ConfigureAwait(false);

                    _log.DebugFormat("Data sent to CorrelationId:{0} Connection:{1}", sendTask.Payload.CorrelationId, Endpoint);
                    sendTask.Tcp.TrySetResult(sendTask.Payload);
                }
                catch (Exception ex)
                {
                    failed = true;
                    if (_disposeToken.IsCancellationRequested)
                    {
                        var exception = new ObjectDisposedException("Object is disposing.");
                        sendTask.Tcp.TrySetException(exception);
                        throw exception;
                    }

                    sendTask.Tcp.TrySetException(ex);
                    throw;
                }
                finally
                {
                    if (UseStatisticsTracker())
                    {
                        StatisticsTracker.DecrementGauge(StatisticGauge.ActiveWriteOperation);
                        StatisticsTracker.CompleteNetworkWrite(sendTask.Payload, sw.ElapsedMilliseconds, failed);
                    }
                }
            }
        }
Пример #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);
                    }
                }
            }
        }