diff --git a/.gitignore b/.gitignore index 4711ea7..00826da 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,43 @@ -# ---> Yii -assets/* -!assets/.gitignore -protected/runtime/* -!protected/runtime/.gitignore -protected/data/*.db -themes/classic/views/ +# yii console commands +/yii +/yii_test +/yii_test.bat +.vscode +# phpstorm project files +.idea +# netbeans project files +nbproject + +# zend studio for eclipse project files +.buildpath +.project +.settings + +# windows thumbnail cache +Thumbs.db + +# composer vendor dir +/vendor/* + +# composer itself is not needed +composer.phar + +# Mac DS_Store Files +.DS_Store + +# phpunit itself is not needed +phpunit.phar + +# local phpunit config +/phpunit.xml + +# vagrant runtime +/.vagrant +/vagrant +/common/cert +dump.rdb +/node_modules +package-lock.json +node_modules +yarn-error.log diff --git a/bin/server b/bin/server new file mode 100755 index 0000000..9e57fd3 --- /dev/null +++ b/bin/server @@ -0,0 +1,18 @@ +#!/usr/bin/env php +run(); +?> \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..9078f40 --- /dev/null +++ b/composer.json @@ -0,0 +1,22 @@ +{ + "name": "blobt/airpc", + "description": "A tiny Rpc implement", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "blobt", + "email": "klm777x7@gmail.com" + } + ], + "minimum-stability": "dev", + "require": { + "yiisoft/yii2": "^2.0@dev", + "swoole/ide-helper": "dev-master" + }, + "autoload": { + "psr-4": { + "blobt\\airpc\\": "src" + } + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..8380bfb --- /dev/null +++ b/composer.lock @@ -0,0 +1,476 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "04cbcc938ac71579c75a6ca70e0a613c", + "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", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "efd67d1dc14a7ef4fc4e518e7dee91c271d524e4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/efd67d1dc14a7ef4fc4e518e7dee91c271d524e4", + "reference": "efd67d1dc14a7ef4fc4e518e7dee91c271d524e4", + "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\\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" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Log\\": "Psr/Log/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "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": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interfaces for simple caching", + "keywords": [ + "cache", + "caching", + "psr", + "psr-16", + "simple-cache" + ], + "time": "2020-04-21T06:43:17+00:00" + }, + { + "name": "swoole/ide-helper", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/swoole/ide-helper.git", + "reference": "e45892fb913204fc776bdec019ca8b723e7249b1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/swoole/ide-helper/zipball/e45892fb913204fc776bdec019ca8b723e7249b1", + "reference": "e45892fb913204fc776bdec019ca8b723e7249b1", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require-dev": { + "guzzlehttp/guzzle": "~6.5.0", + "laminas/laminas-code": "~3.4.0", + "squizlabs/php_codesniffer": "~3.5.0", + "symfony/filesystem": "~4.0" + }, + "type": "library", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Team Swoole", + "email": "team@swoole.com" + } + ], + "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" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "dev", + "stability-flags": { + "yiisoft/yii2": 20, + "swoole/ide-helper": 20 + }, + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "1.1.0" +} diff --git a/examples/client.php b/examples/client.php new file mode 100644 index 0000000..7bfa5f8 --- /dev/null +++ b/examples/client.php @@ -0,0 +1,42 @@ +set(array( + 'open_length_check' => true, + 'package_max_length' => 81920, + 'package_length_type' => 'N', + 'package_length_offset' => 9, + 'package_body_offset' => 13 + )); + + if (!$client->connect($host, $port, 0.5)) { + return false; + } + + $client->send($pack); + $pack = $client->recv(); + $ret = unpack('Ctype/Ntime/Nserid/Nlength/a*data', $pack); + + return $ret['data']; +} + +$ret = remoteCall("127.0.0.1", "5188", "sasa"); + + +echo $ret; +?> + diff --git a/examples/index.php b/examples/index.php new file mode 100644 index 0000000..692bced --- /dev/null +++ b/examples/index.php @@ -0,0 +1,8 @@ +run(); diff --git a/examples/server.php b/examples/server.php new file mode 100644 index 0000000..dcfaea5 --- /dev/null +++ b/examples/server.php @@ -0,0 +1,17 @@ +run(); +?> \ No newline at end of file diff --git a/examples/test.php b/examples/test.php new file mode 100644 index 0000000..8814be8 --- /dev/null +++ b/examples/test.php @@ -0,0 +1,17 @@ +$v){ + if($v>1){ + $duplicates[$k]=$v; + } +} +echo '
';
+print_r($duplicates);
+echo '
'; +?> \ No newline at end of file diff --git a/src/Protocol.php b/src/Protocol.php new file mode 100644 index 0000000..946e22a --- /dev/null +++ b/src/Protocol.php @@ -0,0 +1,25 @@ +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 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}"); + } + + /** + * task 处理函数 + * @param Swoole\Server $server + * @param int $task_id + * @param int $src_worker_id + * @param mixed $data + */ + public function task(Swoole\Server $server, int $task_id, int $src_worker_id, $data) { + echo "task: {$data}\n"; + } + + /** + * 链接成功事件处理 + * @param Swoole\Server $server + * @param int $fd + * @param int $reactorId + */ + public function connect(Swoole\Server $server, int $fd, int $reactorId) { + //echo "connected: {$fd} {$reactorId}\n"; + } + + /** + * + * @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); + + try { + $ret = include __DIR__ . '/../examples/index.php'; + $this->send($fd, $ret); + } catch (\Error $e) { + $this->send($fd, "YeeError:" . $e->getMessage()); + } catch (\Exception $e) { + $this->send($fd, "YeeException:" . $e->getMessage()); + } + } + + /** + * 关闭链接时候触发 + * @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 new file mode 100644 index 0000000..64a6d12 --- /dev/null +++ b/src/Yee.php @@ -0,0 +1,13 @@ +