using System.Net.Sockets; using System.Net; using BCG; using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Threading; using System.Reflection.Metadata.Ecma335; namespace bts { // Unlike AsyncTCPConnectionAcceptor_APM this class is static since the requests to connect are stand alone requests // whereas AsyncTCPConnectionAcceptor_APM is always listening for incoming connections which lends itself to more // statefullness and hence non static public static class AsyncTCPConnector_AsyncWrapper { public static void connect_to(RemoteConnectionInfo server_address, ITCPConnectionHandler handler) { bts.Log.Gen.info($"AsyncTCPConnector_AsyncWrapper.connect_to server: {server_address}"); //Task.Run(() => _connect_to(server_address, handler)); Action connect_once = () => { _connect_to(server_address, handler); }; ThreadPool.QueueUserWorkItem(_ => _keep_trying_blocking_function(connect_once, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5))); } // this is the actual fuction thta calls TCPCLient sync methof and establishes a connection private static void _connect_to(RemoteConnectionInfo server_rci, ITCPConnectionHandler handler) { // TODO, random sleep bts.Log.Gen.info($"AsyncTCPConnector_AsyncWrapper._connect_to server: {server_rci}"); TcpClient tcp_client = new TcpClient(server_rci.ip, server_rci.port); bts.Log.Gen.info($"AsyncTCPConnector_AsyncWrapper._connect_to server new TcpClient returned, connected: {tcp_client.Connected}"); ITCPConnection conn = _create_connection(tcp_client); var conn_rci = conn.get_peer_address(); bts.Log.Gen.info($"AsyncTCPConnector_AsyncWrapper._connect_to new connection, server_rci: {server_rci} connection.peer_address: {conn_rci}"); Log.assert(server_rci.Equals(conn_rci), $"{server_rci}!={conn_rci}"); bts.Log.Gen.info($"AsyncTCPConnector_AsyncWrapper._connect_to new connection, self: {conn.get_self_address()} peer: {conn.get_peer_address()}, calling onConnection now"); handler.onConnection(conn); } private static ITCPConnection _create_connection(TcpClient tcp_client) { ITCPConnection conn = new AsyncTCPConnection_AsyncWrapper(tcp_client); // ITCPConnection conn = new AsyncTCPConnection_APM(tcp_client.GetStream().Socket, TimeSpan.FromSeconds(1), ITCPConnection.ConnectionSide.Client); return conn; } // this is a ganeric function that calls a sync action function, and if unsuccessful timesout after "timeout" // wais for wait_before_retrying before trying again public static void _keep_trying_blocking_function(Action action, TimeSpan timeout, TimeSpan wait_before_retrying) { Func try_once = () => { var cts = new CancellationTokenSource(); var token = cts.Token; var task = Task.Run(action, token); if (Task.WaitAny(task, Task.Delay(timeout, token)) == 0) { // Task completed within timeout task.GetAwaiter().GetResult(); // Ensure any exceptions/cancellation are rethrown return true; } else { // Timeout cts.Cancel(); return false; } }; bool connected = false; while (!connected) { connected = try_once(); System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); } } } }