Init Repo

This commit is contained in:
root
2019-09-06 23:53:10 +08:00
commit f0ef89dfbb
7905 changed files with 914138 additions and 0 deletions

View File

@ -0,0 +1,101 @@
<?php
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| Copyright (c) 2012-2017 The Swoole Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| license@swoole.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
abstract class TestServer
{
protected $index = array();
protected $recv_bytes = 0;
protected $count = 0;
protected $show_lost_package = false;
const PKG_NUM = 100000;
const LEN_MIN = 0;
const LEN_MAX = 200;
/**
* @var swoole_server
*/
protected $serv;
abstract function onReceive($serv, $fd, $reactor_id, $data);
function __construct($base = false)
{
$mode = $base ? SWOOLE_BASE : SWOOLE_PROCESS;
$serv = new swoole_server("127.0.0.1", 9501, $mode);
$serv->on('Connect', [$this, 'onConnect']);
$serv->on('receive', [$this, '_receive']);
$serv->on('workerStart', [$this, 'onWorkerStart']);
$serv->on('Close', [$this, 'onClose']);
$this->serv = $serv;
}
function onConnect($serv, $fd, $from_id)
{
}
function _receive($serv, $fd, $from_id, $data)
{
$this->count++;
$this->recv_bytes += strlen($data);
$this->onReceive($serv, $fd, $from_id, $data);
if ($this->count == self::PKG_NUM)
{
$serv->send($fd, "end\n");
}
}
function onClose($serv, $fd, $from_id)
{
echo "Total count={$this->count}, bytes={$this->recv_bytes}\n";
if ($this->show_lost_package)
{
for ($i = 0; $i < self::PKG_NUM; $i++)
{
if (!isset($this->index[$i]))
{
echo "lost package#$i\n";
}
}
}
$this->count = $this->recv_bytes = 0;
unset($this->index);
$this->index = array();
}
function set($conf)
{
$this->serv->set($conf);
}
function start()
{
$this->serv->start();
}
function onWorkerStart($serv, $id)
{
//sleep(1);
}
static function random()
{
return rand(self::LEN_MIN, self::LEN_MAX);
}
}

View File

