forked from PlumpMath/php-flame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp.php
61 lines (58 loc) · 1.67 KB
/
tcp.php
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
* 提供对 TCP 网络操作的相关封装
*/
namespace flame\tcp;
/**
* 连接指定地址, 并返回 socket 对象
* @param string $address 连接地址, 例如: "127.0.0.1:8687"
*/
function connect(string $address):socket {
return new socket();
}
class socket {
/**
* 本地地址(含端口)
* @var string
*/
public $local_address;
/**
* 远端地址(含端口)
* @var string
*/
public $remote_address;
/**
* 读取一定量的数据, 实际读取方式根据 $completion 参数决定;
* @param mixed $completion
* * 参数为 null 时, 表示随机读取一定量数据 (不会超过 8192 字节);
* * 参数为 int 时, 表示读取指定量的数据, 例如 $completion = 1024 表示读取 1024 字节;
* * 参数为 string 时, 表示读取到指定的结束符, 例如 $completion = "\r\n" 读取到 "abc\r\n" 字符串;
*/
function read($completion = null) {}
/**
* 发送数据
* @param string $data
*/
function write(string $data) {}
/**
* 关闭连接
*/
function close() {}
}
class server {
/**
* 创建一个服务器, 绑定在指定的地址上, 准备服务
* @param string $address 绑定地址, 例如: "0.0.0.0:8888" 或 ":::8888"
*/
function __construct(string $address) {}
/**
* 启动服务器, 当有连接建立时, 启用协程执行回调函数;
* @param callable $cb 回调函数, 形如:
* function($socket) {}
*/
function run(callable $cb) {}
/**
* 关闭服务器
*/
function close() {}
}