You've already forked qlg.tsgz.moe
Init Repo
This commit is contained in:
117
vendor/swoole/examples/coroutine/TestHttpServ.php
vendored
Executable file
117
vendor/swoole/examples/coroutine/TestHttpServ.php
vendored
Executable file
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
/**
|
||||
* @Author: winterswang
|
||||
* @Date: 2015-06-18 16:45:09
|
||||
* @Last Modified by: winterswang
|
||||
* @Last Modified time: 2016-09-18 17:33:51
|
||||
*/
|
||||
|
||||
class TestHttpServer {
|
||||
|
||||
public $http;
|
||||
public $queue;
|
||||
public $setting = array();
|
||||
|
||||
/**
|
||||
* [__construct description]
|
||||
* @param array $setting [description]
|
||||
*/
|
||||
public function __construct(){
|
||||
|
||||
}
|
||||
|
||||
public function set($setting){
|
||||
|
||||
$this ->setting = $setting;
|
||||
}
|
||||
|
||||
/**
|
||||
* [init description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function init(){
|
||||
|
||||
if (!isset($this ->setting['host'])) {
|
||||
$this ->setting['host'] = '0.0.0.0';
|
||||
}
|
||||
if (!isset($this ->setting['port'])) {
|
||||
$this ->setting['port'] = '9999';
|
||||
}
|
||||
|
||||
$this ->http = new swoole_http_server($this ->setting['host'], $this ->setting['port']);
|
||||
$this ->http ->set($this ->setting);
|
||||
|
||||
$this ->http ->on('request', array($this, 'onRequest'));
|
||||
$this ->http ->on('close', array($this, 'onClose'));
|
||||
}
|
||||
|
||||
/**
|
||||
* [onRequest description]
|
||||
* @param [type] $request [description]
|
||||
* @param [type] $response [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function onRequest($request, $response){
|
||||
|
||||
// $udp = new swoole_client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_ASYNC);
|
||||
// $udp->on("connect", function(swoole_client $cli) {
|
||||
// $cli->send("udp test");
|
||||
// });
|
||||
// $udp->on("receive", function(swoole_client $cli, $data)use($response){
|
||||
|
||||
$tcp = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
|
||||
$tcp->on("connect", function(swoole_client $cli) {
|
||||
$cli->send("tcp test");
|
||||
});
|
||||
$tcp->on("receive", function(swoole_client $cli, $data)use($response){
|
||||
$response ->end("<h1> swoole response</h1>");
|
||||
});
|
||||
$tcp->on("close", function(swoole_client $cli){
|
||||
});
|
||||
$tcp->on("error", function(swoole_client $cli){
|
||||
});
|
||||
$tcp->connect('10.100.64.151', 9805);
|
||||
|
||||
// });
|
||||
// $udp->on("close", function(swoole_client $cli){
|
||||
// });
|
||||
// $udp->connect('10.100.65.222', 9906);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* [onClose description]
|
||||
* @param [type] $server [description]
|
||||
* @param [type] $fd [description]
|
||||
* @param [type] $from_id [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function onClose($server, $fd, $from_id){
|
||||
|
||||
//echo " on close fd = $fd from_id = $from_id \n";
|
||||
}
|
||||
|
||||
/**
|
||||
* [start description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function start(){
|
||||
|
||||
$this ->init();
|
||||
$this ->http ->start();
|
||||
}
|
||||
}
|
||||
|
||||
$setting = array(
|
||||
'host' => '0.0.0.0',
|
||||
'port' => 10005,
|
||||
'worker_num' => 4,
|
||||
'dispatch_mode' => 2, //固定分配请求到worker
|
||||
'reactor_num' => 4, //亲核
|
||||
'daemonize' => 1, //守护进程
|
||||
'backlog' => 128,
|
||||
'log_file' => '/data/log/test_http_server.log',
|
||||
);
|
||||
$th = new TestHttpServer();
|
||||
$th ->set($setting);
|
||||
$th ->start();
|
27
vendor/swoole/examples/coroutine/client_send_yield.php
vendored
Executable file
27
vendor/swoole/examples/coroutine/client_send_yield.php
vendored
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$client->set(array(
|
||||
'socket_buffer_size' => 1024 * 512,
|
||||
));
|
||||
if (!$client->connect('127.0.0.1', 9501, -1))
|
||||
{
|
||||
exit("connect failed. Error: {$client->errCode}\n");
|
||||
}
|
||||
$length = 0;
|
||||
$size = 1024 * 64;
|
||||
while (true)
|
||||
{
|
||||
$ret = $client->send(str_repeat('A', $size));
|
||||
if ($ret == false)
|
||||
{
|
||||
var_dump($ret);
|
||||
break;
|
||||
}
|
||||
$length += $size;
|
||||
echo "send $length success\n";
|
||||
}
|
||||
var_dump($client->errCode);
|
||||
});
|
||||
|
||||
swoole_event_wait();
|
27
vendor/swoole/examples/coroutine/client_send_yield_server.php
vendored
Executable file
27
vendor/swoole/examples/coroutine/client_send_yield_server.php
vendored
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
$socket = stream_socket_server("tcp://0.0.0.0:9501", $errno, $errstr);
|
||||
if (!$socket) {
|
||||
echo "$errstr ($errno)<br />\n";
|
||||
} else {
|
||||
while (true) {
|
||||
$conn = stream_socket_accept($socket);
|
||||
if (!$conn) {
|
||||
continue;
|
||||
}
|
||||
$i = 0;
|
||||
$length = 0;
|
||||
while(true) {
|
||||
$data = fread($conn, 8192);
|
||||
if ($data == false)
|
||||
{
|
||||
break;
|
||||
}
|
||||
$length += strlen($data);
|
||||
echo "recv " . $length . " bytes\n";
|
||||
usleep(100000);
|
||||
}
|
||||
fclose($conn);
|
||||
echo "closed\n";
|
||||
}
|
||||
fclose($socket);
|
||||
}
|
19
vendor/swoole/examples/coroutine/coro_array_map.php
vendored
Executable file
19
vendor/swoole/examples/coroutine/coro_array_map.php
vendored
Executable file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
co::create(function() {
|
||||
array_map("test",array("func param\n"));
|
||||
echo "co flow end\n";
|
||||
});
|
||||
|
||||
function test($p) {
|
||||
go(function() use ($p){
|
||||
echo $p;
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 1);
|
||||
echo "co resume : connect ret = ".var_export($res,1)."\n";
|
||||
echo "map func end \n";
|
||||
});
|
||||
}
|
||||
echo "main end\n";
|
20
vendor/swoole/examples/coroutine/coro_call_user.php
vendored
Executable file
20
vendor/swoole/examples/coroutine/coro_call_user.php
vendored
Executable file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
co::set(['trace_flags' => 1]);
|
||||
|
||||
co::create(function() {
|
||||
echo "co func start\n";
|
||||
$name = "call_user_func";
|
||||
$ret = $name("test","test\n");
|
||||
echo "co func end ret:{$ret}\n";
|
||||
});
|
||||
|
||||
function test($params)
|
||||
{
|
||||
echo "func params:$params";
|
||||
co::sleep(1);
|
||||
echo "func end\n";
|
||||
return "test return\n";
|
||||
}
|
||||
echo "main script last\n";
|
||||
|
25
vendor/swoole/examples/coroutine/coro_channel.php
vendored
Executable file
25
vendor/swoole/examples/coroutine/coro_channel.php
vendored
Executable file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
$http = new swoole_http_server("127.0.0.1", 9501, SWOOLE_BASE);
|
||||
$http->set(array(
|
||||
'log_file' => '/dev/null'
|
||||
));
|
||||
use Swoole\Coroutine as co;
|
||||
// $http->on("WorkerStart", function (\swoole_server $serv)
|
||||
// {
|
||||
//
|
||||
// });
|
||||
$http->on('request', function (swoole_http_request $request, swoole_http_response $response)
|
||||
{
|
||||
$ch = new co\Channel(1);
|
||||
$out = new co\Channel(1);
|
||||
Swoole\Coroutine::create(function() use ($ch, $out) {
|
||||
$out->push("OK");
|
||||
$out->push("OK");
|
||||
});
|
||||
$ret = $out->pop();
|
||||
var_dump($ret);
|
||||
$ret = $out->pop();
|
||||
var_dump($ret);
|
||||
$response->end("$ret\n");
|
||||
});
|
||||
$http->start();
|
28
vendor/swoole/examples/coroutine/coro_destruct.php
vendored
Executable file
28
vendor/swoole/examples/coroutine/coro_destruct.php
vendored
Executable file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
class T
|
||||
{
|
||||
function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
function test()
|
||||
{
|
||||
echo "call function \n";
|
||||
}
|
||||
|
||||
function __destruct()
|
||||
{
|
||||
go(function () {
|
||||
echo "coro start\n";
|
||||
co::sleep(1.0);
|
||||
echo "coro exit\n";
|
||||
});
|
||||
echo "111\n";
|
||||
}
|
||||
}
|
||||
|
||||
$t = new T();
|
||||
$t->test();
|
||||
echo "end \n";
|
32
vendor/swoole/examples/coroutine/coro_destuct.php
vendored
Executable file
32
vendor/swoole/examples/coroutine/coro_destuct.php
vendored
Executable file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
require __DIR__ . "/coro_include.php";
|
||||
|
||||
class T
|
||||
{
|
||||
function __construct()
|
||||
{
|
||||
echo "call __construct \n";
|
||||
}
|
||||
|
||||
function test()
|
||||
{
|
||||
echo "call function \n";
|
||||
}
|
||||
|
||||
function __destruct()
|
||||
{
|
||||
echo "call __destruct \n";
|
||||
go(function () {
|
||||
echo "co[1] start\n";
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 1);
|
||||
co::sleep(1.0);
|
||||
echo "co[1] resume : connect ret = ".var_export($res,1)."\n";
|
||||
echo "co[1] exit\n";
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
$t = new T();
|
||||
$t->test();
|
||||
unset($t);
|
9
vendor/swoole/examples/coroutine/coro_empty.php
vendored
Executable file
9
vendor/swoole/examples/coroutine/coro_empty.php
vendored
Executable file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
// co::set(['trace_flags' => 1]);
|
||||
|
||||
co::create(function () {
|
||||
echo "no coro exit\n";
|
||||
});
|
||||
echo "exec file end\n";
|
||||
|
11
vendor/swoole/examples/coroutine/coro_gethost.php
vendored
Executable file
11
vendor/swoole/examples/coroutine/coro_gethost.php
vendored
Executable file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
require __DIR__ . "/coro_include.php";
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
co::create(function () {
|
||||
$ip = co::gethostbyname('www.baidu.com');
|
||||
var_dump($ip);
|
||||
});
|
||||
echo "111\n";
|
||||
|
||||
echo "222\n";
|
9
vendor/swoole/examples/coroutine/coro_include.php
vendored
Executable file
9
vendor/swoole/examples/coroutine/coro_include.php
vendored
Executable file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
if (substr(PHP_OS, 0, 3) == 'WIN')
|
||||
{
|
||||
exit("skip for Windows");
|
||||
}
|
||||
if (!extension_loaded("swoole"))
|
||||
{
|
||||
exit("swoole extension is required");
|
||||
}
|
22
vendor/swoole/examples/coroutine/coro_invoke.php
vendored
Executable file
22
vendor/swoole/examples/coroutine/coro_invoke.php
vendored
Executable file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
co::set(['trace_flags' => 1]);
|
||||
|
||||
co::create(function() {
|
||||
|
||||
|
||||
$function = new ReflectionFunction('title');
|
||||
|
||||
$function->invoke();
|
||||
echo "invoke444\n";
|
||||
|
||||
});
|
||||
|
||||
function title() {
|
||||
echo "333invoke_________________________________\n";
|
||||
$tcpclient = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
var_dump($tcpclient->connect('127.0.0.1', 9501, 1));
|
||||
|
||||
}
|
||||
|
||||
echo "111\n";
|
18
vendor/swoole/examples/coroutine/coro_nested.php
vendored
Executable file
18
vendor/swoole/examples/coroutine/coro_nested.php
vendored
Executable file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
require __DIR__ . "/coro_include.php";
|
||||
echo "before coro\n";
|
||||
go(function () {
|
||||
echo "co[1] start\n";
|
||||
go(function () {
|
||||
echo "co[2] start\n";
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 1);
|
||||
echo "co[2] resume : connect ret = ".var_export($res,1)."\n";
|
||||
echo "co[2] exit\n";
|
||||
});
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 1);
|
||||
echo "co[1] resume : connect ret = ".var_export($res,1)."\n";
|
||||
echo "co[1] exit\n";
|
||||
});
|
||||
echo "out coro \n";
|
16
vendor/swoole/examples/coroutine/coro_nested_empty.php
vendored
Executable file
16
vendor/swoole/examples/coroutine/coro_nested_empty.php
vendored
Executable file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
require __DIR__ . "/coro_include.php";
|
||||
function test()
|
||||
{
|
||||
echo "before coro\n";
|
||||
go(function () {
|
||||
echo "co[1] start\n";
|
||||
go(function () {
|
||||
echo "co[2] start\n";
|
||||
echo "co[2] exit\n";
|
||||
});
|
||||
echo "co[1] exit\n";
|
||||
});
|
||||
echo "func end \n";
|
||||
}
|
||||
test();
|
39
vendor/swoole/examples/coroutine/coro_serialize.php
vendored
Executable file
39
vendor/swoole/examples/coroutine/coro_serialize.php
vendored
Executable file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
class Obj {
|
||||
public $a;
|
||||
protected $b;
|
||||
private $c;
|
||||
var $d;
|
||||
|
||||
function __construct($a, $b, $c, $d) {
|
||||
$this->a = $a;
|
||||
$this->b = $b;
|
||||
$this->c = $c;
|
||||
$this->d = $d;
|
||||
}
|
||||
|
||||
function __sleep() {
|
||||
// co::sleep(0.5);
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 10);
|
||||
var_dump($res);
|
||||
if ($res)
|
||||
{
|
||||
echo("connect success. Error: {$client->errCode}\n");
|
||||
}
|
||||
echo "sleep\n";
|
||||
return array('a', 'b', 'c');
|
||||
}
|
||||
|
||||
// function __wakeup() {
|
||||
// $this->d = $this->a + $this->b + $this->c;
|
||||
// }
|
||||
}
|
||||
$o = new Obj(1, 2, 3, 4);
|
||||
co::create(function() use($o) {
|
||||
$serialized = serialize($o);
|
||||
$unserialized = unserialize($serialized);
|
||||
echo "res:".var_export($unserialized,1)."\n";
|
||||
echo "call user\n";
|
||||
});
|
9
vendor/swoole/examples/coroutine/coro_sleep.php
vendored
Executable file
9
vendor/swoole/examples/coroutine/coro_sleep.php
vendored
Executable file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
// require __DIR__ . "/coro_include.php";
|
||||
use Swoole\Coroutine as co;
|
||||
co::create(function () {
|
||||
echo "start\n";
|
||||
co::sleep(0.5);
|
||||
echo "OK\n";
|
||||
});
|
||||
echo "11\n";
|
19
vendor/swoole/examples/coroutine/coro_stackless.php
vendored
Executable file
19
vendor/swoole/examples/coroutine/coro_stackless.php
vendored
Executable file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
require __DIR__ . "/coro_include.php";
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
echo "start\n";
|
||||
co::create(function () {
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 1);
|
||||
var_dump($res);
|
||||
if ($res) {
|
||||
echo ("connect success. Error: {$client->errCode}\n");
|
||||
}
|
||||
|
||||
$res = $client->send("hello");
|
||||
echo "send res:" . var_export($res, 1) . "\n";
|
||||
$data = $client->recv();
|
||||
echo "recv data" . var_export($data, 1) . "\n";
|
||||
});
|
||||
echo "end\n";
|
16
vendor/swoole/examples/coroutine/coro_util.php
vendored
Executable file
16
vendor/swoole/examples/coroutine/coro_util.php
vendored
Executable file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
$id = go(function(){
|
||||
$id = co::getUid();
|
||||
echo "start coro $id\n";
|
||||
co::suspend($id);
|
||||
echo "resume coro $id @1\n";
|
||||
co::suspend($id);
|
||||
echo "resume coro $id @2\n";
|
||||
});
|
||||
echo "start to resume $id @1\n";
|
||||
co::resume($id);
|
||||
echo "start to resume $id @2\n";
|
||||
co::resume($id);
|
||||
echo "main\n";
|
79
vendor/swoole/examples/coroutine/defer_client.php
vendored
Executable file
79
vendor/swoole/examples/coroutine/defer_client.php
vendored
Executable file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/* new multi implement test */
|
||||
$server = new Swoole\Http\Server("127.0.0.1", 9502, SWOOLE_BASE);
|
||||
|
||||
$server->set([
|
||||
'worker_num' => 1,
|
||||
]);
|
||||
|
||||
$server->on('Request', function ($request, $response) {
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$redis->setDefer(true);
|
||||
$redis->get('key');
|
||||
$res = $redis->get('key');//get false
|
||||
var_dump($res);
|
||||
|
||||
var_dump($redis->setDefer());//get true
|
||||
var_dump($redis->setDefer(false));//get false
|
||||
|
||||
//穿插其他client也能正常工作
|
||||
$redis_tmp = new Swoole\Coroutine\Redis();
|
||||
$res = $redis_tmp->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $redis_tmp->set('key_tmp', 'HaHa');//get true
|
||||
var_dump($res);
|
||||
|
||||
|
||||
$http_client= new Swoole\Coroutine\Http\Client('km.oa.com', 80);
|
||||
$http_client->setDefer();
|
||||
$http_client->get('/');
|
||||
|
||||
$mysql = new Swoole\Coroutine\MySQL();
|
||||
$res = $mysql->connect(['host' => '192.168.244.128', 'user' => 'mha_manager', 'password' => 'mhapass', 'database' => 'tt']);
|
||||
if ($res == false) {
|
||||
$response->end("MySQL connect fail!");
|
||||
return;
|
||||
}
|
||||
$mysql->setDefer(true);
|
||||
$mysql->query('select sleep(1)', 2);
|
||||
|
||||
$udp = new Swoole\Coroutine\Client(SWOOLE_SOCK_UDP);
|
||||
$res = $udp->connect("127.0.0.1", 9906, 2);
|
||||
$udp->send('Hello World!');
|
||||
|
||||
//穿插其他client也能正常工作
|
||||
$udp_tmp = new Swoole\Coroutine\Client(SWOOLE_SOCK_UDP);
|
||||
$res = $udp_tmp->connect("127.0.0.1", 9909, 2);//nonexistent server
|
||||
$res = $udp_tmp->recv();//get false with timeout
|
||||
var_dump($res);
|
||||
|
||||
$udp_res = $udp->recv();
|
||||
$res = $mysql->query('select sleep(1)', 2);//get false
|
||||
var_dump($res);
|
||||
$res = $mysql->setDefer(false);
|
||||
var_dump($res);//get false
|
||||
$res = $mysql->setDefer();
|
||||
var_dump($res);//get true
|
||||
$mysql_res = $mysql->recv();
|
||||
$res = $redis->get('key');//get false
|
||||
var_dump($res);
|
||||
$redis_res = $redis->recv();
|
||||
$res = $http_client->get('/');
|
||||
var_dump($res);//get false
|
||||
$res = $http_client->recv();
|
||||
var_dump($res);//get true
|
||||
|
||||
var_dump($udp_res, $mysql_res, $redis_res, $http_client);
|
||||
var_dump($http_client->setDefer(false));
|
||||
var_dump($mysql->getDefer(), $redis->getDefer(), $http_client->getDefer());
|
||||
$response->end('Test End');
|
||||
});
|
||||
$server->start();
|
27
vendor/swoole/examples/coroutine/enable_coroutine.php
vendored
Executable file
27
vendor/swoole/examples/coroutine/enable_coroutine.php
vendored
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
|
||||
$http = new swoole_http_server('127.0.0.1', 9501);
|
||||
|
||||
$http->set([
|
||||
'enable_coroutine' => false, // close build-in coroutine
|
||||
]);
|
||||
|
||||
$http->on('workerStart', function () {
|
||||
echo "Coroutine is " . (Co::getuid() > 0 ? 'enable' : 'disable')."\n";
|
||||
});
|
||||
|
||||
$http->on("request", function (Request $request, Response $response) {
|
||||
$response->header("Content-Type", "text/plain");
|
||||
if ($request->server['request_uri'] == '/co') {
|
||||
go(function () use ($response) {
|
||||
$response->end("Hello Coroutine #" . Co::getuid());
|
||||
});
|
||||
} else {
|
||||
$response->end("Hello Swoole #" . Co::getuid());
|
||||
}
|
||||
});
|
||||
|
||||
$http->start();
|
16
vendor/swoole/examples/coroutine/exception/empty.php
vendored
Executable file
16
vendor/swoole/examples/coroutine/exception/empty.php
vendored
Executable file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
go(function () {
|
||||
try {
|
||||
echo "before\n";
|
||||
co::sleep(0.5);
|
||||
echo "after\n";
|
||||
throw new Exception('coro Exception.');
|
||||
} catch (Exception $e) {
|
||||
echo 'Caught exception: ', $e->getMessage(), "\n";
|
||||
} finally {
|
||||
echo "First finally.\n";
|
||||
}
|
||||
});
|
||||
echo "exec file end\n";
|
||||
|
||||
|
20
vendor/swoole/examples/coroutine/fgets.php
vendored
Executable file
20
vendor/swoole/examples/coroutine/fgets.php
vendored
Executable file
@ -0,0 +1,20 @@
|
||||
<?Php
|
||||
$fp = fopen(__DIR__ . "/defer_client.php", "r");
|
||||
stream_set_chunk_size($fp, 1024);
|
||||
|
||||
go(function () use ($fp)
|
||||
{
|
||||
for($i = 0; $i<100;$i++) {
|
||||
$r = co::fgets($fp);
|
||||
if (empty($r) and feof($fp))
|
||||
{
|
||||
//echo "EOF\n";
|
||||
break;
|
||||
}
|
||||
//echo "len=".strlen($r)."\n";
|
||||
echo $r;
|
||||
//echo "---------------------------------------\n";
|
||||
//var_dump($r);
|
||||
//co::sleep(1);
|
||||
}
|
||||
});
|
11
vendor/swoole/examples/coroutine/fread.php
vendored
Executable file
11
vendor/swoole/examples/coroutine/fread.php
vendored
Executable file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
$fp = fopen(__DIR__ . "/defer_client.php", "r");
|
||||
|
||||
co::create(function () use ($fp)
|
||||
{
|
||||
fseek($fp, 256);
|
||||
$r = co::fread($fp);
|
||||
var_dump($r);
|
||||
});
|
10
vendor/swoole/examples/coroutine/fwrite.php
vendored
Executable file
10
vendor/swoole/examples/coroutine/fwrite.php
vendored
Executable file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
$fp = fopen(__DIR__ . "/test.data", "a+");
|
||||
|
||||
co::create(function () use ($fp)
|
||||
{
|
||||
$r = co::fwrite($fp, "hello world\n", 5);
|
||||
var_dump($r);
|
||||
});
|
8
vendor/swoole/examples/coroutine/gethostbyname.php
vendored
Executable file
8
vendor/swoole/examples/coroutine/gethostbyname.php
vendored
Executable file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
co::create(function() {
|
||||
$ip = co::gethostbyname("www.baidu.com");
|
||||
echo "IP: $ip\n";
|
||||
});
|
||||
|
71
vendor/swoole/examples/coroutine/http2_client.php
vendored
Executable file
71
vendor/swoole/examples/coroutine/http2_client.php
vendored
Executable file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
//const TEST = array('get', 'post', 'pipeline');
|
||||
const TEST = array('pipeline');
|
||||
|
||||
co::create(function () use ($fp)
|
||||
{
|
||||
$cli = new co\Http2\Client('127.0.0.1', 9518);
|
||||
|
||||
$cli->set([ 'timeout' => 1]);
|
||||
var_dump($cli->connect());
|
||||
|
||||
if (in_array('get', TEST))
|
||||
{
|
||||
$req = new co\Http2\Request;
|
||||
$req->path = "/index.html";
|
||||
$req->headers = [
|
||||
'host' => "localhost",
|
||||
"user-agent" => 'Chrome/49.0.2587.3',
|
||||
'accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'accept-encoding' => 'gzip',
|
||||
];
|
||||
$req->cookies = ['name' => 'rango', 'email' => '1234@qq.com'];
|
||||
var_dump($cli->send($req));
|
||||
|
||||
$resp = $cli->recv();
|
||||
var_dump($resp);
|
||||
}
|
||||
|
||||
if (in_array('post', TEST))
|
||||
{
|
||||
$req2 = new co\Http2\Request;
|
||||
$req2->path = "/index.php";
|
||||
$req2->headers = [
|
||||
'host' => "localhost",
|
||||
"user-agent" => 'Chrome/49.0.2587.3',
|
||||
'accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'accept-encoding' => 'gzip',
|
||||
];
|
||||
$req2->data = "hello world\n";
|
||||
var_dump($cli->send($req2));
|
||||
|
||||
$resp = $cli->recv();
|
||||
var_dump($resp);
|
||||
}
|
||||
|
||||
if (in_array('pipeline', TEST))
|
||||
{
|
||||
$req3 = new co\Http2\Request;
|
||||
$req3->path = "/index.php";
|
||||
$req3->headers = [
|
||||
'host' => "localhost",
|
||||
"user-agent" => 'Chrome/49.0.2587.3',
|
||||
'accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'accept-encoding' => 'gzip',
|
||||
];
|
||||
$req3->pipeline = true;
|
||||
$req3->method = "POST";
|
||||
$streamId = $cli->send($req3);
|
||||
|
||||
$cli->write($streamId, ['int' => rand(1000, 9999)]);
|
||||
$cli->write($streamId, ['int' => rand(1000, 9999)]);
|
||||
//end stream
|
||||
$cli->write($streamId, ['int' => rand(1000, 9999), 'end' => true], true);
|
||||
|
||||
var_dump($cli->recv());
|
||||
}
|
||||
|
||||
// $cli->close();
|
||||
});
|
43
vendor/swoole/examples/coroutine/http_backend_serv.php
vendored
Executable file
43
vendor/swoole/examples/coroutine/http_backend_serv.php
vendored
Executable file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* @Author: syyuanyizhi@163.com
|
||||
connect refuse: errorCode 111
|
||||
I/O timeout:errorCode 110
|
||||
http 9510
|
||||
tcp 9511
|
||||
|
||||
*/
|
||||
class Server
|
||||
{
|
||||
public $server;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->server = new Swoole\Http\Server("0.0.0.0", 9510);
|
||||
$this->server->set([
|
||||
'worker_num' => 1,
|
||||
'daemonize' => true,
|
||||
'log_file' => '/data/markyuan/swoole.log',
|
||||
]);
|
||||
$this->server->on('Request', ['Server', 'onRequest']);
|
||||
$this->server->start();
|
||||
}
|
||||
public static function onRequest($request, $response)
|
||||
{
|
||||
|
||||
$response->end('xxxx');
|
||||
}
|
||||
|
||||
|
||||
public static function staticFunc()
|
||||
{
|
||||
echo "in static function";
|
||||
}
|
||||
}
|
||||
|
||||
$server = new Server();
|
||||
|
||||
$server->run();
|
||||
|
||||
|
||||
|
12
vendor/swoole/examples/coroutine/http_client.php
vendored
Executable file
12
vendor/swoole/examples/coroutine/http_client.php
vendored
Executable file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
co::create(function () {
|
||||
$cli = new co\http\client('127.0.0.1', 9501);
|
||||
$cli->setHeaders(['Host' => 'localhost']);
|
||||
$cli->set(['http_proxy_host' => HTTP_PROXY_HOST, 'http_proxy_port' => HTTP_PROXY_PORT]);
|
||||
$result = $cli->get('/get?json=true');
|
||||
var_dump($cli->body);
|
||||
// assert($result);
|
||||
// $ret = json_decode($cli->body, true);
|
||||
// assert(is_array($ret) and $ret['json'] == 'true');
|
||||
});
|
13
vendor/swoole/examples/coroutine/http_download.php
vendored
Executable file
13
vendor/swoole/examples/coroutine/http_download.php
vendored
Executable file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$host = 'www.swoole.com';
|
||||
$cli = new \Swoole\Coroutine\Http\Client($host, 443, true);
|
||||
$cli->set(['timeout' => -1]);
|
||||
$cli->setHeaders([
|
||||
'Host' => $host,
|
||||
"User-Agent" => 'Chrome/49.0.2587.3',
|
||||
'Accept' => '*',
|
||||
'Accept-Encoding' => 'gzip'
|
||||
]);
|
||||
$cli->download('/static/files/swoole-logo.svg', __DIR__ . '/logo.svg');
|
||||
});
|
37
vendor/swoole/examples/coroutine/http_server.php
vendored
Executable file
37
vendor/swoole/examples/coroutine/http_server.php
vendored
Executable file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
ini_set("memory_limit","512M");
|
||||
use Swoole\Coroutine as co;
|
||||
class Server
|
||||
{
|
||||
public $server;
|
||||
public $redisPool = [];
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->server = new Swoole\Http\Server("0.0.0.0", 9502, SWOOLE_BASE);
|
||||
$this->server->set([
|
||||
'worker_num' => 1,
|
||||
]);
|
||||
|
||||
// $this->server->on('Connect', [$this, 'onConnect']);
|
||||
$this->server->on('Request', [$this, 'onRequest']);
|
||||
// $this->server->on('Close', [$this, 'onClose']);
|
||||
$this->server->set(['trace_flags' => 1 << 15, 'log_level' => 0]);
|
||||
$this->server->start();
|
||||
}
|
||||
|
||||
public function onRequest($request, $response)
|
||||
{
|
||||
$fd = $request->fd;
|
||||
co::create(function () {
|
||||
co::sleep(0.1);
|
||||
});
|
||||
$response->end(111);
|
||||
}
|
||||
}
|
||||
|
||||
$server = new Server();
|
||||
Swoole\Coroutine::set(array(
|
||||
'max_coroutine' => 1000,
|
||||
));
|
||||
$server->run();
|
165
vendor/swoole/examples/coroutine/httpmulti.php
vendored
Executable file
165
vendor/swoole/examples/coroutine/httpmulti.php
vendored
Executable file
@ -0,0 +1,165 @@
|
||||
<?php
|
||||
/**
|
||||
* @Author: syyuanyizhi@163.com
|
||||
connect refuse: errorCode 111
|
||||
I/O timeout:errorCode 110
|
||||
http 9510
|
||||
tcp 9511
|
||||
|
||||
*/
|
||||
class Server
|
||||
{
|
||||
public $server;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->server = new Swoole\Http\Server("0.0.0.0", 9508);
|
||||
$this->server->set([
|
||||
'worker_num' => 1,
|
||||
'daemonize' => true,
|
||||
'log_file' => '/data/markyuan/swoole.log',
|
||||
]);
|
||||
$this->server->on('Request', ['Server', 'onRequest']);
|
||||
$this->server->start();
|
||||
}
|
||||
|
||||
private static function https(){
|
||||
//--enable-openssl
|
||||
for($i=0;$i<2;$i++){
|
||||
$cli = new Swoole\Coroutine\Http\Client('0.0.0.0',443,TRUE );
|
||||
$cli->set([ 'timeout' => 1]);
|
||||
$cli->setHeaders([
|
||||
'Host' => "api.mp.qq.com",
|
||||
"User-Agent" => 'Chrome/49.0.2587.3',
|
||||
'Accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'Accept-Encoding' => 'gzip',
|
||||
]);
|
||||
$ret = ($cli->get('/cgi-bin/token?appid=3333&secret=222'.$i.$i.$i.$i.$i));
|
||||
error_log(__LINE__.var_export($cli,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$cli->close();
|
||||
}
|
||||
}
|
||||
|
||||
private static function http(){
|
||||
error_log(__LINE__.'---------- begin --- http --------------'.PHP_EOL,3,'/tmp/markyuan');
|
||||
for($i=0;$i<2;$i++){
|
||||
$cli = new Swoole\Coroutine\Http\Client('0.0.0.0', 9510);
|
||||
$cli->set([ 'timeout' => 1]);
|
||||
$cli->setHeaders([
|
||||
'Host' => "api.mp.qq.com",
|
||||
"User-Agent" => 'Chrome/49.0.2587.3',
|
||||
'Accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'Accept-Encoding' => 'gzip',
|
||||
]);
|
||||
error_log(__LINE__.var_export($cli,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$ret = ($cli->get('/cn/token?appid=1FxxxxS9V'.$i.$i.$i.$i.$i));
|
||||
error_log(__LINE__.var_export($ret,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cli,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$cli->close();
|
||||
}
|
||||
error_log(__LINE__.'---------- end --- http --------------'.PHP_EOL,3,'/tmp/markyuan');
|
||||
|
||||
}
|
||||
|
||||
private static function multihttp(){
|
||||
|
||||
error_log(__LINE__.'---------- begin --- multi --------------'.PHP_EOL,3,'/tmp/markyuan');
|
||||
|
||||
$cliAA= new Swoole\Coroutine\Http\Client('0.0.0.0', 9510);
|
||||
$cliAA->set(['timeout' => 1]);
|
||||
$cliAA->setHeaders([
|
||||
'Host' => "api.mp.qq.com",
|
||||
"User-Agent" => 'Chrome/49.0.2587.3',
|
||||
]);
|
||||
$cliBB= new Swoole\Coroutine\Http\Client('0.0.0.0', 9510);
|
||||
$cliBB->set([ 'timeout' => 1]);//
|
||||
$cliBB->setHeaders([
|
||||
'Host' => "api.mp.qq.com",
|
||||
"User-Agent" => 'Chrome/49.0.2587.3',
|
||||
]);
|
||||
error_log(__LINE__.var_export($cliAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$retAA=$cliAA->setDefer(1);
|
||||
$retBB=$cliBB->setDefer(1);
|
||||
error_log(__LINE__.var_export($retAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($retBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$retAA = ($cliAA->get('/cn/token?appid=AAA'));
|
||||
$retBB = ($cliBB->get('/cn/token?appid=BBB'));
|
||||
error_log(__LINE__.var_export($retAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($retBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$retAA=$cliAA->recv();
|
||||
$retBB=$cliBB->recv();
|
||||
error_log(__LINE__.var_export($retAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($retBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$retAA=$cliAA->close();
|
||||
$retBB=$cliBB->close();
|
||||
error_log(__LINE__.'---------- end --- multi --------------'.PHP_EOL,3,'/tmp/markyuan');
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static function tcp(){
|
||||
for($i=0;$i<2;$i++){
|
||||
$tcp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$ret = $tcp_cli ->connect("0.0.0.0", 9511);
|
||||
$ret = $tcp_cli ->send('test for the coro');
|
||||
$ret = $tcp_cli ->recv();
|
||||
$ret=$tcp_cli->close();
|
||||
}
|
||||
}
|
||||
|
||||
private static function coro_dns(){
|
||||
swoole_async_set(array('use_async_resolver'=>1));
|
||||
swoole_async_set(array('dns_cache_refresh_time'=>0));
|
||||
$ret=swoole_async_dns_lookup_coro("www.baidu.com",0.5);
|
||||
error_log(' ip and host '.$host.print_r($ret,true),'3','/home/yuanyizhi/markyuan/markyuan.log');
|
||||
return $ret;
|
||||
// swoole_async_dns_lookup("www.baidu.com", function($host, $ip){
|
||||
// error_log(' ip and host '.$host.' and ip '.$ip,'3','/home/yuanyizhi/markyuan/markyuan.log');
|
||||
// });
|
||||
}
|
||||
|
||||
|
||||
private static function tcpmulti(){
|
||||
$cliAA = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$cliBB = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$retAA = $cliAA ->connect("0.0.0.0", 9511);
|
||||
$retBB = $cliBB ->connect("0.0.0.0", 9511);
|
||||
$retAA = $cliAA ->send('test for the coro');
|
||||
$retBB = $cliBB ->send('test for the coro');
|
||||
$retAA = $cliAA->recv();
|
||||
$retBB = $cliBB->recv();
|
||||
$cliAA->close();
|
||||
$cliBB->close();
|
||||
}
|
||||
|
||||
public static function onRequest($request, $response)
|
||||
{
|
||||
// self::multihttp();
|
||||
// self::http();
|
||||
//self::https();
|
||||
// self::tcp();
|
||||
// self::tcpmulti();
|
||||
$ret=self::coro_dns();
|
||||
$response->end(print_r($ret,true));
|
||||
}
|
||||
|
||||
|
||||
public static function staticFunc()
|
||||
{
|
||||
echo "in static function";
|
||||
}
|
||||
}
|
||||
|
||||
$server = new Server();
|
||||
|
||||
$server->run();
|
||||
|
||||
|
||||
|
32
vendor/swoole/examples/coroutine/mysql_chan.php
vendored
Executable file
32
vendor/swoole/examples/coroutine/mysql_chan.php
vendored
Executable file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
$chan = new chan(4);
|
||||
|
||||
go(function () use ($chan) {
|
||||
|
||||
$db = new co\MySQL();
|
||||
$server = array(
|
||||
'host' => '127.0.0.1',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test',
|
||||
);
|
||||
|
||||
echo "connect\n";
|
||||
$ret1 = $db->connect($server);
|
||||
var_dump($ret1);
|
||||
|
||||
echo "prepare\n";
|
||||
$ret2 = $db->query('SELECT * FROM userinfo WHERE id=3');
|
||||
var_dump($ret2);
|
||||
|
||||
$chan->push($db);
|
||||
});
|
||||
|
||||
go(function () use ($chan) {
|
||||
$db = $chan->pop();
|
||||
$ret2 = $db->query('SELECT * FROM userinfo WHERE id=3');
|
||||
var_dump($ret2);
|
||||
});
|
14
vendor/swoole/examples/coroutine/mysql_execute_empty.php
vendored
Executable file
14
vendor/swoole/examples/coroutine/mysql_execute_empty.php
vendored
Executable file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$db = new Swoole\Coroutine\Mysql;
|
||||
$server = [
|
||||
'host' => '127.0.0.1',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test'
|
||||
];
|
||||
$db->connect($server);
|
||||
$stmt = $db->prepare('SELECT * FROM `userinfo`');
|
||||
$ret = $stmt->execute();
|
||||
var_dump($ret);
|
||||
});
|
42
vendor/swoole/examples/coroutine/mysql_prepare.php
vendored
Executable file
42
vendor/swoole/examples/coroutine/mysql_prepare.php
vendored
Executable file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
co::create(function() {
|
||||
|
||||
$db = new co\MySQL();
|
||||
$server = array(
|
||||
'host' => '127.0.0.1',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test',
|
||||
);
|
||||
|
||||
echo "connect\n";
|
||||
$ret1 = $db->connect($server);
|
||||
var_dump($ret1);
|
||||
|
||||
echo "prepare [1]\n";
|
||||
$stmt1 = $db->prepare('SELECT * FROM userinfo WHERE id=?');
|
||||
var_dump($stmt1);
|
||||
if ($stmt1 == false)
|
||||
{
|
||||
var_dump($db->errno, $db->error);
|
||||
}
|
||||
|
||||
echo "execute\n";
|
||||
$ret3 = $stmt1->execute(array(10));
|
||||
var_dump(count($ret3));
|
||||
|
||||
echo "prepare [2]\n";
|
||||
$stmt2 = $db->prepare('SELECT * FROM userinfo WHERE id > ? and level > ?');
|
||||
var_dump($stmt2);
|
||||
if ($stmt2 == false)
|
||||
{
|
||||
var_dump($db->errno, $db->error);
|
||||
}
|
||||
|
||||
echo "execute\n";
|
||||
$ret4 = $stmt2->execute(array(10, 99));
|
||||
var_dump($ret4);
|
||||
});
|
||||
|
25
vendor/swoole/examples/coroutine/mysql_prepare_2.php
vendored
Executable file
25
vendor/swoole/examples/coroutine/mysql_prepare_2.php
vendored
Executable file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
|
||||
go(function() {
|
||||
|
||||
$db = new Co\MySQL();
|
||||
$server = array(
|
||||
'host' => '127.0.0.1',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test',
|
||||
);
|
||||
|
||||
echo "connect\n";
|
||||
$ret1 = $db->connect($server);
|
||||
var_dump($ret1);
|
||||
|
||||
echo "prepare [1]\n";
|
||||
$stmt1 = $db->prepare('show tables');
|
||||
echo "execute\n";
|
||||
$ret1 = $stmt1->execute([]);
|
||||
var_dump($ret1);
|
||||
|
||||
});
|
||||
|
28
vendor/swoole/examples/coroutine/mysql_procedure_exec.php
vendored
Executable file
28
vendor/swoole/examples/coroutine/mysql_procedure_exec.php
vendored
Executable file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
go(function () {
|
||||
$db = new Swoole\Coroutine\Mysql;
|
||||
$server = [
|
||||
'host' => '127.0.0.1',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test'
|
||||
];
|
||||
|
||||
$clear = <<<SQL
|
||||
DROP PROCEDURE IF EXISTS `say`
|
||||
SQL;
|
||||
$procedure = <<<SQL
|
||||
CREATE DEFINER=`root`@`localhost` PROCEDURE `say`(content varchar(255))
|
||||
BEGIN
|
||||
SELECT concat('you said: \"', content, '\"');
|
||||
END
|
||||
SQL;
|
||||
|
||||
$db->connect($server);
|
||||
if ($db->query($clear) && $db->query($procedure)) {
|
||||
$stmt = $db->prepare('CALL say(?)');
|
||||
$ret = $stmt->execute(['hello mysql!']);
|
||||
var_dump(current($ret[0])); // you said: "hello mysql!"
|
||||
}
|
||||
});
|
26
vendor/swoole/examples/coroutine/mysql_query.php
vendored
Executable file
26
vendor/swoole/examples/coroutine/mysql_query.php
vendored
Executable file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
co::set(['trace_flags' => 1]);
|
||||
|
||||
co::create(function() {
|
||||
|
||||
|
||||
$function = new ReflectionFunction('title');
|
||||
|
||||
$function->invoke();
|
||||
echo "invoke444\n";
|
||||
|
||||
});
|
||||
|
||||
function title() {
|
||||
echo "333invoke_________________________________\n";
|
||||
$tcpclient = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
var_dump($tcpclient->connect('127.0.0.1', 9501, 1));
|
||||
|
||||
}
|
||||
|
||||
echo "111\n";
|
||||
|
||||
|
||||
echo "222\n";
|
||||
co::go();
|
15
vendor/swoole/examples/coroutine/mysql_unixsocket.php
vendored
Executable file
15
vendor/swoole/examples/coroutine/mysql_unixsocket.php
vendored
Executable file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
go(function(){
|
||||
$db = new Swoole\Coroutine\Mysql;
|
||||
$server = [
|
||||
'host' => 'unix:/tmp/mysql.sock',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test'
|
||||
];
|
||||
$db->connect($server);
|
||||
$stmt = $db->prepare('SELECT * FROM `user` WHERE id=?');
|
||||
$ret = $stmt->execute([1]);
|
||||
var_dump($ret);
|
||||
});
|
71
vendor/swoole/examples/coroutine/reconnect_test.php
vendored
Executable file
71
vendor/swoole/examples/coroutine/reconnect_test.php
vendored
Executable file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
/* new multi implement test */
|
||||
$server = new Swoole\Http\Server("127.0.0.1", 9502, SWOOLE_BASE);
|
||||
|
||||
$server->set([
|
||||
'worker_num' => 1,
|
||||
]);
|
||||
|
||||
$server->on('Request', function ($request, $response) {
|
||||
|
||||
/*
|
||||
$mysql = new Swoole\Coroutine\MySQL();
|
||||
$res = $mysql->connect(['host' => '192.168.244.128', 'user' => 'mha_manager', 'password' => 'mhapass', 'database' => 'tt']);
|
||||
if ($res == false) {
|
||||
$response->end("MySQL connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $mysql->connect(['host' => '192.168.244.128', 'user' => 'mha_manager', 'password' => 'mhapass', 'database' => 'tt']);
|
||||
if ($res == false) {
|
||||
$response->end("MySQL connect fail!");
|
||||
return;
|
||||
}
|
||||
$mysql->close();
|
||||
|
||||
$res = $mysql->connect(['host' => '192.168.244.128', 'user' => 'mha_manager', 'password' => 'mhapass', 'database' => 'tt']);
|
||||
if ($res == false) {
|
||||
$response->end("MySQL connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $mysql->query('select sleep(1)', 2);
|
||||
var_dump($res);
|
||||
|
||||
$res = $mysql->connect(['host' => '192.168.244.128', 'user' => 'mha_manager', 'password' => 'mhapass', 'database' => 'tt']);
|
||||
if ($res == false) {
|
||||
$response->end("MySQL connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $mysql->query('select sleep(1)', 2);
|
||||
var_dump($res);
|
||||
*/
|
||||
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$redis->close();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $redis->get('key');
|
||||
var_dump($res);
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $redis->get('key');
|
||||
var_dump($res);
|
||||
|
||||
$response->end('Test End');
|
||||
});
|
||||
$server->start();
|
8
vendor/swoole/examples/coroutine/redis/auth.php
vendored
Executable file
8
vendor/swoole/examples/coroutine/redis/auth.php
vendored
Executable file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$redis = new Swoole\Coroutine\Redis;
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
$redis->auth('root');
|
||||
$redis->set('key', 'swoole redis work');
|
||||
var_dump($redis->get('key'));
|
||||
});
|
7
vendor/swoole/examples/coroutine/redis/eval.php
vendored
Executable file
7
vendor/swoole/examples/coroutine/redis/eval.php
vendored
Executable file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
go(function (){
|
||||
$redis = new Co\Redis;
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
$res = $redis->eval("return redis.call('get', 'key')");
|
||||
var_dump($res);
|
||||
});
|
7
vendor/swoole/examples/coroutine/redis/request.php
vendored
Executable file
7
vendor/swoole/examples/coroutine/redis/request.php
vendored
Executable file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$redis = new Co\Redis;
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
$res = $redis->request(['object', 'encoding', 'key1']);
|
||||
var_dump($res);
|
||||
});
|
23
vendor/swoole/examples/coroutine/redis_pool.php
vendored
Executable file
23
vendor/swoole/examples/coroutine/redis_pool.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
$count = 0;
|
||||
$pool = new SplQueue();
|
||||
$server = new Swoole\Http\Server('127.0.0.1', 9501, SWOOLE_BASE);
|
||||
|
||||
$server->on('Request', function($request, $response) use(&$count, $pool) {
|
||||
if (count($pool) == 0) {
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$pool->push($redis);
|
||||
}
|
||||
$redis = $pool->pop();
|
||||
$count ++;
|
||||
$ret = $redis->set('key', 'value');
|
||||
$response->end("swoole response is ok, count = $count, result=" . var_export($ret, true));
|
||||
$pool->push($redis);
|
||||
});
|
||||
|
||||
$server->start();
|
15
vendor/swoole/examples/coroutine/redis_subscribe.php
vendored
Executable file
15
vendor/swoole/examples/coroutine/redis_subscribe.php
vendored
Executable file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
co::create(function () {
|
||||
$redis = new co\Redis();
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
while (true)
|
||||
{
|
||||
$val = $redis->subscribe(['test']);
|
||||
//订阅的channel,以第一次调用subscribe时的channel为准,后续的subscribe调用是为了收取Redis Server的回包
|
||||
//如果需要改变订阅的channel,请close掉连接,再调用subscribe
|
||||
var_dump($val);
|
||||
}
|
||||
});
|
33
vendor/swoole/examples/coroutine/select/1.php
vendored
Executable file
33
vendor/swoole/examples/coroutine/select/1.php
vendored
Executable file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
$c1 = new chan();
|
||||
//consumer first with select mode
|
||||
$num = 10;
|
||||
go(function () use ($c1,$num) {
|
||||
$read_list = [$c1];
|
||||
$write_list = null;
|
||||
echo "select yield\n";
|
||||
$result = chan::select($read_list, $write_list, 2);
|
||||
echo "select resume res: ".var_export($result,1)."\n";
|
||||
if ($read_list)
|
||||
{
|
||||
foreach($read_list as $ch)
|
||||
{
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $ch->pop();
|
||||
echo "pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1,$num) {
|
||||
echo "push start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c1->push("data-$i");
|
||||
echo "push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
|
||||
});
|
||||
echo "main end\n";
|
23
vendor/swoole/examples/coroutine/select/2.php
vendored
Executable file
23
vendor/swoole/examples/coroutine/select/2.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
$c1 = new chan();
|
||||
//consumer first without select mode
|
||||
$num = 10;
|
||||
go(function () use ($c1, $num) {
|
||||
echo "pop start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c1->pop();
|
||||
echo "pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1,$num) {
|
||||
echo "push start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c1->push("data-$i");
|
||||
echo "push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
|
||||
});
|
||||
echo "main end\n";
|
32
vendor/swoole/examples/coroutine/select/3.php
vendored
Executable file
32
vendor/swoole/examples/coroutine/select/3.php
vendored
Executable file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
$c1 = new chan(1);
|
||||
//product first with select mode
|
||||
$num = 10;
|
||||
go(function () use ($c1,$num) {
|
||||
echo "push start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c1->push("data-$i");
|
||||
echo "push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1,$num) {
|
||||
$read_list = [$c1];
|
||||
$write_list = null;
|
||||
echo "select yield\n";
|
||||
$result = chan::select($read_list, $write_list, 2);
|
||||
echo "select resume res: ".var_export($result,1)."\n";
|
||||
if ($read_list)
|
||||
{
|
||||
foreach($read_list as $ch)
|
||||
{
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $ch->pop();
|
||||
echo "pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
echo "main end\n";
|
23
vendor/swoole/examples/coroutine/select/4.php
vendored
Executable file
23
vendor/swoole/examples/coroutine/select/4.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
$c1 = new chan(2);
|
||||
//product first without select mode
|
||||
$num = 10;
|
||||
go(function () use ($c1,$num) {
|
||||
echo "push start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c1->push("data-$i");
|
||||
echo "push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1, $num) {
|
||||
echo "pop start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c1->pop();
|
||||
echo "pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
});
|
||||
echo "main end\n";
|
||||
|
37
vendor/swoole/examples/coroutine/select/5.php
vendored
Executable file
37
vendor/swoole/examples/coroutine/select/5.php
vendored
Executable file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
$c1 = new chan();
|
||||
$c2 = new chan();
|
||||
function fibonacci($c1, $c2)
|
||||
{
|
||||
go(function () use ($c1, $c2) {
|
||||
$a = 0;
|
||||
$b = 1;
|
||||
while(1) {
|
||||
$read_list = [$c2];
|
||||
$write_list = [$c1];
|
||||
$result = chan::select($read_list, $write_list, 2);
|
||||
if ($write_list) {
|
||||
$t = $a + $b;
|
||||
$a = $b;
|
||||
$b = $t;
|
||||
$write_list[0]->push($a);
|
||||
}
|
||||
if ($read_list) {
|
||||
$ret = $read_list[0]->pop();
|
||||
if ($ret === 1) {
|
||||
echo "quit\n";
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
$num = 10;
|
||||
go(function () use ($c1, $c2, $num) {
|
||||
for ($i = 0; $i < $num; $i ++) {
|
||||
$ret = $c1->pop();
|
||||
echo "fibonacci @$i $ret\n";
|
||||
}
|
||||
$c2->push(1);
|
||||
});
|
||||
fibonacci($c1, $c2);
|
38
vendor/swoole/examples/coroutine/select/6.php
vendored
Executable file
38
vendor/swoole/examples/coroutine/select/6.php
vendored
Executable file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
$c1 = new chan();
|
||||
|
||||
$num = 10;
|
||||
go(function () use ($c1,$num) {
|
||||
$read_list = [$c1];
|
||||
$write_list = null;
|
||||
echo "select yield\n";
|
||||
$result = chan::select($read_list, $write_list, 2);
|
||||
echo "select resume res: ".var_export($result,1)."\n";
|
||||
if ($read_list)
|
||||
{
|
||||
foreach($read_list as $ch)
|
||||
{
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $ch->pop();
|
||||
echo "pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1,$num) {
|
||||
echo "push start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
if ($i == 2) {
|
||||
echo "start sleep\n";
|
||||
co:sleep(1);
|
||||
echo "end sleep\n";
|
||||
}
|
||||
$ret = $c1->push("data-$i");
|
||||
echo "push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
|
||||
});
|
||||
echo "main end\n";
|
54
vendor/swoole/examples/coroutine/select/7.php
vendored
Executable file
54
vendor/swoole/examples/coroutine/select/7.php
vendored
Executable file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
//chan1 block and chan buffer
|
||||
$c1 = new chan();
|
||||
$c2 = new chan(10);
|
||||
$num = 10;
|
||||
go(function () use ($c2,$num) {
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c2->push("chan2-$i");
|
||||
echo "chan 2 push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
});
|
||||
go(function () use ($c1,$num) {
|
||||
$read_list = [$c1];
|
||||
$write_list = null;
|
||||
$result = chan::select($read_list, $write_list, 2);
|
||||
echo "select resume res: ".var_export($result,1)."\n";
|
||||
if ($read_list)
|
||||
{
|
||||
foreach($read_list as $ch)
|
||||
{
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $ch->pop();
|
||||
echo "chan1 pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1,$num) {
|
||||
echo "chan1 push start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
if ($i == 2) {
|
||||
echo "start sleep\n";
|
||||
co:sleep(1);
|
||||
echo "end sleep\n";
|
||||
}
|
||||
$ret = $c1->push("chan1-$i");
|
||||
echo "chan1 push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
go(function () use ($c2,$num) {
|
||||
echo "chan2 pop start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c2->pop();
|
||||
echo "chan2 pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
});
|
||||
echo "main end\n";
|
48
vendor/swoole/examples/coroutine/select/8.php
vendored
Executable file
48
vendor/swoole/examples/coroutine/select/8.php
vendored
Executable file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
//chan1 block and chan buffer
|
||||
$c1 = new chan();
|
||||
$c2 = new chan(10);
|
||||
$num = 10;
|
||||
go(function () use ($c2,$num) {
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c2->push("chan2-$i");
|
||||
echo "chan 2 push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1,$c2,$num) {
|
||||
$ori_list = $read_list = [$c1,$c2];
|
||||
$write_list = null;
|
||||
$result = chan::select($read_list, $write_list, 2);
|
||||
echo "select resume res: ".var_export($result,1)."\n";
|
||||
|
||||
if ($ori_list)
|
||||
{
|
||||
foreach ($ori_list as $chan => $ch)
|
||||
{
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $ch->pop();
|
||||
$chan_id = $chan + 1;
|
||||
echo "chan{$chan_id} pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1,$num) {
|
||||
echo "chan1 push start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
if ($i == 2) {
|
||||
echo "start sleep\n";
|
||||
co:sleep(1);
|
||||
echo "end sleep\n";
|
||||
}
|
||||
$ret = $c1->push("chan1-$i");
|
||||
echo "chan1 push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
|
||||
});
|
||||
echo "main end\n";
|
28
vendor/swoole/examples/coroutine/send_yield.php
vendored
Executable file
28
vendor/swoole/examples/coroutine/send_yield.php
vendored
Executable file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
$serv = new Swoole\Server("0.0.0.0", 9501, SWOOLE_BASE);
|
||||
$serv->set(array(
|
||||
'worker_num' => 1,
|
||||
'send_yield' => true,
|
||||
'socket_buffer_size' => 512 * 1024,
|
||||
'kernel_socket_buffer_size' => 65536,
|
||||
));
|
||||
$serv->on('connect', function ($serv, $fd) {
|
||||
echo "Client:Connect.\n";
|
||||
});
|
||||
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
|
||||
$length = 0;
|
||||
$size = 1024 * 128;
|
||||
while (true)
|
||||
{
|
||||
$ret = $serv->send($fd, str_repeat('A', $size));
|
||||
if ($ret == false) {
|
||||
break;
|
||||
}
|
||||
$length += $size;
|
||||
echo "send $length success\n";
|
||||
}
|
||||
});
|
||||
$serv->on('close', function ($serv, $fd) {
|
||||
echo "Client: Close.\n";
|
||||
});
|
||||
$serv->start();
|
26
vendor/swoole/examples/coroutine/send_yield_client.php
vendored
Executable file
26
vendor/swoole/examples/coroutine/send_yield_client.php
vendored
Executable file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP);
|
||||
$client->set(array(
|
||||
'kernel_socket_buffer_size' => 65536,
|
||||
));
|
||||
|
||||
if (!$client->connect('127.0.0.1', 9501, -1))
|
||||
{
|
||||
exit("connect failed. Error: {$client->errCode}\n");
|
||||
}
|
||||
|
||||
var_dump($client->getsockname());
|
||||
|
||||
$client->send("start\n");
|
||||
$length = 0;
|
||||
|
||||
while(true)
|
||||
{
|
||||
$data = $client->recv(65536);
|
||||
if ($data == false) {
|
||||
break;
|
||||
}
|
||||
$length += strlen($data);
|
||||
echo "recv ".$length." bytes\n";
|
||||
usleep(100000);
|
||||
}
|
13
vendor/swoole/examples/coroutine/sleep.php
vendored
Executable file
13
vendor/swoole/examples/coroutine/sleep.php
vendored
Executable file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
$server = new Swoole\Http\Server("127.0.0.1", 9502, SWOOLE_BASE);
|
||||
|
||||
$server->set([
|
||||
'worker_num' => 1,
|
||||
]);
|
||||
|
||||
$server->on('Request', function ($request, $response) {
|
||||
Swoole\Coroutine::sleep(0.2);
|
||||
$response->end('Test End');
|
||||
});
|
||||
|
||||
$server->start();
|
17
vendor/swoole/examples/coroutine/socket/accept.php
vendored
Executable file
17
vendor/swoole/examples/coroutine/socket/accept.php
vendored
Executable file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$sock = new Swoole\Coroutine\Socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
|
||||
$ret = $sock->bind('127.0.0.1', 9601);
|
||||
var_dump($ret);
|
||||
assert($sock->listen(512));
|
||||
$conn = $sock->accept();
|
||||
|
||||
$data = $conn->recv();
|
||||
var_dump($data);
|
||||
$json = json_decode($data, true);
|
||||
var_dump($json);
|
||||
$ret = $conn->send("world\n");
|
||||
echo "send res {$ret} \n";
|
||||
$conn->close();
|
||||
});
|
||||
|
12
vendor/swoole/examples/coroutine/socket/sendto.php
vendored
Executable file
12
vendor/swoole/examples/coroutine/socket/sendto.php
vendored
Executable file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
echo "start \n";
|
||||
go(function () {
|
||||
$conn = new Swoole\Coroutine\Socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
|
||||
$ret = $conn->connect('127.0.0.1', 9601);
|
||||
echo "connect ret:".var_export($ret,1)." error:".var_export($conn->errCode,1)."\n";
|
||||
$ret = $conn->send(json_encode(['data' => 'hello']));
|
||||
echo "send ret:".var_export($ret,1)."\n";
|
||||
echo $conn->recv();
|
||||
});
|
||||
echo "end \n";
|
20
vendor/swoole/examples/coroutine/stack.php
vendored
Executable file
20
vendor/swoole/examples/coroutine/stack.php
vendored
Executable file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
co::set(['stack_size' => 8192*4]);
|
||||
|
||||
function test($n)
|
||||
{
|
||||
$a = 1;
|
||||
$b = 2;
|
||||
$c = 3;
|
||||
$d = 4;
|
||||
static $i;
|
||||
|
||||
usleep(100000);
|
||||
echo "index=".($i++)."\n";
|
||||
|
||||
return test($n + $a + $b + $c + $d);
|
||||
}
|
||||
|
||||
go(function () {
|
||||
test(9);
|
||||
});
|
27
vendor/swoole/examples/coroutine/task_co.php
vendored
Executable file
27
vendor/swoole/examples/coroutine/task_co.php
vendored
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
$server = new Swoole\Http\Server("127.0.0.1", 9502, SWOOLE_BASE);
|
||||
|
||||
$server->set([
|
||||
'worker_num' => 1,
|
||||
'task_worker_num' => 2,
|
||||
]);
|
||||
|
||||
$server->on('Task', function (swoole_server $serv, $task_id, $worker_id, $data) {
|
||||
echo "#{$serv->worker_id}\tonTask: worker_id={$worker_id}, task_id=$task_id\n";
|
||||
if ($serv->worker_id == 1) {
|
||||
sleep(1);
|
||||
}
|
||||
return $data;
|
||||
});
|
||||
|
||||
$server->on('Finish', function (swoole_server $serv, $task_id, $data) {
|
||||
echo "Task#$task_id finished, data_len=".strlen($data).PHP_EOL;
|
||||
});
|
||||
|
||||
$server->on('Request', function ($request, $response) use ($server)
|
||||
{
|
||||
$result = $server->taskCo(["hello world", ['data' => 1234, 'code' => 200]], 0.5);
|
||||
$response->end('Test End, Result: '.var_export($result, true));
|
||||
});
|
||||
|
||||
$server->start();
|
18
vendor/swoole/examples/coroutine/tcp_backend_serv.php
vendored
Executable file
18
vendor/swoole/examples/coroutine/tcp_backend_serv.php
vendored
Executable file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
$serv = new Swoole\Server("0.0.0.0", 9511);
|
||||
$serv->set(array(
|
||||
'worker_num' => 1, //工作进程数量
|
||||
'daemonize' => true, //是否作为守护进程
|
||||
));
|
||||
$serv->on('connect', function ($serv, $fd){
|
||||
echo "Client:Connect.\n";
|
||||
});
|
||||
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
|
||||
$serv->send($fd, 'Swoole: '.$data);
|
||||
$serv->close($fd);
|
||||
});
|
||||
$serv->on('close', function ($serv, $fd) {
|
||||
echo "Client: Close.\n";
|
||||
});
|
||||
$serv->start();
|
||||
|
23
vendor/swoole/examples/coroutine/tcp_echo.php
vendored
Executable file
23
vendor/swoole/examples/coroutine/tcp_echo.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
$serv = new swoole_server("0.0.0.0", 9501);
|
||||
$serv->on('connect', function ($serv, $fd, $reactor_id){
|
||||
echo "[#".posix_getpid()."]\tClient@[$fd]: Connect.\n";
|
||||
});
|
||||
$serv->set(array(
|
||||
'worker_num' => 1,
|
||||
|
||||
));
|
||||
|
||||
$serv->on('receive', function (swoole_server $serv, $fd, $reactor_id, $data) {
|
||||
echo "[#".$serv->worker_id."]\tClient[$fd] receive data: $data\n";
|
||||
if ($serv->send($fd, "hello {$data}\n") == false)
|
||||
{
|
||||
echo "error\n";
|
||||
}
|
||||
});
|
||||
|
||||
$serv->on('close', function ($serv, $fd, $reactor_id) {
|
||||
echo "[#".posix_getpid()."]\tClient@[$fd]: Close.\n";
|
||||
});
|
||||
|
||||
$serv->start();
|
16
vendor/swoole/examples/coroutine/timer_test.php
vendored
Executable file
16
vendor/swoole/examples/coroutine/timer_test.php
vendored
Executable file
@ -0,0 +1,16 @@
|
||||
<?
|
||||
/**
|
||||
* @Author: winterswang
|
||||
* @Date: 2016-06-26 16:34:02
|
||||
* @Last Modified by: winterswang
|
||||
* @Last Modified time: 2016-06-26 16:41:46
|
||||
*/
|
||||
|
||||
swoole_timer_after(1000, function(){
|
||||
echo " timer after timeout\n";
|
||||
});
|
||||
|
||||
swoole_timer_tick(1000, function(){
|
||||
echo "timer tick timeout\n";
|
||||
});
|
||||
?>
|
74
vendor/swoole/examples/coroutine/udp_client.php
vendored
Executable file
74
vendor/swoole/examples/coroutine/udp_client.php
vendored
Executable file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
class Client
|
||||
{
|
||||
private $ip = "127.0.0.1";
|
||||
const PORT = 8888;
|
||||
private $data;
|
||||
|
||||
public function sendRequest()
|
||||
{
|
||||
$this->data = "swoole test";
|
||||
$this->send();
|
||||
$this->moreThanOneRecv();
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public function send()
|
||||
{
|
||||
$cli = new swoole_client_coro(SWOOLE_SOCK_UDP);
|
||||
$ret = $cli->connect($this->ip, self::PORT);
|
||||
$cli->send($this->data);
|
||||
$ret = $cli->recv();
|
||||
$cli->close();
|
||||
}
|
||||
|
||||
public function moreThanOneRecv()
|
||||
{
|
||||
$cli = new swoole_client_coro(SWOOLE_SOCK_UDP);
|
||||
$ret = $cli->connect($this->ip, self::PORT);
|
||||
$cli->send("sent by cli");
|
||||
|
||||
$cli2 = new swoole_client_coro(SWOOLE_SOCK_UDP);
|
||||
$ret = $cli2->connect($this->ip, self::PORT);
|
||||
$cli2->send("sent by cli2");
|
||||
|
||||
$cli3 = new swoole_client_coro(SWOOLE_SOCK_UDP);
|
||||
$ret = $cli3->connect($this->ip, self::PORT);
|
||||
$cli3->send("sent by cli3");
|
||||
|
||||
sleep(1);
|
||||
$ret = $cli3->recv();
|
||||
$ret = $cli2->recv();
|
||||
$ret = $cli->recv();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
class Server
|
||||
{
|
||||
public $server;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->server = new swoole_http_server("127.0.0.1", 9502);
|
||||
$this->server->set([
|
||||
'worker_num' => 1,
|
||||
'daemonize' => true,
|
||||
'log_file' => '/tmp/swoole.log',
|
||||
]);
|
||||
$this->server->on('Request',['Server', 'onRequest']);
|
||||
$this->server->start();
|
||||
}
|
||||
|
||||
public static function onRequest($request, $response)
|
||||
{
|
||||
self::staticFunc();
|
||||
$cli = new swoole_client_coro(SWOOLE_SOCK_UDP);
|
||||
$client = new Client();
|
||||
$ret = $client->sendRequest();
|
||||
$response->end($ret);
|
||||
}
|
||||
}
|
||||
|
||||
$server = new Server();
|
||||
$server->run();
|
126
vendor/swoole/examples/coroutine/udp_tcp_timeout.php
vendored
Executable file
126
vendor/swoole/examples/coroutine/udp_tcp_timeout.php
vendored
Executable file
@ -0,0 +1,126 @@
|
||||
<?php
|
||||
/**
|
||||
* @Author: winterswang
|
||||
* @Date: 2015-06-18 16:45:09
|
||||
* @Last Modified by: winterswang
|
||||
* @Last Modified time: 2016-09-18 17:36:18
|
||||
*/
|
||||
|
||||
class TestHttpServer {
|
||||
|
||||
public $http;
|
||||
public $queue;
|
||||
public $setting = array();
|
||||
|
||||
/**
|
||||
* [__construct description]
|
||||
* @param array $setting [description]
|
||||
*/
|
||||
public function __construct(){
|
||||
|
||||
}
|
||||
|
||||
public function set($setting){
|
||||
|
||||
$this ->setting = $setting;
|
||||
}
|
||||
|
||||
/**
|
||||
* [init description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function init(){
|
||||
|
||||
if (!isset($this ->setting['host'])) {
|
||||
$this ->setting['host'] = '0.0.0.0';
|
||||
}
|
||||
if (!isset($this ->setting['port'])) {
|
||||
$this ->setting['port'] = '9999';
|
||||
}
|
||||
|
||||
$this ->http = new Swoole\Http\Server($this ->setting['host'], $this ->setting['port']);
|
||||
$this ->http ->set($this ->setting);
|
||||
|
||||
$this ->http ->on('request', array($this, 'onRequest'));
|
||||
$this ->http ->on('close', array($this, 'onClose'));
|
||||
}
|
||||
|
||||
/**
|
||||
* [onRequest description]
|
||||
* @param [type] $request [description]
|
||||
* @param [type] $response [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function onRequest($request, $response){
|
||||
|
||||
|
||||
//$udp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_UDP);
|
||||
$tcp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
|
||||
// $ret = $udp_cli ->connect('10.100.65.222', 9906);
|
||||
// $ret = $udp_cli ->send('test for the coro');
|
||||
// $ret = $udp_cli ->recv(100);
|
||||
// $udp_cli->close();
|
||||
|
||||
// if ($ret) {
|
||||
// //error_log(" udp cli get rsp == " . print_r($ret, true),3, '/data/log/udp_timeout.log');
|
||||
// }
|
||||
// else{
|
||||
// error_log(" udp cli timeout \n",3, '/data/log/udp_timeout.log');
|
||||
// }
|
||||
|
||||
$ret = $tcp_cli ->connect("10.100.64.151", 9805);
|
||||
$ret = $tcp_cli ->send('test for the coro');
|
||||
$ret = $tcp_cli ->recv(100);
|
||||
$tcp_cli->close();
|
||||
|
||||
if ($ret) {
|
||||
//error_log(" tcp cli get rsp == " . print_r($ret, true) . PHP_EOL, 3, '/data/log/udp_timeout.log');
|
||||
}
|
||||
else{
|
||||
error_log(" tcp cli timeout \n",3, '/data/log/udp_timeout.log');
|
||||
}
|
||||
|
||||
$response ->end(" swoole response is ok");
|
||||
}
|
||||
|
||||
/**
|
||||
* [onClose description]
|
||||
* @param [type] $server [description]
|
||||
* @param [type] $fd [description]
|
||||
* @param [type] $from_id [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function onClose($server, $fd, $from_id){
|
||||
|
||||
//echo " on close fd = $fd from_id = $from_id \n";
|
||||
}
|
||||
|
||||
/**
|
||||
* [start description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function start(){
|
||||
|
||||
$this ->init();
|
||||
$this ->http ->start();
|
||||
}
|
||||
}
|
||||
|
||||
$setting = array(
|
||||
'host' => '0.0.0.0',
|
||||
'port' => 10006,
|
||||
'worker_num' => 4,
|
||||
'dispatch_mode' => 3, //固定分配请求到worker
|
||||
'reactor_num' => 4, //亲核
|
||||
'daemonize' => 1, //守护进程
|
||||
'backlog' => 128,
|
||||
'log_file' => '/data/log/test_http_server.log',
|
||||
);
|
||||
$th = new TestHttpServer();
|
||||
$th ->set($setting);
|
||||
$th ->start();
|
||||
|
||||
|
||||
|
||||
|
17
vendor/swoole/examples/coroutine/user_coroutine.php
vendored
Executable file
17
vendor/swoole/examples/coroutine/user_coroutine.php
vendored
Executable file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
for($i = 0; $i < 100; $i++) {
|
||||
Swoole\Coroutine::create(function() use ($i) {
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
$ret = $redis->incr('coroutine');
|
||||
$redis->close();
|
||||
if ($i == 50) {
|
||||
Swoole\Coroutine::create(function() use ($i) {
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
$ret = $redis->set('coroutine_i', 50);
|
||||
$redis->close();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
24
vendor/swoole/examples/coroutine/websocket.php
vendored
Executable file
24
vendor/swoole/examples/coroutine/websocket.php
vendored
Executable file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
$ws = new swoole_websocket_server("127.0.0.1", 9501, SWOOLE_BASE);
|
||||
$ws->set(array(
|
||||
'log_file' => '/dev/null'
|
||||
));
|
||||
$ws->on("WorkerStart", function (\swoole_server $serv) {
|
||||
|
||||
});
|
||||
|
||||
$ws->on('open', function ($serv, swoole_http_request $request) {
|
||||
//$ip = co::gethostbyname('www.baidu.com');
|
||||
if (1) {
|
||||
$serv->push($request->fd, "start\n");
|
||||
}
|
||||
});
|
||||
|
||||
$ws->on('message', function ($serv, $frame) {
|
||||
var_dump($frame);
|
||||
co::sleep(0.1);
|
||||
$data = $frame->data;
|
||||
$serv->push($frame->fd, "hello client {$data}\n");
|
||||
});
|
||||
|
||||
$ws->start();
|
32
vendor/swoole/examples/coroutine/websocket_client.php
vendored
Executable file
32
vendor/swoole/examples/coroutine/websocket_client.php
vendored
Executable file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
// go(function () {
|
||||
// $cli = new Co\http\Client("127.0.0.1", 9501);
|
||||
// $ret = $cli->upgrade("/");
|
||||
|
||||
// if ($ret) {
|
||||
// while(true) {
|
||||
// $cli->push("hello");
|
||||
// var_dump($cli->recv());
|
||||
// co::sleep(0.1);
|
||||
// }
|
||||
// }
|
||||
|
||||
// });
|
||||
go(function () {
|
||||
$cli = new Co\http\Client("127.0.0.1", 9501);
|
||||
$cli->set([
|
||||
'timeout' => 1
|
||||
]);
|
||||
$ret = $cli->upgrade("/");
|
||||
|
||||
if (!$ret) {
|
||||
echo "ERROR\n";
|
||||
return;
|
||||
}
|
||||
var_dump($cli->recv());
|
||||
for ($i = 0; $i < 5; $i ++) {
|
||||
$cli->push("hello @$i");
|
||||
var_dump($cli->recv());
|
||||
co::sleep(0.1);
|
||||
}
|
||||
});
|
Reference in New Issue
Block a user