PageRenderTime 62ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 12ms app.codeStats 0ms

/src/Manos.IO/Manos.IO/Errors.cs

http://github.com/jacksonh/manos
C# | 144 lines | 106 code | 38 blank | 0 comment | 5 complexity | cd19932132aba5218e9e9f37dc26772e MD5 | raw file
  1using System;
  2using Mono.Unix.Native;
  3
  4namespace Manos.IO
  5{
  6	static class Errors
  7	{
  8		public static string ErrorToString (int errno)
  9		{
 10			return Syscall.strerror (NativeConvert.ToErrno (errno));
 11		}
 12		
 13		public static SocketError? ErrorToSocketError (int errno)
 14		{
 15			var err = NativeConvert.ToErrno (errno);
 16			switch (err) {
 17				case Errno.EAFNOSUPPORT:
 18					return SocketError.AddressFamilyNotSupported;
 19					
 20				case Errno.EMFILE:
 21				case Errno.ENFILE:
 22					return SocketError.TooManyOpenSockets;
 23					
 24				case Errno.EPROTONOSUPPORT:
 25					return SocketError.ProtocolNotSupported;
 26					
 27				case Errno.EPROTOTYPE:
 28					return SocketError.ProtocolType;
 29					
 30				case Errno.EOPNOTSUPP:
 31					return SocketError.OperationNotSupported;
 32					
 33				case Errno.ENOTCONN:
 34					return SocketError.NotConnected;
 35					
 36				case Errno.EADDRINUSE:
 37					return SocketError.AddressAlreadyInUse;
 38					
 39				case Errno.EADDRNOTAVAIL:
 40					return SocketError.AddressNotAvailable;
 41					
 42				case Errno.EISCONN:
 43					return SocketError.IsConnected;
 44					
 45				case Errno.ECONNRESET:
 46					return SocketError.ConnectionReset;
 47					
 48				case Errno.ENETDOWN:
 49					return SocketError.NetworkDown;
 50					
 51				case Errno.EHOSTUNREACH:
 52					return SocketError.HostUnreachable;
 53					
 54				case Errno.ENETUNREACH:
 55					return SocketError.NetworkUnreachable;
 56					
 57				case Errno.EALREADY:
 58					return SocketError.AlreadyInProgress;
 59					
 60				case Errno.ECONNREFUSED:
 61					return SocketError.ConnectionRefused;
 62					
 63				case Errno.ETIMEDOUT:
 64					return SocketError.TimedOut;
 65				
 66				default:
 67					return null;
 68			}
 69		}
 70		
 71		public static SocketError ErrorToSocketError (System.Net.Sockets.SocketError err)
 72		{
 73			switch (err) {
 74				case System.Net.Sockets.SocketError.AddressFamilyNotSupported:
 75					return SocketError.AddressFamilyNotSupported;
 76					
 77				case System.Net.Sockets.SocketError.TooManyOpenSockets:
 78					return SocketError.TooManyOpenSockets;
 79					
 80				case System.Net.Sockets.SocketError.ProtocolNotSupported:
 81					return SocketError.ProtocolNotSupported;
 82					
 83				case System.Net.Sockets.SocketError.ProtocolType:
 84					return SocketError.ProtocolType;
 85					
 86				case System.Net.Sockets.SocketError.OperationNotSupported:
 87					return SocketError.OperationNotSupported;
 88					
 89				case System.Net.Sockets.SocketError.NotConnected:
 90					return SocketError.NotConnected;
 91					
 92				case System.Net.Sockets.SocketError.AddressAlreadyInUse:
 93					return SocketError.AddressAlreadyInUse;
 94					
 95				case System.Net.Sockets.SocketError.AddressNotAvailable:
 96					return SocketError.AddressNotAvailable;
 97					
 98				case System.Net.Sockets.SocketError.IsConnected:
 99					return SocketError.IsConnected;
100					
101				case System.Net.Sockets.SocketError.ConnectionReset:
102					return SocketError.ConnectionReset;
103					
104				case System.Net.Sockets.SocketError.NetworkDown:
105					return SocketError.NetworkDown;
106					
107				case System.Net.Sockets.SocketError.HostUnreachable:
108					return SocketError.HostUnreachable;
109					
110				case System.Net.Sockets.SocketError.NetworkUnreachable:
111					return SocketError.NetworkUnreachable;
112					
113				case System.Net.Sockets.SocketError.AlreadyInProgress:
114					return SocketError.AlreadyInProgress;
115					
116				case System.Net.Sockets.SocketError.ConnectionRefused:
117					return SocketError.ConnectionRefused;
118					
119				case System.Net.Sockets.SocketError.TimedOut:
120					return SocketError.TimedOut;
121				
122				default:
123					return SocketError.Failure;
124			}
125		}
126		
127		public static IOException SocketStreamFailure (string text, int errno)
128		{
129			var err = ErrorToSocketError (errno);
130			if (err != null) {
131				return new IOException (text, new Manos.IO.SocketException (err.Value));
132			} else {
133				return new IOException (text, new InvalidOperationException (ErrorToString (errno)));
134			}
135		}
136		
137		public static Manos.IO.SocketException SocketFailure (string text, int errno)
138		{
139			var err = ErrorToSocketError (errno);
140			return new Manos.IO.SocketException (text, err ?? SocketError.Failure);
141		}
142	}
143}
144