Exemple #1
0
        public static async Task InitPlanets(Dictionary <string, string> hostLookup)
        {
            string blah = $"https://{ProxyManagerConfig.Instance.BoundlexxApiBase}/api/v2/worlds/?limit=10000&active=True&is_locked=False";

            HttpClient client = new HttpClient();
            var        result = await client.GetAsync(blah);

            if (!result.IsSuccessStatusCode)
            {
                throw new Exception("Error getting planets from server");
            }

            var serverList = JObject.Parse(await result.Content.ReadAsStringAsync());

            int curPort = 1000;

            foreach (var something in serverList["results"])
            {
                string planetId   = something["name"].Value <string>();
                string planetName = something["display_name"].Value <string>();
                var    planetNum  = something["id"].Value <int>();

                string addr   = something["address"].Value <string>();
                string ipAddr = hostLookup[addr];

                try
                {
                    planetLookup.Add(planetId, new KeyValuePair <string, int>(planetName, planetNum));
                }
                catch (System.ArgumentException)
                {
                    System.Diagnostics.Debug.WriteLine("Duplicate planet: {1}", planetId);
                }


                UdpProxy proxy = null;

                while (curPort.ToString().Length == 4)
                {
                    try
                    {
                        proxy = new UdpProxy(curPort++, ipAddr, addr);
                    }
                    catch
                    {
                        continue;
                    }

                    break;
                }

                if (proxy == null)
                {
                    throw new Exception("too many ports in use");
                }

                planetPorts.Add(planetId, proxy);
            }

            // UDP Thread
            new Thread(() =>
            {
                var tasks = planetPorts.Values.Select(cur => cur.StartAsync()).ToArray();
                Task.WaitAny(tasks);

                var exception = tasks.FirstOrDefault(cur => cur.Exception != null)?.Exception?.InnerException;

                if (exception != null)
                {
                    Log.Error(exception, "UDP Thread error");
                    MessageBox.Show(exception.Message);
                    KillUdp();
                }
            }).Start();
        }
Exemple #2
0
        public static async Task InitPlanets(Dictionary <string, string> hostLookup)
        {
            string blah = $"https://{Constants.DiscoveryServer}:8902/list-gameservers";

            HttpClient client = new HttpClient();
            var        result = await client.GetAsync(blah);

            if (!result.IsSuccessStatusCode)
            {
                throw new Exception("Error getting planets from server");
            }

            var serverList = JArray.Parse(await result.Content.ReadAsStringAsync());

            int curPort = 1000;

            foreach (var something in serverList)
            {
                string planetId   = something["name"].Value <string>();
                string planetName = something["displayName"].Value <string>();
                var    planetNum  = something["id"].Value <int>();

                string addr   = something["addr"].Value <string>();
                string ipAddr = hostLookup[addr];

                planetLookup.Add(planetId, new KeyValuePair <string, int>(planetName, planetNum));

                UdpProxy proxy = null;

                while (curPort.ToString().Length == 4)
                {
                    try
                    {
                        proxy = new UdpProxy(curPort++, ipAddr, addr);
                    }
                    catch
                    {
                        continue;
                    }

                    break;
                }

                if (proxy == null)
                {
                    throw new Exception("too many ports in use");
                }

                planetPorts.Add(planetId, proxy);
            }

            // UDP Thread
            new Thread(() =>
            {
                var tasks = planetPorts.Values.Select(cur => cur.StartAsync()).ToArray();
                Task.WaitAny(tasks);

                var exception = tasks.FirstOrDefault(cur => cur.Exception != null)?.Exception?.InnerException;

                if (exception != null)
                {
                    MessageBox.Show(exception.Message);
                    KillUdp();
                }
            }).Start();
        }