Exemple #1
0
        internal void ParseFromBytes(byte[] bytes, SourceRcon parent)
        {
            try
            {
                int          BPtr = 0;
                ArrayList    stringcache;
                UTF8Encoding utf = new UTF8Encoding();

                // First 4 bytes are ReqId.
                RequestId = BitConverter.ToInt32(bytes, BPtr);
                BPtr     += 4;
                // Next 4 are server data.
                ServerDataReceived = (SERVERDATA_rec)BitConverter.ToInt32(bytes, BPtr);
                BPtr += 4;
                // string1 till /0
                stringcache = new ArrayList();
                while (bytes[BPtr] != 0)
                {
                    stringcache.Add(bytes[BPtr]);
                    BPtr++;
                }
                String1 = utf.GetString((byte[])stringcache.ToArray(typeof(byte)));
                BPtr++;

                // string2 till /0

                stringcache = new ArrayList();
                while (bytes[BPtr] != 0)
                {
                    stringcache.Add(bytes[BPtr]);
                    BPtr++;
                }
                String2 = utf.GetString((byte[])stringcache.ToArray(typeof(byte)));
                BPtr++;

                // Repeat if there's more data?

                if (BPtr != bytes.Length)
                {
                    //parent.OnError("Urk, extra data!");
                }
            }
            catch (Exception)
            {
                //throw;
            }
        }
Exemple #2
0
        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (tbIP.Text.ToLower() == "localhost" || tbIP.Text.ToLower() == "127.0.0.1")
            {
                IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
                foreach (IPAddress addr in localIPs)
                {
                    if (addr.AddressFamily == AddressFamily.InterNetwork)
                    {
                        if (addr.ToString().StartsWith("192."))
                        {
                            tbIP.Text = addr.ToString();
                            break;
                        }
                    }
                }
            }


            Match match = Regex.Match(tbIP.Text, @"((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))");

            if (tbIP.Text.StartsWith("0.") || !match.Success)
            {
                MessageBox.Show("Invalid IP");
                return;
            }

            if (btnConnect.Text == "Connect")
            {
                tbIP.Enabled   = false;
                tbPort.Enabled = false;
                tbPw.Enabled   = false;
                //btnConnect.Enabled = false;
                btnConnect.Text    = "Connecting";
                btnConnect.Enabled = false;
                btnConnect.Invalidate();
                btnConnect.Refresh();



                Thread.Sleep(100);

                ip       = tbIP.Text;
                port     = Convert.ToInt32(tbPort.Value);
                password = tbPw.Text;

                Sr                    = new SourceRcon();
                Sr.Errors            += new StringOutput(ErrorOutput);
                Sr.ServerOutput      += new StringOutput(ConsoleOutput);
                Sr.CommandOutput     += new StringOutput(CommandOutput);
                Sr.SuccessOutput     += new StringOutput(SuccessOutput);
                Sr.Disconnect        += new StringOutput(Disconnected);
                Sr.ConnectionSuccess += Sr_ConnectionSuccess;

                var connectTask = ConnectAsync(ip, port, password);
            }
            else if (btnConnect.Text == "Disconnect")
            {
                Sr.S.Close(); // weird way, had to edit lib, add an event called on exception when accessing disposed socket
                tbInput.Text = string.Empty;


                StatusDot.ForeColor = Color.Red;
                timerAlive.Stop();

                if (lastServer.IP == tbIP.Text && lastServer.Port == Convert.ToInt32(tbPort.Value) && lastServer.Password == tbPw.Text) // it's last server
                {
                    if (lastServer.ServerName != "LastServerEntry")
                    {
                        StatusLabel.Text = "Server: " + lastServer.ServerName;
                    }
                    else
                    {
                        //Console.WriteLine("ELSE");
                    }
                }
                else // not last server
                {
                    StatusLabel.Text = "Server: Unsaved Server";
                }
            }
        }