Native.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * This file is part of GameQ.
  4. *
  5. * GameQ is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GameQ is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. namespace GameQ\Query;
  19. use GameQ\Exception\Query as Exception;
  20. /**
  21. * Native way of querying servers
  22. *
  23. * @author Austin Bischoff <[email protected]>
  24. */
  25. class Native extends Core
  26. {
  27. /**
  28. * Get the current socket or create one and return
  29. *
  30. * @return resource|null
  31. * @throws \GameQ\Exception\Query
  32. */
  33. public function get()
  34. {
  35. // No socket for this server, make one
  36. if (is_null($this->socket)) {
  37. $this->create();
  38. }
  39. return $this->socket;
  40. }
  41. /**
  42. * Write data to the socket
  43. *
  44. * @param string $data
  45. *
  46. * @return int The number of bytes written
  47. * @throws \GameQ\Exception\Query
  48. */
  49. public function write($data)
  50. {
  51. try {
  52. // No socket for this server, make one
  53. if (is_null($this->socket)) {
  54. $this->create();
  55. }
  56. // Send the packet
  57. return fwrite($this->socket, $data);
  58. } catch (\Exception $e) {
  59. throw new Exception($e->getMessage(), $e->getCode(), $e);
  60. }
  61. }
  62. /**
  63. * Close the current socket
  64. */
  65. public function close()
  66. {
  67. if ($this->socket) {
  68. fclose($this->socket);
  69. $this->socket = null;
  70. }
  71. }
  72. /**
  73. * Create a new socket for this query
  74. *
  75. * @throws \GameQ\Exception\Query
  76. */
  77. protected function create()
  78. {
  79. // Create the remote address
  80. $remote_addr = sprintf("%s://%s:%d", $this->transport, $this->ip, $this->port);
  81. // Create context
  82. $context = stream_context_create([
  83. 'socket' => [
  84. 'bindto' => '0:0', // Bind to any available IP and OS decided port
  85. ],
  86. ]);
  87. // Define these first
  88. $errno = null;
  89. $errstr = null;
  90. // Create the socket
  91. if (($this->socket =
  92. @stream_socket_client($remote_addr, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $context))
  93. !== false
  94. ) {
  95. // Set the read timeout on the streams
  96. stream_set_timeout($this->socket, $this->timeout);
  97. // Set blocking mode
  98. stream_set_blocking($this->socket, $this->blocking);
  99. } else {
  100. // Reset socket
  101. $this->socket = null;
  102. // Something bad happened, throw query exception
  103. throw new Exception(
  104. __METHOD__ . " - Error creating socket to server {$this->ip}:{$this->port}. Error: " . $errstr,
  105. $errno
  106. );
  107. }
  108. }
  109. /**
  110. * Pull the responses out of the stream
  111. *
  112. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  113. * @SuppressWarnings(PHPMD.NPathComplexity)
  114. *
  115. * @param array $sockets
  116. * @param int $timeout
  117. * @param int $stream_timeout
  118. *
  119. * @return array Raw responses
  120. */
  121. public function getResponses(array $sockets, $timeout, $stream_timeout)
  122. {
  123. // Set the loop to active
  124. $loop_active = true;
  125. // Will hold the responses read from the sockets
  126. $responses = [];
  127. // To store the sockets
  128. $sockets_tmp = [];
  129. // Loop and pull out all the actual sockets we need to listen on
  130. foreach ($sockets as $socket_id => $socket_data) {
  131. // Get the socket
  132. /* @var $socket \GameQ\Query\Core */
  133. $socket = $socket_data['socket'];
  134. // Append the actual socket we are listening to
  135. $sockets_tmp[$socket_id] = $socket->get();
  136. unset($socket);
  137. }
  138. // Init some variables
  139. $read = $sockets_tmp;
  140. $write = null;
  141. $except = null;
  142. // Check to see if $read is empty, if so stream_select() will throw a warning
  143. if (empty($read)) {
  144. return $responses;
  145. }
  146. // This is when it should stop
  147. $time_stop = microtime(true) + $timeout;
  148. // Let's loop until we break something.
  149. while ($loop_active && microtime(true) < $time_stop) {
  150. // Check to make sure $read is not empty, if so we are done
  151. if (empty($read)) {
  152. break;
  153. }
  154. // Now lets listen for some streams, but do not cross the streams!
  155. $streams = stream_select($read, $write, $except, 0, $stream_timeout);
  156. // We had error or no streams left, kill the loop
  157. if ($streams === false || ($streams <= 0)) {
  158. break;
  159. }
  160. // Loop the sockets that received data back
  161. foreach ($read as $socket) {
  162. /* @var $socket resource */
  163. // See if we have a response
  164. if (($response = fread($socket, 8192)) === false) {
  165. continue; // No response yet so lets continue.
  166. }
  167. // Check to see if the response is empty, if so we are done with this server
  168. if (strlen($response) == 0) {
  169. // Remove this server from any future read loops
  170. unset($sockets_tmp[(int)$socket]);
  171. continue;
  172. }
  173. // Add the response we got back
  174. $responses[(int)$socket][] = $response;
  175. }
  176. // Because stream_select modifies read we need to reset it each time to the original array of sockets
  177. $read = $sockets_tmp;
  178. }
  179. // Free up some memory
  180. unset($streams, $read, $write, $except, $sockets_tmp, $time_stop, $response);
  181. // Return all of the responses, may be empty if something went wrong
  182. return $responses;
  183. }
  184. }