优化网络客户端减少new的次数

This commit is contained in:
~ Alone 2024-11-28 22:22:53 +08:00
parent 60aa8883fa
commit 0c25f71bd7
6 changed files with 18 additions and 15 deletions

View File

@ -119,8 +119,8 @@ VOID SocketC::Receive()
else else
Rfunc(buff, len); Rfunc(buff, len);
} }
delete buff;
len = 0; len = 0;
ZeroMemory(buff, 10001);
buff = RecvBuff(NULL, len, isStr); buff = RecvBuff(NULL, len, isStr);
} }
} }
@ -131,7 +131,6 @@ VOID SocketC::Receive()
//处理收到的数据 //处理收到的数据
char* SocketC::RecvBuff(char* buf, int& len, int& isStr) char* SocketC::RecvBuff(char* buf, int& len, int& isStr)
{ {
char* buff = NULL;
MsgHead* h; MsgHead* h;
//lock_guard<std::mutex> guard(msgLock); //lock_guard<std::mutex> guard(msgLock);
//把收到的数据复制到缓冲区 //把收到的数据复制到缓冲区
@ -150,8 +149,6 @@ char* SocketC::RecvBuff(char* buf, int& len, int& isStr)
//判断当前消息是否收完 //判断当前消息是否收完
if (bufLen - sizeof(MsgHead) >= h->bufLen) if (bufLen - sizeof(MsgHead) >= h->bufLen)
{//当前消息收完了 {//当前消息收完了
buff = new char[h->bufLen + 1];
ZeroMemory(buff, h->bufLen + 1);
memcpy_s(buff, h->bufLen, tmpBuf + sizeof(MsgHead), h->bufLen); memcpy_s(buff, h->bufLen, tmpBuf + sizeof(MsgHead), h->bufLen);
len = h->bufLen; len = h->bufLen;
isStr = h->isStr; isStr = h->isStr;
@ -159,9 +156,10 @@ char* SocketC::RecvBuff(char* buf, int& len, int& isStr)
//缓冲区数据前移 //缓冲区数据前移
if (bufLen > 0) if (bufLen > 0)
memcpy_s(tmpBuf, bufLen, tmpBuf + sizeof(MsgHead) + len, bufLen); memcpy_s(tmpBuf, bufLen, tmpBuf + sizeof(MsgHead) + len, bufLen);
return buff;
} }
} }
return buff; return NULL;
} }
//检查消息头 //检查消息头

View File

@ -24,6 +24,7 @@ private:
}; };
char tmpBuf[10240] = { 0 }; //缓冲区 char tmpBuf[10240] = { 0 }; //缓冲区
char buff[10001] = { 0 };
int bufLen = 0; //当前接收了多长的数据 int bufLen = 0; //当前接收了多长的数据
SOCKET sclient = 0; //socket客户端 SOCKET sclient = 0; //socket客户端
BOOL state = 0; //当前状态 BOOL state = 0; //当前状态

File diff suppressed because one or more lines are too long

View File

@ -15,10 +15,10 @@ DWORD WINAPI SocketS::ThreadProc(LPVOID lpParameter)
while (server->g_flag) while (server->g_flag)
{//获取完成端口的数据 {//获取完成端口的数据
BOOL bFlag = GetQueuedCompletionStatus(port, &len, &index, &lpOverlapped, INFINITE); BOOL bFlag = GetQueuedCompletionStatus(port, &len, &index, &lpOverlapped, INFINITE);
//server->Lock.lock();
clienInfo* clien = &server->ClienMap[index]; clienInfo* clien = &server->ClienMap[index];
if (bFlag == TRUE && len > 0) if (bFlag == TRUE && len > 0)
{// 接收到客户端消息 {// 接收到客户端消息
server->Lock.lock();
if (server->m_mode == 0) if (server->m_mode == 0)
{//普通模式 {//普通模式
if (server->Rfunc != NULL) if (server->Rfunc != NULL)
@ -54,17 +54,19 @@ DWORD WINAPI SocketS::ThreadProc(LPVOID lpParameter)
DOEND DOEND
server->PostRecv(index); // 对自己投递接收 server->PostRecv(index); // 对自己投递接收
} }
server->Lock.unlock();
} }
else else
{//客户端关闭 {//客户端关闭
server->Lock.lock();
if (server->Cfunc != NULL) if (server->Cfunc != NULL)
{ {
server->Cfunc(index); server->Cfunc(index);
} }
server->CloseClien(index); server->CloseClien(index);
server->Lock.unlock();
} }
//server->Lock.unlock();
} }
server->Count--; server->Count--;
return 0; return 0;
@ -85,7 +87,7 @@ SocketS::~SocketS()
//投递消息 //投递消息
VOID SocketS::PostRecv(int index) VOID SocketS::PostRecv(int index)
{ {
WSABUF wsabuf; WSABUF wsabuf{};
clienInfo* clien = &ClienMap[index]; clienInfo* clien = &ClienMap[index];
if (m_mode == 0) if (m_mode == 0)
{ {

View File

@ -119,8 +119,8 @@ VOID SocketC::Receive()
else else
Rfunc(buff, len); Rfunc(buff, len);
} }
delete buff;
len = 0; len = 0;
ZeroMemory(buff, 10001);
buff = RecvBuff(NULL, len, isStr); buff = RecvBuff(NULL, len, isStr);
} }
} }
@ -131,7 +131,6 @@ VOID SocketC::Receive()
//处理收到的数据 //处理收到的数据
char* SocketC::RecvBuff(char* buf, int& len, int& isStr) char* SocketC::RecvBuff(char* buf, int& len, int& isStr)
{ {
char* buff = NULL;
MsgHead* h; MsgHead* h;
//lock_guard<std::mutex> guard(msgLock); //lock_guard<std::mutex> guard(msgLock);
//把收到的数据复制到缓冲区 //把收到的数据复制到缓冲区
@ -150,8 +149,6 @@ char* SocketC::RecvBuff(char* buf, int& len, int& isStr)
//判断当前消息是否收完 //判断当前消息是否收完
if (bufLen - sizeof(MsgHead) >= h->bufLen) if (bufLen - sizeof(MsgHead) >= h->bufLen)
{//当前消息收完了 {//当前消息收完了
buff = new char[h->bufLen + 1];
ZeroMemory(buff, h->bufLen + 1);
memcpy_s(buff, h->bufLen, tmpBuf + sizeof(MsgHead), h->bufLen); memcpy_s(buff, h->bufLen, tmpBuf + sizeof(MsgHead), h->bufLen);
len = h->bufLen; len = h->bufLen;
isStr = h->isStr; isStr = h->isStr;
@ -159,9 +156,10 @@ char* SocketC::RecvBuff(char* buf, int& len, int& isStr)
//缓冲区数据前移 //缓冲区数据前移
if (bufLen > 0) if (bufLen > 0)
memcpy_s(tmpBuf, bufLen, tmpBuf + sizeof(MsgHead) + len, bufLen); memcpy_s(tmpBuf, bufLen, tmpBuf + sizeof(MsgHead) + len, bufLen);
return buff;
} }
} }
return buff; return NULL;
} }
//检查消息头 //检查消息头

View File

@ -24,6 +24,7 @@ private:
}; };
char tmpBuf[10240] = { 0 }; //缓冲区 char tmpBuf[10240] = { 0 }; //缓冲区
char buff[10001] = { 0 };
int bufLen = 0; //当前接收了多长的数据 int bufLen = 0; //当前接收了多长的数据
SOCKET sclient = 0; //socket客户端 SOCKET sclient = 0; //socket客户端
BOOL state = 0; //当前状态 BOOL state = 0; //当前状态