UdpConnection.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. * @var \Workerman\Protocols\ProtocolInterface
  24. */
  25. public $protocol = null;
  26. /**
  27. * Udp socket.
  28. * @var resource
  29. */
  30. protected $_socket = null;
  31. /**
  32. * Remote ip.
  33. * @var string
  34. */
  35. protected $_remoteIp = '';
  36. /**
  37. * Remote port.
  38. * @var int
  39. */
  40. protected $_remotePort = 0;
  41. /**
  42. * Remote address.
  43. * @var string
  44. */
  45. protected $_remoteAddress = '';
  46. /**
  47. * Construct.
  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. * @param string $send_buffer
  59. * @param bool $raw
  60. * @return void|boolean
  61. */
  62. public function send($send_buffer, $raw = false)
  63. {
  64. if(false === $raw && $this->protocol)
  65. {
  66. $parser = $this->protocol;
  67. $send_buffer = $parser::encode($send_buffer, $this);
  68. if($send_buffer === '')
  69. {
  70. return null;
  71. }
  72. }
  73. return strlen($send_buffer) === stream_socket_sendto($this->_socket, $send_buffer, 0, $this->_remoteAddress);
  74. }
  75. /**
  76. * Get remote IP.
  77. * @return string
  78. */
  79. public function getRemoteIp()
  80. {
  81. if(!$this->_remoteIp)
  82. {
  83. list($this->_remoteIp, $this->_remotePort) = explode(':', $this->_remoteAddress, 2);
  84. }
  85. return $this->_remoteIp;
  86. }
  87. /**
  88. * Get remote port.
  89. * @return int
  90. */
  91. public function getRemotePort()
  92. {
  93. if(!$this->_remotePort)
  94. {
  95. list($this->_remoteIp, $this->_remotePort) = explode(':', $this->_remoteAddress, 2);
  96. }
  97. return $this->_remotePort;
  98. }
  99. /**
  100. * Close connection.
  101. *
  102. * @param mixed $data
  103. * @return bool
  104. */
  105. public function close($data = null)
  106. {
  107. if($data !== null)
  108. {
  109. $this->send($data);
  110. }
  111. return true;
  112. }
  113. }