AsyncTcpConnection.php 3.9 KB

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