You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1.7 KiB
75 lines
1.7 KiB
<?PHP
|
|
|
|
namespace blobt\airpc;
|
|
|
|
use Swoole;
|
|
use blobt\airpc\Pack;
|
|
|
|
/**
|
|
* 基于swoole的轻量及 RPC
|
|
* 服务器
|
|
*
|
|
*/
|
|
class RpcServer extends BaseService {
|
|
|
|
/**
|
|
* @var string rpc项目入口文件
|
|
*/
|
|
public $entryPoint = false;
|
|
|
|
/**
|
|
* 初始化
|
|
*/
|
|
public function init() {
|
|
if (empty($this->entryPoint)) {
|
|
die("Please set the entry point." . PHP_EOL);
|
|
}
|
|
$this->setProtocol(Pack::$packageLengthType, Pack::$packageMaxLength, Pack::$packageLengthOffset, Pack::$packageBodyOffset);
|
|
}
|
|
|
|
/**
|
|
* 发送数据
|
|
* @param integral $fd 链接文件描述付
|
|
* @param string $data
|
|
* @return boolean
|
|
*/
|
|
public function send(int $fd, string $data) {
|
|
$pack = new Pack($data);
|
|
$this->server->send($fd, (string) $pack);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param Swoole\Server $server
|
|
* @param int $fd
|
|
* @param int $reactorId
|
|
* @param string $pack
|
|
*/
|
|
public function onReceive(Swoole\Server $server, int $fd, int $reactorId, string $pack) {
|
|
$pack = new Pack($pack);
|
|
$data = $pack->data;
|
|
$this->handle($fd, $data);
|
|
}
|
|
|
|
/**
|
|
* 请求处理函数
|
|
* @param int $fd
|
|
* @param string $pack
|
|
*/
|
|
public function handle(int $fd, string $data) {
|
|
$_SERVER['data'] = $data;
|
|
try {
|
|
$ret = include $this->entryPoint;
|
|
$this->send($fd, $ret);
|
|
} catch (\Error $e) {
|
|
//TODO 记录日志
|
|
print_r($e);
|
|
$this->send($fd, "YeeError");
|
|
} catch (\Exception $e) {
|
|
//TODO 记录日志
|
|
print_r($e);
|
|
$this->send($fd, "YeeException");
|
|
}
|
|
}
|
|
|
|
}
|