private static void ReconnectAllSockets() { foreach (KeyValuePair <string, TcpClientEntry> kvPair in DllEntry.tcpClients) { string hash = kvPair.Key; TcpClientEntry entry = kvPair.Value; if (!DllEntry.IsConnected(entry.TcpClient)) { try { entry.TcpClient.Close(); } catch (ObjectDisposedException) { } try { entry.TcpClient = DllEntry.Connect(entry.Uri); Log("Reconnected"); } catch (SocketException e) { Log($"Reconnect - Socket exception {e.Message}"); } } } }
public static string Connect(string address) { string hash = ""; using (SHA1Managed sha1 = new SHA1Managed()) { hash = string.Join("", sha1.ComputeHash(Encoding.Default.GetBytes(address)).Select(b => b.ToString("x2"))).Substring(0, 7); } Log(hash); if (DllEntry.tcpClients.ContainsKey(hash)) { return(hash); } if (!Uri.TryCreate(address, UriKind.Absolute, out Uri uri)) { Log("Malformed address: " + address); return("error"); } Log("Connecting to: " + uri.Host + ":" + uri.Port); TcpClient tcpClient = DllEntry.Connect(uri); DllEntry.tcpClients.Add(hash, new TcpClientEntry() { Uri = uri, TcpClient = tcpClient }); return(hash); }