This commit is contained in:
2024-04-09 18:09:08 -04:00
parent b28abac66c
commit bbf72db61e

View File

@@ -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
{
/// <summary>
/// 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
/// </summary>
/// <param name="socket">The socket</param>
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;
}
}
}
}