Skip to content

Commit

Permalink
优化rpc,udp,websocket的数据获取
Browse files Browse the repository at this point in the history
  • Loading branch information
bingcool committed Jun 11, 2018
1 parent ef6aa6b commit c377add
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 23 deletions.
33 changes: 12 additions & 21 deletions score/Core/SController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Swoolefy\Core\Hook;
use Swoolefy\Core\Application;
use Swoolefy\Core\BaseServer;
use Swoolefy\Tcp\TcpServer;

class SController extends Object {

Expand All @@ -37,6 +38,12 @@ class SController extends Object {
*/
public $fd = null;

/**
* $mixed_params
* @var
*/
public $mixed_params;

/**
* __construct
*/
Expand Down Expand Up @@ -135,47 +142,31 @@ public function isClientPackLength() {
* @return array
*/
public function getRpcPackHeader() {
if(BaseServer::getServiceProtocol() == SWOOLEFY_TCP) {
return $this->rpc_pack_header;
}else {
throw new \Exception("this method only can be called by TCP or RPC server!, because only rpc have pack setting!");
}
return Application::$app->getRpcPackHeader();
}

/**
* getRpcPackBodyParams 获取rpc的包体数据
* @return mixed
*/
public function getRpcPackBodyParams() {
if(BaseServer::getServiceProtocol() == SWOOLEFY_TCP) {
return $this->mixed_params;
}else {
throw new \Exception("this method only can be called by TCP or RPC server!, because only rpc have pack setting!");
}
return Application::$app->getRpcPackBodyParams();
}

/**
* getUdpData 获取udp的数据
* @return mixed
*/
public function getUdpData() {
if(BaseServer::getServiceProtocol() == SWOOLEFY_UDP) {
return $this->mixed_params;
}else {
throw new \Exception("this method only can be called by UDP server!");
}
return Application::$app->getUdpData();
}

/**
* getWebsockMsg 获取udp的参数
* getWebsockMsg 获取websocket的信息
* @return mixed
*/
public function getWebsockMsg() {
if(BaseServer::getServiceProtocol() == SWOOLEFY_WEBSOCKET) {
return $this->mixed_params;
}else {
throw new \Exception("this method only can be called by WEBSOCKET server!");
}
return Application::$app->getWebsockMsg();
}

/**
Expand Down
7 changes: 5 additions & 2 deletions score/Core/ServiceDispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Swoolefy\Core\Swfy;
use Swoolefy\Core\AppDispatch;
use Swoolefy\Core\Application;

class ServiceDispatch extends AppDispatch {
/**
Expand Down Expand Up @@ -41,7 +42,8 @@ public function __construct($callable, $params, $rpc_pack_header = []) {
parent::__construct();
$this->callable = $callable;
$this->params = $params;
$this->rpc_pack_header = $rpc_pack_header;
Application::$app->mixed_params = $params;
Application::$app->rpc_pack_header = $rpc_pack_header;
}

/**
Expand All @@ -62,10 +64,11 @@ public function dispatch() {

$class = str_replace('/','\\', $class);
$serviceInstance = new $class();
$serviceInstance->rpc_pack_header = $this->rpc_pack_header;
$serviceInstance->mixed_params = $this->params;
try{
$serviceInstance->$action($this->params);
}catch(\Exception $e) {
throw new \Exception($e->getMessage());
}catch(\Exception $e) {
throw new \Exception("when dispatch, create $class Instance Fatal error, $class is not exist or $action is not exist!", 1);
}
Expand Down
75 changes: 75 additions & 0 deletions score/Core/Swoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ class Swoole extends Object {
*/
public $fd = null;

/**
* $mixed_params rpc,udp,websocket传递的参数寄存属性
*/
public $mixed_params;

/**
* $rpc_pack_header rpc的包头数据
* @var array
*/
public $rpc_pack_header = [];

/**
* $ExceptionHanderClass 异常处理类
* @var string
Expand Down Expand Up @@ -104,6 +115,70 @@ public static function isTaskProcess() {
return (!self::isWorkerProcess()) ? true : false;
}

/**
* getRpcPackHeader 获取rpc的pack头信息,只适用于rpc服务
* @return array
*/
public function getRpcPackHeader() {
if($this->isWorkerProcess()) {
if(BaseServer::getServiceProtocol() == SWOOLEFY_TCP) {
return $this->rpc_pack_header;
}else {
throw new \Exception("this method only can be called by TCP or RPC server!, because only rpc have pack setting!");
}
}else {
throw new \Exception("getRpcPackHeader() only can use in worker process!", 1);
}
}

/**
* getRpcPackBodyParams 获取rpc的包体数据
* @return mixed
*/
public function getRpcPackBodyParams() {
if($this->isWorkerProcess()) {
if(BaseServer::getServiceProtocol() == SWOOLEFY_TCP) {
return $this->mixed_params;
}else {
throw new \Exception("this method only can be called by TCP or RPC server!, because only rpc have pack setting!");
}
}else {
throw new \Exception("getRpcPackBodyParams() only can use in worker process!", 1);
}
}

/**
* getUdpData 获取udp的数据
* @return mixed
*/
public function getUdpData() {
if($this->isWorkerProcess()) {
if(BaseServer::getServiceProtocol() == SWOOLEFY_UDP) {
return $this->mixed_params;
}else {
throw new \Exception("this method only can be called by UDP server!");
}
}else {
throw new \Exception("getUdpData() only can use in worker process!", 1);
}
}

/**
* getWebsockMsg 获取websocket的信息
* @return mixed
*/
public function getWebsockMsg() {
if($this->isWorkerProcess()) {
if(BaseServer::getServiceProtocol() == SWOOLEFY_WEBSOCKET) {
return $this->mixed_params;
}else {
throw new \Exception("this method only can be called by WEBSOCKET server!");
}
}else {
throw new \Exception("getWebsockMsg() only can use in worker process!", 1);
}
}

/**
* afterRequest 请求结束后注册钩子执行操作
* @param mixed $callback
Expand Down

0 comments on commit c377add

Please sign in to comment.