blobt
5 years ago
11 changed files with 952 additions and 7 deletions
-
49.gitignore
-
18bin/server
-
22composer.json
-
476composer.lock
-
42examples/client.php
-
8examples/index.php
-
17examples/server.php
-
17examples/test.php
-
25src/Protocol.php
-
272src/RpcServer.php
-
13src/Yee.php
@ -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 |
@ -0,0 +1,18 @@ |
|||
#!/usr/bin/env php |
|||
<?PHP |
|||
/* |
|||
* @Descripttion: |
|||
* @version: |
|||
* @Author: sueRimn |
|||
* @Date: 2020-07-30 09:48:18 |
|||
* @LastEditors: sueRimn |
|||
* @LastEditTime: 2020-07-30 14:20:19 |
|||
*/ |
|||
|
|||
include __DIR__."/../vendor/autoload.php"; |
|||
|
|||
use blobt\airpc\RpcServer; |
|||
|
|||
$server = new RpcServer("0.0.0.0", 5188); |
|||
$server->run(); |
|||
?> |
@ -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" |
|||
} |
|||
} |
|||
} |
@ -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" |
|||
} |
@ -0,0 +1,42 @@ |
|||
<?PHP |
|||
|
|||
/** |
|||
* struct |
|||
* { |
|||
* unisgned char type; |
|||
* uint32_t time; |
|||
* uint32_t serid; |
|||
* uint32_t length; |
|||
* char body[0]; |
|||
* } |
|||
*/ |
|||
function remoteCall(string $host, $port, string $data) { |
|||
$length = strlen($data); |
|||
$pack = pack('CNNNa*', 1, time(), 0, $length, $data); |
|||
$client = new Swoole\Client(SWOOLE_SOCK_TCP); |
|||
|
|||
$client->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; |
|||
?>
|
|||
|
@ -0,0 +1,8 @@ |
|||
<?php |
|||
|
|||
include __DIR__ . "/../vendor/autoload.php"; |
|||
|
|||
use blobt\airpc\Yee; |
|||
|
|||
|
|||
return (new Yee())->run(); |
@ -0,0 +1,17 @@ |
|||
<?PHP |
|||
/* |
|||
* @Descripttion: |
|||
* @version: |
|||
* @Author: sueRimn |
|||
* @Date: 2020-07-30 09:48:18 |
|||
* @LastEditors: sueRimn |
|||
* @LastEditTime: 2020-07-30 14:20:19 |
|||
*/ |
|||
|
|||
include __DIR__."/../vendor/autoload.php"; |
|||
|
|||
use blobt\airpc\RpcServer; |
|||
|
|||
$server = new RpcServer("0.0.0.0", 5188); |
|||
$server->run(); |
|||
?>
|
@ -0,0 +1,17 @@ |
|||
<?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>'; |
|||
?>
|
@ -0,0 +1,25 @@ |
|||
<?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
|
|||
} |
@ -0,0 +1,272 @@ |
|||
<?PHP |
|||
|
|||
namespace blobt\airpc; |
|||
|
|||
use Swoole; |
|||
use Swoole\Table as Table; |
|||
|
|||
/** |
|||
* 基于swoole的轻量及 RPC |
|||
* 服务器 |
|||
* |
|||
* Rpc协议 0.3 |
|||
* struct |
|||
* { |
|||
* unisgned char type; |
|||
* unsigned int time; |
|||
* unisgned int serid; |
|||
* unsigned int length; |
|||
* char body[0]; |
|||
* } |
|||
*/ |
|||
class RpcServer { |
|||
|
|||
/** |
|||
* 发送的数据类型 |
|||
*/ |
|||
const DATA_TYPE_SAMPLE_DATA = 1; |
|||
|
|||
/** |
|||
* @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 数据包的最大长度 |
|||
*/ |
|||
public $packageMaxLength = 1024 * 20; |
|||
|
|||
/** |
|||
* @var integral 发送序列号 |
|||
*/ |
|||
private $serid = 0; |
|||
|
|||
/** |
|||
* @var integral 接收序列号 |
|||
*/ |
|||
private $revid = 0; |
|||
|
|||
/** |
|||
* 服务初始化 |
|||
* @param string $listen 监听地址 |
|||
* @param integral $port 监听端口 |
|||
*/ |
|||
public function __construct($listen, $port) { |
|||
$this->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(); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,13 @@ |
|||
<?php |
|||
|
|||
namespace blobt\airpc; |
|||
|
|||
use yii\console\controllers\HelpController; |
|||
class Yee { |
|||
|
|||
public function run() { |
|||
return "some datasasa"; |
|||
} |
|||
|
|||
} |
|||
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue