Beispiel #1
0
        public static async void Server()
        {
            try
            {
                const int tryLimit = 3;
                var       i        = tryLimit;
                while (i > 0)
                {
                    try
                    {
                        PortHelper.CheckPort(Global.Settings.UDPSocketPort, PortType.UDP);
                        if (i != tryLimit)
                        {
                            Configuration.Save();
                        }

                        break;
                    }
                    catch
                    {
                        Global.Settings.UDPSocketPort = PortHelper.GetAvailablePort(PortType.UDP);
                        i--;
                    }
                }

                var data    = new byte[1024];
                var newsock = new UdpClient(new IPEndPoint(IPAddress.Loopback, Global.Settings.UDPSocketPort));

                while (true)
                {
                    var result = await newsock.ReceiveAsync();

                    data = result.Buffer;
                    if (Enum.TryParse <Commands>(Encoding.ASCII.GetString(data, 0, data.Length), out var command))
                    {
                        OnCalled(command);
                    }
                }
            }
            catch (Exception e)
            {
                Logging.Error(e.ToString());
            }
        }