AsyncTcpConnection.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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, EventInterface $event)
  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->_event = $event;
  46. $this->_socket = stream_socket_client("tcp:$address", $errno, $errstr, 0, STREAM_CLIENT_ASYNC_CONNECT);
  47. if(!$this->_socket)
  48. {
  49. $this->emitError(WORKERMAN_CONNECT_FAIL, $errstr);
  50. return;
  51. }
  52. $this->_event->add($this->_socket, EventInterface::EV_WRITE, array($this, 'checkConnection'));
  53. }
  54. protected function emitError($code, $msg)
  55. {
  56. if($this->onError)
  57. {
  58. try{
  59. call_user_func($this->onError, $this, $code, $msg);
  60. }
  61. catch(Exception $e)
  62. {
  63. echo $e;
  64. }
  65. }
  66. }
  67. public function checkConnection($socket)
  68. {
  69. $this->_event->del($this->_socket, EventInterface::EV_WRITE);
  70. // php bug ?
  71. if(!feof($this->_socket) && !feof($this->_socket))
  72. {
  73. stream_set_blocking($this->_socket, 0);
  74. $this->_event->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
  75. if($this->_sendBuffer)
  76. {
  77. $this->_event->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
  78. }
  79. $this->_status = self::STATUS_ESTABLISH;
  80. if($this->onConnect)
  81. {
  82. try
  83. {
  84. call_user_func($this->onConnect, $this);
  85. }
  86. catch(Exception $e)
  87. {
  88. self::$statistics['throw_exception']++;
  89. echo $e;
  90. }
  91. }
  92. }
  93. else
  94. {
  95. $this->emitError(WORKERMAN_CONNECT_FAIL, 'connect fail, maybe timedout');
  96. }
  97. }
  98. /**
  99. * send buffer to client
  100. * @param string $send_buffer
  101. * @return void|boolean
  102. */
  103. public function send($send_buffer)
  104. {
  105. if($this->protocol)
  106. {
  107. $parser = $this->protocol;
  108. $send_buffer = $parser::encode($send_buffer, $this);
  109. }
  110. if($this->_status === self::STATUS_CONNECTING)
  111. {
  112. $this->_sendBuffer .= $send_buffer;
  113. return null;
  114. }
  115. elseif($this->_status == self::STATUS_CLOSED)
  116. {
  117. return false;
  118. }
  119. if($this->_sendBuffer === '')
  120. {
  121. $len = @fwrite($this->_socket, $send_buffer);
  122. if($len === strlen($send_buffer))
  123. {
  124. return true;
  125. }
  126. if($len > 0)
  127. {
  128. $this->_sendBuffer = substr($send_buffer, $len);
  129. }
  130. else
  131. {
  132. if(feof($this->_socket))
  133. {
  134. self::$statistics['send_fail']++;
  135. if($this->onError)
  136. {
  137. call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'client close');
  138. }
  139. $this->destroy();
  140. return false;
  141. }
  142. $this->_sendBuffer = $send_buffer;
  143. }
  144. $this->_event->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
  145. return null;
  146. }
  147. else
  148. {
  149. $this->_sendBuffer .= $send_buffer;
  150. }
  151. }
  152. }