UdpConnection.php 4.3 KB

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