繁体   English   中英

IOCP和覆盖的缓冲区

[英]IOCP and overwritten buffer

好吧,我用以下详细信息制作了一个用于处理客户端连接的IOCP:

 - Threads = (CPU cores * 2)
 - Assigning an completion port to each socket
 - Accessing the socket context by Client Index or overlapped struct (either way is the same)

因此,我尝试调试传入的数据包,它的工作原理像一个魅力,除了一点点但令人讨厌的细节...我在WorkersThread函数上设置了一个断点(我在其中接收数据包) recv,当缓冲区突然被我从客户端获取的新数据包覆盖时。

这是为什么? 根据我阅读的内容,IOCP应该等到我处理该数据包,然后再向客户端发送响应,然后再接收其他任何数据包。 因此,我在套接字上下文中设置了一个名为“ Processing”的标志,但仍收到带有传入数据包的覆盖缓冲区。 所以它根本不让我调试,这让我发疯

是在设置断点时让其他线程运行的ollydbg(调试器)错误吗? 还是我的IOCP实施出现错误?

这是我的WorkerThread的编码方式:

DWORD WINAPI WorkerThread(void* argument)
{
    int BytesTransfer;
    int BytesRecv;
    int ClientID;
    int result;
    OVERLAPPED* overlapped = 0;
    ClientInfo* clientinfo = 0;
    WSABUF wsabuf;
    int flags;
    //Exit only when shutdown signal is recv
    while (WaitForSingleObject(IOCPBase::internaldata->sockcontext.ShutDownSignal, NULL) !=  WAIT_OBJECT_0)
    {
        flags = 0; BytesTransfer = 0; BytesRecv = 0; ClientID = 0; 
        //Get from queued list
        if (GetQueuedCompletionStatus(IOCPBase::internaldata->sockcontext.CompletionPort, (LPDWORD)&BytesTransfer, (PULONG_PTR)&ClientID, &overlapped, INFINITE) == TRUE)
        {
            if (overlapped == 0)
            {
                //Fatal error
                break;
            }
            clientinfo = (ClientInfo*)overlapped;
            if (BytesTransfer != 0)
            {
                //Assign the buffer pointer and buffer len to WSABUF local
                clientinfo->RecvContext.RecvBytes = BytesTransfer;
                wsabuf.buf = (char*)clientinfo->RecvContext.Buffer;
                wsabuf.len = clientinfo->RecvContext.Len;
                //Switch for OperationCode
                //switch (IOCPBase::internaldata->ClientContext[ClientID].OperationCode)
                switch (clientinfo->OperationCode)
                {
                case FD_READ:
                    // Check if we have send all data to the client from a previous send
                    if (clientinfo->SendContext.SendBytes < clientinfo->SendContext.TotalBytes)
                    {
                        clientinfo->OperationCode = FD_READ;             //We set FD_READ caused on the next send, there could still be bytes left to send
                        wsabuf.buf += clientinfo->SendContext.SendBytes; //The buffer position is + sended bytes
                        wsabuf.len = clientinfo->SendContext.TotalBytes - clientinfo->SendContext.SendBytes; //the buffer len is total - sended bytes
                        //Send the remain bytes
                        result = WSASend(clientinfo->sock, &wsabuf, 1, (LPDWORD)&BytesRecv, flags, &clientinfo->overlapped, NULL);
                        if (result == SOCKET_ERROR && (WSAGetLastError() != WSA_IO_PENDING))
                        {
                            CloseClient(ClientID);
                        }
                        clientinfo->SendContext.SendBytes += BytesRecv;
                    }
                    else
                    {
                        if (clientinfo->Processing == 0)
                        {
                            clientinfo->OperationCode = FD_WRITE; //If no more bytes left to send now we can set the operation code to write (in fact is read)
                            memset(clientinfo->RecvContext.Buffer, NULL, MAX_DATA_BUFFER_SIZE); //Clean the buffer for recv new data
                            //Recv data from our client
                            clientinfo->RecvContext.RecvBytes = WSARecv(clientinfo->sock, &wsabuf, 1, (LPDWORD)&BytesRecv, (LPDWORD)&flags, &clientinfo->overlapped, NULL);
                            if (clientinfo->RecvContext.RecvBytes == SOCKET_ERROR &&  WSAGetLastError() != WSA_IO_PENDING)
                            {
                                CloseClient(ClientID);
                                break;
                            }
                        }
                    }
                    break;
                case FD_WRITE:
                    //Send data to the RecvProtocol
                    clientinfo->Processing = 1;
                    IOCPBase::internaldata->callback.RecvProtocol(clientinfo->RecvContext.Buffer, clientinfo->RecvContext.Len, ClientID);
                    clientinfo->Processing = 0;
                default:
                    break;
                }
            }
        }
    }
    return false;
}

查看clientinfo-> RecvContext.Buffer时出现问题。 我正在观察数据包,过了几秒钟,然后缓冲区被新数据包覆盖。

谢谢 !

没关系,我通过将数据包复制到用于分析数据包的函数的堆栈框架中来解决调试问题,这样我就不会出现覆盖问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM