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

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

#1 add filter class

File size: 3.7 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 <string.h>
14
15using namespace std;
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
28THREAD_FUNCTION_RETURN
29THREAD_FUNCTION_CALLING_CONVENTION
30CRXProxyMTWrapper (void * aThreadArg);
31
32int ThreadPool (CRXSocket * aProxySocket,
33 const int aThreadPoolCount);
34
35int main (int argc, char* argv[])
36{
37 int aResult = 0;
38 const short aPort = 8080;
39 const int aThreadPoolCount = 30;
40
41 CRXSocket aProxySocket;
42
43 /*----------------------------------------------------------------*/
44 /* 1. create proxy server */
45 aResult = aProxySocket.CreateServer (aPort);
46 if (aResult < 0)
47 {
48 cout << "Failed to create server." << endl;
49 return aResult;
50 }
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
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 char * aFilterFileExtension = "exe|gif|jpg|png|css|js|ico|";
79 char * aHttpRequest = NULL;
80 int aHttpRequestLength = 0;
81
82 /*----------------------------------------------------------------*/
83 for (;;)
84 {
85 aResult = aProxySocket->Accept ();
86 if (aResult < 0)
87 {
88 cerr << "Failed to accept." << endl;
89 break;
90 }
91
92 if ((aProxy = CRXProxy::GetNewInstance ()) == NULL)
93 {
94 cerr << "Failed to get proxy." << endl;
95 aResult = -1;
96 break;
97 }
98
99 aProxy->SetClientSocket (aResult);
100 aProxy->SetRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION,
101 CRX_FILTER_MATCHED,
102 aFilterFileExtension);
103
104 aResult = aProxy->Forward ();
105 if (aResult < 0)
106 {
107 cerr << aProxy->GetErrorMessage () << endl;
108 }
109
110 if (!aProxy->CheckRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION))
111 {
112 aHttpRequestLength = aProxy->GetHttpRequestLength ();
113 aHttpRequest = (char *) calloc (aHttpRequestLength + 1, 1);
114 cout << aProxy->GetHttpRequest (aHttpRequest, aHttpRequestLength) << endl;
115 free (aHttpRequest);
116 }
117
118 aProxy->ReleaseInstance ();
119 }
120
121 cerr << " thread exit." << endl;
122 /*----------------------------------------------------------------*/
123
124 return (THREAD_FUNCTION_RETURN)0;
125}
126
127int ThreadPool (CRXSocket * aProxySocket,
128 const int aThreadPoolCount)
129{
130 int aResult = 0;
131 int aIter = 0;
132 THREAD_TYPE aThreadID = (THREAD_TYPE)0;
133
134 /*----------------------------------------------------------------*/
135 for (aIter = 0 ; aIter < aThreadPoolCount ; aIter++)
136 {
137#ifdef _WIN32
138 aThreadID = (void *)_beginthreadex (0, 0, CRXProxyMTWrapper, aProxySocket, 0, 0);
139 if (aThreadID == NULL)
140 {
141 aResult = -1;
142 break;
143 }
144 CloseHandle (aThreadID);
145#else
146 aResult = pthread_create (&aThreadID, NULL, CRXProxyMTWrapper, aProxySocket);
147 if (aResult < 0)
148 {
149 break;
150 }
151#endif
152 cout << "Thread(" << aIter + 1 << ") is created successfully." << endl;
153 }
154 /*----------------------------------------------------------------*/
155
156 return aResult;
157}
Note: See TracBrowser for help on using the repository browser.