private void Shutdown(ServiceResult reason)
{
lock (DataLock)
{
// clear an unprocessed chunks.
SaveIntermediateChunk(0, new ArraySegment<byte>());
// halt any scheduled tasks.
if (m_handshakeTimer != null)
{
m_handshakeTimer.Dispose();
m_handshakeTimer = null;
}
// halt any existing handshake.
if (m_handshakeOperation != null && !m_handshakeOperation.IsCompleted)
{
m_handshakeOperation.Fault(reason);
}
// cancel all requests.
List<WriteOperation> operations = new List<WriteOperation>(m_requests.Values);
foreach (WriteOperation operation in operations)
{
operation.Fault(new ServiceResult(StatusCodes.BadSecureChannelClosed, reason));
}
m_requests.Clear();
uint channelId = ChannelId;
// dispose of the tokens.
ChannelId = 0;
DiscardTokens();
// clear the handshake state.
m_handshakeOperation = null;
m_requestedToken = null;
m_reconnecting = false;
// close the socket.
State = TcpChannelState.Closed;
if (Socket != null)
{
Utils.Trace("TCPCLIENTCHANNEL SOCKET CLOSED: {0:X8}, ChannelId={1}", Socket.Handle, channelId);
Socket.Close();
Socket = null;
}
// set the state.
ChannelStateChanged(TcpChannelState.Closed, reason);
}
}