简体   繁体   中英

IOCP and overwritten buffer

Well i make a IOCP for handling client connections with the following details:

 - 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)

So i am trying to debug the incoming packets, its works like a charm, except for a little but nasty detail... I set a break point on WorkersThread function (where i recv the packet) i am watching the buffer with the packet i recv, when suddenly the buffer gets overwritten with a new packet that i got from client.

Why is that? according to what i read, IOCP should wait till i process the packet, send a response to client before recv any other packet. So i set a flag on my socket context called "Processing" and still got the overwritten buffer with an incoming packet. So it doesn't let me debug at all and its driving me crazy

Is ollydbg (debugger) fault that let the other threads running while i set a break point? Or is some error in my IOCP implementation?

Here is how my WorkerThread is coded:

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;
}

The problem appears when looking at clientinfo->RecvContext.Buffer . I am watching the packet, past a few seconds and boom the buffer is overwritten with a new packet.

Thanks !

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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