@ -0,0 +1,16 @@
[2017-01-09 20:48:45 *7139.0] WARNING swServer_tcp_deny_exit: swServer_tcp_deny_exit
[2017-01-09 20:48:45 *7140.1] WARNING swServer_tcp_deny_exit: swServer_tcp_deny_exit
[2017-01-09 20:48:45 #7136.0] WARNING swReactorThread_onPipeReceive: [Master] set worker idle.[work_id=0]
[2017-01-09 20:48:45 #7136.0] WARNING swReactorThread_onPipeReceive: [Master] set worker idle.[work_id=1]
[2017-01-09 20:48:48 #7136.0] WARNING swServer_signal_hanlder: Fatal Error: manager process exit. status=1, signal=23.
[2017-01-09 20:48:48 *7140.1] ERROR swFactoryProcess_finish (ERROR 1004): send 1024 byte failed, because session#1 is closed.
[2017-01-09 20:49:14 *7149.0] WARNING swServer_tcp_deny_exit: swServer_tcp_deny_exit
[2017-01-09 20:49:14 @7146.0] WARNING swReactorThread_onPipeReceive: [Master] set worker idle.[work_id=0]
[2017-01-09 20:49:14 *7150.1] WARNING swServer_tcp_deny_exit: swServer_tcp_deny_exit
[2017-01-09 20:49:14 #7146.0] WARNING swReactorThread_onPipeReceive: [Master] set worker idle.[work_id=1]
[2017-01-09 20:49:16 #7146.0] WARNING swServer_signal_hanlder: Fatal Error: manager process exit. status=24, signal=38.
[2017-01-09 20:50:23 *7158.1] WARNING swServer_tcp_deny_exit: swServer_tcp_deny_exit
[2017-01-09 20:50:23 *7157.0] WARNING swServer_tcp_deny_exit: swServer_tcp_deny_exit
[2017-01-09 20:50:23 #7154.0] WARNING swReactorThread_onPipeReceive: [Master] set worker idle.[work_id=1]
[2017-01-09 20:50:23 #7154.0] WARNING swReactorThread_onPipeReceive: [Master] set worker idle.[work_id=0]
[2017-01-09 20:50:24 #7154.0] WARNING swServer_signal_hanlder: Fatal Error: manager process exit. status=49, signal=82.

View File

@ -0,0 +1,110 @@
<?php
require_once __DIR__ . "/../../../include/bootstrap.php";
//(new OpcodeServer("127.0.0.1", 9999, 9998, 9997))->start(PHP_INT_MAX);
$host = isset($argv[1]) ? $argv[1] : null;
$port = isset($argv[2]) ? $argv[2] : null;
$port1 = isset($argv[3]) ? $argv[3] : null;
$port2 = isset($argv[4]) ? $argv[4] : null;
(new OpcodeServer($host, $port, $port1, $port2))->start();
class OpcodeServer
{
/**
* @var \swoole_server
*/
public $swooleServer;
public function __construct($host, $port, $port1, $port2)
{
$this->swooleServer = new \swoole_server($host, $port, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
$this->swooleServer->set([
'dispatch_mode' => 3,
'worker_num' => 2,
'open_eof_split' => true,
'package_eof' => "\r\n",
]);
$this->swooleServer->on("receive", function(\swoole_server $server, $fd, $fromReactorId, $recv) use($port) {
assert(intval($recv) === $port);
$r = $server->send($fd, opcode_encode("return", $port));
assert($r !== false);
});
$serv1 = $this->swooleServer->listen(TCP_SERVER_HOST, $port1, SWOOLE_SOCK_TCP);
assert($serv1 !== false);
$serv1->set([
'open_eof_split' => true,
'package_eof' => "\r",
]);
$serv1->on("receive", function(\swoole_server $server, $fd, $fromReactorId, $recv) use($port1) {
assert(intval($recv) === $port1);
$r = $server->send($fd, opcode_encode("return", $port1));
assert($r !== false);
});
$serv2 = $this->swooleServer->listen(TCP_SERVER_HOST, $port2, SWOOLE_SOCK_TCP);
assert($serv2 !== false);
$serv2->set([
'open_eof_split' => true,
'package_eof' => "\n",
]);
$serv2->on("receive", function(\swoole_server $server, $fd, $fromReactorId, $recv) use($port2) {
assert(intval($recv) === $port2);
$r = $server->send($fd, opcode_encode("return", $port2));
assert($r !== false);
});
}
public function start($lifetime = 1000)
{
$this->lifetime = $lifetime;
$this->swooleServer->on('start', [$this, 'onStart']);
$this->swooleServer->on('shutdown', [$this, 'onShutdown']);
$this->swooleServer->on('workerStart', [$this, 'onWorkerStart']);
$this->swooleServer->on('workerStop', [$this, 'onWorkerStop']);
$this->swooleServer->on('workerError', [$this, 'onWorkerError']);
$this->swooleServer->on('connect', [$this, 'onConnect']);
$this->swooleServer->on('close', [$this, 'onClose']);
$this->swooleServer->start();
}
public function onConnect() { }
public function onClose() { }
public function onStart(\swoole_server $swooleServer) { }
public function onShutdown(\swoole_server $swooleServer) { }
public function onWorkerStart(\swoole_server $swooleServer, $workerId)
{
if ($workerId === 0) {
swoole_timer_after($this->lifetime, function() {
$this->swooleServer->shutdown();
kill_self_and_descendant(getmypid());
/*
\swoole_process::signal(SIGTERM, swoole_function() {
$this->swooleServer->shutdown();
});
\swoole_process::kill(0, SIGTERM);
*/
});
}
}
public function onWorkerStop(\swoole_server $swooleServer, $workerId) { }
public function onWorkerError(\swoole_server $swooleServer, $workerId, $workerPid, $exitCode, $sigNo) { }
}

View File

@ -0,0 +1,183 @@
<?php
require_once __DIR__ . "/../../../include/bootstrap.php";
// (new OpcodeServer("127.0.0.1", 9999))->start(PHP_INT_MAX);
$host = isset($argv[1]) ? $argv[1] : HTTP_SERVER_HOST;
$port = isset($argv[2]) ? $argv[2] : HTTP_SERVER_PORT;
$port1 = isset($argv[3]) ? $argv[3] : null;
$port2 = isset($argv[4]) ? $argv[4] : null;
(new OpcodeServer($host, $port, $port1, $port2))->start();
class OpcodeServer
{
/**
* @var \swoole_server
*/
public $swooleServer;
public function __construct($host, $port, $port1 = null, $port2 = null)
{
$this->swooleServer = new \swoole_server($host, $port, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
$this->swooleServer->set([
'dispatch_mode' => 3,
'worker_num' => 2,
'task_worker_num' => 2,
'open_length_check' => 1,
'package_length_type' => 'N',
'package_length_offset' => 0,
'package_body_offset' => 0,
"heartbeat_idle_time"=> 20
]);
if ($port1) {
$serv1 = $this->swooleServer->addListener(TCP_SERVER_HOST, $port1, SWOOLE_SOCK_TCP);
assert($serv1 !== false);
}
if ($port2) {
$serv2 = $this->swooleServer->addListener(TCP_SERVER_HOST, $port2, SWOOLE_SOCK_TCP);
assert($serv2 !== false);
}
}
public function start($lifetime = 1000)
{
$this->lifetime = $lifetime;
$this->swooleServer->on('start', [$this, 'onStart']);
$this->swooleServer->on('shutdown', [$this, 'onShutdown']);
$this->swooleServer->on('workerStart', [$this, 'onWorkerStart']);
$this->swooleServer->on('workerStop', [$this, 'onWorkerStop']);
$this->swooleServer->on('workerError', [$this, 'onWorkerError']);
$this->swooleServer->on('connect', [$this, 'onConnect']);
$this->swooleServer->on('receive', [$this, 'onReceive']);
$this->swooleServer->on('close', [$this, 'onClose']);
$this->swooleServer->on('task', [$this, 'onTask']);
$this->swooleServer->on('finish', [$this, 'onFinish']);
$this->swooleServer->on('pipeMessage', [$this, 'onPipeMessage']);
$this->swooleServer->on('packet', [$this, 'onPacket']);
/*
$proc = new \swoole_process(swoole_function(\swoole_process $proc) use($i) {
var_dump($this->swooleServer->id);
sleep(10000);
$r = $this->swooleServer->addProcess($proc);
var_dump($r);
$proc->freeQueue();
});
$proc->useQueue();
// $proc->start();
$proc1 = new \swoole_process(swoole_function(\swoole_process $proc) use($i) {
var_dump($this->swooleServer->id);
sleep(1000);
});
$proc2 = new \swoole_process(swoole_function(\swoole_process $proc) {
var_dump($this->swooleServer->id);
sleep(1000);
});
$r = $this->swooleServer->addProcess($proc);
$r = $this->swooleServer->addProcess($proc1);
$r = $this->swooleServer->addProcess($proc2);
var_dump($this->swooleServer->id);
*/
$this->swooleServer->start();
}
public function onConnect() { }
public function onClose() { }
public function onStart(\swoole_server $swooleServer) { }
public function onShutdown(\swoole_server $swooleServer) { }
public function onWorkerStart(\swoole_server $swooleServer, $workerId)
{
if ($workerId === 0) {
swoole_timer_after($this->lifetime, function() {
$this->swooleServer->shutdown();
kill_self_and_descendant(getmypid());
/*
\swoole_process::signal(SIGTERM, swoole_function() {
$this->swooleServer->shutdown();
});
\swoole_process::kill(0, SIGTERM);
*/
});
}
}
public function onWorkerStop(\swoole_server $swooleServer, $workerId) { }
public function onWorkerError(\swoole_server $swooleServer, $workerId, $workerPid, $exitCode, $sigNo) { }
public function onReceive(\swoole_server $swooleServer, $fd, $fromReactorId, $recv)
{
list($op, $args) = opcode_decode($recv);
switch($op) {
case "sendMessage":
list($msg, $toWorkerId) = $args;
$r = $swooleServer->sendMessage(json_encode([
"fd" => $fd,
"msg" => $msg,
]), $toWorkerId);
assert($r);
return;
case "sendfile":
$len = filesize(__FILE__);
$r = $swooleServer->send($fd, pack("N", $len + 4));
assert($r !== false);
$r =$swooleServer->sendfile($fd, __FILE__);
assert($r !== false);
return;
default:
if (method_exists($swooleServer, $op)) {
$r = call_user_func_array([$swooleServer, $op], $args);
if (is_resource($r)) {
$r = true;
}
$r = $swooleServer->send($fd, opcode_encode("return", $r));
assert($r !== false);
return;
} else {
}
}
}
public function onTask(\swoole_server $swooleServer, $taskId, $fromWorkerId, $recv)
{
$recv = json_decode($recv);
assert(json_last_error() === JSON_ERROR_NONE);
return json_encode($recv);
}
public function onFinish(\swoole_server $swooleServer, $taskId, $recv)
{
$recv = json_decode($recv);
assert(json_last_error() === JSON_ERROR_NONE);
assert(isset($recv["fd"]) && isset($recv["data"]));
$this->swooleServer->send($recv["fd"], opcode_encode("return", $recv["data"]));
}
public function onPipeMessage(\swoole_server $swooleServer, $fromWorkerId, $recv)
{
$recv = json_decode($recv, true);
assert(json_last_error() === JSON_ERROR_NONE);
assert(isset($recv["fd"]) && isset($recv["msg"]));
$this->swooleServer->send($recv["fd"], opcode_encode("return", $recv["msg"]));
}
public function onPacket(\swoole_server $swooleServer, $data, array $clientInfo)
{
}
}

View File

@ -0,0 +1,19 @@
<?php
ini_set("memory_limit", "1024m");
function reconn() {
echo "Reconnect\n";
$cli = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$cli->on("connect", function(swoole_client $cli) {
// client 发送 大包数据
$cli->send(str_repeat("\0", 1024 * 1024 * 1.9));
});
$cli->on("receive", function(swoole_client $cli, $data) {
$cli->send($data);
});
$cli->on("error", function(swoole_client $cli) { echo "error\n"; });
$cli->on("close", function(swoole_client $cli) { echo "close\n"; reconn(); });
$cli->connect("127.0.0.1", 9001);
}
reconn();

View File

@ -0,0 +1,106 @@
<?php
function debug_log($str, $handle = STDERR)
{
if ($handle === STDERR) {
$tpl = "\033[31m[%d %s] %s\033[0m\n";
} else {
$tpl = "[%d %s] %s\n";
}
if (is_resource($handle)) {
fprintf($handle, $tpl, posix_getpid(), date("Y-m-d H:i:s", time()), $str);
} else {
printf($tpl, posix_getpid(), date("Y-m-d H:i:s", time()), $str);
}
}
(new TcpServer())->start();
class TcpServer
{
/**
* @var \swoole_server
*/
public $swooleServer;
public function __construct()
{
$this->swooleServer = new \swoole_server("127.0.0.1", 9001, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
$this->swooleServer->set([
// "buffer_output_size" => 1024 * 1024 * 1024, // 输出限制
'max_connection' => 10240,
'pipe_buffer_size' => 1024 * 1024 * 2,
// 'pipe_buffer_size' => 1024,
'dispatch_mode' => 3,
'open_tcp_nodelay' => 1,
'open_cpu_affinity' => 1,
'daemonize' => 0,
'reactor_num' => 1,
'worker_num' => 2,
'max_request' => 100000,
]);
}
public function start()
{
$this->swooleServer->on('start', [$this, 'onStart']);
$this->swooleServer->on('shutdown', [$this, 'onShutdown']);
$this->swooleServer->on('workerStart', [$this, 'onWorkerStart']);
$this->swooleServer->on('workerStop', [$this, 'onWorkerStop']);
$this->swooleServer->on('workerError', [$this, 'onWorkerError']);
$this->swooleServer->on('connect', [$this, 'onConnect']);
$this->swooleServer->on('receive', [$this, 'onReceive']);
$this->swooleServer->on('close', [$this, 'onClose']);
$this->swooleServer->start();
}
public function onConnect()
{
debug_log("connecting ......");
}
public function onClose()
{
debug_log("closing .....");
}
public function onStart(swoole_server $swooleServer)
{
debug_log("swoole_server starting .....");
}
public function onShutdown(swoole_server $swooleServer)
{
debug_log("swoole_server shutdown .....");
}
public function onWorkerStart(swoole_server $swooleServer, $workerId)
{
debug_log("worker #$workerId starting .....");
}
public function onWorkerStop(swoole_server $swooleServer, $workerId)
{
debug_log("worker #$workerId stopping ....");
}
public function onWorkerError(swoole_server $swooleServer, $workerId, $workerPid, $exitCode, $sigNo)
{
debug_log("worker error happening [workerId=$workerId, workerPid=$workerPid, exitCode=$exitCode, signalNo=$sigNo]...");
}
public function onReceive(swoole_server $swooleServer, $fd, $fromId, $data)
{
echo "close $fd\n";
// var_dump($swooleServer->shutdown());
// swoole_server 接受数据之后立马关闭连接
var_dump($swooleServer->close($fd));
// $swooleServer->send($fd, $data);
}
}

View File

@ -0,0 +1,142 @@
<?php
require_once __DIR__ . "/../../../include/bootstrap.php";
if (pcntl_fork() === 0) {
suicide(1000);
exit();
}
if (pcntl_fork() === 0) {
suicide(1000);
exit();
}
if (pcntl_fork() === 0) {
suicide(1000);
$cli = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
/** @noinspection PhpVoidFunctionResultUsedInspection */
assert($cli->set([
"socket_buffer_size" => 1024,
]));
$cli->on("connect", function(swoole_client $cli) {
swoole_timer_clear($cli->timeo_id);
assert($cli->isConnected() === true);
$cli->send(str_repeat("\0", 1024));
});
$cli->on("receive", function(swoole_client $cli, $data){
$recv_len = strlen($data);
debug_log("receive: len $recv_len");
$cli->send(str_repeat("\0", $recv_len));
});
$cli->on("error", function(swoole_client $cli) {
swoole_timer_clear($cli->timeo_id);
debug_log("error");
});
$cli->on("close", function(swoole_client $cli) {
swoole_timer_clear($cli->timeo_id);
debug_log("close");
});
$cli->connect(TCP_SERVER_HOST, TCP_SERVER_PORT);
$cli->timeo_id = swoole_timer_after(1000, function() use($cli) {
debug_log("connect timeout");
$cli->close();
assert($cli->isConnected() === false);
});
exit();
}
(new TcpServer())->start();
class TcpServer
{
/**
* @var \swoole_server
*/
public $swooleServer;
public function __construct()
{
$this->swooleServer = new \swoole_server(TCP_SERVER_HOST, TCP_SERVER_PORT, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
$this->swooleServer->set([
"buffer_output_size" => 1024 * 1024 * 1024, // 输出限制
"max_connection" => 10240,
"pipe_buffer_size" => 1024 * 1024 * 1024,
'daemonize' => 0,
'worker_num' => 2,
'max_request' => 100000,
'reactor_num' => 1,
// 'package_max_length' => 1024 * 1024 * 2
]);
}
public function start()
{
$this->swooleServer->on('start', [$this, 'onStart']);
$this->swooleServer->on('shutdown', [$this, 'onShutdown']);
$this->swooleServer->on('workerStart', [$this, 'onWorkerStart']);
$this->swooleServer->on('workerStop', [$this, 'onWorkerStop']);
$this->swooleServer->on('workerError', [$this, 'onWorkerError']);
$this->swooleServer->on('connect', [$this, 'onConnect']);
$this->swooleServer->on('receive', [$this, 'onReceive']);
$this->swooleServer->on('close', [$this, 'onClose']);
$this->swooleServer->start();
}
public function onConnect()
{
debug_log("connecting ......");
}
public function onClose()
{
debug_log("closing .....");
}
public function onStart(swoole_server $swooleServer)
{
debug_log("swoole_server starting .....");
}
public function onShutdown(swoole_server $swooleServer)
{
debug_log("swoole_server shutdown .....");
}
public function onWorkerStart(swoole_server $swooleServer, $workerId)
{
debug_log("worker #$workerId starting .....");
}
public function onWorkerStop(swoole_server $swooleServer, $workerId)
{
debug_log("worker #$workerId stopping ....");
}
public function onWorkerError(swoole_server $swooleServer, $workerId, $workerPid, $exitCode, $sigNo)
{
debug_log("worker error happening [workerId=$workerId, workerPid=$workerPid, exitCode=$exitCode, signalNo=$sigNo]...");
}
public function onReceive(swoole_server $swooleServer, $fd, $fromId, $data)
{
$recv_len = strlen($data);
debug_log("receive: len $recv_len");
$swooleServer->send($fd, str_repeat("\0", $recv_len));
}
}

View File

@ -0,0 +1,145 @@
<?php
require_once __DIR__ . "/../../../include/bootstrap.php";
if (pcntl_fork() === 0) {
suicide(3000);
$cli = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
/** @noinspection PhpVoidFunctionResultUsedInspection */
assert($cli->set([
"socket_buffer_size" => 1,
]));
$cli->on("connect", function(swoole_client $cli) {
swoole_timer_clear($cli->timeo_id);
// TODO getSocket BUG
// assert(is_resource($cli->getSocket()));
/*
$cli->getSocket();
// Warning: swoole_client_async::getSocket(): unable to obtain socket family Error: Bad file descriptor[9].
$cli->getSocket();
*/
assert($cli->isConnected() === true);
$cli->send(str_repeat("\0", 1024));
// $cli->sendfile(__DIR__.'/test.txt');
});
$cli->on("receive", function(swoole_client $cli, $data){
$recv_len = strlen($data);
debug_log("receive: len $recv_len");
$cli->send(str_repeat("\0", 1024));
});
$cli->on("error", function(swoole_client $cli) {
swoole_timer_clear($cli->timeo_id);
debug_log("error");
});
$cli->on("close", function(swoole_client $cli) {
swoole_timer_clear($cli->timeo_id);
debug_log("close");
});
$cli->connect(TCP_SERVER_HOST, TCP_SERVER_PORT);
$cli->timeo_id = swoole_timer_after(1000, function() use($cli) {
debug_log("connect timeout");
$cli->close();
assert($cli->isConnected() === false);
});
exit();
}
(new TcpServer())->start();
class TcpServer
{
/**
* @var \swoole_server
*/
public $swooleServer;
public function __construct()
{
$this->swooleServer = new \swoole_server(TCP_SERVER_HOST, TCP_SERVER_PORT, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
$this->swooleServer->set([
"buffer_output_size" => 1024 * 1024 * 1024, // 输出限制
"max_connection" => 10240,
"pipe_buffer_size" => 1024 * 1024 * 1024,
// 'log_file' => __DIR__ . '/send_fast_recv_slow.log',
'daemonize' => 0,
'worker_num' => 2,
'max_request' => 100000,
'reactor_num' => 1,
// 'package_max_length' => 1024 * 1024 * 2
]);
}
public function start()
{
$this->swooleServer->on('start', [$this, 'onStart']);
$this->swooleServer->on('shutdown', [$this, 'onShutdown']);
$this->swooleServer->on('workerStart', [$this, 'onWorkerStart']);
$this->swooleServer->on('workerStop', [$this, 'onWorkerStop']);
$this->swooleServer->on('workerError', [$this, 'onWorkerError']);
$this->swooleServer->on('connect', [$this, 'onConnect']);
$this->swooleServer->on('receive', [$this, 'onReceive']);
$this->swooleServer->on('close', [$this, 'onClose']);
$this->swooleServer->start();
}
public function onConnect()
{
debug_log("connecting ......");
}
public function onClose()
{
debug_log("closing .....");
}
public function onStart(swoole_server $swooleServer)
{
debug_log("swoole_server starting .....");
}
public function onShutdown(swoole_server $swooleServer)
{
debug_log("swoole_server shutdown .....");
}
public function onWorkerStart(swoole_server $swooleServer, $workerId)
{
debug_log("worker #$workerId starting .....");
}
public function onWorkerStop(swoole_server $swooleServer, $workerId)
{
debug_log("worker #$workerId stopping ....");
}
public function onWorkerError(swoole_server $swooleServer, $workerId, $workerPid, $exitCode, $sigNo)
{
debug_log("worker error happening [workerId=$workerId, workerPid=$workerPid, exitCode=$exitCode, signalNo=$sigNo]...");
}
public function onReceive(swoole_server $swooleServer, $fd, $fromId, $data)
{
$recv_len = strlen($data);
debug_log("receive: len $recv_len");
$swooleServer->send($fd, str_repeat("\0", $recv_len));
}
}

View File

@ -0,0 +1,114 @@
<?php
require_once __DIR__ . "/../../../include/bootstrap.php";
/*
if (pcntl_fork() === 0) {
require_once __DIR__ . "/../swoole_client_async/simple_client.php";
exit();
}*/
(new TcpServer())->start();
class TcpServer
{
/**
* @var \swoole_server
*/
public $swooleServer;
public function __construct()
{
$this->swooleServer = new \swoole_server(TCP_SERVER_HOST, TCP_SERVER_PORT, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
$this->swooleServer->set([
// "buffer_output_size" => 1024 * 1024 * 1024, // 输出限制
"max_connection" => 10240,
"pipe_buffer_size" => 1024 * 1024 * 1024,
// 'enable_port_reuse' => true,
// 'user' => 'www-data',
// 'group' => 'www-data',
// 'log_file' => __DIR__ . '/simple_server.log',
'dispatch_mode' => 2,
'open_tcp_nodelay' => 1,
'open_cpu_affinity' => 1,
'daemonize' => 0,
'reactor_num' => 1,
'worker_num' => 1,
'max_request' => 100000,
// 'package_max_length' => 1024 * 1024 * 2
/*
'open_length_check' => 1,
'package_length_type' => 'N',
'package_length_offset' => 0,
'package_body_offset' => 0,
'open_nova_protocol' => 1,
*/
]);
}
public function start()
{
$this->swooleServer->on('start', [$this, 'onStart']);
$this->swooleServer->on('shutdown', [$this, 'onShutdown']);
$this->swooleServer->on('workerStart', [$this, 'onWorkerStart']);
$this->swooleServer->on('workerStop', [$this, 'onWorkerStop']);
$this->swooleServer->on('workerError', [$this, 'onWorkerError']);
$this->swooleServer->on('connect', [$this, 'onConnect']);
$this->swooleServer->on('receive', [$this, 'onReceive']);
$this->swooleServer->on('close', [$this, 'onClose']);
$this->swooleServer->start();
}
public function onConnect()
{
debug_log("connecting ......");
}
public function onClose()
{
debug_log("closing .....");
}
public function onStart(swoole_server $swooleServer)
{
debug_log("swoole_server starting .....");
}
public function onShutdown(swoole_server $swooleServer)
{
debug_log("swoole_server shutdown .....");
}
public function onWorkerStart(swoole_server $swooleServer, $workerId)
{
debug_log("worker #$workerId starting .....");
}
public function onWorkerStop(swoole_server $swooleServer, $workerId)
{
debug_log("worker #$workerId stopping ....");
}
public function onWorkerError(swoole_server $swooleServer, $workerId, $workerPid, $exitCode, $sigNo)
{
debug_log("worker error happening [workerId=$workerId, workerPid=$workerPid, exitCode=$exitCode, signalNo=$sigNo]...");
}
public function onReceive(swoole_server $swooleServer, $fd, $fromId, $data)
{
if (trim($data) == 'shutdown')
{
$swooleServer->shutdown();
return;
}
$recv_len = strlen($data);
debug_log("receive: len $recv_len");
$swooleServer->send($fd, RandStr::gen($recv_len, RandStr::ALL));
// $swooleServer->close($fd);
}
}

View File

@ -0,0 +1,103 @@
<?php
require_once __DIR__ . "/../../../include/bootstrap.php";
//(new TcpServer($argv[1], $argv[2]))->start();
$host = isset($argv[1]) ? $argv[1] : TCP_SERVER_HOST;
$port = isset($argv[2]) ? $argv[2] : TCP_SERVER_PORT;
$server = new TcpServer($host, $port);
$server->start();
class TcpServer
{
/**
* @var \swoole_server
*/
public $swooleServer;
public function __construct($host, $port)
{
echo "swoole_server host:$host, port:$port\n";
$this->swooleServer = new \swoole_server($host, $port, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
$this->swooleServer->set([
"pipe_buffer_size" => 1024 * 1024 * 1024,
'dispatch_mode' => 3,
'open_tcp_nodelay' => 1,
'open_cpu_affinity' => 1,
//'daemonize' => 1,
'reactor_num' => 2,
'worker_num' => 4,
'max_request' => 100000,
]);
}
public function start()
{
$this->swooleServer->on('start', [$this, 'onStart']);
$this->swooleServer->on('shutdown', [$this, 'onShutdown']);
$this->swooleServer->on('workerStart', [$this, 'onWorkerStart']);
$this->swooleServer->on('workerStop', [$this, 'onWorkerStop']);
$this->swooleServer->on('workerError', [$this, 'onWorkerError']);
$this->swooleServer->on('connect', [$this, 'onConnect']);
$this->swooleServer->on('receive', [$this, 'onReceive']);
$this->swooleServer->on('close', [$this, 'onClose']);
$this->swooleServer->start();
}
public function onConnect()
{
debug_log("connecting ......");
}
public function onClose()
{
debug_log("closing .....");
}
public function onStart(swoole_server $swooleServer)
{
debug_log("swoole_server starting .....");
}
public function onShutdown(swoole_server $swooleServer)
{
debug_log("swoole_server shutdown .....");
}
public function onWorkerStart(swoole_server $swooleServer, $workerId)
{
debug_log("worker #$workerId starting .....");
if ($workerId == 0) {
swoole_timer_after(5000, function () {
$this->swooleServer->shutdown();
});
}
}
public function onWorkerStop(swoole_server $swooleServer, $workerId)
{
debug_log("worker #$workerId stopping ....");
}
public function onWorkerError(swoole_server $swooleServer, $workerId, $workerPid, $exitCode, $sigNo)
{
debug_log("worker error happening [workerId=$workerId, workerPid=$workerPid, exitCode=$exitCode, signalNo=$sigNo]...");
}
public function onReceive(swoole_server $swooleServer, $fd, $fromId, $data)
{
//echo "swoole_server receive data: $data\n";
$recv_len = strlen($data);
debug_log("receive: len $recv_len");
//$swooleServer->send($fd, RandStr::gen($recv_len, RandStr::ALL));
$filename = __DIR__ . "/testsendfile.txt";
$swooleServer->sendfile($fd, $filename);
}
}

View File

@ -0,0 +1,89 @@
<?php
require_once __DIR__ . "/../../../include/bootstrap.php";
(new UdpServer())->start();
class UdpServer
{
public $swooleServer;
public function __construct()
{
$this->swooleServer = new \swoole_server(UDP_SERVER_HOST, UDP_SERVER_PORT, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
$this->swooleServer->set([
"max_connection" => 1000,
'dispatch_mode' => 3,
'daemonize' => 0,
'reactor_num' => 4,
'worker_num' => 8,
'max_request' => 1000,
]);
}
public function start()
{
$this->swooleServer->on('start', [$this, 'onStart']);
$this->swooleServer->on('shutdown', [$this, 'onShutdown']);
$this->swooleServer->on('workerStart', [$this, 'onWorkerStart']);
$this->swooleServer->on('workerStop', [$this, 'onWorkerStop']);
$this->swooleServer->on('workerError', [$this, 'onWorkerError']);
$this->swooleServer->on('connect', [$this, 'onConnect']);
$this->swooleServer->on('Packet', [$this, 'onPacket']);
$this->swooleServer->on('close', [$this, 'onClose']);
$this->swooleServer->start();
}
public function onConnect()
{
debug_log("connecting ......");
}
public function onClose()
{
debug_log("closing .....");
}
public function onStart(swoole_server $swooleServer)
{
debug_log("swoole_server starting .....");
}
public function onShutdown(swoole_server $swooleServer)
{
debug_log("swoole_server shutdown .....");
}
public function onWorkerStart(swoole_server $swooleServer, $workerId)
{
debug_log("worker #$workerId starting .....");
swoole_timer_after(3000, function() {
$this->swooleServer->shutdown();
});
}
public function onWorkerStop(swoole_server $swooleServer, $workerId)
{
debug_log("worker #$workerId stopping ....");
}
public function onWorkerError(swoole_server $swooleServer, $workerId, $workerPid, $exitCode, $sigNo)
{
debug_log("worker error happening [workerId=$workerId, workerPid=$workerPid, exitCode=$exitCode, signalNo=$sigNo]...");
}
//UDP: 收到数据帧事件
public function onPacket(swoole_server $swooleServer, $data, $clientInfo)
{
if (trim($data) == 'shutdown')
{
$swooleServer->shutdown();
return;
}
//echo "clientInfo: $clientInfo, receive: $data\n";
$swooleServer->sendto($clientInfo['address'], $clientInfo['port'], $data);
}
}

View File

@ -0,0 +1,111 @@
<?php
require_once __DIR__ . "/../../../include/bootstrap.php";
$host = isset($argv[1]) ? $argv[1] : TCP_SERVER_HOST;
$port = isset($argv[2]) ? $argv[2] : TCP_SERVER_PORT;
$server = new TcpServer($host, $port);
$server->start();
class TcpServer
{
public $swooleServer;
public function __construct($host, $port)
{
echo "swoole_server host:$host, port:$port\n";
$this->swooleServer = new \swoole_server($host, $port, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
$this->swooleServer->set([
"pipe_buffer_size" => 1024 * 1024 * 1024,
'dispatch_mode' => 3,
'open_tcp_nodelay' => 1,
'open_cpu_affinity' => 1,
//'daemonize' => 1,
'reactor_num' => 2,
'worker_num' => 4,
'task_worker_num' => 8,
'max_request' => 100000,
'log_file' => '/tmp/swoole_server.log',
]);
}
public function start()
{
$this->swooleServer->on('start', [$this, 'onStart']);
$this->swooleServer->on('shutdown', [$this, 'onShutdown']);
$this->swooleServer->on('workerStart', [$this, 'onWorkerStart']);
$this->swooleServer->on('workerStop', [$this, 'onWorkerStop']);
$this->swooleServer->on('workerError', [$this, 'onWorkerError']);
$this->swooleServer->on('connect', [$this, 'onConnect']);
$this->swooleServer->on('receive', [$this, 'onReceive']);
$this->swooleServer->on('task', [$this, 'onTask']);
$this->swooleServer->on('finish', [$this, 'onFinish']);
$this->swooleServer->on('close', [$this, 'onClose']);
$this->swooleServer->start();
}
public function onConnect()
{
debug_log("connecting ......");
}
public function onClose()
{
debug_log("closing .....");
}
public function onStart(swoole_server $swooleServer)
{
debug_log("swoole_server starting .....");
}
public function onShutdown(swoole_server $swooleServer)
{
debug_log("swoole_server shutdown .....");
}
public function onWorkerStart(swoole_server $swooleServer, $workerId)
{
debug_log("worker #$workerId starting .....");
if ($workerId == 0) {
//swoole_timer_after(5000, function () {
// $this->swooleServer->shutdown();
//});
}
}
public function onWorkerStop(swoole_server $swooleServer, $workerId)
{
debug_log("worker #$workerId stopping ....");
}
public function onWorkerError(swoole_server $swooleServer, $workerId, $workerPid, $exitCode, $sigNo)
{
debug_log("worker error happening [workerId=$workerId, workerPid=$workerPid, exitCode=$exitCode, signalNo=$sigNo]...");
}
public function onReceive(swoole_server $swooleServer, $fd, $fromId, $data)
{
//echo "swoole_server receive data: $data\n";
$param = array(
'fd' => $fd,
'data' => $data,
);
$swooleServer->task(json_encode($param));
//echo "send data to task worker.\n";
}
public function onTask(swoole_server $swooleServer, $task_id, $fromId, $data)
{
$task_data = json_decode($data, true);
$swooleServer->finish($task_data);
}
public function onFinish(swoole_server $swooleServer, $worker_task_id, $task_data)
{
$swooleServer->send($task_data['fd'], "OK");
}
}

View File

@ -0,0 +1 @@
testsendfile.txt