AsyncUdpConnection.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. use Workerman\Events\EventInterface;
  16. use Workerman\Worker;
  17. use Exception;
  18. /**
  19. * AsyncTcpConnection.
  20. */
  21. class AsyncUdpConnection extends UdpConnection
  22. {
  23. /**
  24. * Emitted when socket connection is successfully established.
  25. *
  26. * @var callback
  27. */
  28. public $onConnect = null;
  29. /**
  30. * Emitted when socket connection closed.
  31. *
  32. * @var callback
  33. */
  34. public $onClose = null;
  35. /**
  36. * Connected or not.
  37. *
  38. * @var bool
  39. */
  40. protected $connected = false;
  41. /**
  42. * Construct.
  43. *
  44. * @param string $remote_address
  45. * @throws Exception
  46. */
  47. public function __construct($remote_address)
  48. {
  49. // Get the application layer communication protocol and listening address.
  50. list($scheme, $address) = explode(':', $remote_address, 2);
  51. // Check application layer protocol class.
  52. if ($scheme !== 'udp') {
  53. $scheme = ucfirst($scheme);
  54. $this->protocol = '\\Protocols\\' . $scheme;
  55. if (!class_exists($this->protocol)) {
  56. $this->protocol = "\\Workerman\\Protocols\\$scheme";
  57. if (!class_exists($this->protocol)) {
  58. throw new Exception("class \\Protocols\\$scheme not exist");
  59. }
  60. }
  61. }
  62. $this->_remoteAddress = substr($address, 2);
  63. }
  64. /**
  65. * For udp package.
  66. *
  67. * @param resource $socket
  68. * @return bool
  69. */
  70. public function baseRead($socket)
  71. {
  72. $recv_buffer = stream_socket_recvfrom($socket, Worker::MAX_UDP_PACKAGE_SIZE, 0, $remote_address);
  73. if (false === $recv_buffer || empty($remote_address)) {
  74. return false;
  75. }
  76. if ($this->onMessage) {
  77. if ($this->protocol) {
  78. $parser = $this->protocol;
  79. $recv_buffer = $parser::decode($recv_buffer, $this);
  80. }
  81. ConnectionInterface::$statistics['total_request']++;
  82. try {
  83. call_user_func($this->onMessage, $this, $recv_buffer);
  84. } catch (\Exception $e) {
  85. Worker::log($e);
  86. exit(250);
  87. } catch (\Error $e) {
  88. Worker::log($e);
  89. exit(250);
  90. }
  91. }
  92. return true;
  93. }
  94. /**
  95. * Sends data on the connection.
  96. *
  97. * @param string $send_buffer
  98. * @param bool $raw
  99. * @return void|boolean
  100. */
  101. public function send($send_buffer, $raw = false)
  102. {
  103. if (false === $raw && $this->protocol) {
  104. $parser = $this->protocol;
  105. $send_buffer = $parser::encode($send_buffer, $this);
  106. if ($send_buffer === '') {
  107. return null;
  108. }
  109. }
  110. if ($this->connected === false) {
  111. $this->connect();
  112. }
  113. return strlen($send_buffer) === stream_socket_sendto($this->_socket, $send_buffer, 0);
  114. }
  115. /**
  116. * Close connection.
  117. *
  118. * @param mixed $data
  119. * @param bool $raw
  120. *
  121. * @return bool
  122. */
  123. public function close($data = null, $raw = false)
  124. {
  125. if ($data !== null) {
  126. $this->send($data, $raw);
  127. }
  128. Worker::$globalEvent->del($this->_socket, EventInterface::EV_READ);
  129. fclose($this->_socket);
  130. $this->connected = false;
  131. // Try to emit onClose callback.
  132. if ($this->onClose) {
  133. try {
  134. call_user_func($this->onClose, $this);
  135. } catch (\Exception $e) {
  136. Worker::log($e);
  137. exit(250);
  138. } catch (\Error $e) {
  139. Worker::log($e);
  140. exit(250);
  141. }
  142. }
  143. $this->onConnect = $this->onMessage = $this->onClose = null;
  144. return true;
  145. }
  146. /**
  147. * Connect.
  148. *
  149. * @return void
  150. */
  151. public function connect()
  152. {
  153. if ($this->connected === true) {
  154. return;
  155. }
  156. $this->_socket = stream_socket_client("udp://{$this->_remoteAddress}");
  157. if ($this->onMessage) {
  158. Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
  159. }
  160. $this->connected = true;
  161. // Try to emit onConnect callback.
  162. if ($this->onConnect) {
  163. try {
  164. call_user_func($this->onConnect, $this);
  165. } catch (\Exception $e) {
  166. Worker::log($e);
  167. exit(250);
  168. } catch (\Error $e) {
  169. Worker::log($e);
  170. exit(250);
  171. }
  172. }
  173. }
  174. }