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