private void Disconnect()
{
lock (_stateLock)
{
// Do nothing if the connection is offline
if (State != ConnectionState.Disconnected)
{
// Change state before doing anything else in case something later in the method throws
State = ConnectionState.Disconnected;
Trace(TraceLevels.StateChanges, "Disconnected");
_disconnectTimeoutOperation.Dispose();
_disconnectCts.Cancel();
_disconnectCts.Dispose();
_receiveQueueMonitor.Dispose();
if (Monitor != null)
{
Monitor.Dispose();
}
if (_transport != null)
{
Trace(TraceLevels.Events, "Transport.Dispose({0})", ConnectionId);
_transport.Dispose();
_transport = null;
}
Trace(TraceLevels.Events, "Closed");
// Clear the state for this connection
ConnectionId = null;
ConnectionToken = null;
GroupsToken = null;
MessageId = null;
_connectionData = null;
#if NETFX_CORE || PORTABLE
// Clear the buffer
_traceWriter.Flush();
#endif
// TODO: Do we want to trigger Closed if we are connecting?
OnClosed();
}
}
}