Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/Transport/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,20 +244,23 @@ protected function waitForReadyRead(int $time = 0): void
return;
}

$read = [$this->stream];
$null = null;
$result = @stream_select($read, $null, $null, $this->config["timeout"]);

if ($result === false) {
throw new TransportException("unable to wait for data from server '" . $this->config["host"] . ":" . $this->config["port"] . "'");
}

if ($result === 0) {
$time = $time ?: $this->config["timeout"];
Signal::getInstance()
->emit(strtolower($this->getAdapterType()) . "WaitTimeout", $time, $this->getAdapter());

throw new TransportException("timed out waiting for data from server '" . $this->config["host"] . ":" . $this->config["port"] . "'");
}
do {
$read = [$this->stream];
$null = null;

if ($time) {
Signal::getInstance()
->emit(strtolower($this->getAdapterType()) . "WaitTimeout", $time, $this->getAdapter());
}

// A zero timeout is valid for polling, but must still advance the
// reported idle time to avoid a busy loop without timeout signals.
$time += max(1, (int) $this->config["timeout"]);
$result = @stream_select($read, $null, $null, $this->config["timeout"]);

if ($result === false) {
throw new TransportException("unable to wait for data from server '" . $this->config["host"] . ":" . $this->config["port"] . "'");
}
} while ($result === 0);
}
}
25 changes: 21 additions & 4 deletions tests/Transport/TCPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use PlanetTeamSpeak\TeamSpeak3Framework\Adapter\MockServerQuery;
use PlanetTeamSpeak\TeamSpeak3Framework\Exception\ServerQueryException;
use PlanetTeamSpeak\TeamSpeak3Framework\Helper\Signal;
use PlanetTeamSpeak\TeamSpeak3Framework\Transport\TCP;
use PlanetTeamSpeak\TeamSpeak3Framework\Exception\TransportException;

Expand Down Expand Up @@ -349,7 +350,7 @@ public function testSendLineNoConnection()
$transport->sendLine('test.sendLine');
}

public function testNonBlockingReadTimesOut(): void
public function testNonBlockingReadContinuesWaitingAfterTimeout(): void
{
$transport = new class (['host' => 'test', 'port' => 12345, 'blocking' => 0, 'timeout' => 0]) extends TCP {
private $peer;
Expand All @@ -363,13 +364,29 @@ public function waitForReadForTest(): void
{
$this->waitForReadyRead();
}

public function makeStreamReadableForTest(): void
{
fwrite($this->peer, "event\n");
}
};

$transport->connectForTest();
$idleTimes = [];
$callback = static function (int $idleTime) use ($transport, &$idleTimes): void {
$idleTimes[] = $idleTime;
$transport->makeStreamReadableForTest();
};

$this->expectException(TransportException::class);
$this->expectExceptionMessage("timed out waiting for data from server 'test:12345'");
$transport->waitForReadForTest();
Signal::getInstance()->subscribe('unknownWaitTimeout', $callback);

try {
$transport->waitForReadForTest();
} finally {
Signal::getInstance()->unsubscribe('unknownWaitTimeout', $callback);
}

$this->assertSame([1], $idleTimes);
}

public function testTlsVerificationIsDisabledByDefault(): void
Expand Down
Loading