AsyncTcpConnection.php 4.2 KB

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