function decode($response, array &$args, stdClass $context)
{
if ($context->oneway) {
return null;
}
if (empty($response)) {
throw new Exception("EOF");
}
if ($response[strlen($response) - 1] !== Tags::TagEnd) {
throw new Exception("Wrong Response: \r\n{$response}");
}
$mode = $context->mode;
if ($mode === ResultMode::RawWithEndTag) {
return $response;
} elseif ($mode === ResultMode::Raw) {
return substr($response, 0, -1);
}
$stream = new BytesIO($response);
$reader = new Reader($stream);
$result = null;
$tag = $stream->getc();
if ($tag === Tags::TagResult) {
if ($mode === ResultMode::Normal) {
$result = $reader->unserialize();
} elseif ($mode === ResultMode::Serialized) {
$result = $reader->readRaw()->toString();
}
$tag = $stream->getc();
if ($tag === Tags::TagArgument) {
$reader->reset();
$arguments = $reader->readList();
$n = min(count($arguments), count($args));
for ($i = 0; $i < $n; $i++) {
$args[$i] = $arguments[$i];
}
$tag = $stream->getc();
}
} elseif ($tag === Tags::TagError) {
$e = new Exception($reader->readString());
$stream->close();
throw $e;
}
if ($tag !== Tags::TagEnd) {
$stream->close();
throw new Exception("Wrong Response: \r\n{$response}");
}
$stream->close();
return $result;
}