Beispiel #1
0
        public async Task <List <ServerItem> > FindMapleServers()
        {
            var hostList   = new List <ServerItem>();
            var listener   = new UdpClient(LISTEN_PORT);
            var ipEndPoint = new IPEndPoint(IPAddress.Any, LISTEN_PORT);

            var timeoutTask = UdpTimeoutTask();

            try
            {
                while (timeoutTask.IsCompleted == false)
                {
                    Console.WriteLine("Waiting for broadcast");

                    var tasks = new Task <UdpReceiveResult>[] { timeoutTask, listener.ReceiveAsync() };

                    int index = 0;

                    await Task.Run(() => index = Task.WaitAny(tasks));

                    var results = tasks[index].Result;

                    if (results.RemoteEndPoint == null)
                    {
                        break;
                    }

                    string host   = Encoding.UTF8.GetString(results.Buffer, 0, results.Buffer.Length);
                    string hostIp = host.Split('=')[1];

                    Console.WriteLine("Received broadcast from {0} :\n {1}\n", hostIp, host);

                    var serverItem = new ServerItem()
                    {
                        Name      = host.Split('=')[0] + " (" + host.Split('=')[1] + ") ",
                        IpAddress = host.Split('=')[1]
                    };

                    if (!hostList.Any(server => server.IpAddress == hostIp))
                    {
                        hostList.Add(serverItem);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                listener.Close();
            }

            return(hostList);
        }
Beispiel #2
0
 public async Task <bool> TurnOnAsync(ServerItem server)
 {
     return(await SendCommandAsync("TurnOn", server.IpAddress));
 }
Beispiel #3
0
 public async Task <bool> TransferFile(ServerItem server)
 {
     return(await SendCommandAsync("TransferFile", server.IpAddress));
 }