VirtualBox

Changeset 26683 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Feb 22, 2010 4:43:37 PM (15 years ago)
Author:
vboxsync
Message:

iprt: RTEXITCODE, RTTcpServerListen2, RTTcpWrite fixes.

Location:
trunk/src/VBox/Runtime
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/misc/message.cpp

    r26674 r26683  
    9999
    100100
    101 RTDECL(int) RTMsgInitFailure(int rcRTR3Init)
     101RTDECL(RTEXITCODE) RTMsgErrorExit(RTEXITCODE enmExitCode, const char *pszFormat, ...)
     102{
     103    va_list va;
     104    va_start(va, pszFormat);
     105    RTMsgErrorV(pszFormat, va);
     106    va_end(va);
     107    return enmExitCode;
     108}
     109RT_EXPORT_SYMBOL(RTMsgErrorExitV);
     110
     111
     112RTDECL(RTEXITCODE) RTMsgErrorExitV(RTEXITCODE enmExitCode, const char *pszFormat, va_list va)
     113{
     114    RTMsgErrorV(pszFormat, va);
     115    return enmExitCode;
     116}
     117RT_EXPORT_SYMBOL(RTMsgErrorExitV);
     118
     119
     120RTDECL(RTEXITCODE) RTMsgInitFailure(int rcRTR3Init)
    102121{
    103122    if (   g_offrtProcName
     
    108127    else
    109128        RTStrmPrintf(g_pStdErr, "fatal error: RTR3Init: %Rrc\n", rcRTR3Init);
    110     return 12;
     129    return RTEXITCODE_INIT;
    111130}
    112131RT_EXPORT_SYMBOL(RTMsgInitFailure);
  • trunk/src/VBox/Runtime/r3/tcp.cpp

    r26612 r26683  
    55
    66/*
    7  * Copyright (C) 2006-2007 Sun Microsystems, Inc.
     7 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3535#ifdef RT_OS_WINDOWS
    3636# include <winsock.h>
    37 # include <limits.h>
    3837#else /* !RT_OS_WINDOWS */
    3938# include <errno.h>
     
    5049# include <unistd.h>
    5150#endif /* !RT_OS_WINDOWS */
     51#include <limits.h>
    5252
    5353#include "internal/iprt.h"
     
    657657
    658658/**
     659 * Listen and accept one incomming connection.
     660 *
     661 * This is an alternative to RTTcpServerListen for the use the callbacks are not
     662 * possible.
     663 *
     664 * @returns IPRT status code.
     665 * @retval  VERR_TCP_SERVER_SHUTDOWN if shut down by RTTcpServerShutdown.
     666 * @retval  VERR_INTERRUPTED if the listening was interrupted.
     667 *
     668 * @param   pServer         The server handle as returned from RTTcpServerCreateEx().
     669 * @param   pSockClient     Where to return the socket handle to the client
     670 *                          connection (on success only).  Use
     671 *                          RTTcpServerDisconnectClient() to clean it, this must
     672 *                          be done before the next call to RTTcpServerListen2.
     673 *
     674 * @todo    This can easily be extended to support multiple connections by
     675 *          adding a new state and a RTTcpServerDisconnectClient variant for
     676 *          closing client sockets.
     677 */
     678RTR3DECL(int) RTTcpServerListen2(PRTTCPSERVER pServer, PRTSOCKET pSockClient)
     679{
     680    /*
     681     * Validate input and retain the instance.
     682     */
     683    AssertPtrReturn(pSockClient, VERR_INVALID_HANDLE);
     684    *pSockClient = NIL_RTSOCKET;
     685    AssertReturn(pServer->u32Magic == RTTCPSERVER_MAGIC, VERR_INVALID_HANDLE);
     686    AssertReturn(RTMemPoolRetain(pServer) != UINT32_MAX, VERR_INVALID_HANDLE);
     687
     688    int rc = VERR_INVALID_STATE;
     689    for (;;)
     690    {
     691        /*
     692         * Change state to accepting.
     693         */
     694        RTTCPSERVERSTATE    enmState   = pServer->enmState;
     695        RTSOCKET            SockServer = pServer->SockServer;
     696        if (    enmState != RTTCPSERVERSTATE_SERVING
     697            &&  enmState != RTTCPSERVERSTATE_CREATED)
     698        {
     699            rc = rcTcpServerListenCleanup(pServer);
     700            break;
     701        }
     702        if (!rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_ACCEPTING, enmState))
     703            continue;
     704        Assert(!pServer->pfnServe);
     705        Assert(!pServer->pvUser);
     706        Assert(pServer->Thread == NIL_RTTHREAD);
     707        Assert(pServer->SockClient == NIL_RTSOCKET);
     708
     709        /*
     710         * Accept connection.
     711         */
     712        struct sockaddr_in  RemoteAddr;
     713        socklen_t           cbRemoteAddr = sizeof(RemoteAddr);
     714        RTSOCKET            Socket;
     715        RT_ZERO(RemoteAddr);
     716        rtTcpErrorReset();
     717        Socket = accept(SockServer, (struct sockaddr *)&RemoteAddr, &cbRemoteAddr);
     718        if (Socket == -1)
     719        {
     720            rc = rtTcpError();
     721            if (!rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_CREATED, RTTCPSERVERSTATE_ACCEPTING))
     722                rc = rcTcpServerListenCleanup(pServer);
     723            if (RT_FAILURE(rc))
     724                break;
     725            continue;
     726        }
     727
     728        /*
     729         * Chance to the 'serving' state and return the socket.
     730         */
     731        if (rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_SERVING, RTTCPSERVERSTATE_ACCEPTING))
     732        {
     733            *pSockClient = Socket;
     734            rc = VINF_SUCCESS;
     735        }
     736        else
     737        {
     738            rtTcpClose(Socket, "RTTcpServerListen2", true /*fTryGracefulShutdown*/);
     739            rc = rcTcpServerListenCleanup(pServer);
     740        }
     741        break;
     742    }
     743
     744    RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
     745    return rc;
     746}
     747
     748
     749/**
    659750 * Terminate the open connection to the server.
    660751 *
     
    855946RTR3DECL(int)  RTTcpWrite(RTSOCKET Sock, const void *pvBuffer, size_t cbBuffer)
    856947{
    857 
    858     do
    859     {
     948    /*
     949     * Try write all at once.
     950     */
    860951#ifdef RT_OS_WINDOWS
    861         int    cbNow = cbBuffer >= INT_MAX/2 ? INT_MAX/2 : (int)cbBuffer;
     952    int     cbNow     = cbBuffer >= INT_MAX / 2 ? INT_MAX / 2 : (int)cbBuffer;
    862953#else
    863         size_t cbNow = cbBuffer;
    864 #endif
    865         ssize_t cbWritten = send(Sock, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL);
    866         if (cbWritten < 0)
    867             return rtTcpError();
    868         AssertMsg(cbBuffer >= (size_t)cbWritten, ("Wrote more than we requested!!! cbWritten=%d cbBuffer=%d rtTcpError()=%d\n",
    869                                                   cbWritten, cbBuffer, rtTcpError()));
    870         cbBuffer -= cbWritten;
    871         pvBuffer = (char *)pvBuffer + cbWritten;
    872     } while (cbBuffer);
    873 
    874     return VINF_SUCCESS;
     954    size_t  cbNow     = cbBuffer >= SSIZE_MAX   ? SSIZE_MAX   :      cbBuffer;
     955#endif
     956    ssize_t cbWritten = send(Sock, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL);
     957    if (RT_LIKELY((size_t)cbWritten == cbBuffer && cbWritten >= 0))
     958        return VINF_SUCCESS;
     959    if (cbWritten < 0)
     960        return rtTcpError();
     961
     962    /*
     963     * Unfinished business, write the remainder of the request.  Must ignore
     964     * VERR_INTERRUPTED here if we've managed to send something.
     965     */
     966    size_t cbSentSoFar = 0;
     967    for (;;)
     968    {
     969        /* advance */
     970        cbBuffer    -= (size_t)cbWritten;
     971        if (!cbBuffer)
     972            return VINF_SUCCESS;
     973        cbSentSoFar += (size_t)cbWritten;
     974        pvBuffer     = (char const *)pvBuffer + cbWritten;
     975
     976        /* send */
     977#ifdef RT_OS_WINDOWS
     978        cbNow = cbBuffer >= INT_MAX / 2 ? INT_MAX / 2 : (int)cbBuffer;
     979#else
     980        cbNow = cbBuffer >= SSIZE_MAX   ? SSIZE_MAX   :      cbBuffer;
     981#endif
     982        cbWritten = send(Sock, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL);
     983        if (cbWritten >= 0)
     984            AssertMsg(cbBuffer >= (size_t)cbWritten, ("Wrote more than we requested!!! cbWritten=%zu cbBuffer=%zu rtTcpError()=%d\n",
     985                                                      cbWritten, cbBuffer, rtTcpError()));
     986        else
     987        {
     988            int rc = rtTcpError();
     989            if (rc != VERR_INTERNAL_ERROR || cbSentSoFar == 0)
     990                return rc;
     991            cbWritten = 0;
     992        }
     993    }
    875994}
    876995
  • trunk/src/VBox/Runtime/r3/test.cpp

    r25645 r26683  
    289289
    290290
    291 RTR3DECL(int) RTTestInitAndCreate(const char *pszTest, PRTTEST phTest)
     291RTR3DECL(RTEXITCODE) RTTestInitAndCreate(const char *pszTest, PRTTEST phTest)
    292292{
    293293    int rc = RTR3Init();
     
    295295    {
    296296        RTStrmPrintf(g_pStdErr, "%s: fatal error: RTR3Init failed with rc=%Rrc\n",  pszTest, rc);
    297         return 16;
     297        return RTEXITCODE_INIT;
    298298    }
    299299    rc = RTTestCreate(pszTest, phTest);
     
    301301    {
    302302        RTStrmPrintf(g_pStdErr, "%s: fatal error: RTTestCreate failed with rc=%Rrc\n",  pszTest, rc);
    303         return 17;
    304     }
    305     return 0;
     303        return RTEXITCODE_INIT;
     304    }
     305    return RTEXITCODE_SUCCESS;
    306306}
    307307
     
    806806 *                      associated with the calling thread.
    807807 */
    808 RTR3DECL(int) RTTestSummaryAndDestroy(RTTEST hTest)
     808RTR3DECL(RTEXITCODE) RTTestSummaryAndDestroy(RTTEST hTest)
    809809{
    810810    PRTTESTINT pTest = hTest;
    811     RTTEST_GET_VALID_RETURN_RC(pTest, 2);
     811    RTTEST_GET_VALID_RETURN_RC(pTest, RTEXITCODE_FAILURE);
    812812
    813813    RTCritSectEnter(&pTest->Lock);
     
    815815    RTCritSectLeave(&pTest->Lock);
    816816
    817     int rc;
     817    RTEXITCODE enmExitCode;
    818818    if (!pTest->cErrors)
    819819    {
    820820        RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "SUCCESS\n", pTest->cErrors);
    821         rc = 0;
     821        enmExitCode = RTEXITCODE_SUCCESS;
    822822    }
    823823    else
    824824    {
    825825        RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "FAILURE - %u errors\n", pTest->cErrors);
    826         rc = 1;
     826        enmExitCode = RTEXITCODE_FAILURE;
    827827    }
    828828
    829829    RTTestDestroy(pTest);
    830     return rc;
    831 }
    832 
    833 
    834 RTR3DECL(int) RTTestSkipAndDestroyV(RTTEST hTest, const char *pszReasonFmt, va_list va)
     830    return enmExitCode;
     831}
     832
     833
     834RTR3DECL(RTEXITCODE) RTTestSkipAndDestroyV(RTTEST hTest, const char *pszReasonFmt, va_list va)
    835835{
    836836    PRTTESTINT pTest = hTest;
    837     RTTEST_GET_VALID_RETURN_RC(pTest, 2);
     837    RTTEST_GET_VALID_RETURN_RC(pTest, RTEXITCODE_SKIPPED);
    838838
    839839    RTCritSectEnter(&pTest->Lock);
     
    841841    RTCritSectLeave(&pTest->Lock);
    842842
    843     int rc;
     843    RTEXITCODE enmExitCode;
    844844    if (!pTest->cErrors)
    845845    {
     
    847847            RTTestPrintfNlV(hTest, RTTESTLVL_FAILURE, pszReasonFmt, va);
    848848        RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "SKIPPED\n", pTest->cErrors);
    849         rc = 2;
     849        enmExitCode = RTEXITCODE_SKIPPED;
    850850    }
    851851    else
    852852    {
    853853        RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "FAILURE - %u errors\n", pTest->cErrors);
    854         rc = 1;
     854        enmExitCode = RTEXITCODE_FAILURE;
    855855    }
    856856
    857857    RTTestDestroy(pTest);
    858     return rc;
    859 }
    860 
    861 
    862 RTR3DECL(int) RTTestSkipAndDestroy(RTTEST hTest, const char *pszReasonFmt, ...)
     858    return enmExitCode;
     859}
     860
     861
     862RTR3DECL(RTEXITCODE) RTTestSkipAndDestroy(RTTEST hTest, const char *pszReasonFmt, ...)
    863863{
    864864    va_list va;
    865865    va_start(va, pszReasonFmt);
    866     int rc = RTTestSkipAndDestroyV(hTest, pszReasonFmt, va);
     866    RTEXITCODE enmExitCode = RTTestSkipAndDestroyV(hTest, pszReasonFmt, va);
    867867    va_end(va);
    868     return rc;
     868    return enmExitCode;
    869869}
    870870
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