Skip to content

Commit

Permalink
Issue #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.

- Adds a new function to check if a connection is established or not
- Adds a PHPUnit test for the new function
- Adds the connection status as condition to loops in order to abort, when it disconnected
  • Loading branch information
Sebi94nbg committed Aug 15, 2023
1 parent 64aa90a commit b3d0ec5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 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()->is_connected() 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()->is_connected() AND !$evt->section(TeamSpeak3::SEPARATOR_CELL)->startsWith(TeamSpeak3::EVENT));

return new Event($evt, $this->getHost());
}
Expand Down
10 changes: 10 additions & 0 deletions src/Transport/TCP.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ public function disconnect(): void
Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "Disconnected");
}

/**
* Returns true, if the connection to a remote server is established.
*
* @return bool
*/
public function is_connected(): bool
{
return ($this->stream === null) ? false : true;
}

/**
* Reads data from the stream.
*
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()->is_connected());
$mockServerQuery->getTransport()->disconnect();
$this->assertFalse($mockServerQuery->getTransport()->is_connected());
}

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

0 comments on commit b3d0ec5

Please sign in to comment.