示例#1
0
文件: RCON.cs 项目: Nestor/IW4M-Admin
        static string[] SendQuery(QueryType Type, Server QueryServer, string Parameters = "")
        {
            using (var ServerOOBConnection = new UdpClient())
            {
                // prevent flooding
                if ((DateTime.Now - LastQuery).TotalMilliseconds < 100)
                {
                    Task.Delay(100).Wait();
                }
                LastQuery = DateTime.Now;

                ServerOOBConnection.Client.SendTimeout    = 1000;
                ServerOOBConnection.Client.ReceiveTimeout = 1000;
                var Endpoint = new IPEndPoint(IPAddress.Parse(QueryServer.GetIP()), QueryServer.GetPort());

                string QueryString = String.Empty;

                switch (Type)
                {
                case QueryType.DVAR:
                case QueryType.COMMAND:
                    QueryString = $"ÿÿÿÿrcon {QueryServer.Password} {Parameters}";
                    break;

                case QueryType.GET_STATUS:
                    QueryString = "ÿÿÿÿ getstatus";
                    break;
                }

                byte[] Payload = GetRequestBytes(QueryString);

                int attempts = 0;
retry:

                try
                {
                    ServerOOBConnection.Connect(Endpoint);
                    ServerOOBConnection.Send(Payload, Payload.Length);

                    byte[]        ReceiveBuffer       = new byte[8192];
                    StringBuilder QueryResponseString = new StringBuilder();

                    do
                    {
                        ReceiveBuffer = ServerOOBConnection.Receive(ref Endpoint);
                        QueryResponseString.Append(Encoding.ASCII.GetString(ReceiveBuffer).TrimEnd('\0'));
                    } while (ServerOOBConnection.Available > 0 && ServerOOBConnection.Client.Connected);

                    if (QueryResponseString.ToString().Contains("Invalid password"))
                    {
                        throw new Exceptions.NetworkException("RCON password is invalid");
                    }
                    if (QueryResponseString.ToString().Contains("rcon_password"))
                    {
                        throw new Exceptions.NetworkException("RCON password has not been set");
                    }

                    int      num           = int.Parse("0a", System.Globalization.NumberStyles.AllowHexSpecifier);
                    string[] SplitResponse = QueryResponseString.ToString().Split(new char[] { (char)num }, StringSplitOptions.RemoveEmptyEntries);
                    return(SplitResponse);
                }

                catch (Exceptions.NetworkException e)
                {
                    throw e;
                }

                catch (Exception e)
                {
                    attempts++;
                    if (attempts > 2)
                    {
                        var ne = new Exceptions.NetworkException("Could not communicate with the server");
                        ne.Data["internal_exception"] = e.Message;
                        ne.Data["server_address"]     = ServerOOBConnection.Client.RemoteEndPoint.ToString();
                        throw ne;
                    }

                    Thread.Sleep(1000);
                    goto retry;
                }
            }
        }
示例#2
0
        void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            // if has no shell yet throws it anyway
            if (shellViewModel == null)
                return;

            // tries to get it as a Scrum factory exceptions
            ScrumFactory.Exceptions.ScrumFactoryException sfEx = null;
            if (e.Exception is ScrumFactory.Exceptions.ScrumFactoryException)
                sfEx = e.Exception as ScrumFactory.Exceptions.ScrumFactoryException;
            if (e.Exception.InnerException is ScrumFactory.Exceptions.ScrumFactoryException)
                sfEx = e.Exception.InnerException as ScrumFactory.Exceptions.ScrumFactoryException;

            if (e.Exception is System.Net.Http.HttpRequestException)
                sfEx = new Exceptions.NetworkException();

            // if is not a factoy exception, throws it to the OS
            if (sfEx==null) {
                if(log!=null)
                    log.LogError(e.Exception);
                return;
            }

            // if is a ScrumFactoryExcpetion let the shell handle it
            shellViewModel.HandleScrumFactoryException(sfEx);
            e.Handled = true;
        }