1
Vote

UdpServer's EndReceive causes exception

description

Hello,
 
This method causes an ObjectDisposedException to be thrown and then it catches it. That seems OK, but it's a scary-sounding exception that is displayed when the debugger is attached (and break-when-thrown is enabled for exceptions).
 
Since this method can be called after mAcceptingConnections is false, simply moving the call to udpClient.EndReceive to be in the if-block will stop the ObjectDisposeException from being thrown. I didn't remove the try-catch completely, just as a precaution, but in my use-case, this change seems to work.
 
Please see my proposed change below.
 
Thank you for the useful and well-written library.
 
--Ron
 
private void EndReceive(IAsyncResult asyncResult)
{
try
{
    if (mAcceptingConnections)
    {
        UdpState udpState = (UdpState)asyncResult.AsyncState;
        UdpClient udpClient = udpState.Client;
        IPEndPoint ipEndPoint = udpState.IPEndPoint;
 
        byte[] data = udpClient.EndReceive(asyncResult, ref ipEndPoint);
        if (data != null && data.Length > 0)
        {
            OnDataReceived(new UdpDataReceivedEventArgs(ipEndPoint, data));
        }
 
        udpClient.BeginReceive(mAsynCallback, udpState);
    }
}
catch (ObjectDisposedException)
{
    // Suppress error
}
}

comments