/// <summary> /// Handles an error in socket communication. /// </summary> /// <param name="socket">The socket that raised the exception.</param> /// <param name="ex">The exception that the socket raised.</param> private void HandleCommunicationError(Socket socket, Exception ex) { if (CloseSocket(ref socket)) { return; } // No longer connected _isConnected = false; // Clear receive queue for this client _receiveBufferQueue.Clear(); // Raise the error event var error = Error; if (error != null) { var socketErrorArgs = _socketErrorArgsPool.Pop(); socketErrorArgs.Exception = ex; error(this, socketErrorArgs); _socketErrorArgsPool.Push(socketErrorArgs); } NotifyDisconnect(); }
/// <summary> /// Handles an error in socket communication. /// </summary> /// <param name="socket">The socket that raised the exception.</param> /// <param name="ex">The exception that the socket raised.</param> private void HandleCommunicationError(Socket socket, Exception ex) { lock (socket) { // Close the socket try { socket.Shutdown(SocketShutdown.Both); } catch (SocketException) { // Socket was not able to be shutdown, likely because it was never opened } catch (ObjectDisposedException) { // Socket was already closed/disposed, so return out to prevent raising the Error event multiple times // This is most likely to happen when an error occurs during heavily multithreaded use return; } // Close / dispose the socket socket.Close(); } // No longer connected _isConnected = false; // Clear receive queue for this client _receiveBufferQueue.Clear(); // Raise the error event var error = Error; if (error != null) { var socketErrorArgs = _socketErrorArgsPool.Pop(); socketErrorArgs.Exception = ex; error(this, socketErrorArgs); _socketErrorArgsPool.Push(socketErrorArgs); } }