我是靠谱客的博主 含糊高跟鞋,这篇文章主要介绍c++使用libhv实现TCP客户端服务端长连接(带有心跳包回复),现在分享给大家,希望可以做个参考。

复制代码
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
利用libhv三方通信库实现tcp客户端、服务端长连接功能,带有心跳包回复,由于hvlib是在时间中创建的hio对象,如果程序是带有界面,直接用单线程创建hio即可,使用单线程对接收到的数据通过std::function发送到界面进行显示 [libhv源码地址](https://download.csdn.net/download/Pailugou/64827333) [libhv编译windows 64库地址](https://download.csdn.net/download/Pailugou/64827902) server端: #pragma once #include <iostream> #include <functional> #include <mutex> #include "hvlib/include/hv/hloop.h" class HVTcpServer { public: typedef struct { std::string cmd; int len; std::string message; }MessageData; explicit HVTcpServer(); ~HVTcpServer(); void openServer(const std::size_t port, const std::string ip); void sendMessage(const std::string message); void setDataCB(std::function<void(std::string)>); private: void initHV(); static void acceptCallBack(hio_t* io); static void hio_closeConnect(hio_t* io); static void hio_readMessage(hio_t* io, void* buf, int readBytes); static void hio_heart_beat(hio_t* io); static void hio_sendMessage(hio_t* io, const void* buf, int writeBytes); void dataCB(); private: std::size_t m_iPort; std::string m_strIP; hio_t* m_pHIO; std::function<void(std::string)> m_pDataCB; }; #include "HVTcpServer.h" #include <thread> #include <vector> #include <QByteArray> std::vector<std::string> g_DataVec; std::mutex g_mutx; hio_t* g_hio; #define CMD_HreatBeat "ServerBackHreatBeat" #define CMD_SendMessage 0x01 HVTcpServer::HVTcpServer() { //initHV(); } HVTcpServer::~HVTcpServer() { } void HVTcpServer::openServer(const std::size_t port, const std::string ip) { m_iPort = port; m_strIP = ip; std::thread runIOThread(&HVTcpServer::initHV, this); runIOThread.detach(); std::thread sendData(&HVTcpServer::dataCB, this); sendData.detach(); } void HVTcpServer::initHV() { std::unique_lock<std::mutex> lock(g_mutx); g_DataVec.clear(); lock.unlock(); hloop_t* pLoop = hloop_new(); m_pHIO = hloop_create_tcp_server(pLoop, m_strIP.c_str(), m_iPort, HVTcpServer::acceptCallBack); hloop_run(pLoop); hloop_free(&pLoop); std::cout << "hclib 初始化完成" << std::endl; } void HVTcpServer::acceptCallBack(hio_t* io) { g_hio = io; std::cout << hio_fd(io) << std::endl; std::cout << "-----connect success" << std::endl; hio_setcb_close(io, HVTcpServer::hio_closeConnect); hio_setcb_read(io, HVTcpServer::hio_readMessage); //hio_set_heartbeat(io, 400, HVTcpServer::hio_heart_beat); //hio_setcb_write(io, HVTcpServer::hio_sendMessage); //hio_set_keepalive_timeout(io, 5000); hio_read(io); } void HVTcpServer::hio_closeConnect(hio_t* io) { std::unique_lock<std::mutex> lock(g_mutx); g_DataVec.clear(); lock.unlock(); hio_close(io); std::cout << hio_fd(io)<<"-----close connect" << std::endl; } void HVTcpServer::hio_readMessage(hio_t* io, void* buf, int readBytes) { std::cout << hio_fd(io) << std::endl; const char* data = /*reinterpret_cast<char*>*/(char*)(buf); std::string msg(data, static_cast<std::size_t>(readBytes)); if (msg.size() == 0)return; std::cout << msg.c_str() << std::endl; std::lock_guard<std::mutex> lock(g_mutx); g_DataVec.emplace_back(msg); if (msg.compare("ClientHreatbeat") == 0) { hio_write(io, CMD_HreatBeat, strlen(CMD_HreatBeat)); } else { std::cout << "send:" << data << std::endl; } fflush(stdout); } void HVTcpServer::hio_heart_beat(hio_t* io) { hio_write(io, "server hreat beat", strlen("server hreat beat")); } void HVTcpServer::hio_sendMessage(hio_t* io, const void* buf, int writeBytes) { std::cout << hio_fd(io) << std::endl; std::cout << "send hio" << std::endl; //hio_write(io, m_strSendMessage, writeBytes); } void HVTcpServer::sendMessage(const std::string message) { std::string str = message; //void* vp = const_cast<void*>(&str); hio_write(g_hio, str.c_str(), strlen(message.c_str())); } void HVTcpServer::dataCB() { while (true) { if (g_DataVec.empty())continue; std::this_thread::sleep_for(std::chrono::milliseconds(200)); std::unique_lock<std::mutex> lock(g_mutx); if (m_pDataCB) m_pDataCB(g_DataVec.front()); g_DataVec.erase(g_DataVec.begin()); lock.unlock(); } } void HVTcpServer::setDataCB(std::function<void(std::string)> cb) { this->m_pDataCB = cb; } client端: #pragma once #include "hloop.h" #include <iostream> #include <functional> #include <thread> #include <mutex> #include <vector> class HVTcpClient { public: explicit HVTcpClient(); ~HVTcpClient(); //开启hio void setNetInfo(const std::size_t port, const std::string ip); //发送消息 void sendMessage(const std::string message); //是否开启心跳 void openHreatBeat(const bool status); //设置数据回调接口 void setDataCB(std::function<void (std::string)>); private: void initHV(); //连接成功接口 static void hioConnect(hio_t* io); //心跳回调接口 static void hioHreatBeat(hio_t* io); //接受数据回调接口 static void hioRecv(hio_t* io, void* buf, int readBytes); //关闭回调接口 static void hioClose(hio_t* io); static void hioHRead(hio_t*, void* buf, int bytes); //单线程将接收到的数据发送到界面上 void threadSendMessageToUI(); private: std::size_t m_iPort; std::string m_strIP; hio_t* m_pSockio; bool m_bOpenHreatBeat; std::function<void(std::string)> m_pDataCB; hloop_t* m_pHloop; }; #include "HVTcpClient.h" std::mutex g_mutex; std::vector<std::string> m_DataVec; #define CMD_HreatBeat "ClientHreatbeat" #define CMD_SendMessage 0x01 HVTcpClient::HVTcpClient() : m_pSockio(nullptr) , m_iPort(6666) , m_strIP("127.0.0.1") { } HVTcpClient::~HVTcpClient() { this->m_pDataCB = nullptr; hio_read_stop(m_pSockio); if (m_pSockio) { delete m_pSockio; m_pSockio = nullptr; } } void HVTcpClient::setNetInfo(const std::size_t port, const std::string ip) { m_iPort = port; m_strIP = ip; /*std::unique_lock<std::mutex> lock(g_mutex); m_hreatBeatVec.clear(); lock.unlock();*/ std::thread runSocketThread(&HVTcpClient::initHV, this); runSocketThread.detach(); std::thread thread(&HVTcpClient::threadSendMessageToUI, this); thread.detach(); //initHV(); } void HVTcpClient::openHreatBeat(const bool status) { if (m_pSockio == nullptr || status == false)return; hio_set_heartbeat(m_pSockio, 5000, HVTcpClient::hioHreatBeat); } void HVTcpClient::setDataCB(std::function<void(std::string)> cb) { this->m_pDataCB = cb; } void HVTcpClient::initHV() { hloop_t* pHloop = hloop_new(HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS); std::cout << pHloop << std::endl;; auto pHIO = hread(pHloop, 0, nullptr, 0, HVTcpClient::hioHRead); if (pHIO == nullptr) { std::cout << "read io is nullptr" << std::endl; return; } m_pSockio = hloop_create_tcp_client(pHloop, m_strIP.c_str(), m_iPort, HVTcpClient::hioConnect); if (m_pSockio == nullptr) { std::cout << "creat socke falid" << std::endl; return; } hloop_run(pHloop); hloop_free(&pHloop); } void HVTcpClient::hioHRead(hio_t*, void* buf, int bytes) { } void HVTcpClient::hioConnect(hio_t* io) { std::cout << "----Connect success" << std::endl; hio_setcb_read(io, HVTcpClient::hioRecv); hio_setcb_close(io, HVTcpClient::hioClose); hio_set_keepalive_timeout(io, 75000); hio_read(io); } void HVTcpClient::hioHreatBeat(hio_t* io) { hio_write(io, CMD_HreatBeat, strlen(CMD_HreatBeat)); } void HVTcpClient::hioRecv(hio_t* io, void* buf, int recvBytes) { char* data = reinterpret_cast<char*>(buf); std::string msg(data, static_cast<std::size_t>(recvBytes)); if (msg.size() == 0)return; std::cout << msg << std::endl; std::lock_guard<std::mutex> lock(g_mutex); m_DataVec.emplace_back(msg); if (msg.compare("ServerBackHreatBeat") == 0) { std::cout << "ServerBackHreatBeat" << std::endl; } else { std::cout << "send:" << data << std::endl; } fflush(stdout); } void HVTcpClient::hioClose(hio_t* io) { std::unique_lock<std::mutex> lock(g_mutex); m_DataVec.clear(); lock.unlock(); hio_close(io); std::cout << hio_fd(io) << "client close" << std::endl; } void HVTcpClient::threadSendMessageToUI() { while (true) { if (m_DataVec.empty())continue; std::this_thread::sleep_for(std::chrono::milliseconds(200)); std::unique_lock<std::mutex> lock(g_mutex); if (m_pDataCB) m_pDataCB(m_DataVec.front()); m_DataVec.erase(m_DataVec.begin()); lock.unlock(); } } void HVTcpClient::sendMessage(const std::string message) { std::lock_guard<std::mutex> lock(g_mutex); if (m_pSockio == nullptr)return; std::string str = message; hio_write(m_pSockio, str.c_str(), strlen(message.c_str())); }

最后

以上就是含糊高跟鞋最近收集整理的关于c++使用libhv实现TCP客户端服务端长连接(带有心跳包回复)的全部内容,更多相关c++使用libhv实现TCP客户端服务端长连接(带有心跳包回复)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部