Debian Codegraph @

third-party/libhv-v1.3.3/docs/cn/TcpServer.md

raw
1 TCP 服务端类
2
3 ```c++
4
5 class TcpServer {
6
7 // 返回索引的事件循环
8 EventLoopPtr loop(int idx = -1);
9
10 // 创建套接字
11 int createsocket(int port, const char* host = "0.0.0.0");
12
13 // 关闭套接字
14 void closesocket();
15
16 // 设置最大连接数
17 void setMaxConnectionNum(uint32_t num);
18
19 // 设置负载均衡策略
20 void setLoadBalance(load_balance_e lb);
21
22 // 设置线程数
23 void setThreadNum(int num);
24
25 // 开始运行
26 void start(bool wait_threads_started = true);
27
28 // 停止运行
29 void stop(bool wait_threads_stopped = true);
30
31 // 设置SSL/TLS加密通信
32 int withTLS(hssl_ctx_opt_t* opt = NULL);
33
34 // 设置拆包规则
35 void setUnpack(unpack_setting_t* setting);
36
37 // 返回当前连接数
38 size_t connectionNum();
39
40 // 遍历连接
41 int foreachChannel(std::function<void(const TSocketChannelPtr& channel)> fn);
42
43 // 广播消息
44 int broadcast(const void* data, int size);
45 int broadcast(const std::string& str);
46
47 // 连接到来/断开回调
48 std::function<void(const TSocketChannelPtr&)> onConnection;
49
50 // 消息回调
51 std::function<void(const TSocketChannelPtr&, Buffer*)> onMessage;
52
53 // 写完成回调
54 std::function<void(const TSocketChannelPtr&, Buffer*)> onWriteComplete;
55
56 };
57
58 ```
59
60 测试代码见 [evpp/TcpServer_test.cpp](../../evpp/TcpServer_test.cpp)