evpp tcpclient 封装成类
时间: 2023-07-25 18:34:52 浏览: 49
如果需要将 evpp tcpclient 封装成一个类,可以按照以下步骤进行:
1. 创建一个类,例如名为 TCPClientWrapper。
```cpp
class TCPClientWrapper {
public:
TCPClientWrapper(const std::string& server_ip, int server_port);
~TCPClientWrapper();
void Connect();
void Send(const std::string& data);
private:
void OnConnection(const evpp::TCPConnPtr& conn);
void OnMessage(const evpp::TCPConnPtr& conn, evpp::Buffer* buf);
evpp::EventLoop loop_;
evpp::TCPClient client_;
};
```
2. 在类的构造函数中初始化 evpp::TCPClient 对象,并设置连接的服务器地址和端口号。
```cpp
TCPClientWrapper::TCPClientWrapper(const std::string& server_ip, int server_port)
: client_(&loop_, server_ip, server_port, "TCPClient") {
client_.SetConnectionCallback(std::bind(&TCPClientWrapper::OnConnection, this, std::placeholders::_1));
client_.SetMessageCallback(std::bind(&TCPClientWrapper::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
}
```
3. 实现连接成功和断开连接的回调函数。
```cpp
void TCPClientWrapper::OnConnection(const evpp::TCPConnPtr& conn) {
if (conn->IsConnected()) {
LOG_INFO << "Connected to server";
} else {
LOG_INFO << "Disconnected from server";
}
}
```
4. 实现收到消息的回调函数。
```cpp
void TCPClientWrapper::OnMessage(const evpp::TCPConnPtr& conn, evpp::Buffer* buf) {
std::string msg = buf->NextAllString();
LOG_INFO << "Received message: " << msg;
}
```
5. 实现 Connect() 和 Send() 函数。
```cpp
void TCPClientWrapper::Connect() {
client_.Connect();
loop_.Run();
}
void TCPClientWrapper::Send(const std::string& data) {
client_.Send(data);
}
```
6. 实现析构函数,在其中停止事件循环。
```cpp
TCPClientWrapper::~TCPClientWrapper() {
loop_.Stop();
}
```
完整的类实现代码如下:
```cpp
class TCPClientWrapper {
public:
TCPClientWrapper(const std::string& server_ip, int server_port);
~TCPClientWrapper();
void Connect();
void Send(const std::string& data);
private:
void OnConnection(const evpp::TCPConnPtr& conn);
void OnMessage(const evpp::TCPConnPtr& conn, evpp::Buffer* buf);
evpp::EventLoop loop_;
evpp::TCPClient client_;
};
TCPClientWrapper::TCPClientWrapper(const std::string& server_ip, int server_port)
: client_(&loop_, server_ip, server_port, "TCPClient") {
client_.SetConnectionCallback(std::bind(&TCPClientWrapper::OnConnection, this, std::placeholders::_1));
client_.SetMessageCallback(std::bind(&TCPClientWrapper::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
}
TCPClientWrapper::~TCPClientWrapper() {
loop_.Stop();
}
void TCPClientWrapper::Connect() {
client_.Connect();
loop_.Run();
}
void TCPClientWrapper::Send(const std::string& data) {
client_.Send(data);
}
void TCPClientWrapper::OnConnection(const evpp::TCPConnPtr& conn) {
if (conn->IsConnected()) {
LOG_INFO << "Connected to server";
} else {
LOG_INFO << "Disconnected from server";
}
}
void TCPClientWrapper::OnMessage(const evpp::TCPConnPtr& conn, evpp::Buffer* buf) {
std::string msg = buf->NextAllString();
LOG_INFO << "Received message: " << msg;
}
```
使用该类的示例代码如下:
```cpp
TCPClientWrapper client("127.0.0.1", 8000);
client.Connect();
client.Send("Hello, server!");
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)