| 1 |
通道类 |
| 2 |
|
| 3 |
```c++ |
| 4 |
|
| 5 |
class Channel { |
| 6 |
|
| 7 |
// 返回底层的io结构体指针 |
| 8 |
hio_t* io() { return io_; } |
| 9 |
|
| 10 |
// 返回socket文件描述符 |
| 11 |
int fd() { return fd_; } |
| 12 |
|
| 13 |
// 返回一个唯一标示id |
| 14 |
uint32_t id() { return id_; } |
| 15 |
|
| 16 |
// 返回错误码 |
| 17 |
int error() { return hio_error(io_); } |
| 18 |
|
| 19 |
// 获取/设置/新建/删除 上下文 |
| 20 |
void* context(); |
| 21 |
void setContext(void* ctx); |
| 22 |
template<class T> T* newContext(); |
| 23 |
template<class T> T* getContext(); |
| 24 |
template<class T> void deleteContext(); |
| 25 |
|
| 26 |
// 获取/设置/新建/删除 上下文智能指针 |
| 27 |
std::shared_ptr<void> contextPtr(); |
| 28 |
void setContextPtr(const std::shared_ptr<void>& ctx); |
| 29 |
void setContextPtr(std::shared_ptr<void>&& ctx); |
| 30 |
template<class T> std::shared_ptr<T> newContextPtr(); |
| 31 |
template<class T> std::shared_ptr<T> getContextPtr(); |
| 32 |
void deleteContextPtr(); |
| 33 |
|
| 34 |
// 是否打开状态 |
| 35 |
bool isOpened(); |
| 36 |
|
| 37 |
// 是否关闭状态 |
| 38 |
bool isClosed(); |
| 39 |
|
| 40 |
// 开始读 |
| 41 |
int startRead(); |
| 42 |
|
| 43 |
// 停止读 |
| 44 |
int stopRead(); |
| 45 |
|
| 46 |
// 读一次 |
| 47 |
int readOnce(); |
| 48 |
// 读一个字符串 |
| 49 |
int readString(); |
| 50 |
// 读一行 |
| 51 |
int readLine(); |
| 52 |
// 读取N个字节 |
| 53 |
int readBytes(int len); |
| 54 |
|
| 55 |
// 写 |
| 56 |
int write(const void* data, int size); |
| 57 |
int write(Buffer* buf); |
| 58 |
int write(const std::string& str); |
| 59 |
|
| 60 |
// 设置最大读缓存 |
| 61 |
void setMaxReadBufsize(uint32_t size); |
| 62 |
// 设置最大写缓存 |
| 63 |
void setMaxWriteBufsize(uint32_t size); |
| 64 |
// 获取当前写缓存大小 |
| 65 |
size_t writeBufsize(); |
| 66 |
// 是否写完成 |
| 67 |
bool isWriteComplete(); |
| 68 |
|
| 69 |
// 关闭 |
| 70 |
int close(bool async = false); |
| 71 |
|
| 72 |
// 读回调 |
| 73 |
std::function<void(Buffer*)> onread; |
| 74 |
// 写回调 |
| 75 |
std::function<void(Buffer*)> onwrite; |
| 76 |
// 关闭回调 |
| 77 |
std::function<void()> onclose; |
| 78 |
}; |
| 79 |
|
| 80 |
// SocketChannel 继承自 Channel |
| 81 |
class SocketChannel : public Channel { |
| 82 |
// 连接状态回调 |
| 83 |
std::function<void()> onconnect; |
| 84 |
// 心跳回调 |
| 85 |
std::function<void()> heartbeat; |
| 86 |
|
| 87 |
// 启用SSL/TLS加密通信 |
| 88 |
int enableSSL(); |
| 89 |
// 是否是SSL/TLS加密通信 |
| 90 |
bool isSSL(); |
| 91 |
// 设置SSL |
| 92 |
int setSSL(hssl_t ssl); |
| 93 |
// 设置SSL_CTX |
| 94 |
int setSslCtx(hssl_ctx_t ssl_ctx); |
| 95 |
// 新建SSL_CTX |
| 96 |
int newSslCtx(hssl_ctx_opt_t* opt); |
| 97 |
// 设置主机名 |
| 98 |
int setHostname(const std::string& hostname); |
| 99 |
|
| 100 |
// 设置连接超时 |
| 101 |
void setConnectTimeout(int timeout_ms); |
| 102 |
|
| 103 |
// 设置关闭超时 (说明:非阻塞写队列非空时,需要等待写完成再关闭) |
| 104 |
void setCloseTimeout(int timeout_ms); |
| 105 |
|
| 106 |
// 设置读超时 (一段时间没有数据到来便自动关闭连接) |
| 107 |
void setReadTimeout(int timeout_ms); |
| 108 |
|
| 109 |
// 设置写超时 (一段时间没有数据发送便自动关闭连接) |
| 110 |
void setWriteTimeout(int timeout_ms); |
| 111 |
|
| 112 |
// 设置keepalive超时 (一段时间没有数据收发便自动关闭连接) |
| 113 |
void setKeepaliveTimeout(int timeout_ms); |
| 114 |
|
| 115 |
// 设置心跳 (定时发送心跳包) |
| 116 |
void setHeartbeat(int interval_ms, std::function<void()> fn); |
| 117 |
|
| 118 |
// 设置拆包规则 |
| 119 |
void setUnpack(unpack_setting_t* setting); |
| 120 |
|
| 121 |
// 开始连接 |
| 122 |
int startConnect(int port, const char* host = "127.0.0.1"); |
| 123 |
int startConnect(struct sockaddr* peeraddr); |
| 124 |
int startConnect(); |
| 125 |
|
| 126 |
// 是否已连接 |
| 127 |
bool isConnected(); |
| 128 |
|
| 129 |
// 返回本地地址 |
| 130 |
std::string localaddr(); |
| 131 |
|
| 132 |
// 返回对端地址 |
| 133 |
std::string peeraddr(); |
| 134 |
}; |
| 135 |
|
| 136 |
// WebSocketChannel 继承自 SocketChannel |
| 137 |
class WebSocketChannel : public SocketChannel { |
| 138 |
|
| 139 |
// 发送文本帧 |
| 140 |
int send(const std::string& msg, enum ws_opcode opcode = WS_OPCODE_TEXT, bool fin = true); |
| 141 |
|
| 142 |
// 发送二进制帧 |
| 143 |
int send(const char* buf, int len, enum ws_opcode opcode = WS_OPCODE_BINARY, bool fin = true); |
| 144 |
|
| 145 |
// 分片发送 |
| 146 |
int send(const char* buf, int len, int fragment, enum ws_opcode opcode = WS_OPCODE_BINARY); |
| 147 |
|
| 148 |
// 关闭 |
| 149 |
int close(); |
| 150 |
|
| 151 |
}; |
| 152 |
|
| 153 |
``` |