Core.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /**
  20. * Core for the query mechanisms
  21. *
  22. * @author Austin Bischoff <[email protected]>
  23. */
  24. abstract class Core
  25. {
  26. /**
  27. * The socket used by this resource
  28. *
  29. * @type null|resource
  30. */
  31. public $socket = null;
  32. /**
  33. * The transport type (udp, tcp, etc...)
  34. * See http://php.net/manual/en/transports.php for the supported list
  35. *
  36. * @type string
  37. */
  38. protected $transport = null;
  39. /**
  40. * Connection IP address
  41. *
  42. * @type string
  43. */
  44. protected $ip = null;
  45. /**
  46. * Connection port
  47. *
  48. * @type int
  49. */
  50. protected $port = null;
  51. /**
  52. * The time in seconds to wait before timing out while connecting to the socket
  53. *
  54. * @type int
  55. */
  56. protected $timeout = 3; // Seconds
  57. /**
  58. * Socket is blocking?
  59. *
  60. * @type bool
  61. */
  62. protected $blocking = false;
  63. /**
  64. * Called when the class is cloned
  65. */
  66. public function __clone()
  67. {
  68. // Reset the properties for this class when cloned
  69. $this->reset();
  70. }
  71. /**
  72. * Set the connection information for the socket
  73. *
  74. * @param string $transport
  75. * @param string $ip
  76. * @param int $port
  77. * @param int $timeout seconds
  78. * @param bool $blocking
  79. */
  80. public function set($transport, $ip, $port, $timeout = 3, $blocking = false)
  81. {
  82. $this->transport = $transport;
  83. $this->ip = $ip;
  84. $this->port = $port;
  85. $this->timeout = $timeout;
  86. $this->blocking = $blocking;
  87. }
  88. /**
  89. * Reset this instance's properties
  90. */
  91. public function reset()
  92. {
  93. $this->transport = null;
  94. $this->ip = null;
  95. $this->port = null;
  96. $this->timeout = 3;
  97. $this->blocking = false;
  98. }
  99. public function getTransport()
  100. {
  101. return $this->transport;
  102. }
  103. public function getIp()
  104. {
  105. return $this->ip;
  106. }
  107. public function getPort()
  108. {
  109. return $this->port;
  110. }
  111. public function getTimeout()
  112. {
  113. return $this->timeout;
  114. }
  115. public function getBlocking()
  116. {
  117. return $this->blocking;
  118. }
  119. /**
  120. * Create a new socket
  121. *
  122. * @return void
  123. */
  124. abstract protected function create();
  125. /**
  126. * Get the socket
  127. *
  128. * @return mixed
  129. */
  130. abstract public function get();
  131. /**
  132. * Write data to the socket
  133. *
  134. * @param string $data
  135. *
  136. * @return int The number of bytes written
  137. */
  138. abstract public function write($data);
  139. /**
  140. * Close the socket
  141. *
  142. * @return void
  143. */
  144. abstract public function close();
  145. /**
  146. * Read the responses from the socket(s)
  147. *
  148. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  149. *
  150. * @param array $sockets
  151. * @param int $timeout
  152. * @param int $stream_timeout
  153. *
  154. * @return array
  155. */
  156. abstract public function getResponses(array $sockets, $timeout, $stream_timeout);
  157. }