UdpConnection.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. /**
  16. * UdpConnection.
  17. */
  18. class UdpConnection extends ConnectionInterface implements \JsonSerializable
  19. {
  20. /**
  21. * Transport layer protocol.
  22. *
  23. * @var string
  24. */
  25. public string $transport = 'udp';
  26. /**
  27. * Udp socket.
  28. *
  29. * @var resource
  30. */
  31. protected $socket;
  32. /**
  33. * Remote address.
  34. *
  35. * @var string
  36. */
  37. protected string $remoteAddress = '';
  38. /**
  39. * Construct.
  40. *
  41. * @param resource $socket
  42. * @param string $remoteAddress
  43. */
  44. public function __construct($socket, string $remoteAddress)
  45. {
  46. $this->socket = $socket;
  47. $this->remoteAddress = $remoteAddress;
  48. }
  49. /**
  50. * Sends data on the connection.
  51. *
  52. * @param mixed $sendBuffer
  53. * @param bool $raw
  54. * @return void|boolean
  55. */
  56. public function send(mixed $sendBuffer, bool $raw = false)
  57. {
  58. if (false === $raw && $this->protocol) {
  59. $parser = $this->protocol;
  60. $sendBuffer = $parser::encode($sendBuffer, $this);
  61. if ($sendBuffer === '') {
  62. return;
  63. }
  64. }
  65. return \strlen($sendBuffer) === \stream_socket_sendto($this->socket, $sendBuffer, 0, $this->isIpV6() ? '[' . $this->getRemoteIp() . ']:' . $this->getRemotePort() : $this->remoteAddress);
  66. }
  67. /**
  68. * Get remote IP.
  69. *
  70. * @return string
  71. */
  72. public function getRemoteIp(): string
  73. {
  74. $pos = \strrpos($this->remoteAddress, ':');
  75. if ($pos) {
  76. return \trim(\substr($this->remoteAddress, 0, $pos), '[]');
  77. }
  78. return '';
  79. }
  80. /**
  81. * Get remote port.
  82. *
  83. * @return int
  84. */
  85. public function getRemotePort(): int
  86. {
  87. if ($this->remoteAddress) {
  88. return (int)\substr(\strrchr($this->remoteAddress, ':'), 1);
  89. }
  90. return 0;
  91. }
  92. /**
  93. * Get remote address.
  94. *
  95. * @return string
  96. */
  97. public function getRemoteAddress(): string
  98. {
  99. return $this->remoteAddress;
  100. }
  101. /**
  102. * Get local IP.
  103. *
  104. * @return string
  105. */
  106. public function getLocalIp(): string
  107. {
  108. $address = $this->getLocalAddress();
  109. $pos = \strrpos($address, ':');
  110. if (!$pos) {
  111. return '';
  112. }
  113. return \substr($address, 0, $pos);
  114. }
  115. /**
  116. * Get local port.
  117. *
  118. * @return int
  119. */
  120. public function getLocalPort(): int
  121. {
  122. $address = $this->getLocalAddress();
  123. $pos = \strrpos($address, ':');
  124. if (!$pos) {
  125. return 0;
  126. }
  127. return (int)\substr(\strrchr($address, ':'), 1);
  128. }
  129. /**
  130. * Get local address.
  131. *
  132. * @return string
  133. */
  134. public function getLocalAddress(): string
  135. {
  136. return (string)@\stream_socket_get_name($this->socket, false);
  137. }
  138. /**
  139. * Close connection.
  140. *
  141. * @param mixed|null $data
  142. * @return void
  143. */
  144. public function close(mixed $data = null, bool $raw = false)
  145. {
  146. if ($data !== null) {
  147. $this->send($data, $raw);
  148. }
  149. $this->eventLoop = $this->errorHandler = null;
  150. }
  151. /**
  152. * Get the real socket.
  153. *
  154. * @return resource
  155. */
  156. public function getSocket()
  157. {
  158. return $this->socket;
  159. }
  160. /**
  161. * Get the json_encode information.
  162. *
  163. * @return array
  164. */
  165. public function jsonSerialize(): array
  166. {
  167. return [
  168. 'transport' => $this->transport,
  169. 'getRemoteIp' => $this->getRemoteIp(),
  170. 'remotePort' => $this->getRemotePort(),
  171. 'getRemoteAddress' => $this->getRemoteAddress(),
  172. 'getLocalIp' => $this->getLocalIp(),
  173. 'getLocalPort' => $this->getLocalPort(),
  174. 'getLocalAddress' => $this->getLocalAddress(),
  175. 'isIpV4' => $this->isIpV4(),
  176. 'isIpV6' => $this->isIpV6(),
  177. ];
  178. }
  179. }