Changeset 24 in cheroxy for trunk/src


Ignore:
Timestamp:
11/14/12 14:00:40 (12 years ago)
Author:
cheese
Message:

#1 change response, threading and proxy work-flow

Location:
trunk/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/CRXException.cpp

    r19 r24  
    1515CRXException::CRXException (void)
    1616    : mCode (0)
     17{
     18    /*----------------------------------------------------------------*/
     19    /*----------------------------------------------------------------*/
     20}
     21
     22CRXException::~CRXException (void)
    1723{
    1824    /*----------------------------------------------------------------*/
  • trunk/src/CRXHttpMessage.cpp

    r18 r24  
    99
    1010CRXHttpMessage::CRXHttpMessage (void)
     11{
     12}
     13
     14CRXHttpMessage::~CRXHttpMessage (void)
    1115{
    1216}
     
    3034
    3135void
     36CRXHttpMessage::Reset (void)
     37{
     38    mHttpMessage.clear ();
     39}
     40
     41void
    3242CRXHttpMessage::Parse (void)
    3343{
  • trunk/src/CRXHttpRequest.cpp

    r18 r24  
    1717
    1818void
    19 CRXHttpRequest::SetRequest (const char * aHttpRequest)
     19CRXHttpRequest::SetHeader (const char * aHttpRequest)
    2020{
    2121    SetMessage (aHttpRequest);
     
    2323
    2424std::string
    25 CRXHttpRequest::GetRequest (void) const
     25CRXHttpRequest::GetHeader (void) const
    2626{
    2727    return GetMessage ();
     
    4949CRXHttpRequest::operator = (const char * aHttpMessage)
    5050{
    51     SetRequest (aHttpMessage);
     51    SetHeader (aHttpMessage);
    5252    return *this;
    5353}
     
    6969
    7070    /*----------------------------------------------------------------*/
    71     aHttpRequestUri = GetRequest ().substr (0, mHttpMessage.find ('\r'));
     71    aHttpRequestUri = GetHeader ().substr (0, mHttpMessage.find ('\r'));
    7272
    7373    /*----------------------------------------------------------------
  • trunk/src/CRXHttpResponse.cpp

    r18 r24  
    1111#include <stdlib.h>
    1212
     13#define CRLF                "\r\n"
     14#define CRLF2               "\r\n\r\n" 
     15
     16#define CONTENT_LENGTH      "Content-Length: "
     17#define TRANSFER_ENCODING   "Transfer-Encoding: "
     18
    1319CRXHttpResponse::CRXHttpResponse (void)
    14     : mStatusCode   (0)
    15 {
     20    : mStatusCode (0),
     21      mIsChunked (false),
     22      mContentLength (0)
     23{
     24    memset (&mContent, 0x00, sizeof (mContent));
     25}
     26
     27CRXHttpResponse::~CRXHttpResponse (void)
     28{
     29    ResetContent ();
    1630}
    1731
    1832void
    19 CRXHttpResponse::SetResponse (const char * aHttpResponse)
    20 {
    21     SetMessage (aHttpResponse);
    22 }
    23 
    24 std::string
    25 CRXHttpResponse::GetResponse (void) const
    26 {
    27     return GetMessage ();
     33CRXHttpResponse::ResetContent (void)
     34{
     35    if (mContent.mBody)
     36        free (mContent.mBody);
     37
     38    memset (&mContent, 0x00, sizeof (mContent));
     39}
     40
     41int
     42CRXHttpResponse::SetResponse (const char    * aHttpResponse,
     43                              const int     aResponseLength)
     44{
     45    int             aResult = 0;
     46
     47    const char      * aEndOfHeader = NULL;
     48    std::string     aHeader;
     49
     50    int             aHeaderLength = 0;
     51    int             aBodyLength = 0;
     52
     53    /*----------------------------------------------------------------*/
     54    if (aResponseLength <= 0)
     55        return ERROR_HTTP_RESPONSE_INVALID_LENGTH;
     56
     57    if (GetHeader ().length () == 0)
     58    {
     59        aEndOfHeader = strstr (aHttpResponse, CRLF2);
     60        if (aEndOfHeader == NULL)
     61            return ERROR_HTTP_RESPONSE_INVALID_FORMAT;
     62
     63        aEndOfHeader += strlen (CRLF2);
     64        aHeaderLength = aEndOfHeader - aHttpResponse;
     65        aHeader.assign (aHttpResponse, aHeaderLength);
     66
     67        SetMessage (aHeader.c_str ());
     68    }
     69
     70    aResult = ParseContent (aHttpResponse + aHeaderLength, aResponseLength - aHeaderLength);
     71    if (aResult)
     72    {
     73        return ERROR_HTTP_RESPONSE_FAILED_TO_PARSE_CONTENT;
     74    }
     75    /*----------------------------------------------------------------*/
     76
     77    return aResult;
    2878}
    2979
     
    3585}
    3686
     87std::string
     88CRXHttpResponse::GetHeader (void) const
     89{
     90    return GetMessage ();
     91}
     92
     93int
     94CRXHttpResponse::GetStatusCode (void) const
     95{
     96    return mStatusCode;
     97}
     98
     99int
     100CRXHttpResponse::GetContentLength (void) const
     101{
     102    return mContentLength;
     103}
     104
     105const char *
     106CRXHttpResponse::GetContentBody (void) const
     107{
     108    return mContent.mBody;
     109}
     110
    37111void
    38112CRXHttpResponse::Parse (void)
    39113{
    40     std::string aHttpStatus     = "";
     114    std::string aValue;
    41115
    42116    char        aStatusCode[64]     = {0x00, };
    43     char        aStatusString[64]   = {0x00, };
     117    char        aStatusName[64]     = {0x00, };
    44118    char        aHttpVersion[64]    = {0x00, };
    45119
    46     /*----------------------------------------------------------------*/
    47     aHttpStatus = GetResponse ().substr (0, mHttpMessage.find ('\r'));
     120    size_t      aBegin      = 0;
     121    size_t      aOffset     = 0;
     122    char        aBuffer[12] = {0x00, };
     123
     124    /*----------------------------------------------------------------*/
     125    aValue = GetHeader ().substr (0, GetHeader ().find (CRLF, 0));
    48126
    49127    /*----------------------------------------------------------------
    50128     * 1. separate first line to <METHOD>, <URL> and <VERSION>
    51129     *----------------------------------------------------------------*/
    52     sscanf (aHttpStatus.c_str (), "%[^ ] %[^ ] %[^ ]\r\n", aHttpVersion, aStatusCode, aStatusString);
     130    sscanf (aValue.c_str (), "%[^ ] %[^ ] %[^ ]\r\n", aHttpVersion, aStatusCode, aStatusName);
    53131    mHttpVersion.assign (aHttpVersion);
    54     mStatusString.assign (aStatusString);
     132    mStatusString.assign (aStatusName);
    55133    mStatusCode = atoi (aStatusCode);
    56     /*----------------------------------------------------------------*/
    57 }
    58 
    59 int
    60 CRXHttpResponse::GetStatusCode (void) const
    61 {
    62     return mStatusCode;
    63 }
     134
     135    /*----------------------------------------------------------------
     136     * 2. get Content-Length or check is chunked
     137     *----------------------------------------------------------------*/
     138    aBegin = GetHeader ().find (CONTENT_LENGTH);
     139    if (aBegin == std::string::npos)
     140    {
     141        /* is really chunked encoding ? */
     142        aBegin = GetHeader ().find (TRANSFER_ENCODING);
     143        if (aBegin == std::string::npos)
     144            return ;
     145
     146        aBegin += strlen (TRANSFER_ENCODING);
     147        aOffset = GetHeader ().find (CRLF, aBegin) - aBegin;
     148
     149        aValue = GetHeader ().substr (aBegin, aOffset);
     150        if (aValue == "chunked")
     151        {
     152            mIsChunked = true;
     153            return ;
     154        }
     155    }
     156
     157    aBegin += strlen (CONTENT_LENGTH);
     158    aOffset = GetHeader ().find (CRLF, aBegin) - aBegin;
     159
     160    aValue = GetHeader ().substr (aBegin, aOffset);
     161    mContentLength = atoi (aValue.c_str ());
     162    /*----------------------------------------------------------------*/
     163}
     164
     165int
     166CRXHttpResponse::ParseContent (const char   * aContent,
     167                               const int    aLength)
     168{
     169    int     aResult = 0;
     170
     171    char    * aReallocPtr = NULL;
     172
     173    /*----------------------------------------------------------------*/
     174
     175    if (!mIsChunked)
     176    {
     177        if (mContentLength <= mContent.mLength)
     178            return aResult;
     179
     180        if (mContent.mBody == NULL)
     181        {
     182            mContent.mBody = static_cast<char *> (calloc (mContentLength, 1));
     183            if (mContent.mBody == NULL)
     184            {
     185                return ERROR_HTTP_RESPONSE_FAILED_TO_MEMORY_ALLOCATION;
     186            }
     187        }
     188    }
     189    else
     190    {
     191        mContentLength += aLength;
     192        aReallocPtr = static_cast<char *> (realloc (mContent.mBody, mContentLength));
     193        if (aReallocPtr == NULL)
     194        {
     195            return ERROR_HTTP_RESPONSE_FAILED_TO_MEMORY_ALLOCATION;
     196        }
     197        mContent.mBody = aReallocPtr;
     198    }
     199
     200    memcpy (mContent.mBody + mContent.mLength, aContent, aLength);
     201    mContent.mLength += aLength;
     202    /*----------------------------------------------------------------*/
     203
     204    return aResult;
     205}
  • trunk/src/CRXProxy.cpp

    r23 r24  
    33 */
    44
    5 #include "CRXSocket.h"
    6 #include "CRXHttpRequest.h"
    7 #include "CRXHttpResponse.h"
    8 
    95#include "CRXProxy.h"
    106
    117#include <string.h>
    128
    13 #ifdef _DEBUG
    14 # include <iostream>
    15 using namespace std;
    16 # define PRINT_ERROR        cerr
    17 #else
    18 # define PRINT_ERROR
    19 #endif
    20 
    21 #ifdef _WIN32
    22 # include <process.h>
    23 # define THREAD_TYPE                            void *
    24 # define THREAD_FUNCTION_RETURN                 unsigned int
    25 # define THREAD_FUNCTION_CALLING_CONVENTION     WINAPI
    26 #else
    27 # define THREAD_TYPE                            pthread_t
    28 # define THREAD_FUNCTION_RETURN                 void *
    29 # define THREAD_FUNCTION_CALLING_CONVENTION
    30 #endif
    31 
    32 THREAD_FUNCTION_RETURN
    33 THREAD_FUNCTION_CALLING_CONVENTION
    34 CRXProxyWrapper (void * aThreadArg)
    35 {
    36     int         aResult = 0;
    37     CRXProxy    * aProxy = (CRXProxy *) aThreadArg;
     9CRXProxy::CRXProxy (void)
     10    : mClient (0),
     11      mServer (0)
     12{
     13    /*----------------------------------------------------------------*/
     14    /*----------------------------------------------------------------*/
     15}
     16
     17CRXProxy::~CRXProxy (void)
     18{
     19    /*----------------------------------------------------------------*/
     20    Close ();
     21    /*----------------------------------------------------------------*/
     22}
     23
     24CRXProxy *
     25CRXProxy::GetNewInstance(void)
     26{
     27    /*----------------------------------------------------------------*/
     28    /*----------------------------------------------------------------*/
     29
     30    return new(std::nothrow) CRXProxy ();
     31}
     32
     33void
     34CRXProxy::ReleaseInstance (void)
     35{
     36    /*----------------------------------------------------------------*/
     37    delete this;
     38    /*----------------------------------------------------------------*/
     39}
     40
     41void
     42CRXProxy::SetClientSocket (const int aSocket)
     43{
     44    /*----------------------------------------------------------------*/
     45    if (aSocket <= 0)
     46        return ;
     47
     48    mClient = aSocket;
     49    /*----------------------------------------------------------------*/
     50}
     51
     52void
     53CRXProxy::Close (void)
     54{
     55    /*----------------------------------------------------------------*/
     56    mServer.Close ();
     57    mClient.Close ();
     58    /*----------------------------------------------------------------*/
     59}
     60
     61int
     62CRXProxy::GetStatus (void)
     63{
     64    /*----------------------------------------------------------------*/
     65    /*----------------------------------------------------------------*/
     66
     67    return mProxyStatus;
     68}
     69
     70int
     71CRXProxy::Forward (void)
     72{
     73    int     aResult = 0;
     74
     75    /*----------------------------------------------------------------*/
     76    aResult = ReceiveRequest ();
     77    if (aResult < 0)
     78    {
     79        aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
     80        CRX_ERROR (aResult, "Failed to receive from client");
     81    }
     82
     83    aResult = SendRequest ();
     84    if (aResult < 0)
     85    {
     86        aResult = ERROR_PROXY_FAILED_TO_SEND_REQUEST;
     87        CRX_ERROR (aResult, "Failed to send to server");
     88        return aResult;
     89    }
     90
     91    aResult = ReceiveResponse ();
     92    if (aResult < 0)
     93    {
     94        aResult = ERROR_PROXY_FAILED_TO_RECEIVE_RESPONSE;
     95        CRX_ERROR (aResult, "Failed to receive from server");
     96    }
     97
     98    aResult = SendResponse ();
     99    if (aResult < 0)
     100    {
     101        aResult = ERROR_PROXY_FAILED_TO_SEND_RESPONSE;
     102        CRX_ERROR (aResult, "Failed to send to client");
     103        return aResult;
     104    }
     105    /*----------------------------------------------------------------*/
     106
     107    return aResult;
     108}
     109
     110void
     111CRXProxy::SetHttpRequest (const char * aHttpRequest)
     112{
     113    /*----------------------------------------------------------------*/
     114    mHttpRequest = aHttpRequest;
     115    /*----------------------------------------------------------------*/
     116}
     117
     118char *
     119CRXProxy::GetHttpRequest (char      * aBuffer,
     120                          const int aBufferSize) const
     121{
     122    void * aPtr = static_cast<void *> (aBuffer);
     123
     124    /*----------------------------------------------------------------*/
     125    if (aPtr == NULL)
     126        return NULL;
    38127   
    39     aResult = aProxy->Forward ();
    40     if (aResult)
    41         PRINT_ERROR << aProxy->GetErrorMessage ();
    42 
    43     delete aProxy;
    44     return (THREAD_FUNCTION_RETURN)0;
    45 }
    46 
    47 CRXProxy::CRXProxy (const int aSocket)
    48 {
    49     /*----------------------------------------------------------------*/
    50     mClient = aSocket;
    51     /*----------------------------------------------------------------*/
    52 }
    53 
    54 CRXProxy *
    55 CRXProxy::GetNewInstance(const int aSocket)
    56 {
    57     return new(std::nothrow) CRXProxy (aSocket);
    58 }
    59 
    60 int
    61 CRXProxy::ForwardMT (void)
    62 {
    63     THREAD_TYPE     aThreadID = (THREAD_TYPE)0;
    64     /*----------------------------------------------------------------*/
    65 #ifdef _WIN32
    66     aThreadID = reinterpret_cast<void *> (_beginthreadex (0, 0, CRXProxyWrapper, this, 0, 0));
    67     if (aThreadID == NULL)
    68     {
    69         return -1;
    70     }
    71 
    72     CloseHandle (aThreadID);
    73 
    74     return 0;
    75 #else
    76     return pthread_create (&aThreadID, NULL, CRXProxyWrapper, this);
    77 #endif
    78     /*----------------------------------------------------------------*/
    79 }
    80 
    81 int
    82 CRXProxy::Forward (void)
    83 {
    84     int                 aResult         = 0;
    85     int                 aReceivedSize   = 0;
    86 
    87     const unsigned int  aBufferSize     = 1024 * 16;
    88     char                aBuffer[aBufferSize] = {0x00, };
    89 
    90     CRXHttpRequest      aHttpRequest;
    91 
    92     CRXSocket           aWebServer;
    93 
    94     bool                aIsMoreRequest  = false;
    95 
    96     /*----------------------------------------------------------------*/
    97     do
    98     {
    99         /* BROWSER --- request ---> [PROXY]                  SERVER */
    100         aResult = mClient.Receive (aBuffer, aBufferSize);
     128    if (aBufferSize > 0)
     129        memcpy (aPtr, mHttpRequest.GetHeader ().c_str (), aBufferSize);
     130    /*----------------------------------------------------------------*/
     131
     132    return aBuffer;
     133}
     134
     135int
     136CRXProxy::GetHttpRequestLength (void) const
     137{
     138    /*----------------------------------------------------------------*/
     139    /*----------------------------------------------------------------*/
     140
     141    return mHttpRequest.GetHeader().length ();
     142}
     143
     144void
     145CRXProxy::SetHttpResponse (const char * aHttpResponse)
     146{
     147    /*----------------------------------------------------------------*/
     148    mHttpResponse = aHttpResponse;
     149    /*----------------------------------------------------------------*/
     150}
     151
     152char *
     153CRXProxy::GetHttpResponse (char         * aBuffer,
     154                           const int    aBufferSize) const
     155{
     156    void    * aPtr = static_cast<void *> (aBuffer);
     157
     158    int     aRemainLength = aBufferSize;
     159
     160    /*----------------------------------------------------------------*/
     161    if (aPtr == NULL)
     162        return NULL;
     163
     164    if (aRemainLength > 0)
     165        memcpy (aPtr, mHttpResponse.GetHeader ().c_str (), aRemainLength);
     166
     167    aRemainLength -= mHttpResponse.GetHeader ().length ();
     168
     169    if (aRemainLength > 0)
     170        memcpy (aPtr, mHttpResponse.GetContentBody (), aRemainLength);
     171    /*----------------------------------------------------------------*/
     172
     173    return aBuffer;
     174}
     175
     176int
     177CRXProxy::GetHttpResponseLength (void) const
     178{
     179    /*----------------------------------------------------------------*/
     180    /*----------------------------------------------------------------*/
     181
     182    return mHttpResponse.GetHeader ().length ()
     183         + mHttpResponse.GetContentLength ();
     184}
     185
     186int
     187CRXProxy::ReceiveRequest (void)
     188{
     189    int                 aResult     = 0;
     190
     191    const unsigned int  aBufferSize = 1024 * 56;
     192    char                aBuffer[aBufferSize + 1] = {0x00, };
     193
     194    /*----------------------------------------------------------------*/
     195    aResult = mClient.Receive (aBuffer, aBufferSize);
     196    if (aResult < 0)
     197    {
     198        aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
     199        CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ());
     200        CRX_ERROR (aResult, "Failed to receive from client");
     201        return aResult;
     202    }
     203
     204    /* parse http request */
     205    mHttpRequest = aBuffer;
     206    /*----------------------------------------------------------------*/
     207
     208    return aResult < 0 ? aResult : 0;
     209}
     210
     211int
     212CRXProxy::SendRequest (void)
     213{
     214    int     aResult = 0;
     215    int     aSize   = mHttpRequest.GetHeader ().length ();
     216
     217    /*----------------------------------------------------------------*/
     218    if (!mServer)
     219    {
     220        /* connect */
     221        aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), 1);
    101222        if (aResult < 0)
    102223        {
    103             CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ());
    104             CRX_ERROR (aResult, "Failed to receive from browser");
     224            aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER;
     225            CRX_ERROR_ADD (mServer.GetErrorMessage ().c_str ());
     226            CRX_ERROR (aResult, "Failed to connect to server <%s>", mHttpRequest.GetURL ().c_str ());
     227            return aResult;
     228        }
     229    }
     230
     231    aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aSize);
     232    if (aResult != aSize)
     233    {
     234        CRX_ERROR_ADD (mServer.GetErrorMessage ().c_str ());
     235        CRX_ERROR (aResult, "Failed to send to server <%s>", mHttpRequest.GetURL ().c_str ());
     236    }
     237    /*----------------------------------------------------------------*/
     238
     239    return aResult < 0 ? aResult : 0;
     240}
     241
     242int
     243CRXProxy::ReceiveResponse (void)
     244{
     245    int                 aResult         = 0;
     246
     247    const unsigned int  aBufferSize     = 1024 * 64;
     248    char                aBuffer[aBufferSize + 1] = {0x00, };
     249
     250    std::string         aHttpResponse;
     251
     252    /*----------------------------------------------------------------*/
     253    for (;;)
     254    {
     255        memset (aBuffer, 0x00, sizeof (aBuffer));
     256
     257        aResult = mServer.Receive (aBuffer, aBufferSize);
     258        if (aResult < 0)
     259        {
     260            aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
     261            CRX_ERROR_ADD (mServer.GetErrorMessage ().c_str ());
     262            CRX_ERROR (aResult, "Failed to receive from server");
     263            return aResult;
     264        }
     265        else if (aResult == 0)
     266        {
    105267            break;
    106268        }
    107         aReceivedSize = aResult;
    108 
    109         /* BROWSER ?--- waiting --- [PROXY] --- connect ---> SERVER */
    110         if (!aWebServer)
     269
     270        aResult = mHttpResponse.SetResponse (aBuffer, aResult);
     271        if (aResult)
    111272        {
    112             /* parse http request */
    113             aHttpRequest = aBuffer;
    114 
    115             /* connect */
    116             aResult = aWebServer.Connect (aHttpRequest.GetHost (), aHttpRequest.GetPort (), 1);
    117             if (aResult < 0)
    118             {
    119                 CRX_ERROR_ADD (aWebServer.GetErrorMessage ().c_str ());
    120                 CRX_ERROR (aResult, "Failed to connect to server <%s>", aHttpRequest.GetURL ().c_str ());
    121                 return aResult;
    122             }
    123         }
    124 
    125         /* BROWSER ?--- waiting --- [PROXY] --- request ---> SERVER */
    126         aResult = aWebServer.Send (aBuffer, aReceivedSize);
    127         if (aResult != aReceivedSize)
    128         {
    129             CRX_ERROR_ADD (aWebServer.GetErrorMessage ().c_str ());
    130             CRX_ERROR (aResult, "Failed to send to server <%s>", aHttpRequest.GetURL ().c_str ());
     273            aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
     274            CRX_ERROR (aResult, "Failed to set response");
    131275            return aResult;
    132276        }
    133 
    134         /* BROWSER <--- response--- [PROXY] <--- response--- SERVER */
    135         for (;;)
    136         {
    137             memset (aBuffer, 0x00, aBufferSize);
    138 
    139             aResult = aWebServer.Receive (aBuffer, aBufferSize);
    140             if (aResult < 0)
    141             {
    142                 CRX_ERROR_ADD (aWebServer.GetErrorMessage ().c_str ());
    143                 CRX_ERROR (aResult, "Failed to receive <%s>", aHttpRequest.GetURL ().c_str ());
    144                 break;
    145             }
    146             else if (aResult == 0)
    147             {
    148                 break;
    149             }
    150             aReceivedSize = aResult;
    151 
    152             aResult = mClient.Send (aBuffer, aReceivedSize);
    153             if (aResult != aReceivedSize)
    154             {
    155                 CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ());
    156                 CRX_ERROR (aResult, "Failed to send <%s>", aHttpRequest.GetURL ().c_str ());
    157                 break;
    158             }
    159         }
    160     } while (aIsMoreRequest);
    161 
    162     /*----------------------------------------------------------------*/
    163     mClient.Close ();
    164     aWebServer.Close ();
    165 
    166     return aResult;
    167 }
     277    }
     278    /*----------------------------------------------------------------*/
     279
     280    return aResult < 0 ? aResult : 0;
     281}
     282
     283int
     284CRXProxy::SendResponse (void)
     285{
     286    int     aResult = 0;
     287    int     aSize   = 0;
     288
     289    /*----------------------------------------------------------------*/
     290    aSize = mHttpResponse.GetHeader ().length ();;
     291    aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aSize);
     292    if (aResult != aSize)
     293    {
     294        CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ());
     295        CRX_ERROR (aResult, "Failed to send to client");
     296        return aResult;
     297    }
     298
     299    aSize = mHttpResponse.GetContentLength ();
     300    aResult = mClient.Send (mHttpResponse.GetContentBody (), aSize);
     301    if (aResult != aSize)
     302    {
     303        CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ());
     304        CRX_ERROR (aResult, "Failed to send to client");
     305    }
     306    /*----------------------------------------------------------------*/
     307
     308    return aResult < 0 ? aResult : 0;
     309}
  • trunk/src/CRXSocket.cpp

    r18 r24  
    1818{
    1919    /*----------------------------------------------------------------*/
     20    CRXSocket::Initialize ();
     21
    2022    memset ((void *)&mAddress, 0x0, sizeof (struct sockaddr_in));
    21 
    22     if (!CRXSocket::IsInitialized ())
    23         CRXSocket::Initialize ();
    2423    /*----------------------------------------------------------------*/
    2524}
     
    8180
    8281int
    83 CRXSocket::Initialize (void)
    84 {
    85     int aResult = 0;
    86 
    87     /*----------------------------------------------------------------*/
    88     if (CRXSocket::IsInitialized ())
    89         return aResult;
    90 
    91 #ifdef WIN32
    92     WSADATA aWinSockData;
    93    
    94     aResult = WSAStartup (MAKEWORD (2, 0), &aWinSockData);
    95     if (aResult != 0)
    96     {
    97         aResult = ERROR_TCPSOCKET_FAILED_TO_INITIALIZE;
    98         return aResult;
    99     }
    100 #endif
    101 
    102     mInitialized = true;
    103 
    104     /*----------------------------------------------------------------*/
    105     return aResult;
    106 }
    107 
    108 void
    109 CRXSocket::Finalize (void)
    110 {
    111     /*----------------------------------------------------------------*/
    112 #ifdef WIN32
    113     WSACleanup ();
    114 #endif
    115     /*----------------------------------------------------------------*/
    116 }
    117 
    118 bool
    119 CRXSocket::IsInitialized (void)
    120 {
    121     /*----------------------------------------------------------------*/
    122     /*----------------------------------------------------------------*/
    123     return mInitialized;
    124 }
    125 
    126 bool
    127 CRXSocket::IsReady (void) const
    128 {
    129     /*----------------------------------------------------------------*/
    130     /*----------------------------------------------------------------*/
    131     return IsInitialized () && IsCreated ();
    132 }
    133 
    134 bool
    135 CRXSocket::IsCreated (void) const
    136 {
    137     /*----------------------------------------------------------------*/
    138     /*----------------------------------------------------------------*/
    139     return mSocket > 0 ? true : false;
    140 }
    141 
    142 int
    143 CRXSocket::CreateSocket (void)
    144 {
    145     int aResult = -1;
    146 
    147     /*----------------------------------------------------------------*/
    148     if (!IsInitialized () || IsCreated ())
    149     {
    150         aResult = ERROR_TCPSOCKET_ALREADY_IN_USE;
    151         CRX_ERROR (aResult, "Already in use.");
    152         return aResult;
    153     }
    154 
    155     aResult = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
    156     if (aResult < 0)
    157     {
    158         aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET;
    159         CRX_ERROR (aResult, "Failed to create socket.");
    160         return aResult;
    161     }
    162 
    163     mSocket = aResult;
    164 
    165     /*----------------------------------------------------------------*/
    166     return aResult;
    167 }
    168 
    169 int
    170 CRXSocket::Connect (const std::string       aUrl,
    171                     const unsigned short    aPort,
    172                     const int               aTimeout)
    173 {
    174     int             aResult = -1;
    175     struct hostent  * aHostEnt;
     82CRXSocket::SetTimeout (const int aTimeout)
     83{
     84    int             aResult = 0;
    17685
    17786#ifndef _WIN32
     
    18493
    18594    /*----------------------------------------------------------------*/
     95    if (!IsReady ())
     96        return ERROR_TCPSOCKET_NOT_READY;
     97
     98    if (aTimeout == TCPSOCKET_NO_TIMEOUT)
     99        return 0;
     100
     101    aResult = setsockopt (mSocket,
     102                          SOL_SOCKET,
     103                          SO_RCVTIMEO,
     104#ifdef _WIN32
     105                          (char *) &aTimeMilliSec,
     106                          sizeof (aTimeMilliSec));
     107#else
     108                          &aTimeVal,
     109                          (socklen_t) sizeof (aTimeVal));
     110#endif
     111    if (aResult < 0)
     112    {
     113        aResult = ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT;
     114        CRX_ERROR (aResult, "Failed to set socket option.");
     115    }
     116    /*----------------------------------------------------------------*/
     117
     118    return aResult;
     119}
     120
     121int
     122CRXSocket::Initialize (void)
     123{
     124    int aResult = 0;
     125
     126    /*----------------------------------------------------------------*/
     127    if (CRXSocket::IsInitialized ())
     128        return aResult;
     129
     130#ifdef WIN32
     131    WSADATA aWinSockData;
     132   
     133    aResult = WSAStartup (MAKEWORD (2, 0), &aWinSockData);
     134    if (aResult != 0)
     135    {
     136        aResult = ERROR_TCPSOCKET_FAILED_TO_INITIALIZE;
     137        return aResult;
     138    }
     139#endif
     140
     141    mInitialized = true;
     142
     143    /*----------------------------------------------------------------*/
     144    return aResult;
     145}
     146
     147void
     148CRXSocket::Finalize (void)
     149{
     150    /*----------------------------------------------------------------*/
     151#ifdef WIN32
     152    WSACleanup ();
     153#endif
     154    /*----------------------------------------------------------------*/
     155}
     156
     157bool
     158CRXSocket::IsInitialized (void)
     159{
     160    /*----------------------------------------------------------------*/
     161    /*----------------------------------------------------------------*/
     162    return mInitialized;
     163}
     164
     165bool
     166CRXSocket::IsReady (void) const
     167{
     168    /*----------------------------------------------------------------*/
     169    /*----------------------------------------------------------------*/
     170    return IsInitialized () && IsCreated ();
     171}
     172
     173bool
     174CRXSocket::IsCreated (void) const
     175{
     176    /*----------------------------------------------------------------*/
     177    /*----------------------------------------------------------------*/
     178    return mSocket > 0 ? true : false;
     179}
     180
     181int
     182CRXSocket::CreateSocket (void)
     183{
     184    int aResult = -1;
     185
     186    /*----------------------------------------------------------------*/
     187    if (!IsInitialized () || IsCreated ())
     188    {
     189        aResult = ERROR_TCPSOCKET_ALREADY_IN_USE;
     190        CRX_ERROR (aResult, "Already in use.");
     191        return aResult;
     192    }
     193
     194    aResult = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
     195    if (aResult < 0)
     196    {
     197        aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET;
     198        CRX_ERROR (aResult, "Failed to create socket(%d).", GetSystemErrorCode ());
     199        return aResult;
     200    }
     201
     202    mSocket = aResult;
     203
     204    /*----------------------------------------------------------------*/
     205    return aResult;
     206}
     207
     208int
     209CRXSocket::Connect (const std::string       aUrl,
     210                    const unsigned short    aPort,
     211                    const int               aTimeout)
     212{
     213    int             aResult = -1;
     214    struct hostent  * aHostEnt;
     215
     216    /*----------------------------------------------------------------*/
    186217    aResult = CreateSocket ();
    187218    if (aResult < 0)
    188219    {
     220        aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET;
     221        CRX_ERROR (aResult, "Failed to create socket(%d).", GetSystemErrorCode ());
    189222        return aResult;
    190223    }
     
    207240    }
    208241
    209     if (aTimeout > TCPSOCKET_NO_TIMEOUT)
    210     {
    211         aResult = setsockopt (mSocket,
    212                               SOL_SOCKET,
    213                               SO_RCVTIMEO,
    214 #ifdef _WIN32
    215                               (char *) &aTimeMilliSec,
    216                               sizeof (aTimeMilliSec));
    217 #else
    218                               &aTimeVal,
    219                               (socklen_t) sizeof (aTimeVal));
    220 #endif
    221         if (aResult < 0)
    222         {
    223             aResult = ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT;
    224             CRX_ERROR (aResult, "Failed to set socket option.");
    225             return aResult;
    226         }
     242    aResult = SetTimeout (aTimeout);
     243    if (aResult < 0)
     244    {
     245        aResult = ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT;
     246        CRX_ERROR (aResult, "Failed to set timeout (%d).", GetSystemErrorCode ());
     247        return aResult;
    227248    }
    228249
     
    231252    {
    232253        aResult = ERROR_TCPSOCKET_FAILED_TO_CONNECT;
    233         CRX_ERROR (aResult, "Failed to connect.");
     254        CRX_ERROR (aResult, "Failed to connect (%d).", GetSystemErrorCode ());
    234255    }
    235256
     
    249270    if (aResult < 0)
    250271    {
     272        aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET;
     273        CRX_ERROR (aResult, "Failed to create socket(%d).", GetSystemErrorCode ());
    251274        return aResult;
    252275    }
     
    260283    {
    261284        aResult = ERROR_TCPSOCKET_FAILED_TO_BIND;
    262         CRX_ERROR (aResult, "Failed to bind.");
     285        CRX_ERROR (aResult, "Failed to bind(%d).", GetSystemErrorCode ());
    263286        return aResult;
    264287    }
     
    268291    {
    269292        aResult = ERROR_TCPSOCKET_FAILED_TO_LISTEN;
    270         CRX_ERROR (aResult, "Failed to listen.");
     293        CRX_ERROR (aResult, "Failed to listen(%d).", GetSystemErrorCode ());
    271294        return aResult;
    272295    }
     
    292315
    293316    /*----------------------------------------------------------------*/
    294     if (!IsReady ())        return ERROR_TCPSOCKET_NOT_READY;
     317    if (!IsReady ())
     318        return ERROR_TCPSOCKET_NOT_READY;
    295319
    296320    aResult = accept (mSocket, (struct sockaddr *) &aAddress, &aLength);
     
    298322    {
    299323        aResult = ERROR_TCPSOCKET_FAILED_TO_ACCEPT;
    300         CRX_ERROR (aResult, "Failed to accept.");
     324        CRX_ERROR (aResult, "Failed to accept(%d).", GetSystemErrorCode ());
    301325        return aResult;
    302326    }
     
    316340
    317341int
    318 CRXSocket::Send (char   * aBuffer,
    319                  int    aSize)
     342CRXSocket::Send (const char * aBuffer,
     343                 int        aSize)
    320344{
    321345    int aResult = -1;
    322346
    323347    /*----------------------------------------------------------------*/
    324     if (!IsReady ())        return ERROR_TCPSOCKET_NOT_READY;
     348    if (!IsReady ())
     349        return ERROR_TCPSOCKET_NOT_READY;
    325350
    326351    aResult = send (mSocket, aBuffer, aSize, 0);
     
    328353    {
    329354        aResult = ERROR_TCPSOCKET_FAILED_TO_SEND;
    330         CRX_ERROR (aResult, "Failed to send.");
     355        CRX_ERROR (aResult, "Failed to send(%d).", GetSystemErrorCode ());
    331356        return aResult;
    332357    }
     
    343368
    344369    /*----------------------------------------------------------------*/
    345     if (!IsReady ())        return ERROR_TCPSOCKET_NOT_READY;
     370    if (!IsReady ())
     371        return ERROR_TCPSOCKET_NOT_READY;
    346372
    347373    aResult = recv (mSocket, aBuffer, aSize, 0);
     
    349375    {
    350376        aResult = ERROR_TCPSOCKET_FAILED_TO_RECEIVE;
    351         CRX_ERROR (aResult, "Failed to receive.");
    352         return aResult;
    353     }
    354 
    355     /*----------------------------------------------------------------*/
    356     return aResult;
    357 }
     377        CRX_ERROR (aResult, "Failed to receive(%d).", GetSystemErrorCode ());
     378        return aResult;
     379    }
     380
     381    /*----------------------------------------------------------------*/
     382    return aResult;
     383}
  • trunk/src/main.cpp

    r22 r24  
    1 // LocalProxy.cpp : ÄÜ¼Ö ÀÀ¿ë ÇÁ·Î±×·¥¿¡ ´ëÇÑ ÁøÀÔÁ¡À» Á¤ÀÇÇÕ´Ï´Ù.
    2 //
    3 
    41#ifdef _WIN32
    52# define _CRT_SECURE_NO_WARNINGS
     
    1815using namespace std;
    1916
     17#ifdef _WIN32
     18# include <process.h>
     19# define THREAD_TYPE                            void *
     20# define THREAD_FUNCTION_RETURN                 unsigned int
     21# define THREAD_FUNCTION_CALLING_CONVENTION     WINAPI
     22#else
     23# define THREAD_TYPE                            pthread_t
     24# define THREAD_FUNCTION_RETURN                 void *
     25# define THREAD_FUNCTION_CALLING_CONVENTION
     26#endif
     27
     28THREAD_FUNCTION_RETURN
     29THREAD_FUNCTION_CALLING_CONVENTION
     30CRXProxyMTWrapper (void * aThreadArg);
     31
     32int ThreadPool (CRXSocket   * aProxySocket,
     33                const int   aThreadPoolCount);
     34
    2035int main (int argc, char* argv[])
    2136{
    22     int                     aResult = 0;
    23     const unsigned short    aPort   = 8080;
     37    int         aResult = 0;
     38    const short aPort   = 8080;
     39    const int   aThreadPoolCount = 30;
    2440
    25     CRXSocket               aSocket;
    26     CRXProxy                * aProxy = NULL;
     41    CRXSocket   aProxySocket;
    2742
    2843    /*----------------------------------------------------------------*/
    29     /*----------------------------------------------------------------
    30      * Initialize
    31      *----------------------------------------------------------------*/
    32     aResult = aSocket.CreateServer (aPort);
     44    /* 1. create proxy server */
     45    aResult = aProxySocket.CreateServer (aPort);
    3346    if (aResult < 0)
    3447    {
     
    3750    }
    3851
     52    /* 2. ready proxy servers */
     53    aResult = ThreadPool (&aProxySocket, aThreadPoolCount);
     54    if (aResult)
     55    {
     56        cout << "Failed to create thread pool." << endl;
     57        return aResult;
     58    }
     59
     60    /* 3. keep process */
     61    while (true);
     62
     63    /*----------------------------------------------------------------*/
     64    aProxySocket.Close ();
     65
     66    return aResult;
     67}
     68
     69THREAD_FUNCTION_RETURN
     70THREAD_FUNCTION_CALLING_CONVENTION
     71CRXProxyMTWrapper (void * aThreadArg)
     72{
     73    int         aResult = 0;
     74
     75    CRXSocket   * aProxySocket = (CRXSocket *)aThreadArg;
     76    CRXProxy    * aProxy = NULL;
     77
     78    /*----------------------------------------------------------------*/
    3979    for (;;)
    4080    {
    41         aResult = aSocket.Accept ();
     81        aResult = aProxySocket->Accept ();
    4282        if (aResult < 0)
    4383        {
    44             cout << "Failed to accept." << endl;
     84            cerr << "Failed to accept." << endl;
    4585            break;
    4686        }
    4787
    48         if ((aProxy = CRXProxy::GetNewInstance (aResult)) == NULL)
     88        if ((aProxy = CRXProxy::GetNewInstance ()) == NULL)
    4989        {
    50             cout << "Failed to get proxy." << endl;
     90            cerr << "Failed to get proxy." << endl;
    5191            aResult = -1;
    5292            break;
    5393        }
    5494
    55         aResult = aProxy->ForwardMT ();
     95        aProxy->SetClientSocket (aResult);
     96
     97        aResult = aProxy->Forward ();
    5698        if (aResult < 0)
    5799        {
    58             cout << "Failed to forward." << endl;
     100            cerr << aProxy->GetErrorMessage () << endl;
     101        }
     102
     103        aProxy->ReleaseInstance ();
     104    }
     105
     106    cerr << " thread exit." << endl;
     107    /*----------------------------------------------------------------*/
     108
     109    return (THREAD_FUNCTION_RETURN)0;
     110}
     111
     112int ThreadPool (CRXSocket   * aProxySocket,
     113                const int   aThreadPoolCount)
     114{
     115    int             aResult = 0;
     116    int             aIter = 0;
     117    THREAD_TYPE     aThreadID = (THREAD_TYPE)0;
     118
     119    /*----------------------------------------------------------------*/
     120    for (aIter = 0 ; aIter < aThreadPoolCount ; aIter++)
     121    {
     122#ifdef _WIN32
     123        aThreadID = (void *)_beginthreadex (0, 0, CRXProxyMTWrapper, aProxySocket, 0, 0);
     124        if (aThreadID == NULL)
     125        {
     126            aResult = -1;
    59127            break;
    60128        }
     129        CloseHandle (aThreadID);
     130#else
     131        aResult = pthread_create (&aThreadID, NULL, CRXProxyMTWrapper, aProxySocket);
     132        if (aResult < 0)
     133        {
     134            break;
     135        }
     136#endif
     137        cout << "Thread(" << aIter + 1 << ") is created successfully." << endl;
    61138    }
    62 
    63139    /*----------------------------------------------------------------*/
    64     aSocket.Close ();
    65140
    66141    return aResult;
Note: See TracChangeset for help on using the changeset viewer.