Skip to content

Add LoopEvent support for >= v1.0 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
}
],
"require": {
"php": ">=5.3",
"react/event-loop": "0.3.*",
"react/promise": "~1.0",
"clue/socket-react": "0.2.*",
"e-butik/iodophor": "1.*"
"php": ">=5.4",
"react/event-loop": "^1.1",
"react/promise": "^2.8",
"clue/socket-react": "dev-master",
"e-butik/iodophor": "^1.0"
},
"autoload": {
"psr-4": {"Clue\\React\\Icmp\\": "src"}
Expand Down
24 changes: 14 additions & 10 deletions src/Icmp.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Iodophor\Io\StringWriter;
use Iodophor\Io\StringReader;
use React\Promise\Deferred;
use React\Promise\When;
use Evenement\EventEmitter;
use React\EventLoop\LoopInterface;
use Socket\React\Datagram\Factory as SocketFactory;
Expand All @@ -16,6 +15,7 @@
use React\EventLoop\Timer\Timer;
use Socket\React\Datagram\Socket as Socket;
use Clue\Hexdump\Hexdump;
use function React\Promise\resolve;

/**
* ICMP (Internet Control Message Protocol) bindings for reactphp
Expand Down Expand Up @@ -53,7 +53,7 @@ public function __construct(LoopInterface $loop, Socket $socket = null)
*
* @param string $remote remote host or IP address to ping
* @param float $timeout maximum time in seconds to wait to receive pong
* @return React\Promise\PromiseInterface resolves with ping round trip time (RTT) in seconds or rejects with Exception
* @return \React\Promise\PromiseInterface resolves with ping round trip time (RTT) in seconds or rejects with Exception
*/
public function ping($remote, $timeout = 5.0)
{
Expand All @@ -64,7 +64,10 @@ public function ping($remote, $timeout = 5.0)

$result = new Deferred();

$timer = $this->loop->addTimer($timeout, function(Timer $timer) use ($that, $result, &$listener) {
$isTimerRunning = true;

$timer = $this->loop->addTimer($timeout, function(Timer $timer) use ($that, $result, &$listener, &$isTimerRunning) {
$isTimerRunning = false;
if ($listener) {
$that->removeListener(Message::TYPE_ECHO_REPLY, $listener);
}
Expand All @@ -78,15 +81,15 @@ public function ping($remote, $timeout = 5.0)
$start,
$timer,
$result,
&$listener
&$listener,
&$isTimerRunning,
$timeout
) {
if (!$timer->isActive()) {
if (!$isTimerRunning) {
// timeout occured while resolving hostname => already canceled, so don't even send a message
return;
}

$ping = $messageFactory->createMessagePing();

$that->sendMessage($ping, $remote);

$id = $ping->getPingId();
Expand All @@ -100,11 +103,12 @@ public function ping($remote, $timeout = 5.0)
$that,
$result,
$timer,
$start
$start,
$timeout
) {
if ($pong->getPingId() === $id && $pong->getPingSequence() === $sequence) {
$that->removeListener(Message::TYPE_ECHO_REPLY, $listener);
$timer->cancel();
$this->loop->cancelTimer($timer);

$time = microtime(true) - $start;
if ($time < 0) {
Expand Down Expand Up @@ -168,6 +172,6 @@ public function sendMessage(Message $message, $remoteAddress)

private function resolve($host)
{
return When::resolve($host);
return resolve($host);
}
}