Example #1
0
        private void LockedShutdown(ShutdownReason reason)
        {
            _shutdownRequested = true;

            if (!_remoteShutdownRequested)
            {
                ChannelDisconnecting(this, reason);
            }

            if (reason == ShutdownReason.Requested)
            {
                var letter = new Letter.Letter(LetterOptions.Ack)
                {
                    Type = LetterType.Shutdown
                };
                Enqueue(letter);

                if (_options.ShutdownGrace.TotalMilliseconds > 0)
                {
                    Thread.Sleep((int)_options.ShutdownGrace.TotalMilliseconds);
                }
                else
                {
                    Thread.Sleep(10);
                }
            }

            _initalizationCount = 0;
            bool wasConnected = IsConnected;

            IsConnected = false;

            if (_transmitter != null)
            {
                _transmitter.Stop();
            }
            if (_receiver != null)
            {
                _receiver.Stop();
            }

            DisconnectSocket();
            WaitForTranseiviersToShutDown();

            FailQueuedLetters();
            FailedReceivedLetters();

            if (wasConnected)
            {
                ChannelDisconnected(this, _remoteShutdownRequested ? ShutdownReason.Remote : reason);
                AfterDisconnectHook(reason);
            }
        }
        private void LockedShutdown(ShutdownReason reason)
        {
            if (!_remoteShutdownRequested)
            {
                ChannelDisconnecting?.Invoke(this, reason);
            }

            if (reason == ShutdownReason.Requested)
            {
                var letter = new Letter.Letter(LetterOptions.Ack)
                {
                    Type = LetterType.Shutdown
                };
                InternalEnqueue(letter);

                if (_options.ShutdownGrace.TotalMilliseconds > 0)
                {
                    Thread.Sleep((int)_options.ShutdownGrace.TotalMilliseconds);
                }
                else
                {
                    Thread.Sleep(10);
                }
            }

            bool wasConnected = IsConnected;

            _initalizationCount = 0;
            IsConnected         = false;

            _transmitter?.Stop();
            _receiver?.Stop();

            DisconnectSocket();
            WaitForTranseiviersToShutDown();

            FailQueuedLetters();
            FailedReceivedLetters();

            if (wasConnected)
            {
                ChannelDisconnected?.Invoke(this, _remoteShutdownRequested ? ShutdownReason.Remote : reason);
            }
        }