123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- using System;
- using System.IO;
- using System.Net.Sockets;
- namespace Vinno.IUS.Common.Network
- {
- public abstract class NetworkException :Exception
- {
- public string Name { get; protected set; }
- protected NetworkException(string message = "") : base(message)
- {
-
- }
- /// <summary>
- /// Get the network exception by given exception.
- /// </summary>
- public static Exception Convert(Exception ex)
- {
- var ioException = ex as IOException;
- var socketException = ioException?.InnerException as SocketException;
- if (socketException != null)
- {
- if (socketException.SocketErrorCode == SocketError.NetworkReset ||
- socketException.SocketErrorCode == SocketError.ConnectionAborted ||
- socketException.SocketErrorCode == SocketError.ConnectionReset)
- {
- return new ConnectionAbortException();
- }
- if (socketException.SocketErrorCode == SocketError.NotConnected ||
- socketException.SocketErrorCode == SocketError.Shutdown)
- {
- return new NotConnectedException();
- }
- if (socketException.SocketErrorCode == SocketError.ConnectionRefused ||
- socketException.SocketErrorCode == SocketError.HostDown ||
- socketException.SocketErrorCode == SocketError.HostUnreachable)
- {
- return new CanNotConnectException();
- }
- if (socketException.SocketErrorCode == SocketError.TimedOut)
- {
- return new ConnectionTimeoutException();
- }
- }
- return ex;
- }
- }
- /// <summary>
- /// The network is aborted exception
- /// </summary>
- public class ConnectionAbortException : NetworkException
- {
- public ConnectionAbortException()
- {
- Name = "ConnectionAbort";
- }
- }
- /// <summary>
- /// Network is not connected exception
- /// </summary>
- public class NotConnectedException :NetworkException
- {
- public NotConnectedException()
- {
- Name = "NotConnected";
- }
- }
- /// <summary>
- /// Network is not reachable exception.
- /// </summary>
- public class CanNotConnectException : NetworkException
- {
- public CanNotConnectException()
- {
- Name = "CanNotConnect";
- }
- }
- /// <summary>
- /// Connect or write or read timeout.
- /// </summary>
- public class ConnectionTimeoutException : NetworkException
- {
- public ConnectionTimeoutException()
- {
- Name = "ConnectionTmeout";
- }
- }
- public class ErrorDataException : NetworkException
- {
- public ErrorDataException(string message = "")
- {
- Name = "ErrorData";
- }
- }
- }
|