tjone270

PHP Quake Live ZMQ implementation

Jan 12th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.54 KB | None | 0 0
  1. // Created by Thomas 'tjone270' Jones - 12-01-17 6:39pm
  2. class QL_Server {
  3.   public $ip, $persistant;
  4.   protected $rconport, $rconpassword;
  5.   private $zmq, $connectionString;
  6.  
  7.   const IDENTITY = "ControlPanel";
  8.   const WAIT_uS = 1000000;
  9.    
  10.   public function __construct($ip, $rconpassword, $rconport = "28960", $persistant = true) {
  11.     $this->ip = $ip;
  12.     $this->rconport = $rconport;
  13.     $this->rconpassword = $rconpassword;
  14.     $this->persistant = $persistant;
  15.     $this->connectionString = ("tcp://${ip}:${rconport}");
  16.     $this->zmq = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_DEALER);
  17.     $this->zmq->setSockOpt(ZMQ::SOCKOPT_PLAIN_USERNAME, "rcon");
  18.     $this->zmq->setSockOpt(ZMQ::SOCKOPT_PLAIN_PASSWORD, $this->rconpassword);
  19.     $this->zmq->setSockOpt(ZMQ::SOCKOPT_IDENTITY, self::IDENTITY);
  20.     $this->zmq->setSockOpt(ZMQ::SOCKOPT_ZAP_DOMAIN, "rcon");
  21.     $this->zmq->connect($this->connectionString);
  22.     $this->zmq->send("register");
  23.   }
  24.    
  25.   public function __destruct() {
  26.     if (!$this->persistant) {
  27.       $this->zmq->disconnect($this->connectionString);
  28.     }
  29.   }
  30.  
  31.   public function command($command, $filter = true) {
  32.     $this->zmq->send($command);
  33.    
  34.     usleep(self::WAIT_uS); // otherwise the server crashes...
  35.    
  36.     $done = false;
  37.     while (!$done) {
  38.       $recv = $this->zmq->recv();
  39.       if ($recv) {
  40.         $response[] = $recv;
  41.       } else {
  42.         $done = true;
  43.       }
  44.     }
  45.    
  46.     if ($filter) {
  47.       array_shift($response);
  48.     }
  49.  
  50.     return implode($response);
  51.   }
  52. }
Add Comment
Please, Sign In to add comment