优化网络客户端减少new的次数
This commit is contained in:
parent
60aa8883fa
commit
0c25f71bd7
@ -119,8 +119,8 @@ VOID SocketC::Receive()
|
||||
else
|
||||
Rfunc(buff, len);
|
||||
}
|
||||
delete buff;
|
||||
len = 0;
|
||||
ZeroMemory(buff, 10001);
|
||||
buff = RecvBuff(NULL, len, isStr);
|
||||
}
|
||||
}
|
||||
@ -131,7 +131,6 @@ VOID SocketC::Receive()
|
||||
//处理收到的数据
|
||||
char* SocketC::RecvBuff(char* buf, int& len, int& isStr)
|
||||
{
|
||||
char* buff = NULL;
|
||||
MsgHead* h;
|
||||
//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)
|
||||
{//当前消息收完了
|
||||
buff = new char[h->bufLen + 1];
|
||||
ZeroMemory(buff, h->bufLen + 1);
|
||||
memcpy_s(buff, h->bufLen, tmpBuf + sizeof(MsgHead), h->bufLen);
|
||||
len = h->bufLen;
|
||||
isStr = h->isStr;
|
||||
@ -159,9 +156,10 @@ char* SocketC::RecvBuff(char* buf, int& len, int& isStr)
|
||||
//缓冲区数据前移
|
||||
if (bufLen > 0)
|
||||
memcpy_s(tmpBuf, bufLen, tmpBuf + sizeof(MsgHead) + len, bufLen);
|
||||
return buff;
|
||||
}
|
||||
}
|
||||
return buff;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//检查消息头
|
||||
|
@ -24,6 +24,7 @@ private:
|
||||
};
|
||||
|
||||
char tmpBuf[10240] = { 0 }; //缓冲区
|
||||
char buff[10001] = { 0 };
|
||||
int bufLen = 0; //当前接收了多长的数据
|
||||
SOCKET sclient = 0; //socket客户端
|
||||
BOOL state = 0; //当前状态
|
||||
|
File diff suppressed because one or more lines are too long
@ -15,10 +15,10 @@ DWORD WINAPI SocketS::ThreadProc(LPVOID lpParameter)
|
||||
while (server->g_flag)
|
||||
{//获取完成端口的数据
|
||||
BOOL bFlag = GetQueuedCompletionStatus(port, &len, &index, &lpOverlapped, INFINITE);
|
||||
//server->Lock.lock();
|
||||
clienInfo* clien = &server->ClienMap[index];
|
||||
if (bFlag == TRUE && len > 0)
|
||||
{// 接收到客户端消息
|
||||
{// 接收到客户端消息
|
||||
server->Lock.lock();
|
||||
if (server->m_mode == 0)
|
||||
{//普通模式
|
||||
if (server->Rfunc != NULL)
|
||||
@ -54,17 +54,19 @@ DWORD WINAPI SocketS::ThreadProc(LPVOID lpParameter)
|
||||
DOEND
|
||||
server->PostRecv(index); // 对自己投递接收
|
||||
}
|
||||
server->Lock.unlock();
|
||||
}
|
||||
else
|
||||
{//客户端关闭
|
||||
server->Lock.lock();
|
||||
if (server->Cfunc != NULL)
|
||||
{
|
||||
server->Cfunc(index);
|
||||
}
|
||||
server->CloseClien(index);
|
||||
server->Lock.unlock();
|
||||
}
|
||||
|
||||
//server->Lock.unlock();
|
||||
}
|
||||
server->Count--;
|
||||
return 0;
|
||||
@ -85,7 +87,7 @@ SocketS::~SocketS()
|
||||
//投递消息
|
||||
VOID SocketS::PostRecv(int index)
|
||||
{
|
||||
WSABUF wsabuf;
|
||||
WSABUF wsabuf{};
|
||||
clienInfo* clien = &ClienMap[index];
|
||||
if (m_mode == 0)
|
||||
{
|
||||
|
@ -119,8 +119,8 @@ VOID SocketC::Receive()
|
||||
else
|
||||
Rfunc(buff, len);
|
||||
}
|
||||
delete buff;
|
||||
len = 0;
|
||||
ZeroMemory(buff, 10001);
|
||||
buff = RecvBuff(NULL, len, isStr);
|
||||
}
|
||||
}
|
||||
@ -131,7 +131,6 @@ VOID SocketC::Receive()
|
||||
//处理收到的数据
|
||||
char* SocketC::RecvBuff(char* buf, int& len, int& isStr)
|
||||
{
|
||||
char* buff = NULL;
|
||||
MsgHead* h;
|
||||
//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)
|
||||
{//当前消息收完了
|
||||
buff = new char[h->bufLen + 1];
|
||||
ZeroMemory(buff, h->bufLen + 1);
|
||||
memcpy_s(buff, h->bufLen, tmpBuf + sizeof(MsgHead), h->bufLen);
|
||||
len = h->bufLen;
|
||||
isStr = h->isStr;
|
||||
@ -159,9 +156,10 @@ char* SocketC::RecvBuff(char* buf, int& len, int& isStr)
|
||||
//缓冲区数据前移
|
||||
if (bufLen > 0)
|
||||
memcpy_s(tmpBuf, bufLen, tmpBuf + sizeof(MsgHead) + len, bufLen);
|
||||
return buff;
|
||||
}
|
||||
}
|
||||
return buff;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//检查消息头
|
||||
|
@ -24,6 +24,7 @@ private:
|
||||
};
|
||||
|
||||
char tmpBuf[10240] = { 0 }; //缓冲区
|
||||
char buff[10001] = { 0 };
|
||||
int bufLen = 0; //当前接收了多长的数据
|
||||
SOCKET sclient = 0; //socket客户端
|
||||
BOOL state = 0; //当前状态
|
||||
|
Loading…
Reference in New Issue
Block a user