Exemple #1
0
        private static IPAddress GetFirstAddress(string hostname, IKafkaLog log)
        {
            try
            {
                //lookup the IP address from the provided host name
                var addresses = Dns.GetHostAddresses(hostname);

                if (addresses.Length > 0)
                {
                    Array.ForEach(addresses, address => log.DebugFormat("Found address {0} for {1}", address, hostname));

                    var selectedAddress = addresses.FirstOrDefault(item => item.AddressFamily == AddressFamily.InterNetwork) ?? addresses.First();

                    log.DebugFormat("Using address {0} for {1}", selectedAddress, hostname);

                    return(selectedAddress);
                }
            }
            catch
            {
                throw new UnresolvedHostnameException("Could not resolve the following hostname: {0}", hostname);
            }

            throw new UnresolvedHostnameException("Could not resolve the following hostname: {0}", hostname);
        }
        private static IPAddress GetFirstAddress(string hostname, IKafkaLog log)
        {
            try
            {
                //lookup the IP address from the provided host name
                var addresses = Dns.GetHostAddresses(hostname);

                if (addresses.Length > 0)
                {
                    Array.ForEach(addresses, address => log.DebugFormat("Found address {0} for {1}", address, hostname));

                    var selectedAddress = addresses.FirstOrDefault(item => item.AddressFamily == AddressFamily.InterNetwork) ?? addresses.First();

                    log.DebugFormat("Using address {0} for {1}", selectedAddress, hostname);

                    return selectedAddress;
                }
            }
            catch 
            {
                throw new UnresolvedHostnameException("Could not resolve the following hostname: {0}", hostname);
            }

            throw new UnresolvedHostnameException("Could not resolve the following hostname: {0}", hostname);
        }
Exemple #3
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);
                    }
                }
            }
        }
        public static void DisposeSafely(this object objToDispose, IKafkaLog log)
        {
            var disposable = objToDispose as IDisposable;

            if (disposable != null)
            {
                var className = disposable.GetType().Name;
                try
                {
                    disposable.Dispose();
                    log?.DebugFormat("Successfully disposed {0}", className);
                }
                catch (Exception e)
                {
                    log?.WarnFormat("Error disposing {0}, Exception {1}", className, e);
                }
            }
        }
Exemple #5
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();
            }
        }