blobt
4 years ago
14 changed files with 778 additions and 669 deletions
-
15cert/private_key.pem
-
6cert/pub_key.pem
-
4composer.json
-
414composer.lock
-
35examples/client.php
-
2examples/index.php
-
206examples/rsa_test.php
-
4examples/server.php
-
63examples/test.php
-
242src/BaseService.php
-
160src/Pack.php
-
25src/Protocol.php
-
257src/RpcServer.php
-
14src/Yee.php
@ -0,0 +1,15 @@ |
|||
-----BEGIN RSA PRIVATE KEY----- |
|||
MIICXgIBAAKBgQDsldu7vayXD7RE5HEMsvlz/tkq36gBgY1Yz+R9dkZWNDdQTTFm |
|||
O2tWFwBvYRx5Nv/CtR1Nl2+pb/Nd6WrWzs9FR7P84nixA+vfzQjqTIlQViO0vB/a |
|||
gWa2nk5N4e0dsQ7kDr3+7/w1DeFsMlRBDvSHMsdmlNSFDFB6+MVrC93dRQIDAQAB |
|||
AoGBALhxeJZcIDLA7oSLKEuDOJEjDQGszhkmfCDSKstZKyrHmJwFIk8WYe8DDbbH |
|||
meAybaDJfGfieQ46lhwP/tgq8VEM7m/bhkhksXHTuKmPGfr53uev9Rp1+TKG1vFW |
|||
2c8MFMrDg3ZNy8lAfZmhIWUP9ietU5o836d0/3vJ+nBeSZwhAkEA+0luT5ijhfh4 |
|||
Jim81h0yKJFOeg/gjdHtefAWyruAGHZEKz+yLNTPJN0bjPtQAOqSr/Hjt9MmxzPa |
|||
HJ0DuvvgbQJBAPEF1mYmqmpBZTDc2zjOTXzVRsNO/bBcx/q9GkQydn/7BV6Dqr9Y |
|||
cgJV49CWcbZBHLkCxyidKDvZBzTh0b/vWTkCQH3AsLianXLk9lO63WqVb82Hcit5 |
|||
mJnAbcRxFybbkG4teU3fcW4oDYXYNPpnphDfUwtD1P7gZ26XD1b0aLKBbD0CQQC3 |
|||
sCY9Rfyc7GgNfFTLD1e46uZ6VUo8zglPDC3niboMQahO5vQLzoFdTE5Km03gg/E7 |
|||
A2X7iwKt0fKPGAd+SxDpAkEA6OG77IL8cjrlZKk4ilTCz8rkF4CtZWbDLi1IF40i |
|||
jaGlSLvVowHDywtjT8tYfpIH7HI55KS3gj88qAaF38qfRA== |
|||
-----END RSA PRIVATE KEY----- |
@ -0,0 +1,6 @@ |
|||
-----BEGIN PUBLIC KEY----- |
|||
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDsldu7vayXD7RE5HEMsvlz/tkq |
|||
36gBgY1Yz+R9dkZWNDdQTTFmO2tWFwBvYRx5Nv/CtR1Nl2+pb/Nd6WrWzs9FR7P8 |
|||
4nixA+vfzQjqTIlQViO0vB/agWa2nk5N4e0dsQ7kDr3+7/w1DeFsMlRBDvSHMsdm |
|||
lNSFDFB6+MVrC93dRQIDAQAB |
|||
-----END PUBLIC KEY----- |
@ -0,0 +1,206 @@ |
|||
<?php |
|||
|
|||
include __DIR__ . "/../vendor/autoload.php"; |
|||
|
|||
use \Firebase\JWT\JWT; |
|||
|
|||
$key = "73937393"; |
|||
|
|||
$privateKey = <<<EOD |
|||
-----BEGIN RSA PRIVATE KEY----- |
|||
MIICXgIBAAKBgQDsldu7vayXD7RE5HEMsvlz/tkq36gBgY1Yz+R9dkZWNDdQTTFm |
|||
O2tWFwBvYRx5Nv/CtR1Nl2+pb/Nd6WrWzs9FR7P84nixA+vfzQjqTIlQViO0vB/a |
|||
gWa2nk5N4e0dsQ7kDr3+7/w1DeFsMlRBDvSHMsdmlNSFDFB6+MVrC93dRQIDAQAB |
|||
AoGBALhxeJZcIDLA7oSLKEuDOJEjDQGszhkmfCDSKstZKyrHmJwFIk8WYe8DDbbH |
|||
meAybaDJfGfieQ46lhwP/tgq8VEM7m/bhkhksXHTuKmPGfr53uev9Rp1+TKG1vFW |
|||
2c8MFMrDg3ZNy8lAfZmhIWUP9ietU5o836d0/3vJ+nBeSZwhAkEA+0luT5ijhfh4 |
|||
Jim81h0yKJFOeg/gjdHtefAWyruAGHZEKz+yLNTPJN0bjPtQAOqSr/Hjt9MmxzPa |
|||
HJ0DuvvgbQJBAPEF1mYmqmpBZTDc2zjOTXzVRsNO/bBcx/q9GkQydn/7BV6Dqr9Y |
|||
cgJV49CWcbZBHLkCxyidKDvZBzTh0b/vWTkCQH3AsLianXLk9lO63WqVb82Hcit5 |
|||
mJnAbcRxFybbkG4teU3fcW4oDYXYNPpnphDfUwtD1P7gZ26XD1b0aLKBbD0CQQC3 |
|||
sCY9Rfyc7GgNfFTLD1e46uZ6VUo8zglPDC3niboMQahO5vQLzoFdTE5Km03gg/E7 |
|||
A2X7iwKt0fKPGAd+SxDpAkEA6OG77IL8cjrlZKk4ilTCz8rkF4CtZWbDLi1IF40i |
|||
jaGlSLvVowHDywtjT8tYfpIH7HI55KS3gj88qAaF38qfRA== |
|||
-----END RSA PRIVATE KEY----- |
|||
EOD; |
|||
|
|||
|
|||
$publicKey = <<<EOD |
|||
-----BEGIN PUBLIC KEY----- |
|||
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDsldu7vayXD7RE5HEMsvlz/tkq |
|||
36gBgY1Yz+R9dkZWNDdQTTFmO2tWFwBvYRx5Nv/CtR1Nl2+pb/Nd6WrWzs9FR7P8 |
|||
4nixA+vfzQjqTIlQViO0vB/agWa2nk5N4e0dsQ7kDr3+7/w1DeFsMlRBDvSHMsdm |
|||
lNSFDFB6+MVrC93dRQIDAQAB |
|||
-----END PUBLIC KEY----- |
|||
EOD; |
|||
|
|||
|
|||
$payload = [ |
|||
'id' => 'backend', |
|||
'basePath' => dirname(__DIR__), |
|||
'controllerNamespace' => 'backend\controllers', |
|||
'bootstrap' => ['log'], |
|||
'components' => [ |
|||
'assetManager' => [ |
|||
'baseUrl' => '@web' . (getenv('KUBERNETES_NAMESPACE') ? '/' . getenv('KUBERNETES_NAMESPACE') : '') . '/assets' |
|||
], |
|||
'request' => [ |
|||
'parsers' => [ |
|||
'application/json' => 'yii\web\JsonParser', |
|||
], |
|||
'csrfParam' => '_csrf-api', |
|||
], |
|||
'user' => [ |
|||
'identityClass' => 'backend\models\User', |
|||
'enableAutoLogin' => true, |
|||
'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true], |
|||
], |
|||
'session' => [ |
|||
// this is the name of the session cookie used for login on the app
|
|||
'name' => 'backend', |
|||
], |
|||
'log' => [ |
|||
'traceLevel' => 1, |
|||
'targets' => [ |
|||
[ |
|||
'class' => 'yii\log\FileTarget', |
|||
'levels' => ['error', 'warning'], |
|||
], |
|||
[ |
|||
'class' => 'yii\log\FileTarget', |
|||
'levels' => ['info', 'error'], |
|||
'categories' => ['imagetest'], |
|||
'logFile' => '@app/runtime/logs/imagetest.log', |
|||
'logVars' => [], |
|||
'exportInterval' => 1, |
|||
'prefix' => function ($message) { |
|||
|
|||
} |
|||
], |
|||
[ |
|||
'class' => 'yii\log\FileTarget', |
|||
'levels' => ['error', 'warning', 'info'], |
|||
'categories' => ['refund_log'], |
|||
'logFile' => '@app/runtime/logs/refund_log.log', |
|||
'logVars' => [], |
|||
'exportInterval' => 1, |
|||
'prefix' => function ($message) { |
|||
|
|||
} |
|||
], |
|||
[ |
|||
'class' => 'yii\log\FileTarget', |
|||
'levels' => ['error', 'warning', 'info'], |
|||
'categories' => ['obj'], |
|||
'logFile' => '@app/runtime/logs/obj.log', |
|||
'logVars' => [], |
|||
'exportInterval' => 1, |
|||
'prefix' => function ($message) { |
|||
|
|||
} |
|||
], |
|||
], |
|||
], |
|||
'errorHandler' => [ |
|||
'errorAction' => 'site/error', |
|||
], |
|||
'urlManager' => [ |
|||
'enablePrettyUrl' => true, |
|||
'showScriptName' => false, |
|||
'rules' => |
|||
getenv('KUBERNETES_NAMESPACE') ? [ |
|||
getenv('KUBERNETES_NAMESPACE') . '/<rule:[\S-]+>' => '<rule>' |
|||
] : [], |
|||
], |
|||
'goods' => ['class' => 'goods\goods\logic\goods\GoodsManager'], |
|||
], |
|||
'as access' => [ |
|||
'class' => 'iron\components\AccessControl', |
|||
'allowActions' => [ |
|||
'site/catch-permission', |
|||
] |
|||
], |
|||
'params' => [ |
|||
'assetManager' => [ |
|||
'baseUrl' => '@web' . (getenv('KUBERNETES_NAMESPACE') ? '/' . getenv('KUBERNETES_NAMESPACE') : '') . '/assets' |
|||
], |
|||
'request' => [ |
|||
'parsers' => [ |
|||
'application/json' => 'yii\web\JsonParser', |
|||
], |
|||
'csrfParam' => '_csrf-api', |
|||
], |
|||
'user' => [ |
|||
'identityClass' => 'backend\models\User', |
|||
'enableAutoLogin' => true, |
|||
'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true], |
|||
], |
|||
'session' => [ |
|||
// this is the name of the session cookie used for login on the app
|
|||
'name' => 'backend', |
|||
], |
|||
'log' => [ |
|||
'traceLevel' => 1, |
|||
'targets' => [ |
|||
[ |
|||
'class' => 'yii\log\FileTarget', |
|||
'levels' => ['error', 'warning'], |
|||
], |
|||
[ |
|||
'class' => 'yii\log\FileTarget', |
|||
'levels' => ['info', 'error'], |
|||
'categories' => ['imagetest'], |
|||
'logFile' => '@app/runtime/logs/imagetest.log', |
|||
'logVars' => [], |
|||
'exportInterval' => 1, |
|||
'prefix' => function ($message) { |
|||
|
|||
} |
|||
], |
|||
[ |
|||
'class' => 'yii\log\FileTarget', |
|||
'levels' => ['error', 'warning', 'info'], |
|||
'categories' => ['refund_log'], |
|||
'logFile' => '@app/runtime/logs/refund_log.log', |
|||
'logVars' => [], |
|||
'exportInterval' => 1, |
|||
'prefix' => function ($message) { |
|||
|
|||
} |
|||
], |
|||
[ |
|||
'class' => 'yii\log\FileTarget', |
|||
'levels' => ['error', 'warning', 'info'], |
|||
'categories' => ['obj'], |
|||
'logFile' => '@app/runtime/logs/obj.log', |
|||
'logVars' => [], |
|||
'exportInterval' => 1, |
|||
'prefix' => function ($message) { |
|||
|
|||
} |
|||
], |
|||
], |
|||
], |
|||
'errorHandler' => [ |
|||
'errorAction' => 'site/error', |
|||
], |
|||
'urlManager' => [ |
|||
'enablePrettyUrl' => true, |
|||
'showScriptName' => false, |
|||
'rules' => |
|||
getenv('KUBERNETES_NAMESPACE') ? [ |
|||
getenv('KUBERNETES_NAMESPACE') . '/<rule:[\S-]+>' => '<rule>' |
|||
] : [], |
|||
], |
|||
'goods' => ['class' => 'goods\goods\logic\goods\GoodsManager'], |
|||
], |
|||
]; |
|||
|
|||
$jwt = JWT::encode($payload, $privateKey, 'RS256'); |
|||
echo strlen($jwt) . "\n"; |
|||
echo "Encode:\n" . print_r($jwt, true) . "\n"; |
|||
|
|||
$decoded = JWT::decode($jwt, $publicKey, array('RS256')); |
|||
|
|||
$decoded_array = (array) $decoded; |
|||
echo "Decode:\n" . print_r($decoded_array, true) . "\n"; |
@ -1,17 +1,50 @@ |
|||
<?php |
|||
$units = array(); |
|||
for($i=0;$i<1000000;$i++){ |
|||
$units[] = uniqid(); |
|||
echo $units[$i].PHP_EOL; |
|||
} |
|||
$values = array_count_values($units); |
|||
$duplicates = []; |
|||
foreach($values as $k=>$v){ |
|||
if($v>1){ |
|||
$duplicates[$k]=$v; |
|||
} |
|||
} |
|||
echo '<pre>'; |
|||
print_r($duplicates); |
|||
echo '</pre>'; |
|||
|
|||
include __DIR__ . "/../vendor/autoload.php"; |
|||
|
|||
use \Firebase\JWT\JWT; |
|||
|
|||
$privateKey = <<<EOD |
|||
-----BEGIN RSA PRIVATE KEY----- |
|||
MIICXgIBAAKBgQDsldu7vayXD7RE5HEMsvlz/tkq36gBgY1Yz+R9dkZWNDdQTTFm |
|||
O2tWFwBvYRx5Nv/CtR1Nl2+pb/Nd6WrWzs9FR7P84nixA+vfzQjqTIlQViO0vB/a |
|||
gWa2nk5N4e0dsQ7kDr3+7/w1DeFsMlRBDvSHMsdmlNSFDFB6+MVrC93dRQIDAQAB |
|||
AoGBALhxeJZcIDLA7oSLKEuDOJEjDQGszhkmfCDSKstZKyrHmJwFIk8WYe8DDbbH |
|||
meAybaDJfGfieQ46lhwP/tgq8VEM7m/bhkhksXHTuKmPGfr53uev9Rp1+TKG1vFW |
|||
2c8MFMrDg3ZNy8lAfZmhIWUP9ietU5o836d0/3vJ+nBeSZwhAkEA+0luT5ijhfh4 |
|||
Jim81h0yKJFOeg/gjdHtefAWyruAGHZEKz+yLNTPJN0bjPtQAOqSr/Hjt9MmxzPa |
|||
HJ0DuvvgbQJBAPEF1mYmqmpBZTDc2zjOTXzVRsNO/bBcx/q9GkQydn/7BV6Dqr9Y |
|||
cgJV49CWcbZBHLkCxyidKDvZBzTh0b/vWTkCQH3AsLianXLk9lO63WqVb82Hcit5 |
|||
mJnAbcRxFybbkG4teU3fcW4oDYXYNPpnphDfUwtD1P7gZ26XD1b0aLKBbD0CQQC3 |
|||
sCY9Rfyc7GgNfFTLD1e46uZ6VUo8zglPDC3niboMQahO5vQLzoFdTE5Km03gg/E7 |
|||
A2X7iwKt0fKPGAd+SxDpAkEA6OG77IL8cjrlZKk4ilTCz8rkF4CtZWbDLi1IF40i |
|||
jaGlSLvVowHDywtjT8tYfpIH7HI55KS3gj88qAaF38qfRA== |
|||
-----END RSA PRIVATE KEY----- |
|||
EOD; |
|||
|
|||
|
|||
$publicKey = <<<EOD |
|||
-----BEGIN PUBLIC KEY----- |
|||
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDsldu7vayXD7RE5HEMsvlz/tkq |
|||
36gBgY1Yz+R9dkZWNDdQTTFmO2tWFwBvYRx5Nv/CtR1Nl2+pb/Nd6WrWzs9FR7P8 |
|||
4nixA+vfzQjqTIlQViO0vB/agWa2nk5N4e0dsQ7kDr3+7/w1DeFsMlRBDvSHMsdm |
|||
lNSFDFB6+MVrC93dRQIDAQAB |
|||
-----END PUBLIC KEY----- |
|||
EOD; |
|||
|
|||
$data = [ |
|||
'id' => 23, |
|||
'name' => 'nemo' |
|||
]; |
|||
|
|||
|
|||
|
|||
//$encode = JWT::encode($data, $privateKey, 'RS256');
|
|||
$encode = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eeyJpZCI6MjMsIm5hbWUiOiJuZW1vIn0.xJt8NHFJeQyiWSmAI1C_t2lX99QHChXpR7iNkTQ5oi7_87NPo_S4gbC2zh40SOmv9uk3EoRvsrzzRbtPSeh7D5oCbSVXcqvVwMq0zwYBgW6vyL5DMkuDGyQQpyVGuywy8xY9N9bwUtiL7tVj_S22L1jaYOw7SZtGU4eDa5vU_DM"; |
|||
echo "encode: " . $encode . "\n"; |
|||
|
|||
|
|||
$decoded = (array) JWT::decode($encode, $publicKey, array('RS256')); |
|||
|
|||
print_r($decoded); |
|||
?>
|
@ -0,0 +1,242 @@ |
|||
<?php |
|||
|
|||
/* |
|||
* To change this license header, choose License Headers in Project Properties. |
|||
* To change this template file, choose Tools | Templates |
|||
* and open the template in the editor. |
|||
*/ |
|||
|
|||
namespace blobt\airpc; |
|||
|
|||
use Swoole; |
|||
|
|||
/** |
|||
* Description of Service |
|||
* |
|||
* @author blobt |
|||
*/ |
|||
abstract class BaseService { |
|||
|
|||
/** |
|||
* @var Swoole\Http\Server |
|||
*/ |
|||
public $server = null; |
|||
|
|||
/** |
|||
* @var integral worker进程数量,worker进程是用来处理链接的,默认是cpu核心数 |
|||
*/ |
|||
public $workerNum = 4; |
|||
|
|||
/** |
|||
* @var integral reactor线程数量,reactor线程是用来分发fd到worker的,默认是cpu核心数 |
|||
*/ |
|||
public $reactorNum = 1; |
|||
|
|||
/** |
|||
* @var integral task进程数目 |
|||
*/ |
|||
public $taskWorkerNum = 0; |
|||
|
|||
/** |
|||
* @var integral 最大处理任务数量,处理玩最大任务数量个任务后,进程会自动烧毁,用来防止内存泄漏 |
|||
*/ |
|||
public $maxRequest = 0; |
|||
|
|||
/** |
|||
* @var integral 最大链接数 |
|||
*/ |
|||
public $maxConn = 1000; |
|||
|
|||
/** |
|||
* @var bool是否以守护进程模式运行 |
|||
*/ |
|||
public $daemonize = false; |
|||
|
|||
/** |
|||
* @var Swoole\Table |
|||
*/ |
|||
public $table = null; |
|||
|
|||
/** |
|||
* @var integral 发送序列号 |
|||
*/ |
|||
private $serid = 0; |
|||
|
|||
/** |
|||
* @var integral 接收序列号 |
|||
*/ |
|||
private $revid = 0; |
|||
|
|||
/** |
|||
* @var string 全局数据 |
|||
*/ |
|||
public $data; |
|||
|
|||
/** |
|||
* @var array swoole配置数组 |
|||
*/ |
|||
public $config; |
|||
|
|||
/** |
|||
* @param string $listen 监听地址 |
|||
* @param integral $port 监听端口 |
|||
*/ |
|||
public function __construct($listen, $port) { |
|||
$this->server = new \Swoole\Server($listen, $port, SWOOLE_PROCESS, SWOOLE_SOCK_TCP); |
|||
$this->config = [ |
|||
'worker_num' => $this->workerNum, |
|||
'reactor_num' => $this->reactorNum, |
|||
'task_worker_num' => $this->taskWorkerNum, |
|||
'max_request' => $this->maxRequest, |
|||
'max_conn' => $this->maxConn, |
|||
'daemonize' => $this->daemonize |
|||
]; |
|||
} |
|||
|
|||
/** |
|||
* 初始化 |
|||
*/ |
|||
public function init() { |
|||
|
|||
} |
|||
|
|||
/** |
|||
* 注册swoole事件 |
|||
* 所有以on开头函数都会被认为是swoole事件函数,函数名为事件名,并自动注册 |
|||
*/ |
|||
private function setEvents() { |
|||
$methods = get_class_methods(self::class); |
|||
foreach ($methods as $method) { |
|||
if (strncmp($method, "on", 2) === 0) { |
|||
$event = substr($method, 2); |
|||
$this->server->on($event, [$this, $method]); |
|||
} |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 配置swoole参数 |
|||
*/ |
|||
private function setConfig() { |
|||
$this->server->set($this->config); |
|||
} |
|||
|
|||
/** |
|||
* 设置协议 |
|||
* @param char $type 储存包体长度的字节,N代表大端字节序的4个字节 |
|||
* @param integral $maxLength 数据包最大长度 |
|||
* @param integral $lengthOffset 从包体的第几个字节开始是包体长度信息 |
|||
* @param integral $bodyOffset 从包体的第几个字节开始是包体信息 |
|||
*/ |
|||
public function setProtocol($type, $maxLength, $lengthOffset, $bodyOffset) { |
|||
$this->config['open_length_check'] = true; |
|||
$this->config['package_length_type'] = $type; |
|||
$this->config['package_max_length'] = $maxLength; |
|||
$this->config['package_length_offset'] = $lengthOffset; |
|||
$this->config['package_body_offset'] = $bodyOffset; |
|||
} |
|||
|
|||
/** |
|||
* master 进程启动时候执行函数 |
|||
* @param Swoole\Server $server |
|||
*/ |
|||
public function onStart(Swoole\Server $server) { |
|||
swoole_set_process_name("ai:master"); |
|||
} |
|||
|
|||
/** |
|||
* manager进程启动时候执行函数 |
|||
* @param Swoole\Server $server |
|||
*/ |
|||
public function onManagerStart(Swoole\Server $server) { |
|||
swoole_set_process_name("ai:manager"); |
|||
} |
|||
|
|||
/** |
|||
* worker进程启动后执行的回调 |
|||
* @param Swoole\Server $server |
|||
* @param int $workerId |
|||
*/ |
|||
public function onWorkerStart(Swoole\Server $server, int $workerId) { |
|||
swoole_set_process_name("ai:worker{$workerId}"); |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* @param Swoole\Server $server |
|||
* @param int $fd |
|||
* @param int $reactorId |
|||
* @param string $pack |
|||
*/ |
|||
abstract public function onReceive(Swoole\Server $server, int $fd, int $reactorId, string $pack); |
|||
|
|||
/** |
|||
* task 处理函数 |
|||
* @param Swoole\Server $server |
|||
* @param int $task_id |
|||
* @param int $src_worker_id |
|||
* @param mixed $data |
|||
*/ |
|||
public function onTask(Swoole\Server $server, int $task_id, int $src_worker_id, $data) { |
|||
//TODO
|
|||
} |
|||
|
|||
/** |
|||
* 链接成功事件处理 |
|||
* @param Swoole\Server $server |
|||
* @param int $fd |
|||
* @param int $reactorId |
|||
*/ |
|||
public function onConnect(Swoole\Server $server, int $fd, int $reactorId) { |
|||
//TODO
|
|||
} |
|||
|
|||
/** |
|||
* 错误处理 |
|||
* @param Swoole\Server $server |
|||
* @param int $worker_id |
|||
* @param int $worker_pid |
|||
* @param int $exit_code |
|||
* @param int $signal |
|||
*/ |
|||
function onWorkerError(Swoole\Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal) { |
|||
//TODO
|
|||
} |
|||
|
|||
/** |
|||
* worker进程停止 |
|||
* @param Swoole\Server $server |
|||
* @param int $workerId |
|||
*/ |
|||
function onWorkerStop(Swoole\Server $server, int $workerId) { |
|||
//TODO
|
|||
} |
|||
|
|||
/** |
|||
* 关闭链接时候触发 |
|||
* @param Swoole\Server $server |
|||
* @param int $fd |
|||
* @param int $reactorId |
|||
*/ |
|||
public function onClose(Swoole\Server $server, int $fd, int $reactorId) { |
|||
//TODO
|
|||
} |
|||
|
|||
/** |
|||
* 启动swoole |
|||
*/ |
|||
public function start() { |
|||
$this->server->start(); |
|||
} |
|||
|
|||
/** |
|||
* 服务启动 |
|||
*/ |
|||
public function run() { |
|||
$this->init(); |
|||
$this->setConfig(); |
|||
$this->setEvents(); |
|||
$this->start(); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,160 @@ |
|||
<?php |
|||
|
|||
/* |
|||
* To change this license header, choose License Headers in Project Properties. |
|||
* To change this template file, choose Tools | Templates |
|||
* and open the template in the editor. |
|||
*/ |
|||
|
|||
namespace blobt\airpc; |
|||
|
|||
/** |
|||
* |
|||
* @author blobt |
|||
* Rpc协议 0.3 |
|||
* struct |
|||
* { |
|||
* unisgned char type; |
|||
* unsigned int time; |
|||
* unisgned int serid; |
|||
* unsigned int length; |
|||
* char body[0]; |
|||
* } |
|||
*/ |
|||
class Pack { |
|||
|
|||
/** |
|||
* 发送的数据类型 |
|||
*/ |
|||
const DATA_TYPE_SAMPLE_DATA = 1; |
|||
|
|||
/** |
|||
* @var integral 包的最大长度 |
|||
*/ |
|||
static public $packageMaxLength = 1024 * 20; |
|||
|
|||
/** |
|||
* @var char 储存包体长度的字节数,N代表大端字节序的4个字节 |
|||
*/ |
|||
static public $packageLengthType = 'N'; |
|||
|
|||
/** |
|||
* @var integral 从包体的第几个字节开始是包体长度信息 |
|||
*/ |
|||
static public $packageLengthOffset = 9; |
|||
|
|||
/** |
|||
* @var 从包体的第几个字节开始是包体信息 |
|||
*/ |
|||
static public $packageBodyOffset = 13; |
|||
|
|||
/** |
|||
* @var 该数组定义了协议包每一项的字节大小和名称 |
|||
*/ |
|||
private $items = [ |
|||
'type' => 'C', |
|||
'time' => 'N', |
|||
'serid' => 'N', |
|||
'length' => 'N', |
|||
'data' => 'a*' |
|||
]; |
|||
|
|||
/** |
|||
* @var integral 包的类型 |
|||
*/ |
|||
public $type; |
|||
|
|||
/** |
|||
* @var string 包体数据 |
|||
*/ |
|||
public $data; |
|||
|
|||
/** |
|||
* @var integral 包生成的时间 |
|||
*/ |
|||
public $time; |
|||
|
|||
/** |
|||
* @var integral 包的序列id |
|||
*/ |
|||
public $serid = 0; |
|||
|
|||
/** |
|||
* @var integral 包体长度 |
|||
*/ |
|||
public $length; |
|||
|
|||
/** |
|||
* 创建包分两种情况,$data可以是二进制流,也可以是字符数据 |
|||
* @param string $data 包体数据 |
|||
* @param type $serid 包的序列号 |
|||
*/ |
|||
public function __construct(string $data, $serid = 0) { |
|||
$ret = $this->unpack($data); |
|||
if (is_array($ret)) { |
|||
$this->type = $ret['type']; |
|||
$this->time = $ret['time']; |
|||
$this->serid = $ret['serid']; |
|||
$this->length = $ret['length']; |
|||
$this->data = $ret['data']; |
|||
} else { |
|||
$this->type = self::DATA_TYPE_SAMPLE_DATA; |
|||
$this->data = $data; |
|||
$this->serid = $serid; |
|||
$this->length = strlen($data); |
|||
$this->data = $data; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 生产格式字符串 |
|||
* @param boolean $isUnpack |
|||
*/ |
|||
private function makeFormat($isUnpack = false) { |
|||
$format = ''; |
|||
if ($isUnpack) { |
|||
foreach ($this->items as $k => $v) { |
|||
$format .= "{$v}{$k}/"; |
|||
} |
|||
$format = substr($format, 0, -1); |
|||
} else { |
|||
foreach ($this->items as $k => $v) { |
|||
$format .= $v; |
|||
} |
|||
} |
|||
return $format; |
|||
} |
|||
|
|||
/** |
|||
* 数据打包,数据打包成二进制流 |
|||
* @return string |
|||
*/ |
|||
public function pack() { |
|||
return pack($this->makeFormat(), $this->type, $this->time, $this->serid, $this->length, $this->data); |
|||
} |
|||
|
|||
/** |
|||
* 解包,把二进制流转换成格式数组 |
|||
* @param string $pack |
|||
* @return string | boolean |
|||
*/ |
|||
public function unpack(string $pack) { |
|||
$ret = unpack($this->makeFormat(true), $pack); |
|||
|
|||
if ($ret['type'] == self::DATA_TYPE_SAMPLE_DATA |
|||
&& $ret['length'] == strlen($ret['data']) |
|||
&& (int) $ret['length'] <= self::$packageMaxLength) { |
|||
return $ret; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function __toString() { |
|||
return $this->pack(); |
|||
} |
|||
|
|||
} |
@ -1,25 +0,0 @@ |
|||
<?php |
|||
|
|||
/* |
|||
* To change this license header, choose License Headers in Project Properties. |
|||
* To change this template file, choose Tools | Templates |
|||
* and open the template in the editor. |
|||
*/ |
|||
|
|||
namespace blobt\airpc; |
|||
|
|||
/** |
|||
* struct |
|||
* { |
|||
* unisgned char type; |
|||
* uint32_t time; |
|||
* uint32_t serid; |
|||
* uint32_t length; |
|||
* char body[0]; |
|||
* } |
|||
* |
|||
* @author blobt |
|||
*/ |
|||
class Protocol { |
|||
//put your code here
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue