public function onReceive($server, $fd, $fromId, $data)
{
$data = trim($data);
if ($data === '') {
return;
}
/**
* @var \Swoole\Server $server
*/
$tmp = @msgpack_unpack($data);
if (is_object($tmp) && $tmp instanceof \stdClass) {
$rpc = static::$defaultRPC;
if ($tmp->rpc) {
if (preg_match('#^[\\\\0-9a-z]+$#i', $tmp->rpc)) {
$rpc = $tmp->rpc;
}
}
/**
* @var RPC $rpc
*/
$key = $rpc::_getRpcKey();
if ($key) {
# 解密数据
$tmp = self::decryption($tmp, $key, $rpc);
if (false === $tmp) {
# 数据错误
$server->send($fd, '{"type":"close","code":0,"msg":"decryption fail."}' . static::$EOF);
$server->close($fd, $fromId);
\MyQEE\Server\Server::$instance->debug("register server decryption error, data: " . substr($data, 0, 256) . (strlen($data) > 256 ? '...' : ''));
return;
}
}
$data = $tmp;
unset($tmp);
if ('bind' === $data->type) {
if ($this->server->setting['dispatch_mode'] === 5 && is_int($data->id)) {
# 支持 worker 绑定
$this->server->bind($fd, $data->id);
}
return;
}
$data->rpc = $rpc;
if (in_array($this->server->setting['dispatch_mode'], [1, 3]) && ($workerNum = $this->server->setting['worker_num']) > 1) {
# 服务器是轮循或抢占模式, 每次请求的可能不是同一个 worker
$workerId = $fd % $workerNum;
if ($workerId !== $this->server->worker_id) {
# 发送给对应的进程去处理
$obj = new \stdClass();
$obj->type = 'call';
$obj->fd = $fd;
$obj->fromId = $fromId;
$obj->data = $data;
$this->sendMessage($data, $workerId);
return;
}
}
# 执行RPC调用
$this->call($fd, $fromId, $data);
} else {
\MyQEE\Server\Server::$instance->warn("rpc get error msgpack data: " . substr($data, 0, 256) . (strlen($data) > 256 ? '...' : ''));
$this->server->close($fd, $fromId);
return;
}
}