Skip to content

Commit

Permalink
Merge pull request #206 from Sebbo94BY/Issue-204-Exit-loop-on-disconnect
Browse files Browse the repository at this point in the history
Issue #204: Fix high CPU utilization after disconnects / connection losses
  • Loading branch information
Sebbo94BY committed Aug 16, 2023
2 parents adbe553 + c8e8cfe commit cd05928
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Adapter/ServerQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ protected function syn(): void
*/
public function __destruct()
{
// do not disconnect, when acting as bot in non-blocking mode
if (! $this->getTransport()->getConfig("blocking")) {
return;
}

if ($this->getTransport() instanceof Transport && $this->transport->isConnected()) {
try {
$this->request("quit");
Expand Down Expand Up @@ -137,6 +142,9 @@ public function request(string $cmd, bool $throw = true): Reply
$rpl = [];

do {
if (! $this->getTransport()->isConnected()) {
break;
}
$str = $this->getTransport()->readLine();
$rpl[] = $str;
} while ($str->section(TeamSpeak3::SEPARATOR_CELL) != TeamSpeak3::ERROR);
Expand All @@ -163,6 +171,9 @@ public function wait(): Event
}

do {
if (! $this->getTransport()->isConnected()) {
break;
}
$evt = $this->getTransport()->readLine();
} while (!$evt->section(TeamSpeak3::SEPARATOR_CELL)->startsWith(TeamSpeak3::EVENT));

Expand Down
2 changes: 1 addition & 1 deletion src/Transport/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public function getMetaData(): array
*/
public function isConnected(): bool
{
return is_resource($this->stream);
return ($this->getStream() === null) ? false : true;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/Transport/TCPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PlanetTeamSpeak\TeamSpeak3Framework\Tests\Transport;

use PHPUnit\Framework\TestCase;
use PlanetTeamSpeak\TeamSpeak3Framework\Adapter\MockServerQuery;
use PlanetTeamSpeak\TeamSpeak3Framework\Adapter\ServerQuery;
use PlanetTeamSpeak\TeamSpeak3Framework\Exception\ServerQueryException;
use PlanetTeamSpeak\TeamSpeak3Framework\Transport\TCP;
Expand Down Expand Up @@ -91,6 +92,25 @@ public function testGetStream()
$this->assertNull($transport->getStream());
}

/**
* @throws AdapterException
*/
protected function createMockServerQuery(): MockServerQuery
{
return new MockServerQuery(['host' => '0.0.0.0', 'port' => 9987]);
}

/**
* Tests if the connection status gets properly returned.
*/
public function testConnectionStatus()
{
$mockServerQuery = $this->createMockServerQuery();
$this->assertTrue($mockServerQuery->getTransport()->isConnected());
$mockServerQuery->getTransport()->disconnect();
$this->assertFalse($mockServerQuery->getTransport()->isConnected());
}

/**
* @throws TransportException
* @throws ServerQueryException
Expand Down

0 comments on commit cd05928

Please sign in to comment.