Changeset 24 in cheroxy for trunk/src/main.cpp


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

#1 change response, threading and proxy work-flow

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.