AsyncTcpConnection.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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\Libevent;
  16. use Workerman\Events\Select;
  17. use Workerman\Events\EventInterface;
  18. use Workerman\Worker;
  19. use \Exception;
  20. /**
  21. * 异步tcp连接类
  22. */
  23. class AsyncTcpConnection extends TcpConnection
  24. {
  25. /**
  26. * 当连接成功时,如果设置了连接成功回调,则执行
  27. * @var callback
  28. */
  29. public $onConnect = null;
  30. /**
  31. * 连接状态 连接中
  32. * @var int
  33. */
  34. protected $_status = self::STATUS_CONNECTING;
  35. /**
  36. * 构造函数,创建连接
  37. * @param resource $socket
  38. * @param EventInterface $event
  39. */
  40. public function __construct($remote_address)
  41. {
  42. list($scheme, $address) = explode(':', $remote_address, 2);
  43. if($scheme != 'tcp')
  44. {
  45. // 判断协议类是否存在
  46. $scheme = ucfirst($scheme);
  47. $this->protocol = '\\Protocols\\'.$scheme;
  48. if(!class_exists($this->protocol))
  49. {
  50. $this->protocol = '\\Workerman\\Protocols\\' . $scheme;
  51. if(!class_exists($this->protocol))
  52. {
  53. throw new Exception("class \\Protocols\\$scheme not exist");
  54. }
  55. }
  56. }
  57. $this->_remoteAddress = substr($address, 2);
  58. $this->id = self::$_idRecorder++;
  59. // 统计数据
  60. self::$statistics['connection_count']++;
  61. $this->maxSendBufferSize = self::$defaultMaxSendBufferSize;
  62. }
  63. public function connect()
  64. {
  65. // 创建异步连接
  66. $this->_socket = stream_socket_client("tcp://{$this->_remoteAddress}", $errno, $errstr, 0, STREAM_CLIENT_ASYNC_CONNECT);
  67. // 如果失败尝试触发失败回调(如果有回调的话)
  68. if(!$this->_socket)
  69. {
  70. $this->_status = self::STATUS_CLOSED;
  71. $this->emitError(WORKERMAN_CONNECT_FAIL, $errstr);
  72. return;
  73. }
  74. // 监听连接可写事件(可写意味着连接已经建立或者已经出错)
  75. Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'checkConnection'));
  76. }
  77. /**
  78. * 尝试触发失败回调
  79. * @param int $code
  80. * @param string $msg
  81. * @return void
  82. */
  83. protected function emitError($code, $msg)
  84. {
  85. if($this->onError)
  86. {
  87. try{
  88. call_user_func($this->onError, $this, $code, $msg);
  89. }
  90. catch(Exception $e)
  91. {
  92. echo $e;
  93. }
  94. }
  95. }
  96. /**
  97. * 检查连接状态,连接成功还是失败
  98. * @param resource $socket
  99. * @return void
  100. */
  101. public function checkConnection($socket)
  102. {
  103. // 删除连接可写监听
  104. Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
  105. // 需要判断两次连接是否已经断开
  106. if(!feof($this->_socket) && !feof($this->_socket))
  107. {
  108. // 设置非阻塞
  109. stream_set_blocking($this->_socket, 0);
  110. // 监听可读事件
  111. Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
  112. // 如果发送缓冲区有数据则执行发送
  113. if($this->_sendBuffer)
  114. {
  115. Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
  116. }
  117. // 标记状态为连接已经建立
  118. $this->_status = self::STATUS_ESTABLISH;
  119. // 如果有设置onConnect回调,则执行
  120. if($this->onConnect)
  121. {
  122. try
  123. {
  124. call_user_func($this->onConnect, $this);
  125. }
  126. catch(Exception $e)
  127. {
  128. self::$statistics['throw_exception']++;
  129. echo $e;
  130. }
  131. }
  132. }
  133. else
  134. {
  135. $this->_status = self::STATUS_CLOSED;
  136. // 关闭socket
  137. @fclose($this->_socket);
  138. // 连接未建立成功
  139. $this->emitError(WORKERMAN_CONNECT_FAIL, 'connect fail');
  140. }
  141. }
  142. }