Changeset 14 in cheroxy for trunk/src


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

Location:
trunk/src
Files:
3 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
  • trunk/src/main.cpp

    r6 r14  
    4242        __stream << __message;                                                                          \
    4343        string __string = __stream.str ();                                                              \
    44         sprintf (__error, "[%s][%d] %d : %s\n", __func__, __LINE__, __code, __string.c_str ());         \
     44        sprintf (__error, "[%s][%d] (%d) %s\n", __func__, __LINE__, __code, __string.c_str ());         \
    4545        cout << __error;                                                                                \
    4646    } while (0)
     
    5454int main (int argc, char* argv[])
    5555{
    56     int                 aResult = 0;
    57     const ushort        aPort   = 8080;
     56    int                     aResult = 0;
     57    const unsigned short    aPort   = 8080;
    5858
    5959    CRXSocket           aSocket;
     
    119119CRXProxyThread (void * aThreadArg)
    120120{
    121     int             aResult         = 0;
    122     int             aReceivedSize   = 0;
    123     int             aSentSize       = 0;
    124 
    125     const uint      aBufferSize     = 1024 * 64;
    126     char            aBuffer[aBufferSize] = {0x00, };
    127 
    128     CRXHttpRequest  aHttpRequest;
    129     CRXHttpResponse aHttpResponse;
    130     string          aUrl    = "";
    131     string          aHost   = "";
    132     int             aPort   = 0;
    133 
    134     CRXSocket       aWebServer;
    135     CRXSocket       aWebBrowser = ((CRXProxyThreadArgs *)aThreadArg)->mSocket;
    136 
    137     bool            aIsMoreRequest = false;
     121    int                 aResult         = 0;
     122    int                 aReceivedSize   = 0;
     123    int                 aSentSize       = 0;
     124
     125    const unsigned int  aBufferSize     = 1024 * 64;
     126    char                aBuffer[aBufferSize] = {0x00, };
     127
     128    CRXHttpRequest      aHttpRequest;
     129    CRXHttpResponse     aHttpResponse;
     130    string              aUrl    = "";
     131    string              aHost   = "";
     132    int                 aPort   = 0;
     133
     134    CRXSocket           aWebServer;
     135    CRXSocket           aWebBrowser = ((CRXProxyThreadArgs *)aThreadArg)->mSocket;
     136
     137    bool                aIsMoreRequest = false;
    138138    /*----------------------------------------------------------------*/
    139139    delete (CRXProxyThreadArgs *)aThreadArg;
     
    145145        if (aReceivedSize < 0)
    146146        {
    147             CRX_PRINT_ERROR (aReceivedSize, "failed to receive from client");
    148             aResult = CRX_ERROR ();
     147            CRX_PRINT_ERROR (aReceivedSize, "failed to receive from client (" << CRX_ERROR () << ")");
    149148            return aResult;
    150149        }
     
    157156
    158157            /* connect */
    159             aResult = aWebServer.Connect (aHttpRequest.GetHost (), aHttpRequest.GetPort ());
     158            aResult = aWebServer.Connect (aHttpRequest.GetHost (), aHttpRequest.GetPort (), 1);
    160159            if (aResult < 0)
    161160            {
    162                 CRX_PRINT_ERROR (aResult, "failed to connect to server <" << aHost << ":" << aPort << ">");
    163                 aResult = CRX_ERROR ();
     161                CRX_PRINT_ERROR (aResult, "failed to connect to server <" << aHost << ":" << aPort << "> (" << CRX_ERROR () << ")");
    164162                return aResult;
    165163            }
     
    170168        if (aSentSize != aReceivedSize)
    171169        {
    172             CRX_PRINT_ERROR (aResult, "failed to send to server");
    173             aResult = CRX_ERROR ();
     170            CRX_PRINT_ERROR (aResult, "failed to send to server (" << CRX_ERROR () << ")");
    174171            return aResult;
    175172        }
     
    183180            if (aReceivedSize < 0)
    184181            {
    185                 CRX_PRINT_ERROR (aReceivedSize, "failed to receive");
    186                 aResult = CRX_ERROR ();
     182                CRX_PRINT_ERROR (aReceivedSize, "failed to receive (" << CRX_ERROR () << ")");
    187183                break;
    188184            }
     
    196192            if (aSentSize != aReceivedSize)
    197193            {
    198                 CRX_PRINT_ERROR (aSentSize, "failed to send");
    199                 aResult = CRX_ERROR ();
     194                CRX_PRINT_ERROR (aSentSize, "failed to send (" << CRX_ERROR () << ")");
    200195                break;
    201196            }
  • trunk/src/makefile

    r11 r14  
    6262dummy:
    6363
    64 test: $(TARGET)
     64test: all
    6565    $(TARGET)
    6666
Note: See TracChangeset for help on using the changeset viewer.