From 893ae3bc2e31ff0976a48429b5dcb53ca6befbd6 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 21 Jul 2024 04:33:14 +0000 Subject: [PATCH] fix: Catch exception when creating TcpClient --- .../AsyncTCPConnector_AsyncWrapper.cs | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_AsyncWrapper.cs b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_AsyncWrapper.cs index 31a641de7..af474dbfa 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_AsyncWrapper.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_AsyncWrapper.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using System.Threading; using System.Reflection.Metadata.Ecma335; +using System.Diagnostics.CodeAnalysis; namespace bts { @@ -17,15 +18,17 @@ namespace bts { 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))); + ThreadPool.QueueUserWorkItem(_ => _keep_trying_blocking_function( + () => _connect_to(server_address, handler), 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) { + private static bool _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); + if (!CreateTcpClient(out var tcp_client)) { + return false; + } 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(); @@ -33,6 +36,18 @@ namespace bts { 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); + return true; + + bool CreateTcpClient([NotNullWhen(true)]out TcpClient? tcpClient) { + try { + tcpClient = new(server_rci.ip, server_rci.port); + return true; + } catch (SocketException ex) { + Log.Gen.warn($"Failed to connect. server_rci: {server_rci} Exception:\n{ex}"); + } + tcpClient = null; + return false; + } } private static ITCPConnection _create_connection(TcpClient tcp_client) { @@ -43,10 +58,10 @@ namespace bts { // 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) { + public static void _keep_trying_blocking_function(Func action, TimeSpan timeout, TimeSpan wait_before_retrying) { Func try_once = () => { var task = Task.Run(action); - return Task.WhenAny(task, Task.Delay(timeout)).GetAwaiter().GetResult() == task && task.IsCompletedSuccessfully; + return Task.WhenAny(task, Task.Delay(timeout)).GetAwaiter().GetResult() == task && task.IsCompletedSuccessfully && task.Result; }; bool connected = false; while (!connected) {