AsyncTcpConnection.php 4.0 KB

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