TcpConnection.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Workerman\Connection;
  15. use Workerman\Events\EventInterface;
  16. use Workerman\Worker;
  17. use Exception;
  18. /**
  19. * TcpConnection.
  20. */
  21. class TcpConnection extends ConnectionInterface
  22. {
  23. /**
  24. * Read buffer size.
  25. *
  26. * @var int
  27. */
  28. const READ_BUFFER_SIZE = 65535;
  29. /**
  30. * Status initial.
  31. *
  32. * @var int
  33. */
  34. const STATUS_INITIAL = 0;
  35. /**
  36. * Status connecting.
  37. *
  38. * @var int
  39. */
  40. const STATUS_CONNECTING = 1;
  41. /**
  42. * Status connection established.
  43. *
  44. * @var int
  45. */
  46. const STATUS_ESTABLISHED = 2;
  47. /**
  48. * Status closing.
  49. *
  50. * @var int
  51. */
  52. const STATUS_CLOSING = 4;
  53. /**
  54. * Status closed.
  55. *
  56. * @var int
  57. */
  58. const STATUS_CLOSED = 8;
  59. /**
  60. * Emitted when data is received.
  61. *
  62. * @var callback
  63. */
  64. public $onMessage = null;
  65. /**
  66. * Emitted when the other end of the socket sends a FIN packet.
  67. *
  68. * @var callback
  69. */
  70. public $onClose = null;
  71. /**
  72. * Emitted when an error occurs with connection.
  73. *
  74. * @var callback
  75. */
  76. public $onError = null;
  77. /**
  78. * Emitted when the send buffer becomes full.
  79. *
  80. * @var callback
  81. */
  82. public $onBufferFull = null;
  83. /**
  84. * Emitted when the send buffer becomes empty.
  85. *
  86. * @var callback
  87. */
  88. public $onBufferDrain = null;
  89. /**
  90. * Application layer protocol.
  91. * The format is like this Workerman\\Protocols\\Http.
  92. *
  93. * @var \Workerman\Protocols\ProtocolInterface
  94. */
  95. public $protocol = null;
  96. /**
  97. * Transport (tcp/udp/unix/ssl).
  98. *
  99. * @var string
  100. */
  101. public $transport = 'tcp';
  102. /**
  103. * Which worker belong to.
  104. *
  105. * @var Worker
  106. */
  107. public $worker = null;
  108. /**
  109. * Bytes read.
  110. *
  111. * @var int
  112. */
  113. public $bytesRead = 0;
  114. /**
  115. * Bytes written.
  116. *
  117. * @var int
  118. */
  119. public $bytesWritten = 0;
  120. /**
  121. * Connection->id.
  122. *
  123. * @var int
  124. */
  125. public $id = 0;
  126. /**
  127. * A copy of $worker->id which used to clean up the connection in worker->connections
  128. *
  129. * @var int
  130. */
  131. protected $_id = 0;
  132. /**
  133. * Sets the maximum send buffer size for the current connection.
  134. * OnBufferFull callback will be emited When the send buffer is full.
  135. *
  136. * @var int
  137. */
  138. public $maxSendBufferSize = 1048576;
  139. /**
  140. * Default send buffer size.
  141. *
  142. * @var int
  143. */
  144. public static $defaultMaxSendBufferSize = 1048576;
  145. /**
  146. * Maximum acceptable packet size.
  147. *
  148. * @var int
  149. */
  150. public static $maxPackageSize = 10485760;
  151. /**
  152. * Id recorder.
  153. *
  154. * @var int
  155. */
  156. protected static $_idRecorder = 1;
  157. /**
  158. * Socket
  159. *
  160. * @var resource
  161. */
  162. protected $_socket = null;
  163. /**
  164. * Send buffer.
  165. *
  166. * @var string
  167. */
  168. protected $_sendBuffer = '';
  169. /**
  170. * Receive buffer.
  171. *
  172. * @var string
  173. */
  174. protected $_recvBuffer = '';
  175. /**
  176. * Current package length.
  177. *
  178. * @var int
  179. */
  180. protected $_currentPackageLength = 0;
  181. /**
  182. * Connection status.
  183. *
  184. * @var int
  185. */
  186. protected $_status = self::STATUS_ESTABLISHED;
  187. /**
  188. * Remote address.
  189. *
  190. * @var string
  191. */
  192. protected $_remoteAddress = '';
  193. /**
  194. * Is paused.
  195. *
  196. * @var bool
  197. */
  198. protected $_isPaused = false;
  199. /**
  200. * SSL handshake completed or not.
  201. *
  202. * @var bool
  203. */
  204. protected $_sslHandshakeCompleted = false;
  205. /**
  206. * All connection instances.
  207. *
  208. * @var array
  209. */
  210. public static $connections = array();
  211. /**
  212. * Status to string.
  213. *
  214. * @var array
  215. */
  216. public static $_statusToString = array(
  217. self::STATUS_INITIAL => 'INITIAL',
  218. self::STATUS_CONNECTING => 'CONNECTING',
  219. self::STATUS_ESTABLISHED => 'ESTABLISHED',
  220. self::STATUS_CLOSING => 'CLOSING',
  221. self::STATUS_CLOSED => 'CLOSED',
  222. );
  223. /**
  224. * Construct.
  225. *
  226. * @param resource $socket
  227. * @param string $remote_address
  228. */
  229. public function __construct($socket, $remote_address = '')
  230. {
  231. self::$statistics['connection_count']++;
  232. $this->id = $this->_id = self::$_idRecorder++;
  233. $this->_socket = $socket;
  234. stream_set_blocking($this->_socket, 0);
  235. // Compatible with hhvm
  236. if (function_exists('stream_set_read_buffer')) {
  237. stream_set_read_buffer($this->_socket, 0);
  238. }
  239. Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
  240. $this->maxSendBufferSize = self::$defaultMaxSendBufferSize;
  241. $this->_remoteAddress = $remote_address;
  242. static::$connections[$this->id] = $this;
  243. }
  244. /**
  245. * Get status.
  246. *
  247. * @param bool $raw_output
  248. *
  249. * @return int
  250. */
  251. public function getStatus($raw_output = true)
  252. {
  253. if ($raw_output) {
  254. return $this->_status;
  255. }
  256. return self::$_statusToString[$this->_status];
  257. }
  258. /**
  259. * Sends data on the connection.
  260. *
  261. * @param string $send_buffer
  262. * @param bool $raw
  263. * @return void|bool|null
  264. */
  265. public function send($send_buffer, $raw = false)
  266. {
  267. if ($this->_status === self::STATUS_CLOSING || $this->_status === self::STATUS_CLOSED) {
  268. return false;
  269. }
  270. // Try to call protocol::encode($send_buffer) before sending.
  271. if (false === $raw && $this->protocol !== null) {
  272. $parser = $this->protocol;
  273. $send_buffer = $parser::encode($send_buffer, $this);
  274. if ($send_buffer === '') {
  275. return null;
  276. }
  277. }
  278. if ($this->_status !== self::STATUS_ESTABLISHED ||
  279. ($this->transport === 'ssl' && $this->_sslHandshakeCompleted !== true)
  280. ) {
  281. if ($this->_sendBuffer) {
  282. if ($this->bufferIsFull()) {
  283. self::$statistics['send_fail']++;
  284. return false;
  285. }
  286. }
  287. $this->_sendBuffer .= $send_buffer;
  288. $this->checkBufferWillFull();
  289. return null;
  290. }
  291. // Attempt to send data directly.
  292. if ($this->_sendBuffer === '') {
  293. $len = @fwrite($this->_socket, $send_buffer, 8192);
  294. // send successful.
  295. if ($len === strlen($send_buffer)) {
  296. $this->bytesWritten += $len;
  297. return true;
  298. }
  299. // Send only part of the data.
  300. if ($len > 0) {
  301. $this->_sendBuffer = substr($send_buffer, $len);
  302. $this->bytesWritten += $len;
  303. } else {
  304. // Connection closed?
  305. if (!is_resource($this->_socket) || feof($this->_socket)) {
  306. self::$statistics['send_fail']++;
  307. if ($this->onError) {
  308. try {
  309. call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'client closed');
  310. } catch (\Exception $e) {
  311. Worker::log($e);
  312. exit(250);
  313. } catch (\Error $e) {
  314. Worker::log($e);
  315. exit(250);
  316. }
  317. }
  318. $this->destroy();
  319. return false;
  320. }
  321. $this->_sendBuffer = $send_buffer;
  322. }
  323. Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
  324. // Check if the send buffer will be full.
  325. $this->checkBufferWillFull();
  326. return null;
  327. } else {
  328. if ($this->bufferIsFull()) {
  329. self::$statistics['send_fail']++;
  330. return false;
  331. }
  332. $this->_sendBuffer .= $send_buffer;
  333. // Check if the send buffer is full.
  334. $this->checkBufferWillFull();
  335. }
  336. }
  337. /**
  338. * Get remote IP.
  339. *
  340. * @return string
  341. */
  342. public function getRemoteIp()
  343. {
  344. $pos = strrpos($this->_remoteAddress, ':');
  345. if ($pos) {
  346. return substr($this->_remoteAddress, 0, $pos);
  347. }
  348. return '';
  349. }
  350. /**
  351. * Get remote port.
  352. *
  353. * @return int
  354. */
  355. public function getRemotePort()
  356. {
  357. if ($this->_remoteAddress) {
  358. return (int)substr(strrchr($this->_remoteAddress, ':'), 1);
  359. }
  360. return 0;
  361. }
  362. /**
  363. * Get remote address.
  364. *
  365. * @return string
  366. */
  367. public function getRemoteAddress()
  368. {
  369. return $this->_remoteAddress;
  370. }
  371. /**
  372. * Get local IP.
  373. *
  374. * @return string
  375. */
  376. public function getLocalIp()
  377. {
  378. $address = $this->getLocalAddress();
  379. $pos = strrpos($address, ':');
  380. if (!$pos) {
  381. return '';
  382. }
  383. return substr($address, 0, $pos);
  384. }
  385. /**
  386. * Get local port.
  387. *
  388. * @return int
  389. */
  390. public function getLocalPort()
  391. {
  392. $address = $this->getLocalAddress();
  393. $pos = strrpos($address, ':');
  394. if (!$pos) {
  395. return 0;
  396. }
  397. return (int)substr(strrchr($address, ':'), 1);
  398. }
  399. /**
  400. * Get local address.
  401. *
  402. * @return string
  403. */
  404. public function getLocalAddress()
  405. {
  406. return (string)@stream_socket_get_name($this->_socket, false);
  407. }
  408. /**
  409. * Get send buffer queue size.
  410. *
  411. * @return integer
  412. */
  413. public function getSendBufferQueueSize()
  414. {
  415. return strlen($this->_sendBuffer);
  416. }
  417. /**
  418. * Get recv buffer queue size.
  419. *
  420. * @return integer
  421. */
  422. public function getRecvBufferQueueSize()
  423. {
  424. return strlen($this->_recvBuffer);
  425. }
  426. /**
  427. * Is ipv4.
  428. *
  429. * return bool.
  430. */
  431. public function isIpV4()
  432. {
  433. if ($this->transport === 'unix') {
  434. return false;
  435. }
  436. return strpos($this->getRemoteIp(), ':') === false;
  437. }
  438. /**
  439. * Is ipv6.
  440. *
  441. * return bool.
  442. */
  443. public function isIpV6()
  444. {
  445. if ($this->transport === 'unix') {
  446. return false;
  447. }
  448. return strpos($this->getRemoteIp(), ':') !== false;
  449. }
  450. /**
  451. * Pauses the reading of data. That is onMessage will not be emitted. Useful to throttle back an upload.
  452. *
  453. * @return void
  454. */
  455. public function pauseRecv()
  456. {
  457. Worker::$globalEvent->del($this->_socket, EventInterface::EV_READ);
  458. $this->_isPaused = true;
  459. }
  460. /**
  461. * Resumes reading after a call to pauseRecv.
  462. *
  463. * @return void
  464. */
  465. public function resumeRecv()
  466. {
  467. if ($this->_isPaused === true) {
  468. Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
  469. $this->_isPaused = false;
  470. $this->baseRead($this->_socket, false);
  471. }
  472. }
  473. /**
  474. * Base read handler.
  475. *
  476. * @param resource $socket
  477. * @param bool $check_eof
  478. * @return void
  479. */
  480. public function baseRead($socket, $check_eof = true)
  481. {
  482. // SSL handshake.
  483. if ($this->transport === 'ssl' && $this->_sslHandshakeCompleted !== true) {
  484. $ret = stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv2_SERVER |
  485. STREAM_CRYPTO_METHOD_SSLv3_SERVER | STREAM_CRYPTO_METHOD_SSLv23_SERVER);
  486. // Negotiation has failed.
  487. if(false === $ret) {
  488. if (!feof($socket)) {
  489. echo "\nSSL Handshake fail. \nBuffer:".bin2hex(fread($socket, 8182))."\n";
  490. }
  491. return $this->destroy();
  492. } elseif(0 === $ret) {
  493. // There isn't enough data and should try again.
  494. return;
  495. }
  496. if (isset($this->onSslHandshake)) {
  497. try {
  498. call_user_func($this->onSslHandshake, $this);
  499. } catch (\Exception $e) {
  500. Worker::log($e);
  501. exit(250);
  502. } catch (\Error $e) {
  503. Worker::log($e);
  504. exit(250);
  505. }
  506. }
  507. $this->_sslHandshakeCompleted = true;
  508. if ($this->_sendBuffer) {
  509. Worker::$globalEvent->add($socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
  510. }
  511. return;
  512. }
  513. $buffer = @fread($socket, self::READ_BUFFER_SIZE);
  514. // Check connection closed.
  515. if ($buffer === '' || $buffer === false) {
  516. if ($check_eof && (feof($socket) || !is_resource($socket) || $buffer === false)) {
  517. $this->destroy();
  518. return;
  519. }
  520. } else {
  521. $this->bytesRead += strlen($buffer);
  522. $this->_recvBuffer .= $buffer;
  523. }
  524. // If the application layer protocol has been set up.
  525. if ($this->protocol !== null) {
  526. $parser = $this->protocol;
  527. while ($this->_recvBuffer !== '' && !$this->_isPaused) {
  528. // The current packet length is known.
  529. if ($this->_currentPackageLength) {
  530. // Data is not enough for a package.
  531. if ($this->_currentPackageLength > strlen($this->_recvBuffer)) {
  532. break;
  533. }
  534. } else {
  535. // Get current package length.
  536. $this->_currentPackageLength = $parser::input($this->_recvBuffer, $this);
  537. // The packet length is unknown.
  538. if ($this->_currentPackageLength === 0) {
  539. break;
  540. } elseif ($this->_currentPackageLength > 0 && $this->_currentPackageLength <= self::$maxPackageSize) {
  541. // Data is not enough for a package.
  542. if ($this->_currentPackageLength > strlen($this->_recvBuffer)) {
  543. break;
  544. }
  545. } // Wrong package.
  546. else {
  547. echo 'error package. package_length=' . var_export($this->_currentPackageLength, true);
  548. $this->destroy();
  549. return;
  550. }
  551. }
  552. // The data is enough for a packet.
  553. self::$statistics['total_request']++;
  554. // The current packet length is equal to the length of the buffer.
  555. if (strlen($this->_recvBuffer) === $this->_currentPackageLength) {
  556. $one_request_buffer = $this->_recvBuffer;
  557. $this->_recvBuffer = '';
  558. } else {
  559. // Get a full package from the buffer.
  560. $one_request_buffer = substr($this->_recvBuffer, 0, $this->_currentPackageLength);
  561. // Remove the current package from the receive buffer.
  562. $this->_recvBuffer = substr($this->_recvBuffer, $this->_currentPackageLength);
  563. }
  564. // Reset the current packet length to 0.
  565. $this->_currentPackageLength = 0;
  566. if (!$this->onMessage) {
  567. continue;
  568. }
  569. try {
  570. // Decode request buffer before Emitting onMessage callback.
  571. call_user_func($this->onMessage, $this, $parser::decode($one_request_buffer, $this));
  572. } catch (\Exception $e) {
  573. Worker::log($e);
  574. exit(250);
  575. } catch (\Error $e) {
  576. Worker::log($e);
  577. exit(250);
  578. }
  579. }
  580. return;
  581. }
  582. if ($this->_recvBuffer === '' || $this->_isPaused) {
  583. return;
  584. }
  585. // Applications protocol is not set.
  586. self::$statistics['total_request']++;
  587. if (!$this->onMessage) {
  588. $this->_recvBuffer = '';
  589. return;
  590. }
  591. try {
  592. call_user_func($this->onMessage, $this, $this->_recvBuffer);
  593. } catch (\Exception $e) {
  594. Worker::log($e);
  595. exit(250);
  596. } catch (\Error $e) {
  597. Worker::log($e);
  598. exit(250);
  599. }
  600. // Clean receive buffer.
  601. $this->_recvBuffer = '';
  602. }
  603. /**
  604. * Base write handler.
  605. *
  606. * @return void|bool
  607. */
  608. public function baseWrite()
  609. {
  610. $len = @fwrite($this->_socket, $this->_sendBuffer, 8192);
  611. if ($len === strlen($this->_sendBuffer)) {
  612. $this->bytesWritten += $len;
  613. Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
  614. $this->_sendBuffer = '';
  615. // Try to emit onBufferDrain callback when the send buffer becomes empty.
  616. if ($this->onBufferDrain) {
  617. try {
  618. call_user_func($this->onBufferDrain, $this);
  619. } catch (\Exception $e) {
  620. Worker::log($e);
  621. exit(250);
  622. } catch (\Error $e) {
  623. Worker::log($e);
  624. exit(250);
  625. }
  626. }
  627. if ($this->_status === self::STATUS_CLOSING) {
  628. $this->destroy();
  629. }
  630. return true;
  631. }
  632. if ($len > 0) {
  633. $this->bytesWritten += $len;
  634. $this->_sendBuffer = substr($this->_sendBuffer, $len);
  635. } else {
  636. self::$statistics['send_fail']++;
  637. $this->destroy();
  638. }
  639. }
  640. /**
  641. * This method pulls all the data out of a readable stream, and writes it to the supplied destination.
  642. *
  643. * @param TcpConnection $dest
  644. * @return void
  645. */
  646. public function pipe($dest)
  647. {
  648. $source = $this;
  649. $this->onMessage = function ($source, $data) use ($dest) {
  650. $dest->send($data);
  651. };
  652. $this->onClose = function ($source) use ($dest) {
  653. $dest->destroy();
  654. };
  655. $dest->onBufferFull = function ($dest) use ($source) {
  656. $source->pauseRecv();
  657. };
  658. $dest->onBufferDrain = function ($dest) use ($source) {
  659. $source->resumeRecv();
  660. };
  661. }
  662. /**
  663. * Remove $length of data from receive buffer.
  664. *
  665. * @param int $length
  666. * @return void
  667. */
  668. public function consumeRecvBuffer($length)
  669. {
  670. $this->_recvBuffer = substr($this->_recvBuffer, $length);
  671. }
  672. /**
  673. * Close connection.
  674. *
  675. * @param mixed $data
  676. * @param bool $raw
  677. * @return void
  678. */
  679. public function close($data = null, $raw = false)
  680. {
  681. if ($this->_status === self::STATUS_CLOSING || $this->_status === self::STATUS_CLOSED) {
  682. return;
  683. } else {
  684. if ($data !== null) {
  685. $this->send($data, $raw);
  686. }
  687. $this->_status = self::STATUS_CLOSING;
  688. }
  689. if ($this->_sendBuffer === '') {
  690. $this->destroy();
  691. }
  692. }
  693. /**
  694. * Get the real socket.
  695. *
  696. * @return resource
  697. */
  698. public function getSocket()
  699. {
  700. return $this->_socket;
  701. }
  702. /**
  703. * Check whether the send buffer will be full.
  704. *
  705. * @return void
  706. */
  707. protected function checkBufferWillFull()
  708. {
  709. if ($this->maxSendBufferSize <= strlen($this->_sendBuffer)) {
  710. if ($this->onBufferFull) {
  711. try {
  712. call_user_func($this->onBufferFull, $this);
  713. } catch (\Exception $e) {
  714. Worker::log($e);
  715. exit(250);
  716. } catch (\Error $e) {
  717. Worker::log($e);
  718. exit(250);
  719. }
  720. }
  721. }
  722. }
  723. /**
  724. * Whether send buffer is full.
  725. *
  726. * @return bool
  727. */
  728. protected function bufferIsFull()
  729. {
  730. // Buffer has been marked as full but still has data to send then the packet is discarded.
  731. if ($this->maxSendBufferSize <= strlen($this->_sendBuffer)) {
  732. if ($this->onError) {
  733. try {
  734. call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
  735. } catch (\Exception $e) {
  736. Worker::log($e);
  737. exit(250);
  738. } catch (\Error $e) {
  739. Worker::log($e);
  740. exit(250);
  741. }
  742. }
  743. return true;
  744. }
  745. return false;
  746. }
  747. /**
  748. * Destroy connection.
  749. *
  750. * @return void
  751. */
  752. public function destroy()
  753. {
  754. // Avoid repeated calls.
  755. if ($this->_status === self::STATUS_CLOSED) {
  756. return;
  757. }
  758. // Remove event listener.
  759. Worker::$globalEvent->del($this->_socket, EventInterface::EV_READ);
  760. Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
  761. // Close socket.
  762. @fclose($this->_socket);
  763. // Remove from worker->connections.
  764. if ($this->worker) {
  765. unset($this->worker->connections[$this->_id]);
  766. }
  767. unset(static::$connections[$this->_id]);
  768. $this->_status = self::STATUS_CLOSED;
  769. // Try to emit onClose callback.
  770. if ($this->onClose) {
  771. try {
  772. call_user_func($this->onClose, $this);
  773. } catch (\Exception $e) {
  774. Worker::log($e);
  775. exit(250);
  776. } catch (\Error $e) {
  777. Worker::log($e);
  778. exit(250);
  779. }
  780. }
  781. // Try to emit protocol::onClose
  782. if (method_exists($this->protocol, 'onClose')) {
  783. try {
  784. call_user_func(array($this->protocol, 'onClose'), $this);
  785. } catch (\Exception $e) {
  786. Worker::log($e);
  787. exit(250);
  788. } catch (\Error $e) {
  789. Worker::log($e);
  790. exit(250);
  791. }
  792. }
  793. if ($this->_status === self::STATUS_CLOSED) {
  794. // Cleaning up the callback to avoid memory leaks.
  795. $this->onMessage = $this->onClose = $this->onError = $this->onBufferFull = $this->onBufferDrain = null;
  796. }
  797. }
  798. /**
  799. * Destruct.
  800. *
  801. * @return void
  802. */
  803. public function __destruct()
  804. {
  805. self::$statistics['connection_count']--;
  806. }
  807. }