AsyncTcpConnection.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. * async connection
  10. * @author walkor<walkor@workerman.net>
  11. */
  12. class AsyncTcpConnection extends TcpConnection
  13. {
  14. /**
  15. * status
  16. * @var int
  17. */
  18. protected $_status = self::STATUS_CONNECTING;
  19. /**
  20. * when connect success , onConnect will be run
  21. * @var callback
  22. */
  23. public $onConnect = null;
  24. /**
  25. * create a connection
  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. $scheme = ucfirst($scheme);
  35. $this->protocol = '\\Protocols\\'.$scheme;
  36. if(!class_exists($this->protocol))
  37. {
  38. $this->protocol = '\\Workerman\\Protocols\\' . $scheme;
  39. if(!class_exists($this->protocol))
  40. {
  41. throw new Exception("class \\Protocols\\$scheme not exist");
  42. }
  43. }
  44. }
  45. $this->_socket = stream_socket_client("tcp:$address", $errno, $errstr, 0, STREAM_CLIENT_ASYNC_CONNECT);
  46. if(!$this->_socket)
  47. {
  48. $this->emitError(WORKERMAN_CONNECT_FAIL, $errstr);
  49. return;
  50. }
  51. Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'checkConnection'));
  52. }
  53. protected function emitError($code, $msg)
  54. {
  55. if($this->onError)
  56. {
  57. try{
  58. call_user_func($this->onError, $this, $code, $msg);
  59. }
  60. catch(Exception $e)
  61. {
  62. echo $e;
  63. }
  64. }
  65. }
  66. public function checkConnection($socket)
  67. {
  68. Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
  69. // php bug ?
  70. if(!feof($this->_socket) && !feof($this->_socket))
  71. {
  72. stream_set_blocking($this->_socket, 0);
  73. Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
  74. if($this->_sendBuffer)
  75. {
  76. Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
  77. }
  78. $this->_status = self::STATUS_ESTABLISH;
  79. if($this->onConnect)
  80. {
  81. try
  82. {
  83. call_user_func($this->onConnect, $this);
  84. }
  85. catch(Exception $e)
  86. {
  87. self::$statistics['throw_exception']++;
  88. echo $e;
  89. }
  90. }
  91. }
  92. else
  93. {
  94. $this->emitError(WORKERMAN_CONNECT_FAIL, 'connect fail, maybe timedout');
  95. }
  96. }
  97. /**
  98. * send buffer to client
  99. * @param string $send_buffer
  100. * @return void|boolean
  101. */
  102. public function send($send_buffer)
  103. {
  104. if($this->protocol)
  105. {
  106. $parser = $this->protocol;
  107. $send_buffer = $parser::encode($send_buffer, $this);
  108. }
  109. if($this->_status === self::STATUS_CONNECTING)
  110. {
  111. $this->_sendBuffer .= $send_buffer;
  112. return null;
  113. }
  114. elseif($this->_status == self::STATUS_CLOSED)
  115. {
  116. return false;
  117. }
  118. if($this->_sendBuffer === '')
  119. {
  120. $len = @fwrite($this->_socket, $send_buffer);
  121. if($len === strlen($send_buffer))
  122. {
  123. return true;
  124. }
  125. if($len > 0)
  126. {
  127. $this->_sendBuffer = substr($send_buffer, $len);
  128. }
  129. else
  130. {
  131. if(feof($this->_socket))
  132. {
  133. self::$statistics['send_fail']++;
  134. if($this->onError)
  135. {
  136. call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'client close');
  137. }
  138. $this->destroy();
  139. return false;
  140. }
  141. $this->_sendBuffer = $send_buffer;
  142. }
  143. Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
  144. return null;
  145. }
  146. else
  147. {
  148. $this->_sendBuffer .= $send_buffer;
  149. }
  150. }
  151. }