diff --git a/cert/private_key.pem b/cert/private_key.pem new file mode 100644 index 0000000..61af3eb --- /dev/null +++ b/cert/private_key.pem @@ -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----- diff --git a/cert/pub_key.pem b/cert/pub_key.pem new file mode 100644 index 0000000..1da52e8 --- /dev/null +++ b/cert/pub_key.pem @@ -0,0 +1,6 @@ +-----BEGIN PUBLIC KEY----- +MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDsldu7vayXD7RE5HEMsvlz/tkq +36gBgY1Yz+R9dkZWNDdQTTFmO2tWFwBvYRx5Nv/CtR1Nl2+pb/Nd6WrWzs9FR7P8 +4nixA+vfzQjqTIlQViO0vB/agWa2nk5N4e0dsQ7kDr3+7/w1DeFsMlRBDvSHMsdm +lNSFDFB6+MVrC93dRQIDAQAB +-----END PUBLIC KEY----- diff --git a/composer.json b/composer.json index 9078f40..c49e206 100644 --- a/composer.json +++ b/composer.json @@ -11,8 +11,8 @@ ], "minimum-stability": "dev", "require": { - "yiisoft/yii2": "^2.0@dev", - "swoole/ide-helper": "dev-master" + "swoole/ide-helper": "dev-master", + "firebase/php-jwt": "dev-master" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 8380bfb..f5d9aaa 100644 --- a/composer.lock +++ b/composer.lock @@ -4,86 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "04cbcc938ac71579c75a6ca70e0a613c", + "content-hash": "be484b9b2956b9cbaf4eac46f8dd344a", "packages": [ { - "name": "cebe/markdown", - "version": "1.1.2", - "source": { - "type": "git", - "url": "https://github.com/cebe/markdown.git", - "reference": "25b28bae8a6f185b5030673af77b32e1163d5c6e" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/cebe/markdown/zipball/25b28bae8a6f185b5030673af77b32e1163d5c6e", - "reference": "25b28bae8a6f185b5030673af77b32e1163d5c6e", - "shasum": "", - "mirrors": [ - { - "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", - "preferred": true - } - ] - }, - "require": { - "lib-pcre": "*", - "php": ">=5.4.0" - }, - "require-dev": { - "cebe/indent": "*", - "facebook/xhprof": "*@dev", - "phpunit/phpunit": "4.1.*" - }, - "bin": [ - "bin/markdown" - ], - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.1.x-dev" - } - }, - "autoload": { - "psr-4": { - "cebe\\markdown\\": "" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Carsten Brandt", - "email": "mail@cebe.cc", - "homepage": "http://cebe.cc/", - "role": "Creator" - } - ], - "description": "A super fast, highly extensible markdown parser for PHP", - "homepage": "https://github.com/cebe/markdown#readme", - "keywords": [ - "extensible", - "fast", - "gfm", - "markdown", - "markdown-extra" - ], - "time": "2017-07-16T21:13:23+00:00" - }, - { - "name": "psr/http-message", + "name": "firebase/php-jwt", "version": "dev-master", "source": { "type": "git", - "url": "https://github.com/php-fig/http-message.git", - "reference": "efd67d1dc14a7ef4fc4e518e7dee91c271d524e4" + "url": "https://github.com/firebase/php-jwt.git", + "reference": "feb0e820b8436873675fd3aca04f3728eb2185cb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/http-message/zipball/efd67d1dc14a7ef4fc4e518e7dee91c271d524e4", - "reference": "efd67d1dc14a7ef4fc4e518e7dee91c271d524e4", + "url": "https://api.github.com/repos/firebase/php-jwt/zipball/feb0e820b8436873675fd3aca04f3728eb2185cb", + "reference": "feb0e820b8436873675fd3aca04f3728eb2185cb", "shasum": "", "mirrors": [ { @@ -95,145 +29,38 @@ "require": { "php": ">=5.3.0" }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "Psr\\Http\\Message\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" - } - ], - "description": "Common interface for HTTP messages", - "homepage": "https://github.com/php-fig/http-message", - "keywords": [ - "http", - "http-message", - "psr", - "psr-7", - "request", - "response" - ], - "time": "2019-08-29T13:16:46+00:00" - }, - { - "name": "psr/log", - "version": "1.0.2", - "source": { - "type": "git", - "url": "https://github.com/php-fig/log.git", - "reference": "4ebe3a8bf773a19edfe0a84b6585ba3d401b724d" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/php-fig/log/zipball/4ebe3a8bf773a19edfe0a84b6585ba3d401b724d", - "reference": "4ebe3a8bf773a19edfe0a84b6585ba3d401b724d", - "shasum": "", - "mirrors": [ - { - "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", - "preferred": true - } - ] - }, - "require": { - "php": ">=5.3.0" + "require-dev": { + "phpunit/phpunit": ">=4.8 <=9" }, "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, "autoload": { "psr-4": { - "Psr\\Log\\": "Psr/Log/" + "Firebase\\JWT\\": "src" } }, "notification-url": "https://packagist.org/downloads/", "license": [ - "MIT" + "BSD-3-Clause" ], "authors": [ { - "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" - } - ], - "description": "Common interface for logging libraries", - "homepage": "https://github.com/php-fig/log", - "keywords": [ - "log", - "psr", - "psr-3" - ], - "time": "2016-10-10T12:19:37+00:00" - }, - { - "name": "psr/simple-cache", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/php-fig/simple-cache.git", - "reference": "5a7b96b1dda5d957e01bc1bfe77dcca09c5a7474" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/5a7b96b1dda5d957e01bc1bfe77dcca09c5a7474", - "reference": "5a7b96b1dda5d957e01bc1bfe77dcca09c5a7474", - "shasum": "", - "mirrors": [ - { - "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", - "preferred": true - } - ] - }, - "require": { - "php": ">=5.3.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "Psr\\SimpleCache\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ + "name": "Neuman Vong", + "email": "neuman+pear@twilio.com", + "role": "Developer" + }, { - "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" + "name": "Anant Narayanan", + "email": "anant@php.net", + "role": "Developer" } ], - "description": "Common interfaces for simple caching", + "description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.", + "homepage": "https://github.com/firebase/php-jwt", "keywords": [ - "cache", - "caching", - "psr", - "psr-16", - "simple-cache" + "jwt", + "php" ], - "time": "2020-04-21T06:43:17+00:00" + "time": "2020-03-25T18:49:23+00:00" }, { "name": "swoole/ide-helper", @@ -241,12 +68,12 @@ "source": { "type": "git", "url": "https://github.com/swoole/ide-helper.git", - "reference": "e45892fb913204fc776bdec019ca8b723e7249b1" + "reference": "9082983c671cd79f461fcb9e52687dccca993e9b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/swoole/ide-helper/zipball/e45892fb913204fc776bdec019ca8b723e7249b1", - "reference": "e45892fb913204fc776bdec019ca8b723e7249b1", + "url": "https://api.github.com/repos/swoole/ide-helper/zipball/9082983c671cd79f461fcb9e52687dccca993e9b", + "reference": "9082983c671cd79f461fcb9e52687dccca993e9b", "shasum": "", "mirrors": [ { @@ -273,200 +100,15 @@ } ], "description": "IDE help files for Swoole.", - "time": "2020-05-28T19:17:49+00:00" - }, - { - "name": "yiisoft/yii2", - "version": "2.1.x-dev", - "source": { - "type": "git", - "url": "https://github.com/yiisoft/yii2-framework.git", - "reference": "86f1bfd7016524a6988005f3cdf9d01528df39fb" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/yiisoft/yii2-framework/zipball/86f1bfd7016524a6988005f3cdf9d01528df39fb", - "reference": "86f1bfd7016524a6988005f3cdf9d01528df39fb", - "shasum": "", - "mirrors": [ - { - "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", - "preferred": true - } - ] - }, - "require": { - "cebe/markdown": "~1.0.0 | ~1.1.0", - "ext-ctype": "*", - "ext-mbstring": "*", - "lib-pcre": "*", - "php": ">=7.1.0", - "psr/http-message": "~1.0.0", - "psr/log": "~1.0.2", - "psr/simple-cache": "~1.0.0", - "yiisoft/yii2-composer": "~2.0.4" - }, - "suggest": { - "ezyang/htmlpurifier": "version '~4.6' required at 'yii\\helpers\\HtmlPurifier' for 'html' data format support (e.g. 'yii\\i18n\\Formatter:asHtml()' and 'yii\\helpers\\StringHelper::truncateHtml()')" - }, - "bin": [ - "yii" - ], - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "yii\\": "" - }, - "classmap": [ - "Yii.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Qiang Xue", - "email": "qiang.xue@gmail.com", - "homepage": "http://www.yiiframework.com/", - "role": "Founder and project lead" - }, - { - "name": "Alexander Makarov", - "email": "sam@rmcreative.ru", - "homepage": "http://rmcreative.ru/", - "role": "Core framework development" - }, - { - "name": "Maurizio Domba", - "homepage": "http://mdomba.info/", - "role": "Core framework development" - }, - { - "name": "Carsten Brandt", - "email": "mail@cebe.cc", - "homepage": "http://cebe.cc/", - "role": "Core framework development" - }, - { - "name": "Timur Ruziev", - "email": "resurtm@gmail.com", - "homepage": "http://resurtm.com/", - "role": "Core framework development" - }, - { - "name": "Paul Klimov", - "email": "klimov.paul@gmail.com", - "role": "Core framework development" - }, - { - "name": "Dmitry Naumenko", - "email": "d.naumenko.a@gmail.com", - "role": "Core framework development" - }, - { - "name": "Boudewijn Vahrmeijer", - "email": "info@dynasource.eu", - "homepage": "http://dynasource.eu", - "role": "Core framework development" - } - ], - "description": "Yii PHP Framework Version 2", - "homepage": "http://www.yiiframework.com/", - "keywords": [ - "framework", - "yii2" - ], - "time": "2018-06-12T19:38:40+00:00" - }, - { - "name": "yiisoft/yii2-composer", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/yiisoft/yii2-composer.git", - "reference": "c372400aa6b6f2c52a82764496b37eb50d6e479f" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/yiisoft/yii2-composer/zipball/c372400aa6b6f2c52a82764496b37eb50d6e479f", - "reference": "c372400aa6b6f2c52a82764496b37eb50d6e479f", - "shasum": "", - "mirrors": [ - { - "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", - "preferred": true - } - ] - }, - "require": { - "composer-plugin-api": "^1.0 | ^2.0" - }, - "require-dev": { - "composer/composer": "^1.0 | ^2.0@dev", - "phpunit/phpunit": "<7" - }, - "type": "composer-plugin", - "extra": { - "class": "yii\\composer\\Plugin", - "branch-alias": { - "dev-master": "2.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "yii\\composer\\": "" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Qiang Xue", - "email": "qiang.xue@gmail.com" - }, - { - "name": "Carsten Brandt", - "email": "mail@cebe.cc" - } - ], - "description": "The composer plugin for Yii extension installer", - "keywords": [ - "composer", - "extension installer", - "yii2" - ], - "funding": [ - { - "url": "https://github.com/yiisoft", - "type": "github" - }, - { - "url": "https://opencollective.com/yiisoft", - "type": "open_collective" - }, - { - "url": "https://tidelift.com/funding/github/packagist/yiisoft/yii2-composer", - "type": "tidelift" - } - ], - "time": "2020-06-24T00:04:19+00:00" + "time": "2020-08-05T18:18:55+00:00" } ], "packages-dev": [], "aliases": [], "minimum-stability": "dev", "stability-flags": { - "yiisoft/yii2": 20, - "swoole/ide-helper": 20 + "swoole/ide-helper": 20, + "firebase/php-jwt": 20 }, "prefer-stable": false, "prefer-lowest": false, diff --git a/examples/client.php b/examples/client.php index 7bfa5f8..a1be59b 100644 --- a/examples/client.php +++ b/examples/client.php @@ -4,10 +4,10 @@ * struct * { * unisgned char type; - * uint32_t time; - * uint32_t serid; - * uint32_t length; - * char body[0]; + * unsigned int time; + * unsigned int serid; + * unsigned int length; + * char body[length]; * } */ function remoteCall(string $host, $port, string $data) { @@ -26,17 +26,32 @@ function remoteCall(string $host, $port, string $data) { if (!$client->connect($host, $port, 0.5)) { return false; } - + $client->send($pack); - $pack = $client->recv(); + $pack = @$client->recv(); + $client->close(); + + if ($pack === false) { + //echo $client->errCode . "\n"; + //TODO 获取错误码并进行处理 + return false; + } else if (empty($pack)) { + echo "close by peer\n"; + return false; + } + $ret = unpack('Ctype/Ntime/Nserid/Nlength/a*data', $pack); return $ret['data']; } -$ret = remoteCall("127.0.0.1", "5188", "sasa"); - - -echo $ret; +$c = 0; +while(1){ +$c++; +$ret = remoteCall("127.0.0.1", "5188", '{"controller":"site","methor":"test","params":{"id":'.$c.',"status":0}}'); +echo "{$ret}\n"; +break; +} +//var_dump($ret); ?> diff --git a/examples/index.php b/examples/index.php index 692bced..ce06521 100644 --- a/examples/index.php +++ b/examples/index.php @@ -5,4 +5,4 @@ include __DIR__ . "/../vendor/autoload.php"; use blobt\airpc\Yee; -return (new Yee())->run(); +return Yee::getInstaance()->run(); diff --git a/examples/rsa_test.php b/examples/rsa_test.php new file mode 100644 index 0000000..60a5dc0 --- /dev/null +++ b/examples/rsa_test.php @@ -0,0 +1,206 @@ + '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') . '/' => '' + ] : [], + ], + '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') . '/' => '' + ] : [], + ], + '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"; diff --git a/examples/server.php b/examples/server.php index dcfaea5..6bc4a48 100644 --- a/examples/server.php +++ b/examples/server.php @@ -13,5 +13,9 @@ include __DIR__."/../vendor/autoload.php"; use blobt\airpc\RpcServer; $server = new RpcServer("0.0.0.0", 5188); +$server->reactorNum = 2; +$server->workerNum = 6; +$server->daemonize = false; +$server->entryPoint = '/home/blobt/Documents/dev/php/yii2-app-rpc/rpc/index.php'; $server->run(); ?> \ No newline at end of file diff --git a/examples/test.php b/examples/test.php index 8814be8..8055e3c 100644 --- a/examples/test.php +++ b/examples/test.php @@ -1,17 +1,50 @@ $v){ - if($v>1){ - $duplicates[$k]=$v; - } -} -echo '
';
-print_r($duplicates);
-echo '
'; + +include __DIR__ . "/../vendor/autoload.php"; + +use \Firebase\JWT\JWT; + +$privateKey = << 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); ?> \ No newline at end of file diff --git a/src/BaseService.php b/src/BaseService.php new file mode 100644 index 0000000..abdaee2 --- /dev/null +++ b/src/BaseService.php @@ -0,0 +1,242 @@ +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(); + } + +} diff --git a/src/Pack.php b/src/Pack.php new file mode 100644 index 0000000..616a9be --- /dev/null +++ b/src/Pack.php @@ -0,0 +1,160 @@ + '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(); + } + +} diff --git a/src/Protocol.php b/src/Protocol.php deleted file mode 100644 index 946e22a..0000000 --- a/src/Protocol.php +++ /dev/null @@ -1,25 +0,0 @@ -server = new \Swoole\Server($listen, $port, SWOOLE_PROCESS, SWOOLE_SOCK_TCP); - $this->init(); - } - - /** - * 配置swoole参数 - */ - private function setting() { - $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, - 'open_length_check' => true, - 'package_max_length' => $packageMaxLength, - 'package_length_type' => 'N', - 'package_length_offset' => 9, - 'package_body_offset' => 13 - ]; - $this->server->set($config); - } + public $entryPoint = false; /** * 初始化 */ public function init() { - $this->setting(); - $this->server->on('Close', [$this, "close"]); - $this->server->on("Start", [$this, "start"]); - $this->server->on("ManagerStart", [$this, "managerStart"]); - $this->server->on("WorkerStart", [$this, "workerStart"]); - $this->server->on("Task", [$this, "task"]); - $this->server->on("Connect", [$this, "connect"]); - $this->server->on("Receive", [$this, "receive"]); - $this->server->on("WorkerError", [$this, "error"]); - $this->server->on("WorkerStop", [$this, "stop"]); - } - - /** - * master 进程启动时候执行函数 - * @param Swoole\Server $server - */ - public function start(Swoole\Server $server) { - swoole_set_process_name("php:master"); - } - - /** - * manager进程启动时候执行函数 - * @param Swoole\Server $server - */ - public function managerStart(Swoole\Server $server) { - swoole_set_process_name("php:manager"); - } - - /** - * worker进程启动后执行的回调 - * @param Swoole\Server $server - * @param int $workerId - */ - public function workerStart(Swoole\Server $server, int $workerId) { - swoole_set_process_name("php:worker{$workerId}"); + if (empty($this->entryPoint)) { + die("Please set the entry point." . PHP_EOL); + } + $this->setProtocol(Pack::$packageLengthType, Pack::$packageMaxLength, Pack::$packageLengthOffset, Pack::$packageBodyOffset); } /** - * task 处理函数 - * @param Swoole\Server $server - * @param int $task_id - * @param int $src_worker_id - * @param mixed $data + * 发送数据 + * @param integral $fd 链接文件描述付 + * @param string $data + * @return boolean */ - public function task(Swoole\Server $server, int $task_id, int $src_worker_id, $data) { - echo "task: {$data}\n"; + 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 connect(Swoole\Server $server, int $fd, int $reactorId) { - //echo "connected: {$fd} {$reactorId}\n"; + public function onReceive(Swoole\Server $server, int $fd, int $reactorId, string $pack) { + $pack = new Pack($pack); + $data = $pack->data; + $this->handle($fd, $data); } /** - * - * @param Swoole\Server $server + * 请求处理函数 * @param int $fd - * @param int $reactorId * @param string $pack */ - public function receive(Swoole\Server $server, int $fd, int $reactorId, string $pack) { - - //解包返回请求数据 - $data = $this->unpack($pack); - + public function handle(int $fd, string $data) { + $_SERVER['data'] = $data; try { - $ret = include __DIR__ . '/../examples/index.php'; + $ret = include $this->entryPoint; $this->send($fd, $ret); } catch (\Error $e) { - $this->send($fd, "YeeError:" . $e->getMessage()); + print_r($e); + $this->send($fd, "YeeError"); } catch (\Exception $e) { - $this->send($fd, "YeeException:" . $e->getMessage()); + print_r($e); + $this->send($fd, "YeeException"); } } - /** - * 关闭链接时候触发 - * @param Swoole\Server $server - * @param int $fd - * @param int $reactorId - */ - public function close(Swoole\Server $server, int $fd, int $reactorId) { - //echo "connection close: {$fd} {$reactorId}\n"; - } - - /** - * 错误处理 - * @param Swoole\Server $server - * @param int $worker_id - * @param int $worker_pid - * @param int $exit_code - * @param int $signal - */ - function error(Swoole\Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal) { - //echo "\nError\n"; - } - - /** - * worker进程停止 - * @param Swoole\Server $server - * @param int $workerId - */ - function Stop(Swoole\Server $server, int $workerId) { - //echo "\nStop\n"; - } - - /** - * 数据打包,添加包头和包体 - * @param string $data - * @return string - */ - public function pack(string $data) { - $length = strlen($data); - $pack = pack('CNNNa*', self::DATA_TYPE_SAMPLE_DATA, time(), ++$this->serid, $length, $data); - return $pack; - } - - /** - * 解包,获取报文数据 - * @param string $pack - * @return string - */ - public function unpack(string $pack) { - $ret = unpack('Ctype/Ntime/Nserid/Nlength/a*data', $pack); - if (isset($ret['data'])) { - //TODO 记录日志, 次包不完整 - } - return $ret['data']; - } - - /** - * 发送数据 - * @param integral $fd 链接文件描述付 - * @param string $data - * @return boolean - */ - public function send(int $fd, string $data) { - $pack = $this->pack($data); - $this->server->send($fd, $pack); - } - - - /** - * 服务启动 - */ - public function run() { - $this->server->start(); - } - } diff --git a/src/Yee.php b/src/Yee.php index 64a6d12..29591e0 100644 --- a/src/Yee.php +++ b/src/Yee.php @@ -3,11 +3,21 @@ namespace blobt\airpc; use yii\console\controllers\HelpController; + class Yee { + private static $instance; + + public static function getInstaance() { + if (empty(self::$instance)) { + self::$instance = new self(); + } + return self::$instance; + } + public function run() { - return "some datasasa"; + $ret = "some data ".$_SERVER['data']; + return $ret; } } -