Skip to content

Commit

Permalink
Option to not use exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
calcinai committed May 31, 2019
1 parent beb0cb6 commit 70057c2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
20 changes: 20 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class Client extends EventEmitter
{


/**
* @var LoopInterface
*/
Expand Down Expand Up @@ -52,6 +53,11 @@ class Client extends EventEmitter

private $heartbeat_interval;

/**
* @var bool
*/
public $use_exceptions;

private $state;

const PORT_DEFAULT_HTTP = 80;
Expand Down Expand Up @@ -83,6 +89,7 @@ public function __construct($uri, LoopInterface $loop, Resolver $resolver = null
$this->resolver = $resolver;
$this->state = self::STATE_CLOSED;
$this->heartbeat_interval = null;
$this->use_exceptions = true;
}

public function connect()
Expand Down Expand Up @@ -170,4 +177,17 @@ public function getHeartbeatInterval()
return $this->heartbeat_interval;
}

public function useExceptions()
{
return $this->use_exceptions;
}

/**
* @param bool $use_exceptions
*/
public function setUseExceptions($use_exceptions)
{
$this->use_exceptions = $use_exceptions;
}

}
34 changes: 21 additions & 13 deletions src/Protocol/AbstractProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use React\EventLoop\Timer\Timer;
use React\Socket\ConnectionInterface;

abstract class AbstractProtocol implements ProtocolInterface {
abstract class AbstractProtocol implements ProtocolInterface
{

/**
* @var Client
Expand All @@ -28,7 +29,8 @@ abstract class AbstractProtocol implements ProtocolInterface {
*/
protected $heartbeat_timer;

public function __construct(Client $client, ConnectionInterface $stream) {
public function __construct(Client $client, ConnectionInterface $stream)
{
$this->client = $client;
$this->stream = $stream;

Expand All @@ -41,7 +43,7 @@ public function __construct(Client $client, ConnectionInterface $stream) {
$buffer .= $data;

//If the handler returns true, was successfully processed and can empty buffer
if($that->onStreamData($buffer)) {
if ($that->onStreamData($buffer)) {
$buffer = '';
}
});
Expand All @@ -51,20 +53,23 @@ public function __construct(Client $client, ConnectionInterface $stream) {
$client->setState(Client::STATE_CLOSED);
});

$this->client->on('connect', function(){
if(null !== $this->client->getHeartbeatInterval()){
$this->client->on('connect', function () {
if (null !== $this->client->getHeartbeatInterval()) {
$this->startHeartbeat();
}
});
}

public function startHeartbeat(){
public function startHeartbeat()
{

$this->client->getLoop()->addTimer($this->client->getHeartbeatInterval(), function(){
$this->client->getLoop()->addTimer($this->client->getHeartbeatInterval(), function () {
//Set a new timeout (2 sec seems reasonable)
$this->heartbeat_timer = $this->client->getLoop()->addTimer(2, function(){
$this->heartbeat_timer = $this->client->getLoop()->addTimer(2, function () {
$this->stream->close();
throw new ConnectionLostException();
if ($this->client->useExceptions()) {
throw new ConnectionLostException();
}
});

$this->sendHeartbeat();
Expand All @@ -73,16 +78,19 @@ public function startHeartbeat(){

}

public function onHeartbeat(){
public function onHeartbeat()
{

if(isset($this->heartbeat_timer)){
if (isset($this->heartbeat_timer)) {
$this->client->getLoop()->cancelTimer($this->heartbeat_timer);
}

$this->startHeartbeat();

}

public function sendHeartbeat() {}
public function sendHeartbeat()
{
}

}
}

0 comments on commit 70057c2

Please sign in to comment.