Add
This commit is contained in:
37
TorWebClient/TorClient/Extensions/Extensions.cs
Normal file
37
TorWebClient/TorClient/Extensions/Extensions.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user