Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*!
- * \file starmade_query.inc.php
- * \brief Allows a single call to fetch the server listing data for the specified starmade server.
- * \ingroup TwoSphere
- **/
- /*
- * This program is free software; you can redistribute it and/or modify
- * it in any way you see fit. Just give me credit where it's apropriate.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * Comments are setup for use with DOXYGEN.
- *
- */
- /**
- * \addtogroup TwoSphere
- * @{*/
- namespace TwoSphere\Starmade;
- /**
- * ##Starmade server connection manager
- *
- * \author Stefan Kent
- * \email skent@twosphere.com.au
- * \date 10/2013
- * \version 1.0
- *
- **/
- class server
- {
- private $port;
- private $host;
- private $address;
- private $request;
- public $raw;
- public $version;
- public $connected;
- public $capacity;
- public $name;
- public $description;
- /**
- * \brief Constructor
- *
- * Resolves the hostname, and loads the query packet
- * ready for the query call.
- *
- * \param string $host The hostname or IP address of the server to connect to.
- * \param string $port Specify the port if it is non-standard.
- * \return Null
- */
- public function __construct($host,$port = 4242)
- {
- //Set up the environment.
- $this->request = chr(0x00).chr(0x00).chr(0x00).chr(0x09).chr(0x2a).chr(0xff).chr(0xff).chr(0x01).chr(0x6f).chr(0x00).chr(0x00).chr(0x00).chr(0x00);
- $this->address = gethostbyname($host);
- $this->port = $port;
- $this->host = $host;
- }
- /**
- * \brief Query the server
- *
- * Connects to the server, waits for response,
- * creates a packet object
- * and translates the information into it's respective variables.
- *
- * \return Null
- */
- public function query()
- {
- $socket = socket_create ( AF_INET , SOCK_STREAM , SOL_TCP );
- if(@socket_connect($socket,$this->address,$this->port))
- {
- if(!@socket_send($socket,$this->request,strlen($this->request),MSG_EOF))
- {
- //There was a problem sending the request.
- throw new \Exception("Failed to send query; reason: " . socket_strerror(socket_last_error($socket)));
- }
- if (false !== ($bytes = @socket_recv($socket, $buf, 2048, MSG_WAITALL)))
- {
- //Success
- }
- else
- {
- //There was a problem with the server response.
- throw new \Exception("Failed to recieve response; reason: " . socket_strerror(socket_last_error($socket)));
- }
- socket_close($socket);
- //Interpret translate the stream into an array.
- $this->raw = new server_packet($buf);
- }
- else
- {
- //Couldn't connect to the server at all.
- throw new \Exception("Could not connect to server at:".$this->address." (".$this->host.")");
- }
- //Process the packet.
- $this->version = $this->raw->data[3]['value'];
- $this->name = $this->raw->data[4]['value'];
- $this->description = $this->raw->data[5]['value'];
- $this->connected = $this->raw->data[7]['value'];
- $this->capacity = $this->raw->data[8]['value'];
- }
- }
- /**
- * ##Packet interpreter for starmade server.
- *
- *
- * \author Stefan Kent
- * \email skent@twosphere.com.au
- * \date 10/2013
- * \version 1.0
- *
- **/
- class server_packet
- {
- public $data; /**< The resulting array after conversion. */
- /**
- * \brief Constructor
- *
- * This will take the stream and break it
- * into an array based on the datatype
- * in the order that it was recieved.
- *
- * \param string $buf Raw response from the server.
- * \return Null
- */
- public function __construct($buf)
- {
- $pointer = 6;
- while($pointer < strlen($buf))
- {
- $mark = ord(substr($buf,$pointer,1));
- $pointer++;
- switch($mark)
- {
- case 1: //int
- list(,$unpacked) = unpack("N", substr($buf,$pointer,4));
- if($unpacked >= pow(2, 31)) $unpacked -= pow(2, 32);
- $pointer+=4;
- $this->data[] = array("type"=>"int","value"=>$unpacked);
- break;
- case 3: //float
- list(,$value) = (pack('d', 1) == "\77\360\0\0\0\0\0\0")?unpack('f', substr($buf,$pointer,4)):unpack('f', strrev(substr($buf,$pointer,4)));
- $pointer+=4;
- $this->data[] = array("type"=>"float","value"=>$value);
- break;
- case 4: //string
- list(,$unpacked) = unpack("n", substr($buf,$pointer,2));
- if($unpacked >= pow(2, 15)) $unpacked -= pow(2, 16); // Convert unsigned short to signed short.
- $strlen = $unpacked;
- $pointer+=2;
- $this->data[] = array("type"=>"string","value"=>substr($buf,$pointer,$strlen));
- $pointer+=$strlen;
- break;
- case 6: //Byte
- $this->data[] = array("type"=>"byte","value"=>ord(substr($buf,$pointer,1)));
- $pointer++;
- break;
- case 0: //null
- default:
- break;
- }
- }
- }
- }
- /**@}*/
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement