Changeset 24 in cheroxy for trunk/src/main.cpp
- Timestamp:
- 11/14/12 14:00:40 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/main.cpp
r22 r24 1 // LocalProxy.cpp : ÄÜ¼Ö ÀÀ¿ë ÇÁ·Î±×·¥¿¡ ´ëÇÑ ÁøÀÔÁ¡À» Á¤ÀÇÇÕ´Ï´Ù.2 //3 4 1 #ifdef _WIN32 5 2 # define _CRT_SECURE_NO_WARNINGS … … 18 15 using namespace std; 19 16 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 28 THREAD_FUNCTION_RETURN 29 THREAD_FUNCTION_CALLING_CONVENTION 30 CRXProxyMTWrapper (void * aThreadArg); 31 32 int ThreadPool (CRXSocket * aProxySocket, 33 const int aThreadPoolCount); 34 20 35 int main (int argc, char* argv[]) 21 36 { 22 int aResult = 0; 23 const unsigned short aPort = 8080; 37 int aResult = 0; 38 const short aPort = 8080; 39 const int aThreadPoolCount = 30; 24 40 25 CRXSocket aSocket; 26 CRXProxy * aProxy = NULL; 41 CRXSocket aProxySocket; 27 42 28 43 /*----------------------------------------------------------------*/ 29 /*---------------------------------------------------------------- 30 * Initialize 31 *----------------------------------------------------------------*/ 32 aResult = aSocket.CreateServer (aPort); 44 /* 1. create proxy server */ 45 aResult = aProxySocket.CreateServer (aPort); 33 46 if (aResult < 0) 34 47 { … … 37 50 } 38 51 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 69 THREAD_FUNCTION_RETURN 70 THREAD_FUNCTION_CALLING_CONVENTION 71 CRXProxyMTWrapper (void * aThreadArg) 72 { 73 int aResult = 0; 74 75 CRXSocket * aProxySocket = (CRXSocket *)aThreadArg; 76 CRXProxy * aProxy = NULL; 77 78 /*----------------------------------------------------------------*/ 39 79 for (;;) 40 80 { 41 aResult = a Socket.Accept ();81 aResult = aProxySocket->Accept (); 42 82 if (aResult < 0) 43 83 { 44 c out<< "Failed to accept." << endl;84 cerr << "Failed to accept." << endl; 45 85 break; 46 86 } 47 87 48 if ((aProxy = CRXProxy::GetNewInstance ( aResult)) == NULL)88 if ((aProxy = CRXProxy::GetNewInstance ()) == NULL) 49 89 { 50 c out<< "Failed to get proxy." << endl;90 cerr << "Failed to get proxy." << endl; 51 91 aResult = -1; 52 92 break; 53 93 } 54 94 55 aResult = aProxy->ForwardMT (); 95 aProxy->SetClientSocket (aResult); 96 97 aResult = aProxy->Forward (); 56 98 if (aResult < 0) 57 99 { 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 112 int 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; 59 127 break; 60 128 } 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; 61 138 } 62 63 139 /*----------------------------------------------------------------*/ 64 aSocket.Close ();65 140 66 141 return aResult;
Note:
See TracChangeset
for help on using the changeset viewer.