Exemple #1
0
        internal static Task MessageLoopAsync(
            this TcpClient client,
            ICommunicationChannel channel,
            Action <Exception> errorHandler,
            CancellationToken cancellationToken)
        {
            Exception error = null;

            // Set read timeout to avoid blocking receive raw message
            while (channel != null && !cancellationToken.IsCancellationRequested)
            {
                try
                {
                    if (client.Client.Poll(STREAMREADTIMEOUT, SelectMode.SelectRead))
                    {
                        channel.NotifyDataAvailable();
                    }
                }
                catch (IOException ioException)
                {
                    var socketException = ioException.InnerException as SocketException;
                    if (socketException != null &&
                        socketException.SocketErrorCode == SocketError.TimedOut)
                    {
                        EqtTrace.Info(
                            "Socket: Message loop: failed to receive message due to read timeout {0}",
                            ioException);
                    }
                    else
                    {
                        EqtTrace.Error(
                            "Socket: Message loop: failed to receive message due to socket error {0}",
                            ioException);
                        error = ioException;
                        break;
                    }
                }
                catch (Exception exception)
                {
                    EqtTrace.Error(
                        "Socket: Message loop: failed to receive message {0}",
                        exception);
                    error = exception;
                    break;
                }
            }

            // Try clean up and raise client disconnected events
            errorHandler(error);

            return(Task.FromResult(0));
        }
        internal static Task MessageLoopAsync(
            this TcpClient client,
            ICommunicationChannel channel,
            Action <Exception> errorHandler,
            CancellationToken cancellationToken)
        {
            Exception error = null;

            var remoteEndPoint = string.Empty;
            var localEndPoint  = string.Empty;

            try
            {
                remoteEndPoint = client.Client.RemoteEndPoint?.ToString();
                localEndPoint  = client.Client.LocalEndPoint?.ToString();
            }
            catch (SocketException socketException)
            {
                EqtTrace.Error(
                    "TcpClientExtensions.MessageLoopAsync: Failed to access the endpoint due to socket error: {0}",
                    socketException);
            }

            // Set read timeout to avoid blocking receive raw message
            while (channel != null && !cancellationToken.IsCancellationRequested)
            {
                EqtTrace.Verbose("TcpClientExtensions.MessageLoopAsync: Polling on remoteEndPoint: {0} localEndPoint: {1}", remoteEndPoint, localEndPoint);

                try
                {
                    if (client.Client.Poll(STREAMREADTIMEOUT, SelectMode.SelectRead))
                    {
                        EqtTrace.Verbose("TcpClientExtensions.MessageLoopAsync: NotifyDataAvailable remoteEndPoint: {0} localEndPoint: {1}", remoteEndPoint, localEndPoint);
                        channel.NotifyDataAvailable();
                    }
                }
                catch (IOException ioException)
                {
                    if (ioException.InnerException is SocketException socketException &&
                        socketException.SocketErrorCode == SocketError.TimedOut)
                    {
                        EqtTrace.Info(
                            "Socket: Message loop: failed to receive message due to read timeout {0}, remoteEndPoint: {1} localEndPoint: {2}",
                            ioException,
                            remoteEndPoint,
                            localEndPoint);
                    }