AsyncTcpConnection.php 4.7 KB

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