Exemple #1
0
        private LoadingServerListContext NewThread(Collection <IServerItem> dest, CancellationToken token, int blockCount,
                                                   int i)
        {
            var thread = new Thread(LoadingServerList)
            {
                Name         = "UDP" + (i + 1),
                IsBackground = true,
                Priority     = ThreadPriority.Lowest
            };
            var threadContext = new LoadingServerListContext
            {
                Dest  = dest,
                Ips   = _serverIPListe.Skip(blockCount * i).Take(blockCount).ToArray(),
                Token = token
            };

            UiTask.Run(ReloadThreads.Add, threadContext).Wait(0);
            thread.Start(threadContext);
            return(threadContext);
        }
Exemple #2
0
        public void ReloadServerItems(IEnumerable <IPEndPoint> lastAddresses, CancellationToken cancellationToken)
        {
            var dest     = ServerItems;
            var recently = dest.Where(srv => srv.LastPlayed.HasValue).ToArray();

            //.Select(srv => new System.Net.IPEndPoint(srv.Host, srv.Port))

            try
            {
                UiTask.Run(() => dest.Clear(), cancellationToken).Wait(cancellationToken);
            }
            catch (OperationCanceledException)
            {
                Trace.WriteLine("Reloading canceled");
                return;
            }

            RefreshServerInfoAsync(recently)
            .ContinueWith(t =>
            {
                foreach (var recentlyItem in recently)
                {
                    dest.Add(recentlyItem);
                }
            }, UiTask.UiTaskScheduler).Wait(cancellationToken);


            _serverIPListe = _defaultServerRepository.GetServerList(OnServerGenerated);
            if (lastAddresses.Count() > 0)
            {
                var last = _serverIPListe.Join(lastAddresses,
                                               o => (object)o,
                                               i => (object)i,
                                               (o, i) => o,
                                               _comp.Default).ToArray();

                //die EndPoints aus dem Argument lastAddresses aus der Gesamtliste zunächst entfernen
                _serverIPListe = _serverIPListe.Except(last).ToArray();

                // die EndPoints aus dem Argument lastAddresses vorn einreihen
                _serverIPListe = last.Union(_serverIPListe).ToArray();
            }

            if (recently.Length > 0)
            {
                var recentlyData = _serverIPListe.Join(recently,
                                                       o => (object)o,
                                                       i => i,
                                                       (o, i) => o,
                                                       _comp.Default).ToArray();

                _serverIPListe = _serverIPListe.Except(recentlyData).ToArray();
            }

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            var token = cancellationToken;

            var threadCount = Environment.ProcessorCount * 4;

            var blockCount = Convert.ToInt32(Math.Floor(_serverIPListe.Length / (threadCount * 1d)));

            if (_serverIPListe.Length == 0)
            {
                return;
            }

            var waitArray = new WaitHandle[threadCount + 1];

            for (var i = 0; i < threadCount; i++)
            {
                LoadingServerListContext threadContext = NewThread(dest, token, blockCount, i);
                waitArray[i] = threadContext.Reset;
            }

            // for the rest a single thread
            {
                var threadContext = NewThread(dest, token, blockCount, waitArray.Length - 1);
                waitArray[waitArray.Length - 1] = threadContext.Reset;
            }

            WaitHandle.WaitAll(waitArray);
        }