protected function __send($type, $name, $args = null)
{
if (!$this->__client) {
return false;
}
$obj = new \stdClass();
$obj->id = microtime(1);
$obj->type = $type;
$obj->name = $name;
if ($args) {
$obj->args = $args;
}
/**
* @var RPC $rpc
*/
$rpc = $this->__rpc;
$key = $rpc::_getRpcKey();
$str = Server::encrypt($obj, $key, $this->__rpc) . Server::$EOF;
$len = strlen($str);
$sock = fopen("php://fd/{$this->__client->sock}", 'w');
$wLen = fwrite($sock, $str, $len);
if ($wLen !== $len) {
return false;
}
$rs = fread($sock, 1048576);
fclose($sock);
if ($rs[0] === '{') {
$rs = @json_decode($rs);
} else {
$rs = msgpack_unpack($rs);
if ($key) {
$rs = Server::decryption($rs, $key);
}
}
if (is_object($rs) && $rs instanceof \stdClass) {
switch ($rs->type) {
case 'error':
# 系统返回了一个错误
throw new \Exception($rs->msg, $rs->code);
break;
case 'close':
$this->__closeByServer = true;
throw new \Exception($rs->msg, $rs->code);
break;
}
}
return $rs;
}