协程socket

Catalogue
  1. 1. @Lib\Coroutine\Socket
  2. 2. @Constant
  3. 3. @__construct 初始化
  4. 4. @bind 绑定端口
  5. 5. @listen 启动监听
  6. 6. @accept 接收新连接
  7. 7. @recv 读取缓冲区数据
  8. 8. @send 向对端连接写入数据
  9. 9. @close 关闭连接

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

@Lib\Coroutine\Socket

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
lib_event_init();

cgo(function ()
{
$socket = new Lib\Coroutine\Socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if($socket->fd < 0){
var_dump("err");return;
}
$socket->bind("127.0.0.1", 9999);
$socket->listen();
while (true) {
$conn = $socket->accept();
cgo(function () use($conn)
{
$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();
});
}
});

lib_event_wait();

@Constant

1
AF_INET, SOCK_STREAM, IPPROTO_IP

@__construct 初始化

@bind 绑定端口

@listen 启动监听

@accept 接收新连接

@recv 读取缓冲区数据

@send 向对端连接写入数据

@close 关闭连接