ConnectionInterface.php 3.7 KB

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