2017-06-08 06:33:13 +02:00
|
|
|
<?php
|
2017-11-10 18:35:47 +01:00
|
|
|
|
2017-06-08 06:33:13 +02:00
|
|
|
namespace Amp\Parallel\Sync;
|
|
|
|
|
|
|
|
use Amp\CallableMaker;
|
|
|
|
use Amp\Parser\Parser;
|
|
|
|
|
|
|
|
class ChannelParser extends Parser {
|
|
|
|
use CallableMaker;
|
|
|
|
|
|
|
|
const HEADER_LENGTH = 5;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable(mixed $data) Callback invoked when data is parsed.
|
|
|
|
*/
|
|
|
|
public function __construct(callable $callback) {
|
|
|
|
parent::__construct(self::parser($callback, self::callableFromStaticMethod("errorHandler")));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $data Data to encode to send over a channel.
|
|
|
|
*
|
|
|
|
* @return string Encoded data that can be parsed by this class.
|
|
|
|
*
|
|
|
|
* @throws \Amp\Parallel\Sync\SerializationException
|
|
|
|
*/
|
|
|
|
public function encode($data): string {
|
|
|
|
try {
|
|
|
|
$data = \serialize($data);
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
throw new SerializationException(
|
2017-07-21 06:58:13 +02:00
|
|
|
"The given data cannot be sent because it is not serializable.",
|
|
|
|
$exception
|
2017-06-08 06:33:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return \pack("CL", 0, \strlen($data)) . $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-22 23:12:55 +01:00
|
|
|
* @param callable $push
|
2017-06-08 06:33:13 +02:00
|
|
|
* @param callable $errorHandler
|
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*
|
|
|
|
* @throws \Amp\Parallel\Sync\ChannelException
|
|
|
|
* @throws \Amp\Parallel\Sync\SerializationException
|
|
|
|
*/
|
2018-01-22 23:12:55 +01:00
|
|
|
private static function parser(callable $push, callable $errorHandler): \Generator {
|
2017-06-08 06:33:13 +02:00
|
|
|
while (true) {
|
2017-06-17 20:46:22 +02:00
|
|
|
$header = yield self::HEADER_LENGTH;
|
|
|
|
$data = \unpack("Cprefix/Llength", $header);
|
2017-06-08 06:33:13 +02:00
|
|
|
|
|
|
|
if ($data["prefix"] !== 0) {
|
2018-01-22 23:12:55 +01:00
|
|
|
$data = $header . yield;
|
|
|
|
|
|
|
|
$data = \preg_replace_callback("/[^\x20-\x7e]/", function (array $matches) {
|
|
|
|
return "\\x" . \dechex(\ord($matches[0]));
|
|
|
|
}, $data);
|
|
|
|
|
|
|
|
throw new ChannelException("Invalid packet received: " . $data);
|
2017-06-08 06:33:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$data = yield $data["length"];
|
|
|
|
|
|
|
|
\set_error_handler($errorHandler);
|
|
|
|
|
|
|
|
// Attempt to unserialize the received data.
|
|
|
|
try {
|
|
|
|
$data = \unserialize($data);
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
throw new SerializationException("Exception thrown when unserializing data", $exception);
|
|
|
|
} finally {
|
|
|
|
\restore_error_handler();
|
|
|
|
}
|
|
|
|
|
2018-01-22 23:12:55 +01:00
|
|
|
$push($data);
|
2017-06-08 06:33:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function errorHandler($errno, $errstr, $errfile, $errline) {
|
|
|
|
if ($errno & \error_reporting()) {
|
|
|
|
throw new ChannelException(\sprintf(
|
|
|
|
'Received corrupted data. Errno: %d; %s in file %s on line %d',
|
|
|
|
$errno,
|
|
|
|
$errstr,
|
|
|
|
$errfile,
|
|
|
|
$errline
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|