33using System . Net ;
44using System . Net . Sockets ;
55
6- namespace phat . Services
7- {
8- internal delegate TcpClient onStartHandler ( TcpListener listener ) ;
9-
10- internal delegate void onConnectHandler ( TcpClient client ) ;
6+ namespace phat . Services ;
117
8+ internal delegate TcpClient onStartHandler ( TcpListener listener ) ;
129
13- internal static class ConnectionService
14- {
15- internal static TcpClient Create ( onStartHandler onStart ) => Create ( GetLocalIPAddress ( ) , 0 , onStart ) ;
10+ internal delegate void onConnectHandler ( TcpClient client ) ;
1611
17- internal static TcpClient Create ( IPAddress localIP , onStartHandler onStart ) => Create ( localIP , 0 , onStart ) ;
1812
19- internal static TcpClient Create ( int localPort , onStartHandler onStart ) =>
20- Create ( GetLocalIPAddress ( ) , localPort , onStart ) ;
21-
22- internal static TcpClient Create ( IPAddress localIP , int localPort , onStartHandler onStart )
23- {
24- IPEndPoint localEP = new ( localIP , localPort ) ;
25- TcpListener listener = new ( localEP ) ;
26- listener . Start ( ) ;
27- return onStart . Invoke ( listener ) ;
28- }
13+ internal static class ConnectionService
14+ {
15+ internal static TcpClient Create ( onStartHandler onStart ) => Create ( GetLocalIPAddress ( ) , 0 , onStart ) ;
2916
30- private static ( TcpClient , IPEndPoint ) GetClientWithEndpoint ( IPAddress remoteIP , int remotePort ) => (
31- new TcpClient ( AddressFamily . InterNetwork ) , new IPEndPoint ( remoteIP , remotePort ) ) ;
17+ internal static TcpClient Create ( IPAddress localIP , onStartHandler onStart ) => Create ( localIP , 0 , onStart ) ;
3218
33- private static void Connect ( TcpClient client , IPEndPoint remoteEP , onConnectHandler onConnect )
34- {
35- client . Connect ( remoteEP ) ;
36- onConnect . Invoke ( client ) ;
37- }
19+ internal static TcpClient Create ( int localPort , onStartHandler onStart ) =>
20+ Create ( GetLocalIPAddress ( ) , localPort , onStart ) ;
3821
39- internal static TcpClient Connect ( IPAddress remoteIP ,
40- int remotePort ,
41- onConnectHandler onConnect ,
42- [ Range ( 1 , 8 ) ] int retry ,
43- [ Range ( 1_000 , 10_000 ) ] int retryDuration )
44- {
45- int attempt = 0 ;
46- int socketErrorCode = 0 ;
47- ( TcpClient client , IPEndPoint remoteEP ) = GetClientWithEndpoint ( remoteIP , remotePort ) ;
48- while ( attempt <= retry )
49- {
50- try
51- {
52- Console . WriteLine ( "Attempting to connect: {0}" , attempt ) ;
53- if ( attempt != 0 ) Thread . Sleep ( retryDuration ) ;
54- Connect ( client , remoteEP , onConnect ) ;
55- return client ;
56- }
57- catch ( SocketException se )
58- {
59- attempt ++ ;
60- socketErrorCode = se . ErrorCode ;
61- }
62- }
22+ internal static TcpClient Create ( IPAddress localIP , int localPort , onStartHandler onStart )
23+ {
24+ IPEndPoint localEP = new ( localIP , localPort ) ;
25+ TcpListener listener = new ( localEP ) ;
26+ listener . Start ( ) ;
27+ return onStart . Invoke ( listener ) ;
28+ }
6329
64- client . Dispose ( ) ;
65- throw new ConnectException ( socketErrorCode ) ;
66- }
30+ private static ( TcpClient , IPEndPoint ) GetClientWithEndpoint ( IPAddress remoteIP , int remotePort ) => (
31+ new TcpClient ( AddressFamily . InterNetwork ) , new IPEndPoint ( remoteIP , remotePort ) ) ;
6732
33+ private static void Connect ( TcpClient client , IPEndPoint remoteEP , onConnectHandler onConnect )
34+ {
35+ client . Connect ( remoteEP ) ;
36+ onConnect . Invoke ( client ) ;
37+ }
6838
69- internal static TcpClient Join ( IPAddress remoteIP , int remotePort , onConnectHandler onConnect )
39+ internal static TcpClient Connect ( IPAddress remoteIP ,
40+ int remotePort ,
41+ onConnectHandler onConnect ,
42+ [ Range ( 1 , 8 ) ] int retry ,
43+ [ Range ( 1_000 , 10_000 ) ] int retryDuration )
44+ {
45+ int attempt = 0 ;
46+ int socketErrorCode = 0 ;
47+ ( TcpClient client , IPEndPoint remoteEP ) = GetClientWithEndpoint ( remoteIP , remotePort ) ;
48+ while ( attempt <= retry )
7049 {
71- ( TcpClient client , IPEndPoint remoteEP ) = GetClientWithEndpoint ( remoteIP , remotePort ) ;
7250 try
7351 {
52+ Console . WriteLine ( "Attempting to connect: {0}" , attempt ) ;
53+ if ( attempt != 0 ) Thread . Sleep ( retryDuration ) ;
7454 Connect ( client , remoteEP , onConnect ) ;
7555 return client ;
7656 }
7757 catch ( SocketException se )
7858 {
79- client . Dispose ( ) ;
80- throw new ConnectException ( se . ErrorCode ) ;
59+ attempt ++ ;
60+ socketErrorCode = se . ErrorCode ;
8161 }
8262 }
8363
84- public static IPAddress GetLocalIPAddress ( )
85- {
86- var host = Dns . GetHostEntry ( Dns . GetHostName ( ) ) ;
87- var ip = host . AddressList . FirstOrDefault ( a =>
88- a . AddressFamily == AddressFamily . InterNetwork &&
89- ! IPAddress . IsLoopback ( a ) ) ;
64+ client . Dispose ( ) ;
65+ throw new ConnectException ( socketErrorCode ) ;
66+ }
9067
91- return ip ?? throw new Exception ( "No network adapters with a valid IPv4 address found." ) ;
68+
69+ internal static TcpClient Join ( IPAddress remoteIP , int remotePort , onConnectHandler onConnect )
70+ {
71+ ( TcpClient client , IPEndPoint remoteEP ) = GetClientWithEndpoint ( remoteIP , remotePort ) ;
72+ try
73+ {
74+ Connect ( client , remoteEP , onConnect ) ;
75+ return client ;
76+ }
77+ catch ( SocketException se )
78+ {
79+ client . Dispose ( ) ;
80+ throw new ConnectException ( se . ErrorCode ) ;
9281 }
82+ }
83+
84+ public static IPAddress GetLocalIPAddress ( )
85+ {
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." ) ;
92+ }
9393
9494
95- public static IPEndPoint ? GetRemoteClientEndpoint ( TcpClient client ) =>
96- client . Client . RemoteEndPoint as IPEndPoint ;
95+ public static IPEndPoint ? GetRemoteClientEndpoint ( TcpClient client ) =>
96+ client . Client . RemoteEndPoint as IPEndPoint ;
9797
98- public static IPEndPoint ? GetLocalClientEndpoint ( TcpClient client ) => client . Client . LocalEndPoint as IPEndPoint ;
98+ public static IPEndPoint ? GetLocalClientEndpoint ( TcpClient client ) => client . Client . LocalEndPoint as IPEndPoint ;
9999
100- public static ( string , int ) FlattenIPEndpoint ( IPEndPoint ipEndPoint ) =>
101- ( ipEndPoint . Address . ToString ( ) , ipEndPoint . Port ) ;
102- }
100+ public static ( string , int ) FlattenIPEndpoint ( IPEndPoint ipEndPoint ) =>
101+ ( ipEndPoint . Address . ToString ( ) , ipEndPoint . Port ) ;
103102}
0 commit comments