ConnectionInterface.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. declare(strict_types=1);
  15. namespace Workerman\Connection;
  16. use Throwable;
  17. use Workerman\Events\Event;
  18. use Workerman\Events\EventInterface;
  19. use Workerman\Worker;
  20. /**
  21. * ConnectionInterface.
  22. */
  23. #[\AllowDynamicProperties]
  24. abstract class ConnectionInterface
  25. {
  26. /**
  27. * Connect failed.
  28. *
  29. * @var int
  30. */
  31. const CONNECT_FAIL = 1;
  32. /**
  33. * Send failed.
  34. *
  35. * @var int
  36. */
  37. const SEND_FAIL = 2;
  38. /**
  39. * Statistics for status command.
  40. *
  41. * @var array
  42. */
  43. public static array $statistics = [
  44. 'connection_count' => 0,
  45. 'total_request' => 0,
  46. 'throw_exception' => 0,
  47. 'send_fail' => 0,
  48. ];
  49. /**
  50. * Application layer protocol.
  51. * The format is like this Workerman\\Protocols\\Http.
  52. *
  53. * @var ?string
  54. */
  55. public ?string $protocol = null;
  56. /**
  57. * Emitted when data is received.
  58. *
  59. * @var ?callable
  60. */
  61. public $onMessage = null;
  62. /**
  63. * Emitted when the other end of the socket sends a FIN packet.
  64. *
  65. * @var ?callable
  66. */
  67. public $onClose = null;
  68. /**
  69. * Emitted when an error occurs with connection.
  70. *
  71. * @var ?callable
  72. */
  73. public $onError = null;
  74. /**
  75. * @var ?EventInterface
  76. */
  77. public ?EventInterface $eventLoop = null;
  78. /**
  79. * @var ?callable
  80. */
  81. public $errorHandler = null;
  82. /**
  83. * Sends data on the connection.
  84. *
  85. * @param mixed $sendBuffer
  86. * @param bool $raw
  87. * @return void|boolean
  88. */
  89. abstract public function send(mixed $sendBuffer, bool $raw = false);
  90. /**
  91. * Get remote IP.
  92. *
  93. * @return string
  94. */
  95. abstract public function getRemoteIp(): string;
  96. /**
  97. * Get remote port.
  98. *
  99. * @return int
  100. */
  101. abstract public function getRemotePort(): int;
  102. /**
  103. * Get remote address.
  104. *
  105. * @return string
  106. */
  107. abstract public function getRemoteAddress(): string;
  108. /**
  109. * Get local IP.
  110. *
  111. * @return string
  112. */
  113. abstract public function getLocalIp(): string;
  114. /**
  115. * Get local port.
  116. *
  117. * @return int
  118. */
  119. abstract public function getLocalPort(): int;
  120. /**
  121. * Get local address.
  122. *
  123. * @return string
  124. */
  125. abstract public function getLocalAddress(): string;
  126. /**
  127. * Close connection.
  128. *
  129. * @param mixed|null $data
  130. * @param bool $raw
  131. * @return void
  132. */
  133. abstract public function close(mixed $data = null, bool $raw = false): void;
  134. /**
  135. * Is ipv4.
  136. *
  137. * return bool.
  138. */
  139. abstract public function isIpV4(): bool;
  140. /**
  141. * Is ipv6.
  142. *
  143. * return bool.
  144. */
  145. abstract public function isIpV6(): bool;
  146. /**
  147. * @param Throwable $exception
  148. * @return void
  149. * @throws Throwable
  150. */
  151. public function error(Throwable $exception): void
  152. {
  153. if (!$this->errorHandler) {
  154. Worker::stopAll(250, $exception);
  155. return;
  156. }
  157. try {
  158. ($this->errorHandler)($exception);
  159. } catch (Throwable $exception) {
  160. if ($this->eventLoop instanceof Event) {
  161. echo $exception;
  162. return;
  163. }
  164. throw $exception;
  165. }
  166. }
  167. }