public function syncSendAndReceive($buffer, stdClass $context)
{
$client = $this->client;
$timeout = $context->timeout / 1000;
$sec = floor($timeout);
$usec = ($timeout - $sec) * 1000;
$trycount = 0;
$errno = 0;
$errstr = '';
while ($trycount <= 1) {
if ($this->stream === null) {
$scheme = parse_url($client->uri, PHP_URL_SCHEME);
if ($scheme == 'unix') {
$this->stream = @pfsockopen('unix://' . parse_url($client->uri, PHP_URL_PATH));
} else {
$this->stream = @stream_socket_client($client->uri, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, stream_context_create($client->options));
}
if ($this->stream === false) {
$this->stream = null;
throw new Exception($errstr, $errno);
}
}
$stream = $this->stream;
@stream_set_read_buffer($stream, $client->readBuffer);
@stream_set_write_buffer($stream, $client->writeBuffer);
if (function_exists('socket_import_stream')) {
if ($scheme === 'tcp' || $scheme === 'unix') {
$socket = socket_import_stream($stream);
socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, (int) $client->keepAlive);
if ($scheme === 'tcp') {
socket_set_option($socket, SOL_TCP, TCP_NODELAY, (int) $client->noDelay);
}
}
}
if (@stream_set_timeout($stream, $sec, $usec) == false) {
if ($trycount > 0) {
throw $this->getLastError("unknown error");
}
$trycount++;
} else {
break;
}
}
if ($this->write($stream, $buffer) === false) {
throw $this->getLastError("request write error");
}
$response = $this->read($stream, $buffer);
if ($response === false) {
throw $this->getLastError("response read error");
}
return $response;
}