I'm trying to find strange behaviour in a socket library that has been used in production for a while.
Actually the Receive and send are processed with BeginReceive/EndReceive and BeginSend/EndSend calls , so everything is processed in an asynchronous fashion.
Moreover, I've a MonitorThread that is polling each second for the state of the socket:
void ControlThreadCallback()
{
if (!IsConnected && m_ConnectionIP != null && !m_ConnectionIP.Equals(String.Empty) && m_ConnectionPort > 0)
{
//No connected, perform reconnection, recreate serversocket or dispose client socket if needed
}
}
The IsConnected property is defined in the same class:
public bool IsConnected
{
get
{
try
{
return m_Socket != null && m_Socket.Connected &&
!(m_Socket.Poll(1, SelectMode.SelectRead) && m_Socket.Available == 0);
}
catch (Exception ex)
{
//Log error
//Object Disposed exception is being catched here sometimes
return false;
}
}
}
Sometimes, objectdisposedexception is being catched, thus returning false to IsConnected property and ControlThread thinks that socket is not connected. However, I've activity in the socket, so is not disconnected at all.
This happens only when high volume of data is being sent and received on the socket (that is, more than 20 messages each second or so).
In which strange cases the Socket.Poll is giving objectDisposedException? and, could be some race condition between socket.Poll && socket.Available?
If you need more code I can paste some more snippets.