|
@@ -773,29 +773,34 @@ void CIOCPModel::_RecvProcess(PER_SOCKET_CONTEXT* pSocketContext, PER_IO_CONTEXT
|
|
|
ProHeader* phead = NULL;
|
|
|
if (pSocketContext->lastData.size() == 0)
|
|
|
{
|
|
|
+ // 不足包头;
|
|
|
if (PAK_LEN > pIoContext->m_Overlapped.InternalHigh)
|
|
|
{
|
|
|
+ OutputDebugString("A:不足包头;\n");
|
|
|
pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, pIoContext->m_Overlapped.InternalHigh);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
phead = (ProHeader*)pIoContext->m_wsaBuf.buf;
|
|
|
|
|
|
- // 完成的包;
|
|
|
+ // 完整的包;
|
|
|
if (phead->len == pIoContext->m_Overlapped.InternalHigh)
|
|
|
{
|
|
|
+ OutputDebugString("A:完整的包;\n");
|
|
|
_DeviceProc(pIoContext, (Package*)pIoContext->m_wsaBuf.buf);
|
|
|
}
|
|
|
// 小包;
|
|
|
else if (phead->len > pIoContext->m_Overlapped.InternalHigh)
|
|
|
{
|
|
|
+ OutputDebugString("A:小包;\n");
|
|
|
pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, pIoContext->m_Overlapped.InternalHigh);
|
|
|
}
|
|
|
// 超包;
|
|
|
else if (phead->len < pIoContext->m_Overlapped.InternalHigh)
|
|
|
{
|
|
|
- _DeviceProc(pIoContext, (Package*)pIoContext->m_wsaBuf.buf);
|
|
|
+ OutputDebugString("A:超包;\n");
|
|
|
pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf + phead->len, pIoContext->m_Overlapped.InternalHigh - phead->len);
|
|
|
+ _DeviceProc(pIoContext, (Package*)pIoContext->m_wsaBuf.buf);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -807,47 +812,66 @@ void CIOCPModel::_RecvProcess(PER_SOCKET_CONTEXT* pSocketContext, PER_IO_CONTEXT
|
|
|
phead = (ProHeader*)pSocketContext->lastData.data();
|
|
|
if (phead->len <= pSocketContext->lastData.size() + pIoContext->m_Overlapped.InternalHigh)
|
|
|
{
|
|
|
- lastlen = pSocketContext->lastData.size() + pIoContext->m_Overlapped.InternalHigh - phead->len;
|
|
|
- pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, pIoContext->m_Overlapped.InternalHigh - lastlen);
|
|
|
- // 完整包;
|
|
|
- _DeviceProc(pIoContext, (Package*)pSocketContext->lastData.data());
|
|
|
- // 剩余包;
|
|
|
- pSocketContext->lastData.clear();
|
|
|
- if (lastlen)
|
|
|
- pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf + pIoContext->m_Overlapped.InternalHigh - lastlen, lastlen);
|
|
|
+ if ( phead->len <= pSocketContext->lastData.size() )
|
|
|
+ {
|
|
|
+ OutputDebugString("C:超包;\n");
|
|
|
+ lastlen = pSocketContext->lastData.size() + pIoContext->m_Overlapped.InternalHigh - phead->len;
|
|
|
+ pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, pIoContext->m_Overlapped.InternalHigh - lastlen);
|
|
|
+ // 完整包;
|
|
|
+ _DeviceProc(pIoContext, (Package*)pSocketContext->lastData.data());
|
|
|
+ // 剩余包;
|
|
|
+ pSocketContext->lastData.clear();
|
|
|
+ if (lastlen)
|
|
|
+ pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf + pIoContext->m_Overlapped.InternalHigh - lastlen, lastlen);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ OutputDebugString("C:仍不足一个包;\n");
|
|
|
pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, pIoContext->m_Overlapped.InternalHigh);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ // 包头剩余长度;
|
|
|
int diflen = PAK_LEN - pSocketContext->lastData.size();
|
|
|
+ // 仍不足一个包头;
|
|
|
if ( diflen > pIoContext->m_Overlapped.InternalHigh )
|
|
|
{
|
|
|
+ OutputDebugString("B:仍不足一个包头;\n");
|
|
|
pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, pIoContext->m_Overlapped.InternalHigh);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ // 拼成完整包头;
|
|
|
pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, diflen);
|
|
|
phead = (ProHeader*)pSocketContext->lastData.data();
|
|
|
+
|
|
|
// 完整包;
|
|
|
if ( phead->len == PAK_LEN + pIoContext->m_Overlapped.InternalHigh - diflen )
|
|
|
{
|
|
|
- _DeviceProc(pIoContext, (Package*)pIoContext->m_wsaBuf.buf);
|
|
|
+ OutputDebugString("B:完整包;\n");
|
|
|
+ pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf + diflen, pIoContext->m_Overlapped.InternalHigh - diflen);
|
|
|
+ _DeviceProc(pIoContext, (Package*)pSocketContext->lastData.data());
|
|
|
+ pSocketContext->lastData.clear();
|
|
|
}
|
|
|
// 小包;
|
|
|
else if ( phead->len > PAK_LEN + pIoContext->m_Overlapped.InternalHigh - diflen)
|
|
|
{
|
|
|
+ OutputDebugString("B:小包;\n");
|
|
|
pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf + diflen, pIoContext->m_Overlapped.InternalHigh - diflen);
|
|
|
}
|
|
|
// 超包;
|
|
|
else if (phead->len < PAK_LEN + pIoContext->m_Overlapped.InternalHigh - diflen)
|
|
|
{
|
|
|
+ OutputDebugString("B:超包;\n");
|
|
|
// 组完成包;
|
|
|
pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf + diflen, phead->len - PAK_LEN);
|
|
|
- _DeviceProc(pIoContext, (Package*)pIoContext->m_wsaBuf.buf);
|
|
|
+ _DeviceProc(pIoContext, (Package*)pSocketContext->lastData.data());
|
|
|
pSocketContext->lastData.clear();
|
|
|
int last = pIoContext->m_Overlapped.InternalHigh - diflen - phead->len + PAK_LEN;
|
|
|
if (last)
|
|
@@ -906,16 +930,16 @@ void CIOCPModel::_DeviceProc(PER_IO_CONTEXT* pIoContext, Package* pak)
|
|
|
|
|
|
char* pjdata = cJSON_Print(pJson);
|
|
|
|
|
|
- Package reponse_pak;
|
|
|
- reponse_pak.header.version = 0xAB;
|
|
|
- reponse_pak.header.len = strlen(pjdata) + PAK_LEN;
|
|
|
- reponse_pak.buf = new byte[strlen(pjdata)];
|
|
|
- memcpy(reponse_pak.buf, pjdata, strlen(pjdata));
|
|
|
- if ( pjdata)
|
|
|
+ byte* sdata = new byte[strlen(pjdata) + PAK_LEN];
|
|
|
+ Package* reponse_pak = (Package*)sdata;
|
|
|
+ reponse_pak->header.version = 0xAB;
|
|
|
+ reponse_pak->header.len = strlen(pjdata) + PAK_LEN;
|
|
|
+ memcpy(reponse_pak->buf, pjdata, strlen(pjdata));
|
|
|
+ if (pjdata)
|
|
|
free(pjdata);
|
|
|
|
|
|
- int ret = send(pIoContext->m_sockAccept, (const char*)&reponse_pak, reponse_pak.header.len, 0);
|
|
|
- delete[]reponse_pak.buf;
|
|
|
+ int ret = send(pIoContext->m_sockAccept, (const char*)sdata, reponse_pak->header.len, 0);
|
|
|
+ delete[]sdata;
|
|
|
if ( ret == -1 )
|
|
|
{
|
|
|
DWORD dwEr = GetLastError();
|