Advertisement
Guest User

Untitled

a guest
Aug 20th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. <?php
  2. /*!
  3. * \file starmade_query.inc.php
  4. * \brief Allows a single call to fetch the server listing data for the specified starmade server.
  5. * \ingroup TwoSphere
  6. **/
  7.  
  8. /*
  9. * This program is free software; you can redistribute it and/or modify
  10. * it in any way you see fit. Just give me credit where it's apropriate.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * Comments are setup for use with DOXYGEN.
  17. *
  18. */
  19.  
  20.  
  21. /**
  22. * \addtogroup TwoSphere
  23. * @{*/
  24. namespace TwoSphere\Starmade;
  25.  
  26.  
  27. /**
  28. * ##Starmade server connection manager
  29. *
  30. * \author Stefan Kent
  31. * \email skent@twosphere.com.au
  32. * \date 10/2013
  33. * \version 1.0
  34. *
  35. **/
  36. class server
  37. {
  38. private $port;
  39. private $host;
  40. private $address;
  41. private $request;
  42. public $raw;
  43.  
  44. public $version;
  45. public $connected;
  46. public $capacity;
  47. public $name;
  48. public $description;
  49.  
  50. /**
  51. * \brief Constructor
  52. *
  53. * Resolves the hostname, and loads the query packet
  54. * ready for the query call.
  55. *
  56. * \param string $host The hostname or IP address of the server to connect to.
  57. * \param string $port Specify the port if it is non-standard.
  58. * \return Null
  59. */
  60. public function __construct($host,$port = 4242)
  61. {
  62. //Set up the environment.
  63. $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);
  64. $this->address = gethostbyname($host);
  65. $this->port = $port;
  66. $this->host = $host;
  67. }
  68.  
  69.  
  70. /**
  71. * \brief Query the server
  72. *
  73. * Connects to the server, waits for response,
  74. * creates a packet object
  75. * and translates the information into it's respective variables.
  76. *
  77. * \return Null
  78. */
  79. public function query()
  80. {
  81. $socket = socket_create ( AF_INET , SOCK_STREAM , SOL_TCP );
  82. if(@socket_connect($socket,$this->address,$this->port))
  83. {
  84. if(!@socket_send($socket,$this->request,strlen($this->request),MSG_EOF))
  85. {
  86. //There was a problem sending the request.
  87. throw new \Exception("Failed to send query; reason: " . socket_strerror(socket_last_error($socket)));
  88. }
  89.  
  90. if (false !== ($bytes = @socket_recv($socket, $buf, 2048, MSG_WAITALL)))
  91. {
  92. //Success
  93. }
  94. else
  95. {
  96. //There was a problem with the server response.
  97. throw new \Exception("Failed to recieve response; reason: " . socket_strerror(socket_last_error($socket)));
  98. }
  99. socket_close($socket);
  100.  
  101. //Interpret translate the stream into an array.
  102. $this->raw = new server_packet($buf);
  103. }
  104. else
  105. {
  106. //Couldn't connect to the server at all.
  107. throw new \Exception("Could not connect to server at:".$this->address." (".$this->host.")");
  108. }
  109.  
  110. //Process the packet.
  111. $this->version = $this->raw->data[3]['value'];
  112. $this->name = $this->raw->data[4]['value'];
  113. $this->description = $this->raw->data[5]['value'];
  114. $this->connected = $this->raw->data[7]['value'];
  115. $this->capacity = $this->raw->data[8]['value'];
  116. }
  117. }
  118.  
  119.  
  120. /**
  121. * ##Packet interpreter for starmade server.
  122. *
  123. *
  124. * \author Stefan Kent
  125. * \email skent@twosphere.com.au
  126. * \date 10/2013
  127. * \version 1.0
  128. *
  129. **/
  130. class server_packet
  131. {
  132. public $data; /**< The resulting array after conversion. */
  133.  
  134. /**
  135. * \brief Constructor
  136. *
  137. * This will take the stream and break it
  138. * into an array based on the datatype
  139. * in the order that it was recieved.
  140. *
  141. * \param string $buf Raw response from the server.
  142. * \return Null
  143. */
  144. public function __construct($buf)
  145. {
  146. $pointer = 6;
  147. while($pointer < strlen($buf))
  148. {
  149. $mark = ord(substr($buf,$pointer,1));
  150. $pointer++;
  151.  
  152. switch($mark)
  153. {
  154. case 1: //int
  155. list(,$unpacked) = unpack("N", substr($buf,$pointer,4));
  156. if($unpacked >= pow(2, 31)) $unpacked -= pow(2, 32);
  157. $pointer+=4;
  158. $this->data[] = array("type"=>"int","value"=>$unpacked);
  159.  
  160. break;
  161.  
  162. case 3: //float
  163. 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)));
  164. $pointer+=4;
  165. $this->data[] = array("type"=>"float","value"=>$value);
  166.  
  167. break;
  168.  
  169. case 4: //string
  170.  
  171. list(,$unpacked) = unpack("n", substr($buf,$pointer,2));
  172. if($unpacked >= pow(2, 15)) $unpacked -= pow(2, 16); // Convert unsigned short to signed short.
  173. $strlen = $unpacked;
  174. $pointer+=2;
  175.  
  176. $this->data[] = array("type"=>"string","value"=>substr($buf,$pointer,$strlen));
  177. $pointer+=$strlen;
  178.  
  179. break;
  180.  
  181. case 6: //Byte
  182. $this->data[] = array("type"=>"byte","value"=>ord(substr($buf,$pointer,1)));
  183. $pointer++;
  184.  
  185. break;
  186.  
  187. case 0: //null
  188. default:
  189. break;
  190. }
  191. }
  192. }
  193.  
  194. }
  195. /**@}*/
  196. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement