diff options
Diffstat (limited to 'cs/src/Ice/Network.cs')
-rw-r--r-- | cs/src/Ice/Network.cs | 496 |
1 files changed, 327 insertions, 169 deletions
diff --git a/cs/src/Ice/Network.cs b/cs/src/Ice/Network.cs index 30994abb5ef..3609ac8c6e6 100644 --- a/cs/src/Ice/Network.cs +++ b/cs/src/Ice/Network.cs @@ -29,34 +29,101 @@ namespace IceInternal public const int EnableIPv4 = 0; public const int EnableIPv6 = 1; public const int EnableBoth = 2; - + +#if COMPACT + public static SocketError socketErrorCode(SocketException ex) + { + return (SocketError)ex.ErrorCode; + } +#else + public static SocketError socketErrorCode(SocketException ex) + { + return ex.SocketErrorCode; + } +#endif + +#if COMPACT // - // Magic numbers taken from winsock2.h + // SocketError enumeration isn't available with Silverlight // - const int WSAEINTR = 10004; - const int WSAEFAULT = 10014; - const int WSAEINVAL = 10022; - const int WSAEMFILE = 10024; - const int WSAEWOULDBLOCK = 10035; - const int WSAEINPROGRESS = 10036; // Deprecated in winsock2, but still used by Mono Beta 1 - const int WSAEMSGSIZE = 10040; - const int WSAENETDOWN = 10050; - const int WSAENETUNREACH = 10051; - const int WSAENETRESET = 10052; - const int WSAECONNABORTED = 10053; - const int WSAECONNRESET = 10054; - const int WSAENOBUFS = 10055; - const int WSAENOTCONN = 10057; - const int WSAESHUTDOWN = 10058; - const int WSAETIMEDOUT = 10060; - const int WSAECONNREFUSED = 10061; - const int WSAEHOSTUNREACH = 10065; - const int WSATRY_AGAIN = 11002; - - private static IPEndPoint getAddressImpl(string host, int port, int protocol, bool server) + public enum SocketError + { + Interrupted = 10004, // A blocking Socket call was canceled. + //AccessDenied =10013, // An attempt was made to access a Socket in a way that is forbidden by its access permissions. + Fault = 10014, // An invalid pointer address was detected by the underlying socket provider. + InvalidArgument = 10022, // An invalid argument was supplied to a Socket member. + TooManyOpenSockets = 10024, // There are too many open sockets in the underlying socket provider. + WouldBlock = 10035, // An operation on a nonblocking socket cannot be completed immediately. + InProgress = 10036, // A blocking operation is in progress. + //AlreadyInProgress = 10037, // The nonblocking Socket already has an operation in progress. + //NotSocket = 10038, // A Socket operation was attempted on a non-socket. + //DestinationAddressRequired = 10039, // A required address was omitted from an operation on a Socket. + MessageSize = 10040, // The datagram is too long. + //ProtocolType = 10041, // The protocol type is incorrect for this Socket. + //ProtocolOption = 10042, // An unknown, invalid, or unsupported option or level was used with a Socket. + //ProtocolNotSupported = 10043, // The protocol is not implemented or has not been configured. + //SocketNotSupported = 10044, // The support for the specified socket type does not exist in this address family. + //OperationNotSupported = 10045, // The address family is not supported by the protocol family. + //ProtocolFamilyNotSupported = 10046, // The protocol family is not implemented or has not been configured. + //AddressFamilyNotSupported = 10047, // The address family specified is not supported. + //AddressAlreadyInUse = 10048, // Only one use of an address is normally permitted. + //AddressNotAvailable = 10049, // The selected IP address is not valid in this context. + NetworkDown = 10050, // The network is not available. + NetworkUnreachable = 10051, // No route to the remote host exists. + NetworkReset = 10052, // The application tried to set KeepAlive on a connection that has already timed out. + ConnectionAborted = 10053, // The connection was aborted by the .NET Framework or the underlying socket provider. + ConnectionReset = 10054, // The connection was reset by the remote peer. + NoBufferSpaceAvailable = 10055, // No free buffer space is available for a Socket operation. + //IsConnected = 10056, // The Socket is already connected. + NotConnected = 10057, // The application tried to send or receive data, and the Socket is not connected. + Shutdown = 10058, // A request to send or receive data was disallowed because the Socket has already been closed. + TimedOut = 10060, // The connection attempt timed out, or the connected host has failed to respond. + ConnectionRefused = 10061, // The remote host is actively refusing a connection. + //HostDown = 10064, // The operation failed because the remote host is down. + HostUnreachable = 10065, // There is no network route to the specified host. + //ProcessLimit = 10067, // Too many processes are using the underlying socket provider. + //SystemNotReady = 10091, // The network subsystem is unavailable. + //VersionNotSupported = 10092, // The version of the underlying socket provider is out of range. + //NotInitialized = 10093, // The underlying socket provider has not been initialized. + //Disconnecting = 10101, // A graceful shutdown is in progress. + //TypeNotFound = 10109, // The specified class was not found. + //HostNotFound = 11001, // No such host is known. The name is not an official host name or alias. + TryAgain = 11002, // The name of the host could not be resolved. Try again later. + //NoRecovery = 11003, // The error is unrecoverable or the requested database cannot be located. + //NoData = 11004, // The requested name or IP address was not found on the name server. + //IOPending = 997, // The application has initiated an overlapped operation that cannot be completed immediately. + OperationAborted =995 // The overlapped operation was aborted due to the closure of the Socket. + } +#endif + + private static EndPoint getAddressImpl(string host, int port, int protocol, bool server) { if(host.Length == 0) { +#if SILVERLIGHT + if(server) + { + if(protocol != EnableIPv4) + { + return new DnsEndPoint(IPAddress.IPv6Any.ToString(), port); + } + else + { + return new DnsEndPoint(IPAddress.Any.ToString(), port); + } + } + else + { + if(protocol != EnableIPv4) + { + return new DnsEndPoint(IPAddress.IPv6Loopback.ToString(), port); + } + else + { + return new DnsEndPoint(IPAddress.Loopback.ToString(), port); + } + } +#else if(server) { if(protocol != EnableIPv4) @@ -79,11 +146,15 @@ namespace IceInternal return new IPEndPoint(IPAddress.Loopback, port); } } +#endif } +#if SILVERLIGHT + return new DnsEndPoint(host, port); +#else int retry = 5; - repeatGetHostByName: + repeatGetHostByName: try { try @@ -99,11 +170,11 @@ namespace IceInternal { } -#if COMPACT +# if COMPACT foreach(IPAddress a in Dns.GetHostEntry(host).AddressList) -#else +# else foreach(IPAddress a in Dns.GetHostAddresses(host)) -#endif +# endif { if((a.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) || (a.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4)) @@ -112,9 +183,9 @@ namespace IceInternal } } } - catch(Win32Exception ex) + catch(SocketException ex) { - if(ex.NativeErrorCode == WSATRY_AGAIN && --retry >= 0) + if(socketErrorCode(ex) == SocketError.TryAgain && --retry >= 0) { goto repeatGetHostByName; } @@ -135,65 +206,66 @@ namespace IceInternal Ice.DNSException dns = new Ice.DNSException(); dns.host = host; throw dns; +#endif } - public static bool interrupted(Win32Exception ex) + public static bool interrupted(SocketException ex) { - return ex.NativeErrorCode == WSAEINTR; + return socketErrorCode(ex) == SocketError.Interrupted; } - public static bool acceptInterrupted(Win32Exception ex) + public static bool acceptInterrupted(SocketException ex) { if(interrupted(ex)) { return true; } - int error = ex.NativeErrorCode; - return error == WSAECONNABORTED || - error == WSAECONNRESET || - error == WSAETIMEDOUT; + SocketError error = socketErrorCode(ex); + return error == SocketError.ConnectionAborted || + error == SocketError.ConnectionReset || + error == SocketError.TimedOut; } - public static bool noBuffers(Win32Exception ex) + public static bool noBuffers(SocketException ex) { - int error = ex.NativeErrorCode; - return error == WSAENOBUFS || - error == WSAEFAULT; + SocketError error = socketErrorCode(ex); + return error == SocketError.NoBufferSpaceAvailable || + error == SocketError.Fault; } - public static bool wouldBlock(Win32Exception ex) + public static bool wouldBlock(SocketException ex) { - return ex.NativeErrorCode == WSAEWOULDBLOCK; + return socketErrorCode(ex) == SocketError.WouldBlock; } - public static bool connectFailed(Win32Exception ex) + public static bool connectFailed(SocketException ex) { - int error = ex.NativeErrorCode; - return error == WSAECONNREFUSED || - error == WSAETIMEDOUT || - error == WSAENETUNREACH || - error == WSAEHOSTUNREACH || - error == WSAECONNRESET || - error == WSAESHUTDOWN || - error == WSAECONNABORTED || - error == WSAENETDOWN; + SocketError error = socketErrorCode(ex); + return error == SocketError.ConnectionRefused || + error == SocketError.TimedOut || + error == SocketError.NetworkUnreachable || + error == SocketError.HostUnreachable || + error == SocketError.ConnectionReset || + error == SocketError.Shutdown || + error == SocketError.ConnectionAborted || + error == SocketError.NetworkDown; } - public static bool connectInProgress(Win32Exception ex) + public static bool connectInProgress(SocketException ex) { - int error = ex.NativeErrorCode; - return error == WSAEWOULDBLOCK || - error == WSAEINPROGRESS; + SocketError error = socketErrorCode(ex); + return error == SocketError.WouldBlock || + error == SocketError.InProgress; } - public static bool connectionLost(Win32Exception ex) + public static bool connectionLost(SocketException ex) { - int error = ex.NativeErrorCode; - return error == WSAECONNRESET || - error == WSAESHUTDOWN || - error == WSAECONNABORTED || - error == WSAENETDOWN || - error == WSAENETRESET; + SocketError error = socketErrorCode(ex); + return error == SocketError.ConnectionReset || + error == SocketError.Shutdown || + error == SocketError.ConnectionAborted || + error == SocketError.NetworkDown || + error == SocketError.NetworkReset; } public static bool connectionLost(System.IO.IOException ex) @@ -202,9 +274,9 @@ namespace IceInternal // In some cases the IOException has an inner exception that we can pass directly // to the other overloading of connectionLost(). // - if(ex.InnerException != null && ex.InnerException is Win32Exception) + if(ex.InnerException != null && ex.InnerException is SocketException) { - return connectionLost(ex.InnerException as Win32Exception); + return connectionLost(ex.InnerException as SocketException); } // @@ -229,28 +301,30 @@ namespace IceInternal return false; } - public static bool connectionRefused(Win32Exception ex) + public static bool connectionRefused(SocketException ex) { - return ex.NativeErrorCode == WSAECONNREFUSED; + return socketErrorCode(ex) == SocketError.ConnectionRefused; } - public static bool notConnected(Win32Exception ex) + public static bool notConnected(SocketException ex) { - // BUGFIX: WSAEINVAL because shutdown() under MacOS returns EINVAL if the server side is gone. - // BUGFIX: shutdown() under Vista might return WSAECONNRESET - return ex.NativeErrorCode == WSAENOTCONN || - ex.NativeErrorCode == WSAEINVAL || - ex.NativeErrorCode == WSAECONNRESET; + // BUGFIX: SocketError.InvalidArgument because shutdown() under MacOS returns EINVAL + // if the server side is gone. + // BUGFIX: shutdown() under Vista might return SocketError.ConnectionReset + SocketError error = socketErrorCode(ex); + return error == SocketError.NotConnected || + error == SocketError.InvalidArgument || + error == SocketError.ConnectionReset; } - public static bool recvTruncated(Win32Exception ex) + public static bool recvTruncated(SocketException ex) { - return ex.NativeErrorCode == WSAEMSGSIZE; + return socketErrorCode(ex) == SocketError.MessageSize; } - public static bool operationAborted(Win32Exception ex) + public static bool operationAborted(SocketException ex) { - return ex.NativeErrorCode == 995; + return socketErrorCode(ex) == SocketError.OperationAborted; } public static bool timeout(System.IO.IOException ex) @@ -266,7 +340,7 @@ namespace IceInternal { try { - return ex != null && ((Win32Exception)ex).NativeErrorCode == WSAEMFILE; + return ex != null && socketErrorCode((SocketException)ex) == SocketError.TooManyOpenSockets; } catch(InvalidCastException) { @@ -327,13 +401,20 @@ namespace IceInternal { throw new Ice.SocketException(ex); } + catch(ArgumentException ex) + { + throw new Ice.SocketException(ex); + } + if(!udp) { try { setTcpNoDelay(socket); +#if !SILVERLIGHT socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1); +#endif } catch(SocketException ex) { @@ -376,26 +457,31 @@ namespace IceInternal } } - public static void setBlock(Socket socket, bool block) + public static void setTcpNoDelay(Socket socket) { try { - socket.Blocking = block; +#if SILVERLIGHT + socket.NoDelay = true; +#else + socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1); +#endif } - catch(SocketException ex) + catch(System.Exception ex) { closeSocketNoThrow(socket); throw new Ice.SocketException(ex); } } - public static void setTcpNoDelay(Socket socket) +#if !SILVERLIGHT + public static void setBlock(Socket socket, bool block) { try { - socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1); + socket.Blocking = block; } - catch(System.Exception ex) + catch(SocketException ex) { closeSocketNoThrow(socket); throw new Ice.SocketException(ex); @@ -414,12 +500,17 @@ namespace IceInternal throw new Ice.SocketException(ex); } } +#endif public static void setSendBufferSize(Socket socket, int sz) { try { +#if SILVERLIGHT + socket.SendBufferSize = sz; +#else socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, sz); +#endif } catch(SocketException ex) { @@ -433,7 +524,11 @@ namespace IceInternal int sz; try { +#if SILVERLIGHT + sz = socket.SendBufferSize; +#else sz = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer); +#endif } catch(SocketException ex) { @@ -447,7 +542,11 @@ namespace IceInternal { try { +#if SILVERLIGHT + socket.ReceiveBufferSize = sz; +#else socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, sz); +#endif } catch(SocketException ex) { @@ -461,7 +560,11 @@ namespace IceInternal int sz = 0; try { +#if SILVERLIGHT + sz = socket.ReceiveBufferSize; +#else sz = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer); +#endif } catch(SocketException ex) { @@ -471,6 +574,7 @@ namespace IceInternal return sz; } +#if !SILVERLIGHT public static void setReuseAddress(Socket socket, bool reuse) { try @@ -483,7 +587,7 @@ namespace IceInternal throw new Ice.SocketException(ex); } } - + public static void setMcastGroup(Socket socket, IPAddress group, string iface) { try @@ -496,7 +600,7 @@ namespace IceInternal ifaceAddr = getInterfaceAddress(iface); if(ifaceAddr == IPAddress.Any) { - ifaceAddr = getAddress(iface, 0, EnableIPv4).Address; + ifaceAddr = ((IPEndPoint)getAddress(iface, 0, EnableIPv4)).Address; } } socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, @@ -531,11 +635,15 @@ namespace IceInternal throw new Ice.SocketException(ex); } } - +#endif + public static void setMcastTtl(Socket socket, int ttl, AddressFamily family) { try { +#if SILVERLIGHT + socket.Ttl = (short)ttl; +#else if(family == AddressFamily.InterNetwork) { socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, ttl); @@ -544,6 +652,7 @@ namespace IceInternal { socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.MulticastTimeToLive, ttl); } +#endif } catch(SocketException ex) { @@ -551,7 +660,8 @@ namespace IceInternal throw new Ice.SocketException(ex); } } - + +#if !SILVERLIGHT public static IPEndPoint doBind(Socket socket, EndPoint addr) { try @@ -585,7 +695,9 @@ namespace IceInternal throw new Ice.SocketException(ex); } } +#endif +#if !SILVERLIGHT public static bool doConnect(Socket fd, EndPoint addr) { repeatConnect: @@ -643,7 +755,6 @@ namespace IceInternal throw new Ice.ConnectionRefusedException(); } } - return true; } @@ -665,7 +776,14 @@ namespace IceInternal // IPAddress any = fd.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any; fd.Bind(new IPEndPoint(any, 0)); - return fd.BeginConnect(addr, callback, state); + return fd.BeginConnect(addr, + delegate(IAsyncResult result) + { + if(!result.CompletedSynchronously) + { + callback(result.AsyncState); + } + }, state); } catch(SocketException ex) { @@ -691,7 +809,6 @@ namespace IceInternal // NOTE: It's the caller's responsability to close the socket upon // failure to connect. The socket isn't closed by this method. // - try { fd.EndConnect(result); @@ -728,63 +845,43 @@ namespace IceInternal } } } +#endif - public static IPEndPoint getAddress(string host, int port, int protocol) + public static EndPoint getAddress(string host, int port, int protocol) { return getAddressImpl(host, port, protocol, false); } - - public static IPEndPoint getAddressForServer(string host, int port, int protocol) + + public static EndPoint getAddressForServer(string host, int port, int protocol) { return getAddressImpl(host, port, protocol, true); } - public static int compareAddress(IPEndPoint addr1, IPEndPoint addr2) + public static List<EndPoint> getAddresses(string host, int port, int protocol) { - if(addr1.Port < addr2.Port) - { - return -1; - } - else if(addr2.Port < addr1.Port) - { - return 1; - } - - byte[] larr = addr1.Address.GetAddressBytes(); - byte[] rarr = addr2.Address.GetAddressBytes(); - - if(larr.Length < rarr.Length) - { - return -1; - } - else if(rarr.Length < larr.Length) - { - return 1; - } + return getAddresses(host, port, protocol, true); + } - for(int i = 0; i < larr.Length; i++) + public static List<EndPoint> getAddresses(string host, int port, int protocol, bool blocking) + { + List<EndPoint> addresses = new List<EndPoint>(); +#if SILVERLIGHT + if(host.Length == 0) { - if(larr[i] < rarr[i]) + if(protocol != EnableIPv4) { - return -1; + addresses.Add(new DnsEndPoint(IPAddress.IPv6Loopback.ToString(), port)); } - else if(rarr[i] < larr[i]) + else { - return 1; + addresses.Add(new DnsEndPoint(IPAddress.Loopback.ToString(), port)); } } - - return 0; - } - - public static List<IPEndPoint> getAddresses(string host, int port, int protocol) - { - return getAddresses(host, port, protocol, true); - } - - public static List<IPEndPoint> getAddresses(string host, int port, int protocol, bool blocking) - { - List<IPEndPoint> addresses = new List<IPEndPoint>(); + else + { + addresses.Add(new DnsEndPoint(host, port)); + } +#else if(host.Length == 0) { if(protocol != EnableIPv4) @@ -796,7 +893,6 @@ namespace IceInternal { addresses.Add(new IPEndPoint(IPAddress.Loopback, port)); } - return addresses; } else { @@ -826,11 +922,11 @@ namespace IceInternal } } -#if COMPACT +# if COMPACT foreach(IPAddress a in Dns.GetHostEntry(host).AddressList) -#else +# else foreach(IPAddress a in Dns.GetHostAddresses(host)) -#endif +# endif { if((a.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) || (a.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4)) @@ -839,9 +935,9 @@ namespace IceInternal } } } - catch(Win32Exception ex) + catch(SocketException ex) { - if(ex.NativeErrorCode == WSATRY_AGAIN && --retry >= 0) + if(socketErrorCode(ex) == SocketError.TryAgain && --retry >= 0) { goto repeatGetHostByName; } @@ -866,21 +962,23 @@ namespace IceInternal throw e; } } - +#endif return addresses; } public static IPAddress[] getLocalAddresses(int protocol) { - ArrayList addresses; - +#if SILVERLIGHT + return new List<IPAddress>().ToArray(); +#else + List<IPAddress> addresses; int retry = 5; - repeatGetHostByName: + repeatGetHostByName: try { - addresses = new ArrayList(); -#if !COMPACT + addresses = new List<IPAddress>(); +# if !COMPACT if(AssemblyUtil.runtime_ != AssemblyUtil.Runtime.Mono) { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); @@ -902,13 +1000,13 @@ namespace IceInternal } } else -#endif +# endif { -#if COMPACT +# if COMPACT foreach(IPAddress a in Dns.GetHostEntry(Dns.GetHostName()).AddressList) -#else +# else foreach(IPAddress a in Dns.GetHostAddresses(Dns.GetHostName())) -#endif +# endif { if((a.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) || (a.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4)) @@ -921,9 +1019,9 @@ namespace IceInternal } } } - catch(Win32Exception ex) + catch(SocketException ex) { - if(ex.NativeErrorCode == WSATRY_AGAIN && --retry >= 0) + if(socketErrorCode(ex) == SocketError.TryAgain && --retry >= 0) { goto repeatGetHostByName; } @@ -938,7 +1036,8 @@ namespace IceInternal throw e; } - return (IPAddress[])addresses.ToArray(typeof(IPAddress)); + return addresses.ToArray(); +#endif } public static void @@ -1053,7 +1152,11 @@ namespace IceInternal public static string fdLocalAddressToString(Socket socket) { System.Text.StringBuilder s = new System.Text.StringBuilder(); - IPEndPoint localEndpoint = getLocalAddress(socket); +#if SILVERLIGHT + // Silverlight Socket doesn't expose the local endpoint + s.Append("local address = <not available>"); +#else + IPEndPoint localEndpoint = (IPEndPoint)getLocalAddress(socket); if(localEndpoint == null) { // This might occur if connect was not called yet, see also comments in doBeginConnectAsync @@ -1064,34 +1167,52 @@ namespace IceInternal s.Append("local address = " + localEndpoint.Address); s.Append(":" + localEndpoint.Port); } +#endif return s.ToString(); } public static string - addressesToString(IPEndPoint localEndpoint, IPEndPoint remoteEndpoint) + addressesToString(EndPoint localEndpoint, EndPoint remoteEndpoint) { System.Text.StringBuilder s = new System.Text.StringBuilder(); - if(localEndpoint == null) +#if SILVERLIGHT + DnsEndPoint remoteDnsEndPoint = (DnsEndPoint)remoteEndpoint; + // Silverlight Socket doesn't expose the local endpoint + s.Append("local address = <not available>"); + + if(remoteDnsEndPoint == null) + { + s.Append("\nremote address = <not connected>"); + } + else + { + s.Append("\nremote address = " + remoteDnsEndPoint.Host); + s.Append(":" + remoteDnsEndPoint.Port); + } +#else + IPEndPoint localPEndpoint = (IPEndPoint)localEndpoint; + IPEndPoint remoteIPEndPoint = (IPEndPoint)remoteEndpoint; + if(localPEndpoint == null) { // This might occur if connect was not called yet, see also comments in doBeginConnectAsync s.Append("local address = <not bound>"); } else { - s.Append("local address = " + localEndpoint.Address); - s.Append(":" + localEndpoint.Port); + s.Append("local address = " + localPEndpoint.Address); + s.Append(":" + localPEndpoint.Port); } - if(remoteEndpoint == null) + if(remoteIPEndPoint == null) { s.Append("\nremote address = <not connected>"); } else { - s.Append("\nremote address = " + remoteEndpoint.Address); - s.Append(":" + remoteEndpoint.Port); + s.Append("\nremote address = " + remoteIPEndPoint.Address); + s.Append(":" + remoteIPEndPoint.Port); } - +#endif return s.ToString(); } @@ -1101,28 +1222,31 @@ namespace IceInternal return addr.ToString(); } - public static IPEndPoint + public static EndPoint getLocalAddress(Socket socket) - { - IPEndPoint localEndpoint; + { + EndPoint localEndpoint = null; + // Silverlight socket doesn't exposes a local endpoint +#if !SILVERLIGHT try { - localEndpoint = (IPEndPoint)socket.LocalEndPoint; + localEndpoint = (EndPoint)socket.LocalEndPoint; } catch(SocketException ex) { throw new Ice.SocketException(ex); } +#endif return localEndpoint; } - public static IPEndPoint + public static EndPoint getRemoteAddress(Socket socket) { - IPEndPoint remoteEndpoint = null; + EndPoint remoteEndpoint = null; try { - remoteEndpoint = (IPEndPoint)socket.RemoteEndPoint; + remoteEndpoint = (EndPoint)socket.RemoteEndPoint; } catch(SocketException) { @@ -1133,7 +1257,7 @@ namespace IceInternal private static int getInterfaceIndex(string name) { -#if !COMPACT +#if !COMPACT && !SILVERLIGHT NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach(NetworkInterface ni in nics) { @@ -1154,7 +1278,7 @@ namespace IceInternal private static IPAddress getInterfaceAddress(string name) { -#if !COMPACT +#if !COMPACT && !SILVERLIGHT NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach(NetworkInterface ni in nics) { @@ -1174,5 +1298,39 @@ namespace IceInternal #endif return IPAddress.Any; } + + public static string + endpointAddressToString(EndPoint endpoint) + { + System.Text.StringBuilder s = new System.Text.StringBuilder(); + if(endpoint != null) + { +#if SILVERLIGHT + DnsEndPoint dnsEndpoint = (DnsEndPoint) endpoint; + s.Append(dnsEndpoint.Host); +#else + IPEndPoint ipEndpoint = (IPEndPoint) endpoint; + s.Append(ipEndpoint.Address.ToString()); +#endif + } + return s.ToString(); + } + + public static int + endpointPort(EndPoint endpoint) + { + int port = -1; + if(endpoint != null) + { +#if SILVERLIGHT + DnsEndPoint dnsEndpoint = (DnsEndPoint) endpoint; + port = dnsEndpoint.Port; +#else + IPEndPoint ipEndpoint = (IPEndPoint) endpoint; + port = ipEndpoint.Port; +#endif + } + return port; + } } } |