UdpConnection.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 ip.
  35. *
  36. * @var string
  37. */
  38. protected $_remoteIp = '';
  39. /**
  40. * Remote port.
  41. *
  42. * @var int
  43. */
  44. protected $_remotePort = 0;
  45. /**
  46. * Remote address.
  47. *
  48. * @var string
  49. */
  50. protected $_remoteAddress = '';
  51. /**
  52. * Construct.
  53. *
  54. * @param resource $socket
  55. * @param string $remote_address
  56. */
  57. public function __construct($socket, $remote_address)
  58. {
  59. $this->_socket = $socket;
  60. $this->_remoteAddress = $remote_address;
  61. }
  62. /**
  63. * Sends data on the connection.
  64. *
  65. * @param string $send_buffer
  66. * @param bool $raw
  67. * @return void|boolean
  68. */
  69. public function send($send_buffer, $raw = false)
  70. {
  71. if (false === $raw && $this->protocol) {
  72. $parser = $this->protocol;
  73. $send_buffer = $parser::encode($send_buffer, $this);
  74. if ($send_buffer === '') {
  75. return null;
  76. }
  77. }
  78. return strlen($send_buffer) === stream_socket_sendto($this->_socket, $send_buffer, 0, $this->_remoteAddress);
  79. }
  80. /**
  81. * Get remote IP.
  82. *
  83. * @return string
  84. */
  85. public function getRemoteIp()
  86. {
  87. if (!$this->_remoteIp) {
  88. list($this->_remoteIp, $this->_remotePort) = explode(':', $this->_remoteAddress, 2);
  89. }
  90. return $this->_remoteIp;
  91. }
  92. /**
  93. * Get remote port.
  94. *
  95. * @return int
  96. */
  97. public function getRemotePort()
  98. {
  99. if (!$this->_remotePort) {
  100. list($this->_remoteIp, $this->_remotePort) = explode(':', $this->_remoteAddress, 2);
  101. }
  102. return $this->_remotePort;
  103. }
  104. /**
  105. * Close connection.
  106. *
  107. * @param mixed $data
  108. * @return bool
  109. */
  110. public function close($data = null)
  111. {
  112. if ($data !== null) {
  113. $this->send($data);
  114. }
  115. return true;
  116. }
  117. }