AsyncTcpConnection.php 4.0 KB

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