UdpConnection.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 string
  24. */
  25. public $protocol = '';
  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. * @return void|boolean
  60. */
  61. public function send($send_buffer, $raw = false)
  62. {
  63. if(false === $raw && $this->protocol)
  64. {
  65. $parser = $this->protocol;
  66. $send_buffer = $parser::encode($send_buffer, $this);
  67. if($send_buffer === '')
  68. {
  69. return null;
  70. }
  71. }
  72. return strlen($send_buffer) === stream_socket_sendto($this->_socket, $send_buffer, 0, $this->_remoteAddress);
  73. }
  74. /**
  75. * Get remote IP.
  76. * @return string
  77. */
  78. public function getRemoteIp()
  79. {
  80. if(!$this->_remoteIp)
  81. {
  82. list($this->_remoteIp, $this->_remotePort) = explode(':', $this->_remoteAddress, 2);
  83. }
  84. return $this->_remoteIp;
  85. }
  86. /**
  87. * Get remote port.
  88. * @return int
  89. */
  90. public function getRemotePort()
  91. {
  92. if(!$this->_remotePort)
  93. {
  94. list($this->_remoteIp, $this->_remotePort) = explode(':', $this->_remoteAddress, 2);
  95. }
  96. return $this->_remotePort;
  97. }
  98. /**
  99. * Close connection.
  100. * @void
  101. */
  102. public function close($data = null)
  103. {
  104. if($data !== null)
  105. {
  106. $this->send($data);
  107. }
  108. return true;
  109. }
  110. }