Skip to content

Commit

Permalink
Issue planetteamspeak#204: Exit loops when disconnected
Browse files Browse the repository at this point in the history
This change should help to exit stuck loops, when the library is not connected to the remote server anymore.

- Fixes the existing `isConnected()` function to properly return TRUE or FALSE
- Adds a PHPUnit test for the `isConnected()` function
- Adds the connection status as condition to loops in order to abort, when it disconnected
  • Loading branch information
Sebi94nbg committed Aug 16, 2023
1 parent adbe553 commit 5d86f29
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Adapter/ServerQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function request(string $cmd, bool $throw = true): Reply
do {
$str = $this->getTransport()->readLine();
$rpl[] = $str;
} while ($str->section(TeamSpeak3::SEPARATOR_CELL) != TeamSpeak3::ERROR);
} while ($this->getTransport()->isConnected() and $str->section(TeamSpeak3::SEPARATOR_CELL) != TeamSpeak3::ERROR);

$this->getProfiler()->stop();

Expand All @@ -164,7 +164,7 @@ public function wait(): Event

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

return new Event($evt, $this->getHost());
}
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 5d86f29

Please sign in to comment.