示例#1
0
        public void ReloadServerItem(IPEndPoint iPEndPoint, CancellationToken cancellationToken)
        {
            var dest = ServerItems;

            try
            {
                UiTask.Run(() => dest.Clear(), cancellationToken).Wait(cancellationToken);
            }

            catch (OperationCanceledException)
            {
                Trace.WriteLine("Reloading canceled");
                return;
            }

            var vo = _defaultServerRepository.GetServerInfo(new IPEndPoint(iPEndPoint.Address, iPEndPoint.Port + 1));

            if (cancellationToken.IsCancellationRequested || vo == null)
            {
                return;
            }
            var item = new ServerItem();

            AssignProperties(item, vo);

            UiTask.Run((dest2, item2) => dest2.Add(item2), dest, item).Wait(0);
        }
示例#2
0
        //internal void TestJson()
        //{
        //    _context.TestJson();
        //}

        private async void EndlessRefreshSelecteItem()
        {
            while (true)
            {
                var mainwin = await UiTask.Run(() => App.Current.MainWindow);

                if (mainwin == null)
                {
                    break;
                }
                try
                {
                    var task = UiTask.Run(() => mainwin.WindowState != WindowState.Minimized);
                    task.Wait();
                    if (task.IsCompleted && task.Result && !_isJoining)
                    {
                        var item = SelectedServerItem;
                        if (item != null)
                        {
                            _context.RefreshServerInfo(new[] { item });
                        }
                    }
                    await Task.Delay(3000);
                }
                catch
                {
                }
            }
        }
示例#3
0
        //public void TestJson()
        //{
        //    _defaultBrowserServerRepository.Test();
        //}

        private void ps_Exited(object sender, EventArgs e)
        {
            //Todo Move to ViewModel Code
            var t = UiTask.Run(() =>
            {
                Application.Current.MainWindow.WindowState = WindowState.Normal;
                Application.Current.MainWindow.Activate();
            });
        }
示例#4
0
        private void LoadingServerList(object loadingServerListContext)
        {
            var threadId = Thread.CurrentThread.ManagedThreadId;
            var state    = (LoadingServerListContext)loadingServerListContext;

            foreach (var dataItem in state.Ips)
            {
                if (state.Token.IsCancellationRequested)
                {
                    break;
                }

                var serverQueryEndpoint = new IPEndPoint(dataItem.Host, dataItem.QueryPort);

                if (LiveAction != null)
                {
                    LiveAction(this,
                               string.Format("{2,3} {0} {1}", BitConverter.ToString(dataItem.Host.GetAddressBytes()),
                                             BitConverter.ToString(BitConverter.GetBytes(dataItem.QueryPort)), threadId));
                }

                var vo = _defaultServerRepository.GetServerInfo(serverQueryEndpoint);

                if (state.Token.IsCancellationRequested)
                {
                    break;
                }

                var item = new ServerItem();
                if (vo != null)
                {
                    AssignProperties(item, vo);
                }
                else
                {
                    item.Name      = string.Format("{0}:{1}", serverQueryEndpoint.Address, serverQueryEndpoint.Port - 1);
                    item.Host      = serverQueryEndpoint.Address;
                    item.QueryPort = serverQueryEndpoint.Port;
                }

                state.ProgressValue++;
                state.Ping = item.Ping;

                var t = UiTask.Run((dest2, item2) => dest2.Add(item2), state.Dest, item);
            }
            state.Finished();
            Task.Delay(TimeSpan.FromSeconds(0.5))
            .ContinueWith((t, ctx) => ReloadThreads.Remove((LoadingServerListContext)ctx), state, UiTask.UiTaskScheduler)
            .Wait();
            //UiTask.Run(ctx => ReloadThreads.Remove(ctx), state).Wait();
        }
示例#5
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);
        }
示例#6
0
        private void UpdateServerInfo(IServerItem[] serverItems)
        {
            foreach (ServerItem serverItem in serverItems)
            {
                if (serverItem == null)
                {
                    return;
                }
                if (serverItem.Host == null)
                {
                    return;
                }
                if (serverItem.QueryPort == 0)
                {
                    return;
                }


                ISteamGameServer dataItem = null;
                try
                {
                    dataItem =
                        _defaultServerRepository.GetServerInfo(new IPEndPoint(serverItem.Host, serverItem.QueryPort));
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
                if (dataItem == null)
                {
                    continue;
                }

                UiTask.Run(AssignProperties, serverItem, dataItem).Wait();
            }
        }
示例#7
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);
        }