Exemplo n.º 1
0
        private void HandleReceivedLetter(ILetter receivedLetter, AckState ackState)
        {
            switch (receivedLetter.Type)
            {
            case LetterType.Initialize:
                RemoteNodeId = new Guid(receivedLetter.Parts[0]);
                HandleInitialize();
                break;

            case LetterType.Shutdown:
                _remoteShutdownRequested = true;
                ChannelDisconnecting?.Invoke(this, ShutdownReason.Remote);
                break;

            case LetterType.User:
                Received?.Invoke(receivedLetter, CreateReceivedEventArgs(ackState));
                break;

            case LetterType.Batch:
                for (var i = 0; i < receivedLetter.Parts.Length; i++)
                {
                    var batchedLetter = _letterDeserializer.Deserialize(receivedLetter.Parts[i]);
                    Received?.Invoke(batchedLetter, CreateReceivedEventArgs(ackState));
                }
                break;
            }
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
        public BatchChannel(SocketOptions options, IChannel channel, BatchLetterBuilder batchBuilder)
        {
            _channel      = channel;
            _options      = options.Batch;
            _batchBuilder = batchBuilder;

            _channel.ChannelConnected     += abstractChannel => ChannelConnected?.Invoke(this);
            _channel.ChannelDisconnected  += ChannelOnDisconnected;
            _channel.ChannelQueueEmpty    += abstractChannel => {
                /* NOOP */
            };
            _channel.ChannelInitialized   += ChannelOnInitialized;
            _channel.ChannelConnecting    += abstractChannel => ChannelConnecting?.Invoke(this);
            _channel.ChannelDisconnecting += (abstractChannel, reason) => ChannelDisconnecting?.Invoke(this, reason);

            _channel.Received     += ChannelOnReceived;
            _channel.Sent         += ChannelOnSent;
            _channel.FailedToSend += ChannelOnFailedToSend;

            _slidingTimeoutTimer = new Timer(SlidingTimeoutTimerOnElapsed, null, -1, -1);
        }