VirtualBox

Changeset 58389 in vbox for trunk/src/VBox/Runtime/r3


Ignore:
Timestamp:
Oct 23, 2015 12:03:22 PM (9 years ago)
Author:
vboxsync
Message:

IPRT: Added RTSocketWriteToNB for non-blocking writes to UDP sockets without
locking the socket for exclusive use by the calling thread.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/socket.cpp

    r58295 r58389  
    10701070
    10711071
     1072RTDECL(int) RTSocketWriteToNB(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, PCRTNETADDR pAddr)
     1073{
     1074    /*
     1075     * Validate input.
     1076     */
     1077    RTSOCKETINT *pThis = hSocket;
     1078    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     1079    AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
     1080
     1081    /* no locking since UDP reads may be done concurrently to writes, and
     1082     * this is the normal use case of this code. */
     1083
     1084    int rc = rtSocketSwitchBlockingMode(pThis, false /* fBlocking */);
     1085    if (RT_FAILURE(rc))
     1086        return rc;
     1087
     1088    /* Figure out destination address. */
     1089    struct sockaddr *pSA = NULL;
     1090#ifdef RT_OS_WINDOWS
     1091    int cbSA = 0;
     1092#else
     1093    socklen_t cbSA = 0;
     1094#endif
     1095    RTSOCKADDRUNION u;
     1096    if (pAddr)
     1097    {
     1098        rc = rtSocketAddrFromNetAddr(pAddr, &u, sizeof(u), NULL);
     1099        if (RT_FAILURE(rc))
     1100            return rc;
     1101        pSA = &u.Addr;
     1102        cbSA = sizeof(u);
     1103    }
     1104
     1105    /*
     1106     * Must write all at once, otherwise it is a failure.
     1107     */
     1108#ifdef RT_OS_WINDOWS
     1109    int     cbNow     = cbBuffer >= RTSOCKET_MAX_WRITE ? RTSOCKET_MAX_WRITE : (int)cbBuffer;
     1110#else
     1111    size_t  cbNow     = cbBuffer >= SSIZE_MAX   ? SSIZE_MAX   :      cbBuffer;
     1112#endif
     1113    ssize_t cbWritten = sendto(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL, pSA, cbSA);
     1114    if (RT_LIKELY((size_t)cbWritten == cbBuffer && cbWritten >= 0))
     1115        rc = VINF_SUCCESS;
     1116    else if (cbWritten < 0)
     1117        rc = rtSocketError();
     1118    else
     1119        rc = VERR_TOO_MUCH_DATA;
     1120
     1121    rtSocketUnlock(pThis);
     1122    return rc;
     1123}
     1124
     1125
    10721126RTDECL(int) RTSocketSgWrite(RTSOCKET hSocket, PCRTSGBUF pSgBuf)
    10731127{
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