|
|
@@ -41,7 +41,7 @@ class AsyncTcpConnection extends TcpConnection
|
|
|
*
|
|
|
* @var int
|
|
|
*/
|
|
|
- protected $_status = self::STATUS_CONNECTING;
|
|
|
+ protected $_status = self::STATUS_INITIAL;
|
|
|
|
|
|
/**
|
|
|
* Remote host.
|
|
|
@@ -111,14 +111,23 @@ class AsyncTcpConnection extends TcpConnection
|
|
|
*/
|
|
|
public function connect()
|
|
|
{
|
|
|
+ if ($this->_status !== self::STATUS_INITIAL && $this->_status !== self::STATUS_CLOSING && $this->_status !== self::STATUS_CLOSED) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $this->_status = self::STATUS_CONNECTING;
|
|
|
$this->_connectStartTime = microtime(true);
|
|
|
// Open socket connection asynchronously.
|
|
|
$this->_socket = stream_socket_client("{$this->transport}://{$this->_remoteAddress}", $errno, $errstr, 0,
|
|
|
STREAM_CLIENT_ASYNC_CONNECT);
|
|
|
// If failed attempt to emit onError callback.
|
|
|
if (!$this->_socket) {
|
|
|
- $this->_status = self::STATUS_CLOSED;
|
|
|
$this->emitError(WORKERMAN_CONNECT_FAIL, $errstr);
|
|
|
+ if ($this->_status === self::STATUS_CLOSING) {
|
|
|
+ $this->destroy();
|
|
|
+ }
|
|
|
+ if ($this->_status === self::STATUS_CLOSED) {
|
|
|
+ $this->onConnect = null;
|
|
|
+ }
|
|
|
return;
|
|
|
}
|
|
|
// Add socket to global event loop waiting connection is successfully established or faild.
|
|
|
@@ -144,6 +153,7 @@ class AsyncTcpConnection extends TcpConnection
|
|
|
*/
|
|
|
protected function emitError($code, $msg)
|
|
|
{
|
|
|
+ $this->_status = self::STATUS_CLOSING;
|
|
|
if ($this->onError) {
|
|
|
try {
|
|
|
call_user_func($this->onError, $this, $code, $msg);
|
|
|
@@ -201,8 +211,12 @@ class AsyncTcpConnection extends TcpConnection
|
|
|
} else {
|
|
|
// Connection failed.
|
|
|
$this->emitError(WORKERMAN_CONNECT_FAIL, 'connect ' . $this->_remoteAddress . ' fail after ' . round(microtime(true) - $this->_connectStartTime, 4) . ' seconds');
|
|
|
- $this->destroy();
|
|
|
- $this->onConnect = null;
|
|
|
+ if ($this->_status === self::STATUS_CLOSING) {
|
|
|
+ $this->destroy();
|
|
|
+ }
|
|
|
+ if ($this->_status === self::STATUS_CLOSED) {
|
|
|
+ $this->onConnect = null;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|