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
Line 
1#ifdef _WIN32
2# define _CRT_SECURE_NO_WARNINGS
3#endif
4
5#include "CRXSocket.h"
6#include "CRXProxy.h"
7
8#include <iostream>
9#include <iomanip>
10#include <sstream>
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15
16using namespace std;
17
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
36int main (int argc, char* argv[])
37{
38 int aResult = 0;
39 const short aPort = 8080;
40 const int aThreadPoolCount = 30;
41
42 CRXSocket aProxySocket;
43
44 /*----------------------------------------------------------------*/
45 /* 1. create proxy server */
46 aResult = aProxySocket.CreateServer (aPort);
47 if (aResult < 0)
48 {
49 cout << "Failed to create server." << endl;
50 return aResult;
51 }
52
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
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
102THREAD_FUNCTION_RETURN
103THREAD_FUNCTION_CALLING_CONVENTION
104CRXProxyMTWrapper (void * aThreadArg)
105{
106 int aResult = 0;
107
108 CRXSocket & aProxySocket = *((CRXSocket *)aThreadArg);
109 CRXProxy * aProxy = NULL;
110
111 char aFilterFileExtension[] = "exe|gif|jpg|png|css|js|ico|swf|";
112
113 /*----------------------------------------------------------------*/
114 for (;;)
115 {
116 aResult = aProxySocket.Accept ();
117 if (aResult < 0)
118 {
119 cerr << aProxySocket.GetErrorMessage () << endl;
120 aResult = -1;
121 break;
122 }
123
124 aProxy = new(std::nothrow) CRXProxy (aResult);
125 if (aProxy == NULL)
126 {
127 cerr << "Failed to get proxy." << endl;
128 aResult = -1;
129 break;
130 }
131 aProxy->SetServerTimeout (2);
132
133 CRXFilter & aFilter = aProxy->GetFilter ();
134 CRXHttpRequest & aRequest = aProxy->GetHttpRequest ();
135 CRXHttpResponse & aResponse = aProxy->GetHttpResponse ();
136
137 aFilter.SetRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION,
138 CRX_FILTER_MATCHED,
139 aFilterFileExtension);
140
141 aResult = aProxy->Forward ();
142 if (aResult < 0)
143 {
144 cerr << aProxy->GetErrorMessage () << endl;
145 }
146
147 if (!aFilter.CheckRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION, aRequest))
148 {
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");
154 }
155
156 delete aProxy;
157 }
158
159 cout << " thread exit." << endl;
160 /*----------------------------------------------------------------*/
161
162 return (THREAD_FUNCTION_RETURN)0;
163}
Note: See TracBrowser for help on using the repository browser.