VirtualBox

Changeset 32276 in vbox


Ignore:
Timestamp:
Sep 7, 2010 11:33:52 AM (14 years ago)
Author:
vboxsync
Message:

IPRT/Socket: Add extended select API where the events to wait for can be provided

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/socket.h

    r31103 r32276  
    149149RTDECL(int) RTSocketSelectOne(RTSOCKET hSocket, RTMSINTERVAL cMillies);
    150150
     151/** @name Select events
     152 * @{ */
     153/** Readable without blocking. */
     154#define RTSOCKET_EVT_READ         RT_BIT_32(0)
     155/** Writable without blocking. */
     156#define RTSOCKET_EVT_WRITE        RT_BIT_32(1)
     157/** Error condition, hangup, exception or similar. */
     158#define RTSOCKET_EVT_ERROR        RT_BIT_32(2)
     159/** Mask of the valid bits. */
     160#define RTSOCKET_EVT_VALID_MASK   UINT32_C(0x00000007)
     161/** @} */
     162
     163/**
     164 * Socket I/O multiplexing
     165 * Checks if the socket is ready for one of the given events.
     166 *
     167 * @returns iprt status code.
     168 * @param   Sock        Socket descriptor.
     169 * @param   fEvents     Event mask to wait for.
     170 * @param   pfEvents    Where to store the event mask on return.
     171 * @param   cMillies    Number of milliseconds to wait for the socket.
     172 *                      Use RT_INDEFINITE_WAIT to wait for ever.
     173 */
     174RTR3DECL(int)  RTSocketSelectOneEx(RTSOCKET Sock, uint32_t fEvents, uint32_t *pfEvents,
     175                                   RTMSINTERVAL cMillies);
     176
    151177/**
    152178 * Shuts down one or both directions of communciation.
  • trunk/include/iprt/tcp.h

    r31788 r32276  
    3232#include <iprt/net.h>
    3333#include <iprt/sg.h>
     34#include <iprt/socket.h>
    3435
    3536#ifdef IN_RING0
     
    234235RTR3DECL(int)  RTTcpSelectOne(RTSOCKET Sock, RTMSINTERVAL cMillies);
    235236
     237/**
     238 * Socket I/O multiplexing
     239 * Checks if the socket is ready for one of the given events.
     240 *
     241 * @returns iprt status code.
     242 * @param   Sock        Socket descriptor.
     243 * @param   fEvents     Event mask to wait for.
     244 *                      Use the RTSOCKET_EVT_* defines.
     245 * @param   pfEvents    Where to store the event mask on return.
     246 * @param   cMillies    Number of milliseconds to wait for the socket.
     247 *                      Use RT_INDEFINITE_WAIT to wait for ever.
     248 */
     249RTR3DECL(int)  RTTcpSelectOneEx(RTSOCKET Sock, uint32_t fEvents, uint32_t *pfEvents,
     250                                RTMSINTERVAL cMillies);
    236251
    237252#if 0 /* skipping these for now - RTTcpServer* handles this. */
  • trunk/src/VBox/Runtime/r3/socket.cpp

    r32134 r32276  
    2929*   Header Files                                                               *
    3030*******************************************************************************/
     31#define LOG_ENABLED
    3132#ifdef RT_OS_WINDOWS
    3233# include <winsock2.h>
     
    293294 * @param   ppSocket        Where to return the IPRT socket handle.
    294295 * @param   hNative         The native handle.
     296
    295297 */
    296298int rtSocketCreateForNative(RTSOCKETINT **ppSocket, RTSOCKETNATIVE hNative)
     
    980982
    981983
     984RTDECL(int) RTSocketSelectOneEx(RTSOCKET hSocket, uint32_t fEvents, uint32_t *pfEvents,
     985                                RTMSINTERVAL cMillies)
     986{
     987    /*
     988     * Validate input.
     989     */
     990    RTSOCKETINT *pThis = hSocket;
     991    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     992    AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
     993    AssertPtrReturn(pfEvents, VERR_INVALID_PARAMETER);
     994    AssertReturn(!(fEvents & ~RTSOCKET_EVT_VALID_MASK), VERR_INVALID_PARAMETER);
     995    AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
     996
     997    *pfEvents = 0;
     998
     999    /*
     1000     * Set up the file descriptor sets and do the select.
     1001     */
     1002    fd_set fdsetR;
     1003    fd_set fdsetW;
     1004    fd_set fdsetE;
     1005    FD_ZERO(&fdsetR);
     1006    FD_ZERO(&fdsetW);
     1007    FD_ZERO(&fdsetE);
     1008
     1009    if (fEvents & RTSOCKET_EVT_READ)
     1010        FD_SET(pThis->hNative, &fdsetR);
     1011    if (fEvents & RTSOCKET_EVT_WRITE)
     1012        FD_SET(pThis->hNative, &fdsetW);
     1013    if (fEvents & RTSOCKET_EVT_ERROR)
     1014        FD_SET(pThis->hNative, &fdsetE);
     1015
     1016    int rc;
     1017    if (cMillies == RT_INDEFINITE_WAIT)
     1018        rc = select(pThis->hNative + 1, &fdsetR, &fdsetW, &fdsetE, NULL);
     1019    else
     1020    {
     1021        struct timeval timeout;
     1022        timeout.tv_sec = cMillies / 1000;
     1023        timeout.tv_usec = (cMillies % 1000) * 1000;
     1024        rc = select(pThis->hNative + 1, &fdsetR, &fdsetW, &fdsetE, &timeout);
     1025    }
     1026    if (rc > 0)
     1027    {
     1028        if (FD_ISSET(pThis->hNative, &fdsetR))
     1029            *pfEvents |= RTSOCKET_EVT_READ;
     1030        if (FD_ISSET(pThis->hNative, &fdsetW))
     1031            *pfEvents |= RTSOCKET_EVT_WRITE;
     1032        if (FD_ISSET(pThis->hNative, &fdsetE))
     1033            *pfEvents |= RTSOCKET_EVT_ERROR;
     1034
     1035        rc = VINF_SUCCESS;
     1036    }
     1037    else if (rc == 0)
     1038        rc = VERR_TIMEOUT;
     1039    else
     1040        rc = rtSocketError();
     1041
     1042    return rc;
     1043}
     1044
     1045
    9821046RTDECL(int) RTSocketShutdown(RTSOCKET hSocket, bool fRead, bool fWrite)
    9831047{
     
    14341498    if (RT_FAILURE(rc))
    14351499    {
    1436         /** @todo  */
     1500        WSAResetEvent(pThis->hEvent);
     1501        fd_set fdsetR;
     1502        fd_set fdsetW;
     1503        FD_ZERO(&fdsetR);
     1504        FD_ZERO(&fdsetW);
     1505        FD_SET(pThis->hNative, &fdsetR);
     1506        FD_SET(pThis->hNative, &fdsetW);
     1507
     1508        fd_set fdsetE = fdsetR;
     1509
     1510        struct timeval timeout;
     1511        timeout.tv_sec = 0;
     1512        timeout.tv_usec = 0;
     1513        rc = select(pThis->hNative + 1, &fdsetR, &fdsetW, &fdsetE, &timeout);
     1514        if (rc > 0)
     1515        {
     1516            rc = VINF_SUCCESS;
     1517            if (FD_ISSET(pThis->hNative, &fdsetR))
     1518                fRetEvents |= RTPOLL_EVT_READ;
     1519            if (FD_ISSET(pThis->hNative, &fdsetW))
     1520                fRetEvents |= RTPOLL_EVT_WRITE;
     1521            if (FD_ISSET(pThis->hNative, &fdsetE))
     1522                fRetEvents |= RTPOLL_EVT_ERROR;
     1523        }
    14371524    }
    14381525
  • trunk/src/VBox/Runtime/r3/tcp.cpp

    r31103 r32276  
    10031003
    10041004
     1005RTR3DECL(int)  RTTcpSelectOneEx(RTSOCKET Sock, uint32_t fEvents, uint32_t *pfEvents,
     1006                                RTMSINTERVAL cMillies)
     1007{
     1008    return RTSocketSelectOneEx(Sock, fEvents, pfEvents, cMillies);
     1009}
     1010
     1011
    10051012RTR3DECL(int) RTTcpGetLocalAddress(RTSOCKET Sock, PRTNETADDR pAddr)
    10061013{
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