VirtualBox

Changeset 5756 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Nov 15, 2007 1:41:03 PM (17 years ago)
Author:
vboxsync
Message:

Client end of named pipe must be opened in overlapped mode, else writes from client
will be severely delayed and the client VM will get stuck on disconnect.

Also changed the type of NamedPipe variable to HANDLE to avoid casts and replaced
calls to RTFileClose with CloseHandle since all other calls operating on NamedPipe
were Windows API anyway.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Serial/DrvNamedPipe.cpp

    r4372 r5756  
    7070#ifdef RT_OS_WINDOWS
    7171    /* File handle of the named pipe. */
    72     RTFILE              NamedPipe;
     72    HANDLE              NamedPipe;
    7373    /* Overlapped structure for writes. */
    7474    OVERLAPPED          OverlappedWrite;
     
    104104    Assert(pvBuf);
    105105#ifdef RT_OS_WINDOWS
    106     if (pData->NamedPipe != NIL_RTFILE)
     106    if (pData->NamedPipe != INVALID_HANDLE_VALUE)
    107107    {
    108108        DWORD cbReallyRead;
    109109        pData->OverlappedRead.Offset     = 0;
    110110        pData->OverlappedRead.OffsetHigh = 0;
    111         if (!ReadFile((HANDLE)pData->NamedPipe, pvBuf, *cbRead, &cbReallyRead, &pData->OverlappedRead))
     111        if (!ReadFile(pData->NamedPipe, pvBuf, *cbRead, &cbReallyRead, &pData->OverlappedRead))
    112112        {
    113113            DWORD uError = GetLastError();
     
    129129
    130130                    /* Wait for incoming bytes. */
    131                     if (GetOverlappedResult((HANDLE)pData->NamedPipe, &pData->OverlappedRead, &cbReallyRead, TRUE) == FALSE)
     131                    if (GetOverlappedResult(pData->NamedPipe, &pData->OverlappedRead, &cbReallyRead, TRUE) == FALSE)
    132132                        uError = GetLastError();
    133133                }
     
    148148
    149149            {
    150                 RTFILE tmp = pData->NamedPipe;
    151                 FlushFileBuffers((HANDLE)tmp);
    152                 DisconnectNamedPipe((HANDLE)tmp);
     150                FlushFileBuffers(pData->NamedPipe);
     151                DisconnectNamedPipe(pData->NamedPipe);
    153152                if (!pData->fIsServer)
    154153                {
    155                     pData->NamedPipe = NIL_RTFILE;
    156                     RTFileClose(tmp);
     154                    CloseHandle(pData->NamedPipe);
     155                    pData->NamedPipe = INVALID_HANDLE_VALUE;
    157156                }
    158157                /* pretend success */
     
    202201    Assert(pvBuf);
    203202#ifdef RT_OS_WINDOWS
    204     if (pData->NamedPipe != NIL_RTFILE)
     203    if (pData->NamedPipe != INVALID_HANDLE_VALUE)
    205204    {
    206205        unsigned cbWritten;
    207206        pData->OverlappedWrite.Offset     = 0;
    208207        pData->OverlappedWrite.OffsetHigh = 0;
    209         if (!WriteFile((HANDLE)pData->NamedPipe, pvBuf, *cbWrite, NULL, &pData->OverlappedWrite))
     208        if (!WriteFile(pData->NamedPipe, pvBuf, *cbWrite, NULL, &pData->OverlappedWrite))
    210209        {
    211210            DWORD uError = GetLastError();
     
    226225            {
    227226                /* Wait for the write to complete. */
    228                 if (GetOverlappedResult((HANDLE)pData->NamedPipe, &pData->OverlappedWrite, (DWORD *)&cbWritten, TRUE) == FALSE)
     227                if (GetOverlappedResult(pData->NamedPipe, &pData->OverlappedWrite, (DWORD *)&cbWritten, TRUE) == FALSE)
    229228                    uError = GetLastError();
    230229            }
     
    238237                ||  rc == VERR_BROKEN_PIPE)
    239238            {
    240                 RTFILE tmp = pData->NamedPipe;
    241                 FlushFileBuffers((HANDLE)tmp);
    242                 DisconnectNamedPipe((HANDLE)tmp);
     239                FlushFileBuffers(pData->NamedPipe);
     240                DisconnectNamedPipe(pData->NamedPipe);
    243241                if (!pData->fIsServer)
    244242                {
    245                     pData->NamedPipe = NIL_RTFILE;
    246                     RTFileClose(tmp);
     243                    CloseHandle(pData->NamedPipe);
     244                    pData->NamedPipe = INVALID_HANDLE_VALUE;
    247245                }
    248246                /* pretend success */
     
    317315    int             rc = VINF_SUCCESS;
    318316#ifdef RT_OS_WINDOWS
    319     RTFILE          NamedPipe = pData->NamedPipe;
     317    HANDLE          NamedPipe = pData->NamedPipe;
    320318    HANDLE          hEvent = CreateEvent(NULL, TRUE, FALSE, 0);
    321319#endif
     
    329327        overlapped.hEvent = hEvent;
    330328
    331         BOOL fConnected = ConnectNamedPipe((HANDLE)NamedPipe, &overlapped);
     329        BOOL fConnected = ConnectNamedPipe(NamedPipe, &overlapped);
    332330        if (    !fConnected
    333331            &&  !pData->fShutdown)
     
    340338
    341339                hrc = 0;
    342                 if (GetOverlappedResult((HANDLE)pData->NamedPipe, &overlapped, &dummy, TRUE) == FALSE)
     340                if (GetOverlappedResult(pData->NamedPipe, &overlapped, &dummy, TRUE) == FALSE)
    343341                    hrc = GetLastError();
    344342
     
    417415    pData->fIsServer                    = false;
    418416#ifdef RT_OS_WINDOWS
    419     pData->NamedPipe                    = NIL_RTFILE;
     417    pData->NamedPipe                    = INVALID_HANDLE_VALUE;
    420418#else /* !RT_OS_WINDOWS */
    421419    pData->LocalSocketServer            = NIL_RTSOCKET;
     
    464462            return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NamedPipe#%d failed to create named pipe %s"), pDrvIns->iInstance, pszLocation);
    465463        }
    466         pData->NamedPipe = (HFILE)hPipe;
     464        pData->NamedPipe = hPipe;
    467465
    468466        rc = RTThreadCreate(&pData->ListenThread, drvNamedPipeListenLoop, (void *)pData, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "NamedPipe");
     
    476474    {
    477475        /* Connect to the named pipe. */
    478         rc = RTFileOpen(&pData->NamedPipe, pszLocation, RTFILE_O_READWRITE);
    479         if (VBOX_FAILURE(rc))
     476        HANDLE hPipe = CreateFile(pData->pszLocation, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
     477        if (hPipe == INVALID_HANDLE_VALUE)
     478        {
     479            rc = RTErrConvertFromWin32(GetLastError());
     480            LogRel(("NamedPipe%d: CreateFile failed rc=%Vrc\n", pData->pDrvIns->iInstance));
    480481            return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NamedPipe#%d failed to connect to named pipe %s"), pDrvIns->iInstance, pszLocation);
     482        }
     483        pData->NamedPipe = hPipe;
    481484    }
    482485
     
    571574
    572575#ifdef RT_OS_WINDOWS
    573     if (pData->NamedPipe != NIL_RTFILE)
    574     {
    575         FlushFileBuffers((HANDLE)pData->NamedPipe);
    576         if (!pData->fIsServer)
    577             DisconnectNamedPipe((HANDLE)pData->NamedPipe);
    578 
    579         RTFileClose(pData->NamedPipe);
    580         pData->NamedPipe = NIL_RTFILE;
     576    if (pData->NamedPipe != INVALID_HANDLE_VALUE)
     577    {
     578        if (pData->fIsServer)
     579        {
     580            FlushFileBuffers(pData->NamedPipe);
     581            DisconnectNamedPipe(pData->NamedPipe);
     582        }
     583
     584        CloseHandle(pData->NamedPipe);
     585        pData->NamedPipe = INVALID_HANDLE_VALUE;
    581586        CloseHandle(pData->OverlappedRead.hEvent);
    582587        CloseHandle(pData->OverlappedWrite.hEvent);
    583588    }
    584     /* Wake up listen thread */
    585     RTSemEventSignal(pData->ListenSem);
    586     RTSemEventDestroy(pData->ListenSem);
     589    if (pData->fIsServer)
     590    {
     591        /* Wake up listen thread */
     592        RTSemEventSignal(pData->ListenSem);
     593        RTSemEventDestroy(pData->ListenSem);
     594    }
    587595#else /* !RT_OS_WINDOWS */
    588596    if (pData->fIsServer)
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette