Guest User

Untitled

a guest
Aug 20th, 2014
249
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. * \date 10/2013
  32. * \version 1.0
  33. *
  34. **/
  35. class server
  36. {
  37. private $port;
  38. private $host;
  39. private $address;
  40. private $request;
  41. public $raw;
  42.  
  43. public $version;
  44. public $connected;
  45. public $capacity;
  46. public $name;
  47. public $description;
  48.  
  49. /**
  50. * \brief Constructor
  51. *
  52. * Resolves the hostname, and loads the query packet
  53. * ready for the query call.
  54. *
  55. * \param string $host The hostname or IP address of the server to connect to.
  56. * \param string $port Specify the port if it is non-standard.
  57. * \return Null
  58. */
  59. public function __construct($host,$port = 4242)
  60. {
  61. //Set up the environment.
  62. $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);
  63. $this->address = gethostbyname($host);
  64. $this->port = $port;
  65. $this->host = $host;
  66. }
  67.  
  68.  
  69. /**
  70. * \brief Query the server
  71. *
  72. * Connects to the server, waits for response,
  73. * creates a packet object
  74. * and translates the information into it's respective variables.
  75. *
  76. * \return Null
  77. */
  78. public function query()
  79. {
  80. $socket = socket_create ( AF_INET , SOCK_STREAM , SOL_TCP );
  81. if(@socket_connect($socket,$this->address,$this->port))
  82. {
  83. if(!@socket_send($socket,$this->request,strlen($this->request),MSG_EOF))
  84. {
  85. //There was a problem sending the request.
  86. throw new \Exception("Failed to send query; reason: " . socket_strerror(socket_last_error($socket)));
  87. }
  88.  
  89. if (false !== ($bytes = @socket_recv($socket, $buf, 2048, MSG_WAITALL)))
  90. {
  91. //Success
  92. }
  93. else
  94. {
  95. //There was a problem with the server response.
  96. throw new \Exception("Failed to recieve response; reason: " . socket_strerror(socket_last_error($socket)));
  97. }
  98. socket_close($socket);
  99.  
  100. //Interpret translate the stream into an array.
  101. $this->raw = new server_packet($buf);
  102. }
  103. else
  104. {
  105. //Couldn't connect to the server at all.
  106. throw new \Exception("Could not connect to server at:".$this->address." (".$this->host.")");
  107. }
  108.  
  109. //Process the packet.
  110. $this->version = $this->raw->data[3]['value'];
  111. $this->name = $this->raw->data[4]['value'];
  112. $this->description = $this->raw->data[5]['value'];
  113. $this->connected = $this->raw->data[7]['value'];
  114. $this->capacity = $this->raw->data[8]['value'];
  115. }
  116. }
  117.  
  118.  
  119. /**
  120. * ##Packet interpreter for starmade server.
  121. *
  122. *
  123. * \author Stefan Kent
  124. * \date 10/2013
  125. * \version 1.0
  126. *
  127. **/
  128. class server_packet
  129. {
  130. public $data; /**< The resulting array after conversion. */
  131.  
  132. /**
  133. * \brief Constructor
  134. *
  135. * This will take the stream and break it
  136. * into an array based on the datatype
  137. * in the order that it was recieved.
  138. *
  139. * \param string $buf Raw response from the server.
  140. * \return Null
  141. */
  142. public function __construct($buf)
  143. {
  144. $pointer = 6;
  145. while($pointer < strlen($buf))
  146. {
  147. $mark = ord(substr($buf,$pointer,1));
  148. $pointer++;
  149.  
  150. switch($mark)
  151. {
  152. case 1: //int
  153. list(,$unpacked) = unpack("N", substr($buf,$pointer,4));
  154. if($unpacked >= pow(2, 31)) $unpacked -= pow(2, 32);
  155. $pointer+=4;
  156. $this->data[] = array("type"=>"int","value"=>$unpacked);
  157.  
  158. break;
  159.  
  160. case 3: //float
  161. 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)));
  162. $pointer+=4;
  163. $this->data[] = array("type"=>"float","value"=>$value);
  164.  
  165. break;
  166.  
  167. case 4: //string
  168.  
  169. list(,$unpacked) = unpack("n", substr($buf,$pointer,2));
  170. if($unpacked >= pow(2, 15)) $unpacked -= pow(2, 16); // Convert unsigned short to signed short.
  171. $strlen = $unpacked;
  172. $pointer+=2;
  173.  
  174. $this->data[] = array("type"=>"string","value"=>substr($buf,$pointer,$strlen));
  175. $pointer+=$strlen;
  176.  
  177. break;
  178.  
  179. case 6: //Byte
  180. $this->data[] = array("type"=>"byte","value"=>ord(substr($buf,$pointer,1)));
  181. $pointer++;
  182.  
  183. break;
  184.  
  185. case 0: //null
  186. default:
  187. break;
  188. }
  189. }
  190. }
  191.  
  192. }
  193. /**@}*/
  194. ?>
Advertisement
Add Comment
Please, Sign In to add comment