Server.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Socket;
  36. use Hoa\Consistency;
  37. use Hoa\Stream;
  38. /**
  39. * Class \Hoa\Socket\Server.
  40. *
  41. * Established a server connection.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Server extends Connection
  47. {
  48. /**
  49. * Tell a stream to bind to the specified target.
  50. *
  51. * @const int
  52. */
  53. const BIND = STREAM_SERVER_BIND;
  54. /**
  55. * Tell a stream to start listening on the socket.
  56. *
  57. * @const int
  58. */
  59. const LISTEN = STREAM_SERVER_LISTEN;
  60. /**
  61. * Encryption: SSLv2.
  62. *
  63. * @const int
  64. */
  65. const ENCRYPTION_SSLv2 = STREAM_CRYPTO_METHOD_SSLv2_SERVER;
  66. /**
  67. * Encryption: SSLv3.
  68. *
  69. * @const int
  70. */
  71. const ENCRYPTION_SSLv3 = STREAM_CRYPTO_METHOD_SSLv3_SERVER;
  72. /**
  73. * Encryption: SSLv2.3.
  74. *
  75. * @const int
  76. */
  77. const ENCRYPTION_SSLv23 = STREAM_CRYPTO_METHOD_SSLv23_SERVER;
  78. /**
  79. * Encryption: TLS.
  80. *
  81. * @const int
  82. */
  83. const ENCRYPTION_TLS = STREAM_CRYPTO_METHOD_TLS_SERVER;
  84. /**
  85. * Encryption: TLSv1.0.
  86. *
  87. * @const int
  88. */
  89. const ENCRYPTION_TLSv1_0 = STREAM_CRYPTO_METHOD_TLSv1_0_SERVER;
  90. /**
  91. * Encryption: TLSv1.1.
  92. *
  93. * @const int
  94. */
  95. const ENCRYPTION_TLSv1_1 = STREAM_CRYPTO_METHOD_TLSv1_1_SERVER;
  96. /**
  97. * Encryption: TLSv1.2.
  98. *
  99. * @const int
  100. */
  101. const ENCRYPTION_TLSv1_2 = STREAM_CRYPTO_METHOD_TLSv1_2_SERVER;
  102. /**
  103. * Encryption: ANY
  104. *
  105. * @const int
  106. */
  107. const ENCRYPTION_ANY = STREAM_CRYPTO_METHOD_ANY_SERVER;
  108. /**
  109. * Master connection.
  110. *
  111. * @var resource
  112. */
  113. protected $_master = null;
  114. /**
  115. * All considered server.
  116. *
  117. * @var array
  118. */
  119. protected $_servers = [];
  120. /**
  121. * Masters connection.
  122. *
  123. * @var array
  124. */
  125. protected $_masters = [];
  126. /**
  127. * Stack of connections.
  128. *
  129. * @var array
  130. */
  131. protected $_stack = [];
  132. /**
  133. * Start a connection.
  134. *
  135. * @param string $socket Socket URI.
  136. * @param int $timeout Timeout.
  137. * @param int $flag Flag, see the child::* constants.
  138. * @param string $context Context ID (please, see the
  139. * \Hoa\Stream\Context class).
  140. * @throws \Hoa\Socket\Exception
  141. */
  142. public function __construct(
  143. $socket,
  144. $timeout = 30,
  145. $flag = -1,
  146. $context = null
  147. ) {
  148. $this->setSocket($socket);
  149. $socket = $this->getSocket();
  150. if ($flag == -1) {
  151. switch ($socket->getTransport()) {
  152. case 'tcp':
  153. $flag = self::BIND | self::LISTEN;
  154. break;
  155. case 'udp':
  156. $flag = self::BIND;
  157. break;
  158. }
  159. } else {
  160. switch ($socket->getTransport()) {
  161. case 'tcp':
  162. $flag |= self::LISTEN;
  163. break;
  164. case 'udp':
  165. if ($flag & self::LISTEN) {
  166. throw new Exception(
  167. 'Cannot use the flag ' .
  168. '\Hoa\Socket\Server::LISTEN ' .
  169. 'for connect-less transports (such as UDP).',
  170. 0
  171. );
  172. }
  173. $flag = self::BIND;
  174. break;
  175. }
  176. }
  177. parent::__construct(null, $timeout, $flag, $context);
  178. return;
  179. }
  180. /**
  181. * Open the stream and return the associated resource.
  182. *
  183. * @param string $streamName Socket URI.
  184. * @param \Hoa\Stream\Context $context Context.
  185. * @return resource
  186. * @throws \Hoa\Socket\Exception
  187. */
  188. protected function &_open($streamName, Stream\Context $context = null)
  189. {
  190. if (null === $context) {
  191. $this->_master = @stream_socket_server(
  192. $streamName,
  193. $errno,
  194. $errstr,
  195. $this->getFlag()
  196. );
  197. } else {
  198. $this->_master = @stream_socket_server(
  199. $streamName,
  200. $errno,
  201. $errstr,
  202. $this->getFlag(),
  203. $context->getContext()
  204. );
  205. }
  206. if (false === $this->_master) {
  207. throw new Exception(
  208. 'Server cannot join %s and returns an error (number %d): %s.',
  209. 1,
  210. [$streamName, $errno, $errstr]
  211. );
  212. }
  213. $i = count($this->_masters);
  214. $this->_masters[$i] = $this->_master;
  215. $this->_servers[$i] = $this;
  216. $this->_stack[] = $this->_masters[$i];
  217. return $this->_master;
  218. }
  219. /**
  220. * Close the current stream.
  221. *
  222. * @return bool
  223. */
  224. protected function _close()
  225. {
  226. $current = $this->getStream();
  227. if (false === in_array($current, $this->getMasters(), true)) {
  228. $stack = &$this->getStack();
  229. $i = array_search($current, $stack);
  230. if (false !== $i) {
  231. unset($stack[$i]);
  232. }
  233. // $this->_node is voluntary kept in memory until a new node will be
  234. // used.
  235. unset($this->_nodes[$this->getNodeId($current)]);
  236. @fclose($current);
  237. // Closing slave does not have the same effect that closing master.
  238. return false;
  239. }
  240. return (bool) (@fclose($this->_master) + @fclose($this->getStream()));
  241. }
  242. /**
  243. * Connect and accept the first connection.
  244. *
  245. * @return \Hoa\Socket\Server
  246. * @throws \Hoa\Socket\Exception
  247. */
  248. public function connect()
  249. {
  250. parent::connect();
  251. $client = @stream_socket_accept($this->_master);
  252. if (false === $client) {
  253. throw new Exception(
  254. 'Operation timed out (nothing to accept).',
  255. 2
  256. );
  257. }
  258. $this->_setStream($client);
  259. return $this;
  260. }
  261. /**
  262. * Connect but wait for select and accept new connections.
  263. *
  264. * @return \Hoa\Socket\Server
  265. */
  266. public function connectAndWait()
  267. {
  268. return parent::connect();
  269. }
  270. /**
  271. * Select connections.
  272. *
  273. * @return \Hoa\Socket\Server
  274. * @throws \Hoa\Socket\Exception
  275. */
  276. public function select()
  277. {
  278. $read = $this->getStack();
  279. $write = null;
  280. $except = null;
  281. @stream_select($read, $write, $except, $this->getTimeout(), 0);
  282. foreach ($read as $socket) {
  283. $masters = $this->getMasters();
  284. if (true === in_array($socket, $masters, true)) {
  285. $client = @stream_socket_accept($socket);
  286. if (false === $client) {
  287. throw new Exception(
  288. 'Operation timed out (nothing to accept).',
  289. 3
  290. );
  291. }
  292. $m = array_search($socket, $masters, true);
  293. $server = $this->_servers[$m];
  294. $id = $this->getNodeId($client);
  295. $node = Consistency\Autoloader::dnew(
  296. $server->getNodeName(),
  297. [$id, $client, $server]
  298. );
  299. $this->_nodes[$id] = $node;
  300. $this->_stack[] = $client;
  301. } else {
  302. $this->_iterator[] = $socket;
  303. }
  304. }
  305. return $this;
  306. }
  307. /**
  308. * Consider another server when selecting connection.
  309. *
  310. * @param \Hoa\Socket\Connection $other Other server.
  311. * @return \Hoa\Socket\Server
  312. */
  313. public function consider(Connection $other)
  314. {
  315. if ($other instanceof Client) {
  316. if (true === $other->isDisconnected()) {
  317. $other->connect();
  318. }
  319. $this->_stack[] = $other->getStream();
  320. return $this;
  321. }
  322. if (true === $other->isDisconnected()) {
  323. $other->connectAndWait();
  324. }
  325. $i = count($this->_masters);
  326. $this->_masters[$i] = $other->_master;
  327. $this->_servers[$i] = $other;
  328. $this->_stack[] = $this->_masters[$i];
  329. return $this;
  330. }
  331. /**
  332. * Check if the current node belongs to a specific server.
  333. *
  334. * @param \Hoa\Socket\Connection $server Server.
  335. * @return bool
  336. */
  337. public function is(Connection $server)
  338. {
  339. return $this->getCurrentNode()->getConnection() === $server;
  340. }
  341. /**
  342. * Set and get the current selected connection.
  343. *
  344. * @return \Hoa\Socket\Node
  345. */
  346. public function current()
  347. {
  348. $current = parent::_current();
  349. $id = $this->getNodeId($current);
  350. if (!isset($this->_nodes[$id])) {
  351. return $current;
  352. }
  353. return $this->_node = $this->_nodes[$this->getNodeId($current)];
  354. }
  355. /**
  356. * Check if the server bind or not.
  357. *
  358. * @return bool
  359. */
  360. public function isBinding()
  361. {
  362. return (bool) ($this->getFlag() & self::BIND);
  363. }
  364. /**
  365. * Check if the server is listening or not.
  366. *
  367. * @return bool
  368. */
  369. public function isListening()
  370. {
  371. return (bool) ($this->getFlag() & self::LISTEN);
  372. }
  373. /**
  374. * Return internal considered servers.
  375. *
  376. * @return array
  377. */
  378. protected function getServers()
  379. {
  380. return $this->_servers;
  381. }
  382. /**
  383. * Return internal master connections.
  384. *
  385. * @return array
  386. */
  387. protected function getMasters()
  388. {
  389. return $this->_masters;
  390. }
  391. /**
  392. * Return internal node stack.
  393. *
  394. * @return array
  395. */
  396. protected function &getStack()
  397. {
  398. return $this->_stack;
  399. }
  400. }