VirtualBox

Changeset 45948 in vbox for trunk


Ignore:
Timestamp:
May 8, 2013 12:32:54 PM (12 years ago)
Author:
vboxsync
Message:

IPRT: Added RTTcpClientConnectEx and RTTcpClientCancelConnect.

Location:
trunk
Files:
3 edited

Legend:

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

    r45400 r45948  
    14451445# define RTTarOpen                                      RT_MANGLER(RTTarOpen)
    14461446# define RTTarSeekNextFile                              RT_MANGLER(RTTarSeekNextFile)
     1447# define RTTcpClientCancelConnect                       RT_MANGLER(RTTcpClientCancelConnect)
    14471448# define RTTcpClientClose                               RT_MANGLER(RTTcpClientClose)
    14481449# define RTTcpClientCloseEx                             RT_MANGLER(RTTcpClientCloseEx)
    14491450# define RTTcpClientConnect                             RT_MANGLER(RTTcpClientConnect)
     1451# define RTTcpClientConnectEx                           RT_MANGLER(RTTcpClientConnectEx)
    14501452# define RTTcpFlush                                     RT_MANGLER(RTTcpFlush)
    14511453# define RTTcpGetLocalAddress                           RT_MANGLER(RTTcpGetLocalAddress)
  • trunk/include/iprt/tcp.h

    r33540 r45948  
    171171RTR3DECL(int) RTTcpClientConnect(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock);
    172172
     173/** Opaque pointer used by RTTcpClientConnectEx and RTTcpClientCancelConnect. */
     174typedef struct RTTCPCLIENTCONNECTCANCEL *PRTTCPCLIENTCONNECTCANCEL;
     175
     176/**
     177 * Connect (as a client) to a TCP Server, extended version.
     178 *
     179 * @returns iprt status code.
     180 * @param   pszAddress      The address to connect to.
     181 * @param   uPort           The port to connect to.
     182 * @param   pSock           Where to store the handle to the established connection.
     183 * @param   ppCancelCookie  Where to store information for canceling the
     184 *                          operation (from a different thread). Optional.
     185 *
     186 *                          The pointer _must_ be initialized to NULL before a
     187 *                          series of connection attempts begins, i.e. at a time
     188 *                          where there will be no RTTcpClientCancelConnect
     189 *                          calls racing access.  RTTcpClientCancelConnect will
     190 *                          set it to a special non-NULL value that causes the
     191 *                          current or/and next connect call to fail.
     192 *
     193 * @sa      RTTcpClientCancelConnect
     194 */
     195RTR3DECL(int) RTTcpClientConnectEx(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock,
     196                                   PRTTCPCLIENTCONNECTCANCEL volatile *ppCancelCookie);
     197
     198/**
     199 * Cancels a RTTcpClientConnectEx call on a different thread.
     200 *
     201 * @returns iprt status code.
     202 * @param   ppCancelCookie  The address of the cookie pointer shared with the
     203 *                          connect call.
     204 */
     205RTR3DECL(int) RTTcpClientCancelConnect(PRTTCPCLIENTCONNECTCANCEL volatile *ppCancelCookie);
     206
    173207/**
    174208 * Close a socket returned by RTTcpClientConnect().
  • trunk/src/VBox/Runtime/r3/tcp.cpp

    r44528 r45948  
    806806RTR3DECL(int) RTTcpClientConnect(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock)
    807807{
     808    return RTTcpClientConnectEx(pszAddress, uPort, pSock, NULL);
     809}
     810
     811
     812RTR3DECL(int) RTTcpClientConnectEx(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock,
     813                                   PRTTCPCLIENTCONNECTCANCEL volatile *ppCancelCookie)
     814{
    808815    /*
    809816     * Validate input.
     
    811818    AssertReturn(uPort > 0, VERR_INVALID_PARAMETER);
    812819    AssertPtrReturn(pszAddress, VERR_INVALID_POINTER);
     820    AssertPtrNullReturn(ppCancelCookie, VERR_INVALID_POINTER);
    813821
    814822    /*
     
    829837        RTSocketSetInheritance(Sock, false /*fInheritable*/);
    830838
    831         rc = rtSocketConnect(Sock, &Addr);
     839        if (!ppCancelCookie)
     840            rc = rtSocketConnect(Sock, &Addr);
     841        else
     842        {
     843            RTSocketRetain(Sock);
     844            if (ASMAtomicCmpXchgPtr(ppCancelCookie, (PRTTCPCLIENTCONNECTCANCEL)Sock, NULL))
     845            {
     846                rc = rtSocketConnect(Sock, &Addr);
     847                if (ASMAtomicCmpXchgPtr(ppCancelCookie, NULL, (PRTTCPCLIENTCONNECTCANCEL)Sock))
     848                    RTSocketRelease(Sock);
     849                else
     850                    rc = VERR_CANCELLED;
     851            }
     852            else
     853            {
     854                RTSocketRelease(Sock);
     855                rc = VERR_CANCELLED;
     856            }
     857        }
    832858        if (RT_SUCCESS(rc))
    833859        {
     
    838864        rtTcpClose(Sock, "RTTcpClientConnect", false /*fTryGracefulShutdown*/);
    839865    }
     866    if (ppCancelCookie)
     867        *ppCancelCookie = NULL;
    840868    return rc;
     869}
     870
     871
     872RTR3DECL(int) RTTcpClientCancelConnect(PRTTCPCLIENTCONNECTCANCEL volatile *ppCancelCookie)
     873{
     874    AssertPtrReturn(ppCancelCookie, VERR_INVALID_POINTER);
     875
     876    AssertCompile(NIL_RTSOCKET == NULL);
     877    RTSOCKET hSock = (RTSOCKET)ASMAtomicXchgPtr((void * volatile *)ppCancelCookie, (void *)(uintptr_t)0xdead9999);
     878    if (hSock != NIL_RTSOCKET)
     879    {
     880        int rc = rtTcpClose(hSock, "RTTcpClientCancelConnect", false /*fTryGracefulShutdown*/);
     881        AssertRCReturn(rc, rc);
     882    }
     883
     884    return VINF_SUCCESS;
    841885}
    842886
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