From bbf72db61edd0139c81eaad1587649addf67720a Mon Sep 17 00:00:00 2001 From: Sean Date: Tue, 9 Apr 2024 18:09:08 -0400 Subject: [PATCH] Add --- .../TorClient/Extensions/Extensions.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 TorWebClient/TorClient/Extensions/Extensions.cs diff --git a/TorWebClient/TorClient/Extensions/Extensions.cs b/TorWebClient/TorClient/Extensions/Extensions.cs new file mode 100644 index 0000000..e683b13 --- /dev/null +++ b/TorWebClient/TorClient/Extensions/Extensions.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace Tor.Extensions +{ + + public static class Extensions + { + /// + /// Determine if socket is connected + /// "Poll" returns true if + /// connection is closed, reset, terminated or pending (meaning no active connection) + /// connection is active and there is data available for reading + /// "Available" returns number of bytes available for reading + /// if both are true: + /// There is no data available to read so connection is not active + /// + /// The socket + public static bool IsConnected(this Socket socket) + { + try + { + bool part1 = socket.Poll(1000, SelectMode.SelectRead); + bool part2 = (0 == socket.Available); + return part1 && part2 ? false : true; + } + catch(Exception) + { + return false; + } + } + } +}