55
66namespace phat . Services
77{
8-
98 internal delegate TcpClient onStartHandler ( TcpListener listener ) ;
9+
1010 internal delegate void onConnectHandler ( TcpClient client ) ;
1111
1212
@@ -16,7 +16,8 @@ internal static class ConnectionService
1616
1717 internal static TcpClient Create ( IPAddress localIP , onStartHandler onStart ) => Create ( localIP , 0 , onStart ) ;
1818
19- internal static TcpClient Create ( int localPort , onStartHandler onStart ) => Create ( GetLocalIPAddress ( ) , localPort , onStart ) ;
19+ internal static TcpClient Create ( int localPort , onStartHandler onStart ) =>
20+ Create ( GetLocalIPAddress ( ) , localPort , onStart ) ;
2021
2122 internal static TcpClient Create ( IPAddress localIP , int localPort , onStartHandler onStart )
2223 {
@@ -26,7 +27,8 @@ internal static TcpClient Create(IPAddress localIP, int localPort, onStartHandle
2627 return onStart . Invoke ( listener ) ;
2728 }
2829
29- private static ( TcpClient , IPEndPoint ) GetClientWithEndpoint ( IPAddress remoteIP , int remotePort ) => ( new TcpClient ( AddressFamily . InterNetwork ) , new IPEndPoint ( remoteIP , remotePort ) ) ;
30+ private static ( TcpClient , IPEndPoint ) GetClientWithEndpoint ( IPAddress remoteIP , int remotePort ) => (
31+ new TcpClient ( AddressFamily . InterNetwork ) , new IPEndPoint ( remoteIP , remotePort ) ) ;
3032
3133 private static void Connect ( TcpClient client , IPEndPoint remoteEP , onConnectHandler onConnect )
3234 {
@@ -58,6 +60,7 @@ internal static TcpClient Connect(IPAddress remoteIP,
5860 socketErrorCode = se . ErrorCode ;
5961 }
6062 }
63+
6164 client . Dispose ( ) ;
6265 throw new ConnectException ( socketErrorCode ) ;
6366 }
@@ -80,19 +83,21 @@ internal static TcpClient Join(IPAddress remoteIP, int remotePort, onConnectHand
8083
8184 public static IPAddress GetLocalIPAddress ( )
8285 {
83- return Dns
84- . GetHostEntry ( Dns . GetHostName ( ) )
85- . AddressList
86- . First ( ip => ip . AddressFamily == AddressFamily . InterNetwork ) ;
87- throw new Exception ( "No network adapters with an IPv4 address in the system!" ) ;
86+ var host = Dns . GetHostEntry ( Dns . GetHostName ( ) ) ;
87+ var ip = host . AddressList . FirstOrDefault ( a =>
88+ a . AddressFamily == AddressFamily . InterNetwork &&
89+ ! IPAddress . IsLoopback ( a ) ) ;
90+
91+ return ip ?? throw new Exception ( "No network adapters with a valid IPv4 address found." ) ;
8892 }
8993
90- public static IPEndPoint ? GetRemoteClientEndpoint ( TcpClient client ) => client . Client . RemoteEndPoint as IPEndPoint ;
94+
95+ public static IPEndPoint ? GetRemoteClientEndpoint ( TcpClient client ) =>
96+ client . Client . RemoteEndPoint as IPEndPoint ;
9197
9298 public static IPEndPoint ? GetLocalClientEndpoint ( TcpClient client ) => client . Client . LocalEndPoint as IPEndPoint ;
9399
94- public static ( string , int ) FlattenIPEndpoint ( IPEndPoint ipEndPoint ) => ( ipEndPoint . Address . ToString ( ) , ipEndPoint . Port ) ;
100+ public static ( string , int ) FlattenIPEndpoint ( IPEndPoint ipEndPoint ) =>
101+ ( ipEndPoint . Address . ToString ( ) , ipEndPoint . Port ) ;
95102 }
96- }
97-
98-
103+ }
0 commit comments