UdpConnection.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * udp连接类(udp实际上是无连接的,这里是为了保持与TCP接口一致)
  17. */
  18. class UdpConnection extends ConnectionInterface
  19. {
  20. /**
  21. * 应用层协议
  22. * 值类似于 Workerman\\Protocols\\Http
  23. * @var string
  24. */
  25. public $protocol = '';
  26. /**
  27. * udp socket 资源
  28. * @var resource
  29. */
  30. protected $_socket = null;
  31. /**
  32. * 对端 ip
  33. * @var string
  34. */
  35. protected $_remoteIp = '';
  36. /**
  37. * 对端 端口
  38. * @var int
  39. */
  40. protected $_remotePort = 0;
  41. /**
  42. * 对端 地址
  43. * 值类似于 192.168.10.100:3698
  44. * @var string
  45. */
  46. protected $_remoteAddress = '';
  47. /**
  48. * 构造函数
  49. * @param resource $socket
  50. * @param string $remote_address
  51. */
  52. public function __construct($socket, $remote_address)
  53. {
  54. $this->_socket = $socket;
  55. $this->_remoteAddress = $remote_address;
  56. }
  57. /**
  58. * 发送数据给对端
  59. * @param string $send_buffer
  60. * @return void|boolean
  61. */
  62. public function send($send_buffer, $raw = false)
  63. {
  64. // 如果没有设置以原始数据发送,并且有设置协议则按照协议编码
  65. if(false === $raw && $this->protocol)
  66. {
  67. $parser = $this->protocol;
  68. $send_buffer = $parser::encode($send_buffer, $this);
  69. if($send_buffer === '')
  70. {
  71. return null;
  72. }
  73. }
  74. return strlen($send_buffer) === stream_socket_sendto($this->_socket, $send_buffer, 0, $this->_remoteAddress);
  75. }
  76. /**
  77. * 获得对端 ip
  78. * @return string
  79. */
  80. public function getRemoteIp()
  81. {
  82. if(!$this->_remoteIp)
  83. {
  84. list($this->_remoteIp, $this->_remotePort) = explode(':', $this->_remoteAddress, 2);
  85. }
  86. return $this->_remoteIp;
  87. }
  88. /**
  89. * 获得对端端口
  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. * 关闭连接(此处为了保持与TCP接口一致,提供了close方法)
  101. * @void
  102. */
  103. public function close($data = null)
  104. {
  105. if($data !== null)
  106. {
  107. $this->send($data);
  108. }
  109. return true;
  110. }
  111. }