Exemplo n.º 1
0
        /// <summary>
        /// Handles exceptions thrown during a call to <see cref="OpenClientSession"/> method
        /// </summary>
        /// <param name="exception">The exception to handle</param>
        private static void HandleConnectionException(SocketException exception)
        {
            /*
             * This method checks for conditions that are likely to occur
             * when connecting to a remote host.
             * Such conditions might only be temporary and due to the following situations:
             * - The remote host has no specified port in a listening state
             * - The specified host is not reachable
             * - The network is down
             * - The route to the destination network is not known
             * - The route to the destination host is not known
             * - Connection request to the remote host timed out
             */
            switch (exception.NativeErrorCode)
            {
            case 10050:     //WSAENETDOWN -- The network is down
            case 10051:     //WSAENETUNREACH -- The network is unreachable
            case 10060:     //WSAETIMEDOUT -- Connection time out
            case 10061:     //WSAECONNREFUSED -- No specified port is in a listening state on the remote machine
            case 10064:     //WSAHOSTDOWN -- The remote host is down
            case 10065:     //WSAHOSTUNREACH -- The remote host is unreachable
            case 11001:     //WSAHOST_NOT_FOUND -- Host not found, no such host is known
                //Wrap this exception in a TcpIpConnectionException exception and throw
                throw new TcpIpConnectionException(exception);

            default:
                //For any other error code not listed, throw TcpIpException exception
                TcpIpException.WrapAndThrow(exception);
                break;
            }
        }
Exemplo n.º 2
0
        private void WrapAndThrow(SocketException exception)
        {
            /*
             * This code checks for error codes that a likely to be caused
             * by a failure in connection to a remote host.
             * Such situations will render the session incapable to serve
             * any subsequent requests.
             */
            switch (exception.NativeErrorCode)
            {
            case 10050:     //WSAENETDOWN -- Network is down
            case 10052:     //WSAENETRESET -- Connection broken due to failure in connection
            case 10053:     //WSAECONNABORTED -- Connection was aborted by a software in the host machine
            case 10054:     //WSAECONNRESET -- Remote host forcebly closed connection
            case 10057:     //WSAENOTCONN -- Socket is not connected
            case 10064:     //WSAHOSTDOWN -- Remote host is down
            case 10101:     //WSAEDISCON -- Remote party has called a graceful shutdown
                //Wrap this exception in a new exception
                throw new TcpIpSessionClosedException(exception);

            default:
                //Rethrow the original exception
                TcpIpException.WrapAndThrow(exception);
                break;
            }
        }