source: cheroxy/trunk/src/main.cpp@ 45

Last change on this file since 45 was 45, checked in by cheese, 11 years ago

#1 change access modifier of filter and http messages

File size: 3.9 KB
RevLine 
[6]1#ifdef _WIN32
2# define _CRT_SECURE_NO_WARNINGS
3#endif
4
5#include "CRXSocket.h"
[18]6#include "CRXProxy.h"
[6]7
8#include <iostream>
9#include <iomanip>
10#include <sstream>
11
12#include <stdio.h>
[29]13#include <stdlib.h>
[6]14#include <string.h>
15
16using namespace std;
17
[24]18#ifdef _WIN32
19# include <process.h>
20# define THREAD_TYPE void *
21# define THREAD_FUNCTION_RETURN unsigned int
22# define THREAD_FUNCTION_CALLING_CONVENTION WINAPI
23#else
24# define THREAD_TYPE pthread_t
25# define THREAD_FUNCTION_RETURN void *
26# define THREAD_FUNCTION_CALLING_CONVENTION
27#endif
28
29THREAD_FUNCTION_RETURN
30THREAD_FUNCTION_CALLING_CONVENTION
31CRXProxyMTWrapper (void * aThreadArg);
32
33int ThreadPool (CRXSocket * aProxySocket,
34 const int aThreadPoolCount);
35
[6]36int main (int argc, char* argv[])
37{
[24]38 int aResult = 0;
39 const short aPort = 8080;
40 const int aThreadPoolCount = 30;
[6]41
[24]42 CRXSocket aProxySocket;
[6]43
44 /*----------------------------------------------------------------*/
[24]45 /* 1. create proxy server */
46 aResult = aProxySocket.CreateServer (aPort);
[6]47 if (aResult < 0)
48 {
[22]49 cout << "Failed to create server." << endl;
[6]50 return aResult;
51 }
52
[24]53 /* 2. ready proxy servers */
54 aResult = ThreadPool (&aProxySocket, aThreadPoolCount);
55 if (aResult)
56 {
57 cout << "Failed to create thread pool." << endl;
58 return aResult;
59 }
60
61 /* 3. keep process */
62 while (true);
63
64 /*----------------------------------------------------------------*/
65 aProxySocket.Close ();
66
67 return aResult;
68}
69
[45]70int ThreadPool (CRXSocket * aProxySocket,
71 const int aThreadPoolCount)
72{
73 int aResult = 0;
74 int aIter = 0;
75 THREAD_TYPE aThreadID = (THREAD_TYPE)0;
76
77 /*----------------------------------------------------------------*/
78 for (aIter = 0 ; aIter < aThreadPoolCount ; aIter++)
79 {
80#ifdef _WIN32
81 aThreadID = (void *)_beginthreadex (0, 0, CRXProxyMTWrapper, aProxySocket, 0, 0);
82 if (aThreadID == NULL)
83 {
84 aResult = -1;
85 break;
86 }
87 CloseHandle (aThreadID);
88#else
89 aResult = pthread_create (&aThreadID, NULL, CRXProxyMTWrapper, aProxySocket);
90 if (aResult < 0)
91 {
92 break;
93 }
94#endif
95 cout << "Thread(" << aIter + 1 << ") is created successfully." << endl;
96 }
97 /*----------------------------------------------------------------*/
98
99 return aResult;
100}
101
[24]102THREAD_FUNCTION_RETURN
103THREAD_FUNCTION_CALLING_CONVENTION
104CRXProxyMTWrapper (void * aThreadArg)
105{
106 int aResult = 0;
107
[45]108 CRXSocket & aProxySocket = *((CRXSocket *)aThreadArg);
[24]109 CRXProxy * aProxy = NULL;
110
[35]111 char aFilterFileExtension[] = "exe|gif|jpg|png|css|js|ico|swf|";
[28]112
[24]113 /*----------------------------------------------------------------*/
[6]114 for (;;)
115 {
[45]116 aResult = aProxySocket.Accept ();
[6]117 if (aResult < 0)
118 {
[45]119 cerr << aProxySocket.GetErrorMessage () << endl;
120 aResult = -1;
[6]121 break;
122 }
123
[45]124 aProxy = new(std::nothrow) CRXProxy (aResult);
125 if (aProxy == NULL)
[6]126 {
[24]127 cerr << "Failed to get proxy." << endl;
[6]128 aResult = -1;
129 break;
130 }
[45]131 aProxy->SetServerTimeout (2);
[6]132
[45]133 CRXFilter & aFilter = aProxy->GetFilter ();
134 CRXHttpRequest & aRequest = aProxy->GetHttpRequest ();
135 CRXHttpResponse & aResponse = aProxy->GetHttpResponse ();
136
137 aFilter.SetRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION,
[28]138 CRX_FILTER_MATCHED,
139 aFilterFileExtension);
[24]140
141 aResult = aProxy->Forward ();
[6]142 if (aResult < 0)
143 {
[45]144 cerr << aProxy->GetErrorMessage () << endl;
[6]145 }
[24]146
[45]147 if (!aFilter.CheckRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION, aRequest))
[28]148 {
[45]149 CRX_DEBUG_TRACE ("== Request: \n%s\n", aRequest.GetHeader ().c_str ());
150 CRX_DEBUG_TRACE ("== Response: \n%s\n", aResponse.GetHeader ().c_str ());
151 CRX_DEBUG_TRACE_BIN (aResponse.GetContentBody (),
152 aResponse.GetContentLength (),
153 "== Content-Body: \n");
[28]154 }
155
[44]156 delete aProxy;
[6]157 }
158
[35]159 cout << " thread exit." << endl;
[6]160 /*----------------------------------------------------------------*/
161
[24]162 return (THREAD_FUNCTION_RETURN)0;
[35]163}
Note: See TracBrowser for help on using the repository browser.