协程tcp服务

Catalogue
  1. 1. @Lib\Coroutine\Server
  2. 2. @__construct 初始化
  3. 3. @set_handler($callback) 回调触发事件
  4. 4. start() 启动监听

创建协程版server,封装所有协程api,所有阻塞操作都会触发协程切换

@Lib\Coroutine\Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//初始化全局对象 epoll等内存空间初始化
lib_event_init();

//协程运行
cgo(function ()
{
$server = new Lib\Coroutine\Server("127.0.0.1", 9991);
$server->set_handler(function (Lib\Coroutine\Socket $conn) use($server) {
$data = $conn->recv();
$responseStr = "HTTP/1.1 200 OK\r\n
Content-Type: text/html\r\n
Connection: close\r\n
Content-Length: 11\r\n\r\n
hello world\r\n";
$conn->send($responseStr);
$conn->close();
// Sco::sleep(0.01);
});
$server->start();
});


//epoll event 轮循 检查事件
lib_event_wait();

@__construct 初始化

@set_handler($callback) 回调触发事件

start() 启动监听