簡體   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