-
Notifications
You must be signed in to change notification settings - Fork 0
/
vs_tcpserver.cpp
620 lines (571 loc) · 17.6 KB
/
vs_tcpserver.cpp
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#include "stdafx.h"
#include <unordered_map>
#include <memory>
#include <thread>
#include <mutex>
#include "../vs_libuv/uv.h"
#include "../util/timetool.h"
#include "..\vslog\VsLogServiceApi.h"
#include "vs_tcpserver.h"
constexpr VS_INT8 *MODULE_NAME = "VsTcpServer";
constexpr VS_UINT32 MAX_RECV_DATA_LEN_ONCE = 60 * 1024;//单次接收最大的数据大小
using Task = std::function<void()>;
struct VsTcpServerMeta;
struct TcpConnectInfo
{
TcpConnectInfo()
{
connClientAddr = "";
socketID = 0;
recvDataBuf = nullptr;
cbConnectState = 0;
cbRecvMsg = 0;
hParent = nullptr;
isCanSend = false;
lastAliveHeart = GetSysTime_s();
}
~TcpConnectInfo()
{
if (recvDataBuf)
{
recvDataBuf = nullptr;
}
}
VsTcpServerMeta *hParent;
VS_UINT64 socketID;
std::string connClientAddr;
std::unique_ptr<char[]> recvDataBuf;
bool isCanSend;
VS_UINT32 lastAliveHeart;//最后活动时间
VsTcpServerCbOnConnectState cbConnectState;
VsTcpServerCbOnRcvMessage cbRecvMsg;
uv_tcp_t uvTcpHandle;
uv_write_t writeReq;
};
struct VsTcpServerMeta
{
VsTcpServerMeta()
{
listenPort = 0;
maxConnectNum = 100;
curMaxSocketID = 0;
cbConnectState = 0;
cbRecvMsg = 0;
m_bNeedSend = false;
}
~VsTcpServerMeta()
{
}
std::unordered_map<VS_UINT64, std::shared_ptr<TcpConnectInfo>> m_mapTcpConnect;
std::shared_ptr<std::thread> m_threadTask;
std::mutex m_mutexTask;
std::list<Task> m_listTask;
VsTcpServerCbOnConnectState cbConnectState;
VsTcpServerCbOnRcvMessage cbRecvMsg;
VS_UINT64 curMaxSocketID;
VS_UINT32 maxConnectNum;
VS_UINT16 listenPort;
bool m_bNeedSend;
uv_loop_t uvLoop;
uv_tcp_t uvTcpServer;
uv_timer_t uvTimer;
uv_async_t m_asyncSend;
};
//异步任务
static void VsTcpServer_postTask(VS_HANDLE serviceHandle, Task &&task);
//读内存分配
static void VsTcpServer_cb_alloc_before_read(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
//异步读回调
static void VsTcpServer_cb_read(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf);
//新连接
static void VsTcpServer_cb_newConnect(uv_stream_t * streamServer, VS_INT32 status);
//异步写回调
static void VsTcpServer_cb_write(uv_async_t* handle);
//写结果回调
static void VsTcpServer_cb_write_result(uv_write_t* req, int status);
//定时器回调
static void VsTcpServer_cb_timer(uv_timer_t* timer_handle);
//处理发送数据
static void VsTcpServer_handleSendData(VsTcpServerMeta *_this);
//关闭连接结果
static void VsTcpServer_cb_after_connect_close(uv_handle_t* handle);
inline std::unique_ptr<char[]> MallocRecvBuf()
{
return std::unique_ptr<char[]>(new char[MAX_RECV_DATA_LEN_ONCE]);
}
static VS_UINT64 VsTcpServer_getNextSockID(VS_HANDLE serviceHandle)
{
VsTcpServerMeta *_this = static_cast<VsTcpServerMeta*>(serviceHandle);
if (VS_NULL == _this)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_getNextSockID,VsTcpServerMeta is null");
return 0;
}
//时间戳(32)+累计sn(16)+随机码(16)
static VS_UINT32 sessionSn = 0x1000;
if (++sessionSn > 0xFFF0)
{
sessionSn = 0x1000;
}
sessionSn = sessionSn << 16 | rand() % 0xFFFF;
VS_UINT32 curTime = GetSysTime_s();
VS_UINT64 scoketUID = (VS_UINT64)curTime;
_this->curMaxSocketID = scoketUID << 32 | sessionSn;
return _this->curMaxSocketID;
}
static std::shared_ptr<TcpConnectInfo> VsTcpServer_getConnectInfoByID(VsTcpServerMeta *_this, VS_UINT64 socketID)
{
auto iter = _this->m_mapTcpConnect.find(socketID);
if (iter == _this->m_mapTcpConnect.end())
{
//LOG_ERROR(MODULE_NAME, "VsTcpServer_getConnectInfoByID,TcpConnectInfo no found,socketID:[%llu]", socketID);
return nullptr;
}
return iter->second;
}
void VsTcpServer_postTask(VS_HANDLE serviceHandle, Task &&task)
{
VsTcpServerMeta *_this = static_cast<VsTcpServerMeta*>(serviceHandle);
if (VS_NULL == _this)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_postTask VsTcpServerMeta is null");
return;
}
{
std::lock_guard<std::mutex> locker(_this->m_mutexTask);
_this->m_listTask.emplace_back(std::move(task));
}
uv_async_send(&_this->m_asyncSend);
}
void VsTcpServer_printErrorUvNetworkLog(const VS_INT8* errType,VS_INT32 errCode)
{
LOG_INFO(MODULE_NAME,"VsTcpServer_printErrorUvNetworkLog,type:[%s],uvErrName:[%s],uvErr:[%s],uvCode:[%d]", errType, uv_err_name(errCode), uv_strerror(errCode), errCode);
}
void VsTcpServer_cb_alloc_before_read(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf)
{
suggested_size;
TcpConnectInfo *pConnect = (TcpConnectInfo*)handle->data;
if (nullptr == pConnect)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_cb_alloc_before_read,TcpConnectInfo is null");
return;
}
*buf = uv_buf_init(pConnect->recvDataBuf.get(), MAX_RECV_DATA_LEN_ONCE);
return;
}
void VsTcpServer_cb_read(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf)
{
TcpConnectInfo *pConnect = (TcpConnectInfo*)handle->data;
if (nullptr == pConnect)
{
LOG_ERROR(MODULE_NAME, "VSNetServerServiceEx_cb_read,TcpConnectInfo is null");
return;
}
if (0 > nread)
{
pConnect->isCanSend = false;
VsTcpServer_printErrorUvNetworkLog("VSNetServerServiceEx_cb_read err", nread);
if (pConnect->cbConnectState)
{
pConnect->cbConnectState(pConnect->socketID, pConnect->connClientAddr, VsTcpServerConnectState_DisConnect);
}
VsTcpServerMeta* _this = (VsTcpServerMeta*)pConnect->hParent;
if (VS_NULL == _this)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_cb_read, VsTcpServerMeta is null");
return;
}
_this->m_mapTcpConnect.erase(pConnect->socketID);
return;
}
if (0 == nread)
{
return;
}
pConnect->lastAliveHeart = GetSysTime_s();
if (pConnect->cbRecvMsg)
{
pConnect->cbRecvMsg(pConnect->socketID, buf->base, nread);
}
return;
}
void VsTcpServer_cb_newConnect(uv_stream_t * streamServer, VS_INT32 status)
{
VsTcpServerMeta* _this = (VsTcpServerMeta*)(streamServer->data);
if (VS_NULL == _this)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_cb_newConnect,VsTcpServerMeta is null");
return;
}
if (0 != status)
{
VsTcpServer_printErrorUvNetworkLog("VSNetServerServiceEx_cb_newConnect", status);
return;
}
VS_UINT64 curSocketID = VsTcpServer_getNextSockID(_this);
std::shared_ptr<TcpConnectInfo> curConnectInfo = std::make_shared<TcpConnectInfo>();
curConnectInfo->uvTcpHandle.data =curConnectInfo.get();
curConnectInfo->recvDataBuf = MallocRecvBuf();
curConnectInfo->cbConnectState = _this->cbConnectState;
curConnectInfo->cbRecvMsg = _this->cbRecvMsg;
curConnectInfo->hParent = _this;
curConnectInfo->writeReq.data = curConnectInfo.get();
VS_INT32 ret = uv_tcp_init(&_this->uvLoop, &curConnectInfo->uvTcpHandle);
if (0 != ret)
{
uv_loop_close(&_this->uvLoop);
VsTcpServer_printErrorUvNetworkLog("VsTcpServer_cb_newConnect: uv_tcp_init", ret);
return;
}
ret = uv_accept(streamServer, (uv_stream_t*)&curConnectInfo->uvTcpHandle);
if (0 != ret)
{
uv_close((uv_handle_t*)&curConnectInfo->uvTcpHandle,NULL);
VsTcpServer_printErrorUvNetworkLog("VsTcpServer_cb_newConnect: uv_accept", ret);
return;
}
ret = uv_read_start((uv_stream_t*)&curConnectInfo->uvTcpHandle, VsTcpServer_cb_alloc_before_read, VsTcpServer_cb_read);
if (0 != ret)
{
uv_close((uv_handle_t*)&curConnectInfo->uvTcpHandle, NULL);
VsTcpServer_printErrorUvNetworkLog("VsTcpServer_cb_newConnect: uv_read_start", ret);
return;
}
sockaddr_in newAddr;
VS_INT32 addrLen = sizeof(struct sockaddr);
ret = uv_tcp_getpeername(&curConnectInfo->uvTcpHandle, (sockaddr*)&newAddr, &addrLen);
if (0 != ret)
{
VsTcpServer_printErrorUvNetworkLog("VsTcpServer_cb_newConnect: uv_tcp_getpeername", ret);
return;
}
else
{
VS_INT8 tempAddr[100] = {};
strncpy(tempAddr, inet_ntoa(newAddr.sin_addr), VS_IPADDR_LEN);
curConnectInfo->connClientAddr = tempAddr;
}
curConnectInfo->socketID = curSocketID;
curConnectInfo->isCanSend = true;
_this->m_mapTcpConnect[curSocketID] = curConnectInfo;
LOG_INFO(MODULE_NAME,"tcp server num:[%u]", _this->m_mapTcpConnect.size());
if (_this->cbConnectState)
{
_this->cbConnectState(curSocketID, curConnectInfo->connClientAddr, VsTcpServerConnectState_NewConnect);
}
}
void VsTcpServer_cb_write_result(uv_write_t* req, int status)
{
TcpConnectInfo *pConnect = (TcpConnectInfo*)req->data;
if (nullptr == pConnect)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_cb_write_result,TcpConnectInfo is null");
return;
}
if (0 == status)
{
pConnect->lastAliveHeart = GetSysTime_s();
VsTcpServer_handleSendData(pConnect->hParent);
}
else
{
pConnect->isCanSend = false;
if (status == UV_EAGAIN)
{
VsTcpServer_printErrorUvNetworkLog("VsTcpServer_cb_write_result WRITE BUFF FULL", status);
}
else
{
VsTcpServer_printErrorUvNetworkLog("VSNetServerServiceEx_cb_write_result, conn ERR", status);
pConnect->cbConnectState(pConnect->socketID, pConnect->connClientAddr,VsTcpServerConnectState_DisConnect);
VsTcpServerMeta* _this = (VsTcpServerMeta*)pConnect->hParent;
if (VS_NULL == _this)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_cb_write_result, VsTcpServerMeta is null");
return;
}
_this->m_mapTcpConnect.erase(pConnect->socketID);
}
}
free(req);
req = nullptr;
return;
}
void VsTcpServer_cb_write(uv_async_t* handle)
{
VsTcpServerMeta *_this = static_cast<VsTcpServerMeta*>(handle->data);
if (VS_NULL == _this)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_cb_write VsTcpServerMeta is null");
return;
}
VsTcpServer_handleSendData(_this);
//任务执行完之后,需要继续检查队列是否有数据,因为在写回调里面,尽管有多次请求写,也不会回调了
std::lock_guard<std::mutex> locker(_this->m_mutexTask);
if (!_this->m_listTask.empty()) {
//LOG_INFO(MODULE_NAME, "send finish,task queue is not empty,use timer to send");
_this->m_bNeedSend = VS_TRUE;
}
return;
}
constexpr VS_UINT32 MAX_ALIVE_HEART_TIME = 60;
//10ms
void VsTcpServer_cb_timer(uv_timer_t* timer_handle)
{
VsTcpServerMeta* _this = (VsTcpServerMeta*)(timer_handle->data);
if (VS_NULL == _this)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_cb_timer,VsTcpServerMeta is null");
return;
}
if (VS_TRUE == _this->m_bNeedSend)
{
_this->m_bNeedSend = VS_FALSE;
VsTcpServer_handleSendData(_this);
}
{
static VS_UINT32 heartCount = 0;
if (++heartCount > 100 * 5)//1分钟检测一次
{
heartCount = 0;
VS_UINT32 curTime = GetSysTime_s();
std::vector<VS_UINT64> vecTimeroutConnect;
for (auto iter = _this->m_mapTcpConnect.cbegin(); iter != _this->m_mapTcpConnect.cend(); iter++)
{
if (iter->second->lastAliveHeart < curTime)
{
if (curTime - iter->second->lastAliveHeart > MAX_ALIVE_HEART_TIME)
{
LOG_ERROR(MODULE_NAME, "connect timeout,socketID:[%llu],curTime:[%u],lastAliveTime:[%u],maxTime:[%u]", iter->first, curTime, iter->second->lastAliveHeart, MAX_ALIVE_HEART_TIME);
vecTimeroutConnect.emplace_back(iter->first);
}
}
}
if (vecTimeroutConnect.size() > 0)
{
LOG_INFO(MODULE_NAME, "connect timeout,count:[%u]", vecTimeroutConnect.size());
for (size_t idx = 0; idx < vecTimeroutConnect.size(); idx++)
{
VsTcpServer_close(_this, vecTimeroutConnect[idx]);
}
}
}
}
}
VS_INT32 VsTcpServer_init(VsTcpServerMeta *_this)
{
//事件循环
VS_INT32 ret = uv_loop_init(&_this->uvLoop);
if (0 != ret)
{
VsTcpServer_printErrorUvNetworkLog("uv_loop_init", ret);
return ret;
}
_this->uvLoop.data = _this;
//定时器
ret = uv_timer_init(&_this->uvLoop, &_this->uvTimer);
if (0 != ret)
{
VsTcpServer_printErrorUvNetworkLog("uv_timer_init", ret);
return ret;
}
_this->uvTimer.data = _this;
ret = uv_timer_start(&_this->uvTimer, VsTcpServer_cb_timer, 10, 10);
if (0 != ret)
{
VsTcpServer_printErrorUvNetworkLog("uv_timer_start", ret);
return ret;
}
//发送异步初始化
ret = uv_async_init(&_this->uvLoop, &_this->m_asyncSend, VsTcpServer_cb_write);
if (0 != ret)
{
VsTcpServer_printErrorUvNetworkLog("uv_tcp_init", ret);
return ret;
}
_this->m_asyncSend.data = _this;
//tcp网络初始化
ret = uv_tcp_init(&_this->uvLoop, &_this->uvTcpServer);
if (0 != ret)
{
uv_loop_close(&_this->uvLoop);
LOG_ERROR(MODULE_NAME, "VsTcpServer_initNetwork,uv_tcp_init err:%d", ret);
return ret;
}
_this->uvTcpServer.data = _this;
struct sockaddr_in bind_addr;
ret = uv_ip4_addr("0.0.0.0", _this->listenPort, &bind_addr);
if (0 != ret)
{
uv_loop_close(&_this->uvLoop);
VsTcpServer_printErrorUvNetworkLog("VsTcpServer_initNetwork,uv_ip4_addr", ret);
return ret;
}
ret = uv_tcp_bind(&_this->uvTcpServer, (const struct sockaddr*)&bind_addr, 0);
if (0 != ret)
{
uv_loop_close(&_this->uvLoop);
VsTcpServer_printErrorUvNetworkLog("VsTcpServer_initNetwork,uv_tcp_bind", ret);
return ret;
}
ret = uv_listen((uv_stream_t *)&_this->uvTcpServer, 1024, VsTcpServer_cb_newConnect);
if (0 != ret)
{
uv_loop_close(&_this->uvLoop);
VsTcpServer_printErrorUvNetworkLog("VsTcpServer_initNetwork,uv_listen", ret);
LOG_INFO(MODULE_NAME,"uvlisten port:[%d]", _this->listenPort);
return ret;
}
return 0;
}
void VsTcpServer_threadTask(VS_HANDLE serviceHandle)
{
VsTcpServerMeta* _this = (VsTcpServerMeta*)(serviceHandle);
if (VS_NULL == _this)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_setDelegate,VsTcpServerMeta is null");
return ;
}
VsTcpServer_init(_this);
uv_run(&_this->uvLoop, UV_RUN_DEFAULT);
// uv_close((uv_handle_t*)&netServiceData->uvCloseEvent, VS_NULL);
uv_loop_close(&_this->uvLoop);
LOG_ERROR(MODULE_NAME, "VsTcpServer_start,task thread quit");
}
VS_HANDLE VsTcpServer_start(VS_UINT16 nListenPort)
{
VsTcpServerMeta *_this = new VsTcpServerMeta();
if (VS_NULL == _this)
{
return VS_NULL;
}
_this->listenPort = nListenPort;
_this->m_threadTask = std::make_shared<std::thread>(VsTcpServer_threadTask,_this);
return _this;
}
VS_INT32 VsTcpServer_stop(VS_HANDLE serviceHandle)
{
VsTcpServerMeta* _this = (VsTcpServerMeta*)(serviceHandle);
if (VS_NULL == _this)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_setDelegate,VsTcpServerMeta is null");
return -1;
}
if (_this->m_threadTask)
{
// uv_close(&_this->uvTimer);
// _this->m_timer->close();
// _this->m_asyncSend->close();
// _this->m_asyncClose->close();
// uv_stop(_this->m_loop);
if (_this->m_threadTask->joinable())
{
_this->m_threadTask->join();
_this->m_threadTask = nullptr;
}
}
return 0;
}
void VsTcpServer_setDelegate(VS_HANDLE serviceHandle, VsTcpServerCbOnConnectState state, VsTcpServerCbOnRcvMessage message)
{
VsTcpServerMeta* _this = (VsTcpServerMeta*)(serviceHandle);
if (VS_NULL == _this)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_setDelegate,VsTcpServerMeta is null");
return;
}
_this->cbConnectState = state;
_this->cbRecvMsg = message;
}
void VsTcpServer_cb_after_connect_close(uv_handle_t* handle)
{
TcpConnectInfo* pConnInfo = (TcpConnectInfo*)(handle->data);
if (VS_NULL == pConnInfo)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_cb_after_connect_close,TcpConnectInfo is null");
return;
}
VsTcpServerMeta* _this = (VsTcpServerMeta*)pConnInfo->hParent;
if (VS_NULL == _this)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_cb_after_connect_close, VsTcpServerMeta is null");
return;
}
_this->m_mapTcpConnect.erase(pConnInfo->socketID);
return;
}
VS_INT32 VsTcpServer_close(VS_HANDLE serviceHandle, VS_UINT64 socketID)
{
VsTcpServerMeta* _this = (VsTcpServerMeta*)(serviceHandle);
if (VS_NULL == _this)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_close,VsTcpServerMeta is null");
return -1;
}
VsTcpServer_postTask(serviceHandle, std::bind([_this, socketID]() {
const auto pConnect = VsTcpServer_getConnectInfoByID(_this, socketID);
if (nullptr == pConnect)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_close,VsTcpServer_getConnectInfoByID no found,socketID:[%llu]", socketID);
return ;
}
pConnect->isCanSend = false;
if (uv_is_active((uv_handle_t*)&pConnect->uvTcpHandle)) {
uv_read_stop((uv_stream_t*)&pConnect->uvTcpHandle);
}
uv_close((uv_handle_t*)&pConnect->uvTcpHandle, VsTcpServer_cb_after_connect_close);
}));
return 0;
}
void VsTcpServer_handleSendData(VsTcpServerMeta *_this)
{
do {
Task workTask;
{
std::lock_guard<std::mutex> locker(_this->m_mutexTask);
if (_this->m_listTask.empty()) {
break;
}
workTask = std::move(_this->m_listTask.front());
_this->m_listTask.pop_front();
workTask();
}
} while (true);
}
VS_INT32 VsTcpServer_sendToClient(VS_HANDLE serviceHandle, VS_UINT64 socketID, VS_INT8*msg, VS_UINT32 msgLen)
{
VsTcpServerMeta* _this = (VsTcpServerMeta*)(serviceHandle);
if (VS_NULL == _this)
{
LOG_ERROR(MODULE_NAME, "VsTcpServer_sendToClient,VsTcpServerMeta is null");
return -1;
}
std::vector<VS_INT8> vecTempData;
vecTempData.insert(vecTempData.end(), msg, msg+ msgLen);
VsTcpServer_postTask(serviceHandle, std::bind([_this,socketID](std::vector<VS_INT8> & vecSendData) {
const auto pConnectInfo = VsTcpServer_getConnectInfoByID(_this, socketID);
if (nullptr == pConnectInfo)
{
return;
}
if (false == pConnectInfo->isCanSend)
{
return;
}
uv_buf_t buf = uv_buf_init(vecSendData.data(), vecSendData.size());
uv_write_t * reqWrite = (uv_write_t*)malloc(sizeof(uv_write_t));//&pConnectInfo->writeReq
if (nullptr == reqWrite)
{
return;
}
reqWrite->data = pConnectInfo.get();
VS_INT32 ret = uv_write(reqWrite, (uv_stream_t*)&pConnectInfo->uvTcpHandle, &buf, 1, VsTcpServer_cb_write_result);
if (0 > ret)
{
VsTcpServer_printErrorUvNetworkLog("VsTcpServer_sendToClient", ret);
// 出错后,丢弃继续发, 直到清空缓存
pConnectInfo->isCanSend = false;
}
},std::move(vecTempData)));
return 0;
}