// Override EntityBase's definition of Dispose to Dispose base class objects, but also the Socket object // that is defined in this class. protected override void Dispose(bool disposing) { base.Dispose(disposing); if (disposing) { ConnectionSocket.Close(); ConnectionSocket.Dispose(); } }
/// <summary> /// Closes the socket /// </summary> public void Dispose() { try { DataReceived = null; Disconnected = null; Close(); if (ConnectionSocket != null) { ConnectionSocket.Dispose(); ConnectionSocket = null; } } catch { } }
private int AsyncReceive(out byte[] data, int timeout) { data = new byte[ReceiveBufferSize]; IAsyncResult receiveResult = ConnectionSocket.BeginReceive(data, 0, ReceiveBufferSize, SocketFlags.None, null, null); bool success = false; if (timeout > 0) { success = receiveResult.AsyncWaitHandle.WaitOne(timeout, true); } else if (timeout == 0) { success = receiveResult.AsyncWaitHandle.WaitOne(); } if (!success) { data = null; ConnectionSocket.Dispose(); return(-1); } int size = ConnectionSocket.EndReceive(receiveResult); if (size > 0) { TrimData(ref data, size); } else { data = null; } return(size); }