void TCPSocket_StateChange(object sender, TCPListenerSocketEventArgs e) { if (e.Sender.TCPState == TCPSocketState.Closed) { TCPStreamMonitorStack tsmsStack = GetStackForSocket(e.Sender); if (tsmsStack == null) { throw new InvalidOperationException("TCP stream monitor caught an event of a socket which was not created by it. This should never happen."); } TCPListenerSocket sAlice = tsmsStack.StackAlice.TCPSocket; TCPListenerSocket sBob = tsmsStack.StackBob.TCPSocket; if (sAlice.TCPState == TCPSocketState.Closed && sBob.TCPState == TCPSocketState.Closed) { ShutdownStack(tsmsStack); if (tsmsStack.IsClosed) { lock (lStacks) { lStacks.Remove(tsmsStack); InvokeExternal(StackDestroyed, new TCPStreamMonitorEventArgs(tsmsStack)); } } } } }
protected TCPStreamMonitorStack GetStackForSocket(TCPListenerSocket sock) { lock (lStacks) { foreach (TCPStreamMonitorStack sStack in lStacks) { if (sStack.StackAlice.TCPSocket == sock || sStack.StackBob.TCPSocket == sock) { return(sStack); } } return(null); } }