@@ -43,7 +43,7 @@ You should have received a copy of the GNU General Public License
4343
4444 """ ;
4545
46- static void Main ( string [ ] args )
46+ static async Task Main ( string [ ] args )
4747 {
4848
4949 /*
@@ -65,8 +65,7 @@ static void Main(string[] args)
6565 Console . WriteLine ( LICENSE ) ;
6666
6767 //Setup connected clients
68- List < ConnectedClient > clients = [ ] ;
69- List < ConnectedClient > reapedClients = [ ] ;
68+ Dictionary < uint , ConnectedClient > clients = [ ] ;
7069
7170 var ver = typeof ( ListenServer ) . Assembly . GetName ( ) . Version ;
7271
@@ -123,15 +122,17 @@ static void Main(string[] args)
123122 ( "Status" , "Waiting for connections" )
124123 ] ) ;
125124
126- static ConnectedClient ? TryAcceptClient ( TcpClient client )
125+ void TryAcceptClientCallback ( IAsyncResult ar )
127126 {
127+ var client = listener . EndAcceptTcpClient ( ar ) ;
128+
128129 if ( client . Client . RemoteEndPoint is not EndPoint remote || remote . AddressFamily != AddressFamily . InterNetwork )
129130 {
130131 Logger . LogWarning ( "Refusing a client with bad or unsupported IP information" ) ;
131132 var response = Utilities . GenerateResponse ( 421u , "Misdirected Request" , "<h1>Misdirected Request</h1><p>Unable to service your device's IP configuration</p>" , "text/html" , true ) ;
132133 client . GetStream ( ) . Write ( response , 0 , response . Length ) ;
133134 client . Close ( ) ;
134- return null ;
135+ return ;
135136 }
136137
137138 if ( ! Utilities . SecurityPolicy . AllowClient ( remote ) )
@@ -140,12 +141,15 @@ static void Main(string[] args)
140141 var response = Utilities . GenerateResponse ( 403u , "Client Forbidden" , "<h1>Client Forbidden</h1><p>Access denied, your client details have been logged</p>" , "text/html" ) ;
141142 client . GetStream ( ) . Write ( response , 0 , response . Length ) ;
142143 client . Close ( ) ;
143- return null ;
144+ return ;
144145 }
145146
146- return new ( client , remote ) ;
147+ var newClient = new ConnectedClient ( client , remote ) ;
148+ clients . TryAdd ( newClient . ClientID , newClient ) ;
147149 }
148150
151+ listener . BeginAcceptTcpClient ( TryAcceptClientCallback , listener ) ;
152+
149153 #region Main Loop
150154
151155 while ( true )
@@ -156,36 +160,30 @@ static void Main(string[] args)
156160 break ;
157161 }
158162
159- //Check for pending requests
160- if ( listener . Pending ( ) )
161- {
162- //Logger.LogTrace("Client connected");
163- if ( TryAcceptClient ( listener . AcceptTcpClient ( ) ) is ConnectedClient accepted )
164- {
165- clients . Add ( accepted ) ;
166- }
167- }
163+ var ClientTasks = new List < Task > ( ) ;
168164
169165 //Handle all requests from all clients
170- foreach ( var client in clients )
171- {
172- if ( ! client . CheckIn ( ) )
173- {
174- reapedClients . Add ( client ) ;
175- }
176- }
177-
178- foreach ( var client in reapedClients )
166+ foreach ( var client in clients . Values )
179167 {
180- Console . WriteLine ( $ "Reaping a client: { client . ClientID } ") ;
181- clients . Remove ( client ) ;
168+ ClientTasks . Add ( Task . Run ( client . CheckIn ) . ContinueWith ( ClientTaskEnded , client ) ) ;
182169 }
183170
184- reapedClients . Clear ( ) ;
171+ await Task . Delay ( 10 ) ;
185172 }
186-
187173 #endregion
188174
175+ void ClientTaskEnded ( Task < bool > task , object ? state )
176+ {
177+ if ( task . Result || state is not ConnectedClient client )
178+ return ;
179+
180+ Logger . LogInfo ( "Reaping a client" , [
181+ ( "ClientID" , client . ClientID )
182+ ] ) ;
183+
184+ clients . Remove ( client . ClientID ) ;
185+ }
186+
189187 }
190188 catch ( SocketException ex )
191189 {
@@ -238,5 +236,4 @@ static void Main(string[] args)
238236 EndProgram :
239237 Logger . LogInfo ( "Goodbye!" ) ;
240238 }
241-
242239}
0 commit comments