-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_client.h
More file actions
39 lines (29 loc) · 884 Bytes
/
socket_client.h
File metadata and controls
39 lines (29 loc) · 884 Bytes
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
#pragma once
#include <string>
#include <shared_mutex>
#include <mutex>
class SocketClient {
public:
SocketClient(int selectTimeout, int recvTimeout);
~SocketClient();
// 禁止拷贝和赋值
SocketClient(const SocketClient&) = delete;
SocketClient& operator=(const SocketClient&) = delete;
// 连接到服务器
bool connectToServer(const std::string& host, uint16_t port);
// 发送数据
bool sendData(const void* data, size_t length);
bool isDataAvailable();
// 接收指定长度的数据
bool receiveData(void* buffer, size_t length);
// 关闭连接
void disconnect();
// 检查是否已连接
bool isConnected() const;
private:
const int selectTimeout;
const int recvTimeout;
int m_socket;
std::shared_mutex sock_mtx;
std::mutex send_mtx;
};