Skip to content

Commit

Permalink
Async file reading and dns_lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
mbonneau committed Apr 19, 2018
1 parent 7491223 commit b1ae10e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/Async/dns_lookup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

require_once __DIR__ . '/../../vendor/autoload.php';

$ip = \Rx\Swoole\Async::dns_lookup('www.google.com');

$ip->subscribe(
'print_r',
function (Throwable $throwable) {
echo "Error: " . $throwable->getMessage() . "\n";
},
function () {
echo "Completed.\n";
});
16 changes: 16 additions & 0 deletions examples/Async/read.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

require_once __DIR__ . '/../../vendor/autoload.php';

$file = \Rx\Swoole\Async::read(
__DIR__ . '/some_file.txt',
5);

$file->subscribe(
'print_r',
function (Throwable $throwable) {
echo "Error: " . $throwable->getMessage() . "\n";
},
function () {
echo "Completed.\n";
});
1 change: 1 addition & 0 deletions examples/Async/some_file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
62 changes: 62 additions & 0 deletions src/Async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Rx\Swoole;

use Rx\Disposable\CallbackDisposable;
use Rx\Disposable\EmptyDisposable;
use Rx\Observable\AnonymousObservable;
use Rx\ObserverInterface;

final class Async
{
public static function read(string $filename, int $size = 8192, int $offset = 0)
{
return new AnonymousObservable(function (ObserverInterface $observer) use ($filename, $size, $offset) {
$disposed = false;

$disposable = new CallbackDisposable(function () use (&$disposed) {
$disposed = true;
});

$callback = function ($filename, $data) use ($observer, &$disposed) {
if ($data === '') { // read complete
$observer->onCompleted();
return false;
}
if ($disposed) {
return false;
}

$observer->onNext($data);
return true;
};

$retVal = \Swoole\Async::read($filename, $callback, $size, $offset);

if ($retVal === false) {
$observer->onError(new \Exception('Swoole\Asynd::read returned false'));
return;
}

return $disposable;
});
}

public static function dns_lookup($host)
{
return new AnonymousObservable(function (ObserverInterface $observer) use ($host) {
$callback = function ($host, $ip) use ($observer) {
if (empty($ip)) {
$observer->onError(new \Exception('dns_lookup failed'));
}

$observer->onNext($ip);
$observer->onCompleted();
};

swoole_async_dns_lookup($host, $callback);

return new EmptyDisposable();
});
}
}

0 comments on commit b1ae10e

Please sign in to comment.