Recent

Author Topic: Best way to do this in FPC.  (Read 10301 times)

MuteClown

  • New Member
  • *
  • Posts: 33
Best way to do this in FPC.
« on: March 30, 2010, 04:12:44 am »
Hey,
i am working on building a admin tool for a game server, i am making a bot to connect to the game servers rcon. The thing is i have no experience with how networking works, i guess this is a good time for me to learn  ::)
I found someone made something that would connect to the rcon but with php, so im going to try and replicate it with fpc.

Here is the initial connection class in php:

Code: [Select]
class q3rcon {

        // Some vars needed for rcon info
        private $password;

        // Some vars to store what server we will connect to
        private $address;
        private $port;
        private $socket_connection;

        // Misc. other vars
        private $last_socket_err_num;
        private $last_socket_err_str;

        // Constructor: takes the IP address (or hostname), port, and rcon password of
        // the server and opens a connection.
        public function __construct($serv_address, $serv_port, $serv_password, $timeout=30) {
                $this->address = $serv_address;
                $this->port = intval($serv_port);
                $this->password = $serv_password;

                /*
                echo $this->address;
                echo $this->port;
                echo $this->password;
                */

                $this->last_socket_err_num = -1;
                $this->last_socket_err_str = "";

                // Open up the connection wih the given address and port
                $this->socket_connection = fsockopen("udp://" . $this->address, $this->port, $this->last_socket_err_num, $this->last_socket_err_str, $timeout);
                if (!$this->socket_connection) {
                        die("Could not connect with given ip:port\n<br>errno: $this->last_socket_err_num\n<br>errstr: $this->last_socket_err_str");
                }
                //turn blocking on so that you can't over-spam the connection

        }

        // Precondition: A socket has been opened without error by the constructor
        // Postcondidtion: The given command will be sent and a response will be
        //      recoverable from the function get_response()
        public function send_command($cmd) {
                fwrite($this->socket_connection, str_repeat(chr(255), 4) . "rcon " . $this->password . " " . $cmd . "\n");
        }

        // Get the server's response to our previous query.
        // Precondition: A command should have already been sent with send_command($cmd).
        // Postcondidtion: The server's response string will be returned.
        public function get_response(&$er = false) {
                $er = false;
                stream_set_timeout($this->socket_connection, 0, 500000);
                $buffer = "";
                while ($buff = fread($this->socket_connection, 9999)) {
                        if(!substr($buff, 4, 8) == "print"){
                           $er = true;
                        }
                        $buffer .= substr($buff, 4 + 6);
                }
                return $buffer;
        }

        public function close() {
                fclose($this->socket_connection);
        }
}
?>

what would be the best option to do this in pascal?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8768
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Best way to do this in FPC.
« Reply #1 on: March 30, 2010, 05:54:20 am »
Use some networking library. The best 2 are LNet and Synapse.

MuteClown

  • New Member
  • *
  • Posts: 33
Re: Best way to do this in FPC.
« Reply #2 on: March 30, 2010, 07:57:30 am »
OK tried getting LNet
but when i tried to make i got this:

Code: [Select]
lwebserver.pp(645,53) Error: absolute can only be associated with a var or const
lwebserver.pp(654,27) Warning: Local variable "lServerSocket" does not seem to be initialized
lwebserver.pp(705,53) Error: absolute can only be associated with a var or const
lwebserver.pp(729,7) Warning: Local variable "lServerSocket" does not seem to be initialized
lwebserver.pp(877,52) Error: absolute can only be associated with a var or const
lwebserver.pp(1119,4) Warning: User defined: TODO
lwebserver.pp(1255) Fatal: There were 3 errors compiling module, stopping
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode (normal if you did not specify a source file to be compiled)

And with Synapse how do i link it in lazarus, also where should i be storing it?


theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1929
Re: Best way to do this in FPC.
« Reply #3 on: March 30, 2010, 09:43:14 am »
And with Synapse how do i link it in lazarus, also where should i be storing it?

Create a package or download the one I've posted here
http://forum.lazarus.freepascal.org/index.php/topic,7089.msg33281.html#msg33281
You don't have to rebuild the IDE, because Synapse is not visual components.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8768
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Best way to do this in FPC.
« Reply #4 on: March 30, 2010, 10:23:37 am »
Quote
OK tried getting LNet
but when i tried to make i got this:
Use svn version. There are changes in the compiler regarding usage of absolute.

MuteClown

  • New Member
  • *
  • Posts: 33
Re: Best way to do this in FPC.
« Reply #5 on: March 30, 2010, 09:27:14 pm »
Ok, so i installed lNet and can use it with the GUI, but how do i use it in non-graphical mode?
So i need to link the units in lazarus, -FU -FI how do i do it in lazarus?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8768
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Best way to do this in FPC.
« Reply #6 on: April 05, 2010, 11:47:28 am »
Quote
but how do i use it in non-graphical mode?
Don't use the component palette. Create the connection from code.

 

TinyPortal © 2005-2018