Changeset 14 in cheroxy for trunk/src/CRXSocket.cpp


Ignore:
Timestamp:
10/24/12 11:12:11 (12 years ago)
Author:
cheese
Message:

#1 add timeout to socket connection and fix test label in makefile

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/CRXSocket.cpp

    r6 r14  
    1818{
    1919    /*----------------------------------------------------------------*/
    20     memset ((void *)&mAddress, 0x0, sizeof (stSockAddrIn));
     20    memset ((void *)&mAddress, 0x0, sizeof (struct sockaddr_in));
    2121
    2222    if (!CRXSocket::IsInitialized ())
     
    160160
    161161int
    162 CRXSocket::Connect (std::string aUrl,
    163                     ushort      aPort)
     162CRXSocket::Connect (const std::string       aUrl,
     163                    const unsigned short    aPort,
     164                    const int               aTimeout)
    164165{
    165166    int             aResult = -1;
    166167    struct hostent  * aHostEnt;
     168
     169#ifndef _WIN32
     170    struct timeval  aTimeVal;
     171    aTimeVal.tv_sec = aTimeout;
     172    aTimeVal.tv_usec= 0;
     173#else
     174    int             aTimeMilliSec = aTimeout * 1000;
     175#endif
    167176
    168177    /*----------------------------------------------------------------*/
     
    188197    }
    189198
    190     aResult = connect (mSocket, (stSockAddr*) &mAddress, sizeof (mAddress));
     199    if (aTimeout > TCPSOCKET_NO_TIMEOUT)
     200    {
     201#ifdef _WIN32
     202        aResult = setsockopt (mSocket,
     203                              SOL_SOCKET,
     204                              SO_RCVTIMEO,
     205                              (char *) &aTimeMilliSec,
     206                              sizeof (aTimeMilliSec));
     207#else
     208        aResult = setsockopt (mSocket,
     209                              SOL_SOCKET,
     210                              SO_RCVTIMEO,
     211                              &aTimeVal,
     212                              (socklen_t) sizeof (aTimeVal));
     213#endif
     214        if (aResult < 0)
     215        {
     216            return ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT;
     217        }
     218    }
     219
     220    aResult = connect (mSocket, (struct sockaddr*) &mAddress, sizeof (mAddress));
    191221    if (aResult < 0)
    192222    {
     
    199229
    200230int
    201 CRXSocket::CreateServer (ushort         aPort,
    202                          int            aBacklog,
    203                          stSockAddrIn   * aAddress)
     231CRXSocket::CreateServer (const unsigned short   aPort,
     232                         const int              aBacklog,
     233                         struct sockaddr_in     * aAddress)
    204234{
    205235    int aResult = -1;
     
    216246    mAddress.sin_port       = htons (aPort);
    217247
    218     aResult = bind (mSocket, (stSockAddr *)&mAddress, sizeof (stSockAddr));
     248    aResult = bind (mSocket, (struct sockaddr *)&mAddress, sizeof (struct sockaddr));
    219249    if (aResult < 0)
    220250    {
     
    230260    if (aAddress != NULL)
    231261    {
    232         memset ((void *)aAddress, 0x0, sizeof (stSockAddrIn));
    233         memcpy ((void *)aAddress, (void *)&mAddress, sizeof (stSockAddrIn));
    234     }
    235 
    236     /*----------------------------------------------------------------*/
    237     return aResult;
    238 }
    239 
    240 int
    241 CRXSocket::Accept (stSockAddrIn * aRemoteAddress,
    242                    int          * aAddressLength)
    243 {
    244     int             aResult = -1;
    245 
    246     stSockAddrIn    aAddress;
    247     socklen_t       aLength = sizeof (aAddress);
     262        memset ((void *)aAddress, 0x0, sizeof (struct sockaddr_in));
     263        memcpy ((void *)aAddress, (void *)&mAddress, sizeof (struct sockaddr_in));
     264    }
     265
     266    /*----------------------------------------------------------------*/
     267    return aResult;
     268}
     269
     270int
     271CRXSocket::Accept (struct sockaddr_in   * aRemoteAddress,
     272                   int                  * aAddressLength)
     273{
     274    int                 aResult = -1;
     275
     276    struct sockaddr_in  aAddress;
     277    socklen_t           aLength = sizeof (aAddress);
    248278
    249279    /*----------------------------------------------------------------*/
    250280    if (!IsReady ())        return ERROR_TCPSOCKET_NOT_READY;
    251281
    252     aResult = accept (mSocket, (stSockAddr *) &aAddress, &aLength);
     282    aResult = accept (mSocket, (struct sockaddr *) &aAddress, &aLength);
    253283    if (aResult < 0)
    254284    {
     
    258288    if (aRemoteAddress != NULL)
    259289    {
    260         memset ((void *)aRemoteAddress, 0x0, sizeof (stSockAddrIn));
    261         memcpy ((void *)aRemoteAddress, (void *)&aAddress, sizeof (stSockAddrIn));
     290        memset ((void *)aRemoteAddress, 0x0, sizeof (struct sockaddr_in));
     291        memcpy ((void *)aRemoteAddress, (void *)&aAddress, sizeof (struct sockaddr_in));
    262292    }
    263293
Note: See TracChangeset for help on using the changeset viewer.