Skip to content

Commit aff12ae

Browse files
author
Ares
committed
Fix tcp connection lost bug and EV_WRITE register twice bug.
1.Fix AsyncTcpConnection being unset in TcpConnection::$connections and never add back when reconnected. 2.Fix AsyncTcpConnection adding EV_WRITE event multi times in ssl handshake stage.
1 parent 84d841f commit aff12ae

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

Connection/AsyncTcpConnection.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,12 @@ public function __construct($remote_address, $context_option = null)
158158
self::$statistics['connection_count']++;
159159
$this->maxSendBufferSize = self::$defaultMaxSendBufferSize;
160160
$this->_contextOption = $context_option;
161-
static::$connections[$this->id] = $this;
162161
}
163162

164163
/**
165164
* Do connect.
166165
*
167-
* @return void
166+
* @return void
168167
*/
169168
public function connect()
170169
{
@@ -199,7 +198,7 @@ public function connect()
199198
}
200199
return;
201200
}
202-
// Add socket to global event loop waiting connection is successfully established or faild.
201+
// Add socket to global event loop waiting connection is successfully established or faild.
203202
Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'checkConnection'));
204203
// For windows.
205204
if(DIRECTORY_SEPARATOR === '\\') {
@@ -228,7 +227,7 @@ public function reConnect($after = 0) {
228227
/**
229228
* Get remote address.
230229
*
231-
* @return string
230+
* @return string
232231
*/
233232
public function getRemoteHost()
234233
{
@@ -276,14 +275,8 @@ protected function emitError($code, $msg)
276275
*/
277276
public function checkConnection($socket)
278277
{
279-
// Remove EV_EXPECT for windows.
280-
if(DIRECTORY_SEPARATOR === '\\') {
281-
Worker::$globalEvent->del($socket, EventInterface::EV_EXCEPT);
282-
}
283278
// Check socket state.
284279
if ($address = stream_socket_get_name($socket, true)) {
285-
// Remove write listener.
286-
Worker::$globalEvent->del($socket, EventInterface::EV_WRITE);
287280
// Nonblocking.
288281
stream_set_blocking($socket, 0);
289282
// Compatible with hhvm
@@ -298,14 +291,24 @@ public function checkConnection($socket)
298291
}
299292

300293
// SSL handshake.
301-
if ($this->transport === 'ssl' && $this->doSslHandshake($socket)) {
302-
$this->_sslHandshakeCompleted = true;
294+
if ($this->transport === 'ssl') {
295+
$this->_sslHandshakeCompleted = $this->doSslHandshake($socket);
296+
if(!$this->_sslHandshakeCompleted){
297+
return;
298+
}
299+
}
300+
301+
// Remove EV_EXPECT for windows.
302+
if(DIRECTORY_SEPARATOR === '\\') {
303+
Worker::$globalEvent->del($socket, EventInterface::EV_EXCEPT);
303304
}
305+
// Remove write listener.
306+
Worker::$globalEvent->del($socket, EventInterface::EV_WRITE);
304307

305308
// Register a listener waiting read event.
306309
Worker::$globalEvent->add($socket, EventInterface::EV_READ, array($this, 'baseRead'));
307310
// There are some data waiting to send.
308-
if ($this->_sendBuffer && $this->transport !== 'ssl') {
311+
if ($this->_sendBuffer) {
309312
Worker::$globalEvent->add($socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
310313
}
311314
$this->_status = self::STATUS_ESTABLISHED;
@@ -335,6 +338,7 @@ public function checkConnection($socket)
335338
exit(250);
336339
}
337340
}
341+
static::$connections[$this->id] = $this;
338342
} else {
339343
// Connection failed.
340344
$this->emitError(WORKERMAN_CONNECT_FAIL, 'connect ' . $this->_remoteAddress . ' fail after ' . round(microtime(true) - $this->_connectStartTime, 4) . ' seconds');
@@ -382,9 +386,6 @@ public function doSslHandshake($socket){
382386
exit(250);
383387
}
384388
}
385-
if ($this->_sendBuffer) {
386-
Worker::$globalEvent->add($socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
387-
}
388389
return true;
389390
}
390391
}

0 commit comments

Comments
 (0)