What do you think of the following IDisposable
pattern implementation?
public class Connection : IDisposable
{
private Socket _socket;
public bool IsConnected()
{
if (_socket.Poll(1, SelectMode.SelectRead) && _socket.Available == 0)
return false;
return true;
}
public void Disconnect()
{
if (m_socket != null && IsConnected())
{
try
{
_socket.Shutdown(SocketShutdown.Both);
_socket.Disconnect(false);
}
catch (SocketException se)
{
System.Console.WriteLine(se.Message);
}
}
}
~Connection()
{
Dispose(false);
}
private void Dispose(bool disposing)
{
if (!IsConnected())
{
if (disposing)
{
Disconnect();
}
else
{
AppDomain currentDomain = AppDomain.CurrentDomain;
if (currentDomain.IsFinalizingForUnload() && !Environment.HasShutdownStarted)
{
System.Console.WriteLine("Client failed to call Destroy");
}
}
}
}
}
I was given this error using the code above:
{"An operation was attempted on something that is not a socket"} System.Net.Sockets.Socket.Poll(Int32 microSeconds, SelectMode mode)