Beispiel #1
0
        protected override void ExecuteCore()
        {
            ClientState.Current.Messages.Errors.Clear();

            if (ChannelsManager.Channels.Count == 0)
            {
                return;
            }

            var channels = ListOrSingle <ChannelInstance> .Get(channel, ChannelsManager.Channels);

            var gate = new ConcurrencyGate(2);

            foreach (var channelInstance in channels.Where(c => !c.Configuration.IsConnected))
            {
                var inputChannel = channelInstance.InputChannel;
                var config       = channelInstance.Configuration;

                var foldersTask = new ReceiveFoldersTask(channelInstance.Configuration, inputChannel.Clone());

                foldersTask.FinishedSuccess += ((sender, e) =>
                                                ReceiveFoldersTask_FinishedSuccess((ReceiveFoldersTask)sender, config, inputChannel));

                gate.Add(foldersTask);
            }

            // Wait for all receive tasks to complete, keeping the queue filled for the ReceiveTask.
            // This prevents any new receive task from getting enqueued.
            gate.Execute();

            // Clean up connection scavangers
            ConnectionPoolScavenger.Shutdown();

            GC.Collect(GC.MaxGeneration);
        }
Beispiel #2
0
        void ReceiveFoldersTask_FinishedSuccess(ReceiveFoldersTask foldersTask, ChannelConfiguration config, IClientInputChannel inputChannel)
        {
            var gate    = new ConcurrencyGate(config.InputChannel.MaxConcurrentConnections);
            var folders = foldersTask.Folders.OrderByDescending(f => (int)f.FolderType);
            var group   = new ProgressGroup {
                SourceChannelId = config.ChannelId
            };

            // Folders are pushed by descending order into concurrency gate and then popped again,
            // this way the Inbox folder is processed before the sent items folder etc.
            foreach (var folder in folders)
            {
                if (folder.FolderType == ChannelFolderType.Inbox ||
                    folder.FolderType == ChannelFolderType.SentItems)
                {
                    gate.Add(new ReceiveMessagesTask(config, inputChannel.Clone(), folder, range));
                }
                else if (folder.FolderType == ChannelFolderType.Trash || folder.FolderType == ChannelFolderType.Spam)
                {
                    gate.Add(new EnumerateMessagesFolderTask(config, inputChannel.Clone(), folder, range)
                    {
                        ProgressGroup = group
                    });
                }
                else if (folder.FolderType == ChannelFolderType.Label)
                {
                    if (config.Charasteristics.SupportsLabels)
                    {
                        gate.Add(new EnumerateLabelsFolderTask(config, inputChannel.Clone(), folder, range)
                        {
                            ProgressGroup = group
                        });
                    }
                }
            }

            gate.Execute();
            group.IsCompleted = true;

            // Process any labels that we might have received
            if (config.Charasteristics.SupportsLabels)
            {
                new ProcessLabelsTask(config).Execute();
            }

            if (inputChannel is IPoolableChannel)
            {
                ((IPoolableChannel)inputChannel).FreeAllConnections();
            }
        }
Beispiel #3
0
        protected override void ExecuteCore()
        {
            ClientState.Current.Messages.Errors.Clear();

            if (ChannelsManager.Channels.Count == 0)
                return;

            var channels = ListOrSingle<ChannelInstance>.Get(channel, ChannelsManager.Channels);
            var gate = new ConcurrencyGate(2);

            foreach (var channelInstance in channels.Where(c => !c.Configuration.IsConnected))
            {
                var inputChannel = channelInstance.InputChannel;
                var config = channelInstance.Configuration;

                var foldersTask = new ReceiveFoldersTask(channelInstance.Configuration, inputChannel.Clone());

                foldersTask.FinishedSuccess += ((sender, e) =>
                    ReceiveFoldersTask_FinishedSuccess((ReceiveFoldersTask)sender, config, inputChannel));

                gate.Add(foldersTask);
            }

            // Wait for all receive tasks to complete, keeping the queue filled for the ReceiveTask.
            // This prevents any new receive task from getting enqueued.
            gate.Execute();

            // Clean up connection scavangers
            ConnectionPoolScavenger.Shutdown();

            GC.Collect(GC.MaxGeneration);
        }
Beispiel #4
0
        void ReceiveFoldersTask_FinishedSuccess(ReceiveFoldersTask foldersTask, ChannelConfiguration config, IClientInputChannel inputChannel)
        {
            var gate = new ConcurrencyGate(config.InputChannel.MaxConcurrentConnections);
            var folders = foldersTask.Folders.OrderByDescending(f => (int) f.FolderType);
            var group = new ProgressGroup { SourceChannelId = config.ChannelId };

            // Folders are pushed by descending order into concurrency gate and then popped again,
            // this way the Inbox folder is processed before the sent items folder etc.
            foreach (var folder in folders)
            {
                if (folder.FolderType == ChannelFolderType.Inbox
                    || folder.FolderType == ChannelFolderType.SentItems)
                {
                    gate.Add(new ReceiveMessagesTask(config, inputChannel.Clone(), folder, range));
                }
                else if (folder.FolderType == ChannelFolderType.Trash || folder.FolderType == ChannelFolderType.Spam)
                {
                    gate.Add(new EnumerateMessagesFolderTask(config, inputChannel.Clone(), folder, range) { ProgressGroup = group });
                }
                else if (folder.FolderType == ChannelFolderType.Label)
                {
                    if (config.Charasteristics.SupportsLabels)
                        gate.Add(new EnumerateLabelsFolderTask(config, inputChannel.Clone(), folder, range) { ProgressGroup = group });
                }
            }

            gate.Execute();
            group.IsCompleted = true;

            // Process any labels that we might have received
            if (config.Charasteristics.SupportsLabels)
                new ProcessLabelsTask(config).Execute();

            if (inputChannel is IPoolableChannel)
                ((IPoolableChannel)inputChannel).FreeAllConnections();
        }