AsyncTcpConnection.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. }
  51. public function connect()
  52. {
  53. // 创建异步连接
  54. $this->_socket = stream_socket_client("tcp://{$this->_remoteAddress}", $errno, $errstr, 0, STREAM_CLIENT_ASYNC_CONNECT);
  55. // 如果失败尝试触发失败回调(如果有回调的话)
  56. if(!$this->_socket)
  57. {
  58. $this->_status = self::STATUS_CLOSED;
  59. $this->emitError(WORKERMAN_CONNECT_FAIL, $errstr);
  60. return;
  61. }
  62. // 监听连接可写事件(可写意味着连接已经建立或者已经出错)
  63. Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'checkConnection'));
  64. }
  65. /**
  66. * 尝试触发失败回调
  67. * @param int $code
  68. * @param string $msg
  69. * @return void
  70. */
  71. protected function emitError($code, $msg)
  72. {
  73. if($this->onError)
  74. {
  75. try{
  76. call_user_func($this->onError, $this, $code, $msg);
  77. }
  78. catch(Exception $e)
  79. {
  80. echo $e;
  81. }
  82. }
  83. }
  84. /**
  85. * 检查连接状态,连接成功还是失败
  86. * @param resource $socket
  87. * @return void
  88. */
  89. public function checkConnection($socket)
  90. {
  91. // 删除连接可写监听
  92. Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
  93. // 需要判断两次连接是否已经断开
  94. if(!feof($this->_socket) && !feof($this->_socket))
  95. {
  96. // 设置非阻塞
  97. stream_set_blocking($this->_socket, 0);
  98. // 监听可读事件
  99. Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
  100. // 如果发送缓冲区有数据则执行发送
  101. if($this->_sendBuffer)
  102. {
  103. Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
  104. }
  105. // 标记状态为连接已经建立
  106. $this->_status = self::STATUS_ESTABLISH;
  107. // 如果有设置onConnect回调,则执行
  108. if($this->onConnect)
  109. {
  110. try
  111. {
  112. call_user_func($this->onConnect, $this);
  113. }
  114. catch(Exception $e)
  115. {
  116. self::$statistics['throw_exception']++;
  117. echo $e;
  118. }
  119. }
  120. }
  121. else
  122. {
  123. $this->_status = self::STATUS_CLOSED;
  124. // 关闭socket
  125. @fclose($this->_socket);
  126. // 连接未建立成功
  127. $this->emitError(WORKERMAN_CONNECT_FAIL, 'connect fail');
  128. }
  129. }
  130. }