[C++] 纯文本查看 复制代码
void tcp::RecvData(bool error, P_tcpstruct nop)
{
if (error)
{
EnterCriticalSection(&m_cs);
m_cnum--;
LeaveCriticalSection(&m_cs);
g_fun(this, nop->c_so, tcp_close, NULL, NULL, m_cnum);
//closesockets(nop->c_so);
//nop->c_so = INVALID_SOCKET;
delete[]nop->buf;
nop->buf = NULL;
delete nop;
nop = NULL;
return;
}
if (m_Is)
{
nop->bufOffset += nop->cb;
if (nop->bufOffset < nop->bufSize)
{
nop->state = tcp_recv;
WSABUF wsabuf;
if (m_Is)
{
wsabuf.buf = nop->buf + nop->bufOffset;
wsabuf.len = nop->bufSize - nop->bufOffset;
}
else
{
wsabuf.buf = nop->buf;
wsabuf.len = buf_len;
}
DWORD Cb = 0;
DWORD Flg = 0;
if (WSARecv(nop->c_so, &wsabuf, 1, &Cb, &Flg, (LPWSAOVERLAPPED)nop, NULL))
{
int ercode = WSAGetLastError();
if (ercode == WSAEFAULT)
{
return;
}
else if (ercode != WSA_IO_PENDING)
{
EnterCriticalSection(&m_cs);
m_cnum--;
LeaveCriticalSection(&m_cs);
g_fun(this, nop->c_so, tcp_close, NULL, NULL, m_cnum);
delete[]nop->buf;
nop->buf = NULL;
delete nop;
nop = NULL;
return;
}
}
return;
}
DWORD size = *((DWORD*)(nop->buf));
if (size > 65536000)
{
EnterCriticalSection(&m_cs);
m_cnum--;
LeaveCriticalSection(&m_cs);
g_fun(this, nop->c_so, tcp_close, NULL, NULL, m_cnum);
//closesockets(nop->c_so);
//nop->c_so = INVALID_SOCKET;
delete[]nop->buf;
nop->buf = NULL;
delete nop;
nop = NULL;
return;
}
if (nop->bufOffset < DWORD(sizeof(DWORD)) + size) //ÍêÕûÐÔ¼ì²â
{
delete[] nop->buf;
nop->buf = new char[sizeof(DWORD)+size];
nop->bufSize = sizeof(DWORD)+size;
nop->bufOffset = sizeof(DWORD);
memcpy(nop->buf, &size, sizeof(DWORD));
nop->state = tcp_recv;
WSABUF wsabuf;
if (m_Is)
{
wsabuf.buf = nop->buf + nop->bufOffset;
wsabuf.len = nop->bufSize - nop->bufOffset;
}
else
{
wsabuf.buf = nop->buf;
wsabuf.len = buf_len;
}
DWORD Cb = 0;
DWORD Flg = 0;
if (WSARecv(nop->c_so, &wsabuf, 1, &Cb, &Flg, (LPWSAOVERLAPPED)nop, NULL))
{
int ercode = WSAGetLastError();
if (ercode == WSAEFAULT)
{
return;
}
else if (ercode != WSA_IO_PENDING)
{
EnterCriticalSection(&m_cs);
m_cnum--;
LeaveCriticalSection(&m_cs);
g_fun(this, nop->c_so, tcp_close, NULL, NULL, m_cnum);
delete[]nop->buf;
nop->buf = NULL;
delete nop;
nop = NULL;
return;
}
}
return;
}
if (nop->bufSize - sizeof(DWORD) > 0)
{
g_fun(this, nop->c_so, tcp_recv, nop->buf + sizeof(DWORD), nop->bufSize - sizeof(DWORD), m_cnum);
}
delete[]nop->buf;
nop->buf = new char[sizeof(DWORD)];
nop->bufSize = sizeof(DWORD);
}
else
{
g_fun(this, nop->c_so, tcp_recv, nop->buf, nop->cb, m_cnum);
}
nop->bufOffset = 0;
nop->state = tcp_recv;
WSABUF wsabuf;
if (m_Is)
{
wsabuf.buf = nop->buf + nop->bufOffset;
wsabuf.len = nop->bufSize - nop->bufOffset;
}
else
{
wsabuf.buf = nop->buf;
wsabuf.len = buf_len;
}
DWORD Cb = 0;
DWORD Flg = 0;
if (WSARecv(nop->c_so, &wsabuf, 1, &Cb, &Flg, (LPWSAOVERLAPPED)nop, NULL))
{
int ercode = WSAGetLastError();
if (ercode == WSAEFAULT)
{
return;
}
else if (ercode != WSA_IO_PENDING)
{
EnterCriticalSection(&m_cs);
m_cnum--;
LeaveCriticalSection(&m_cs);
g_fun(this, nop->c_so, tcp_close, NULL, NULL, m_cnum);
//closesocket(nop->c_so);
//nop->c_so = INVALID_SOCKET;
//PostQueuedCompletionStatus(g_iocp, 0, NULL, (LPOVERLAPPED)nop);
delete[]nop->buf;
nop->buf = NULL;
delete nop;
nop = NULL;
}
}
}