-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy paththreaded-server.cpp
More file actions
176 lines (133 loc) · 5.2 KB
/
Copy paththreaded-server.cpp
File metadata and controls
176 lines (133 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/***********************************************************************
threaded-server.cpp - Implements a simple Winsock server that accepts
connections and spins each one off into its own thread, where it's
treated as a blocking socket.
Each connection thread reads data off the socket and echoes it
back verbatim.
Compiling:
VC++: cl -GX threaded-server.cpp main.cpp ws-util.cpp wsock32.lib
BC++: bcc32 threaded-server.cpp main.cpp ws-util.cpp
This program is hereby released into the public domain. There is
ABSOLUTELY NO WARRANTY WHATSOEVER for this product. Caveat hacker.
***********************************************************************/
#include "Proxy.hpp"
#include "ThreadPool.hpp"
#include "Logger.hpp"
#include "ws-util.h"
#include <Ws2tcpip.h>
#include <atomic>
#include <iostream>
#include <sstream>
using namespace std;
//// Global variables //////////////////////////////////////////////////
atomic_int g_numThreads;
//// SetUpListener /////////////////////////////////////////////////////
// Sets up a listener on the given interface and port, returning the
// listening socket if successful; if not, returns INVALID_SOCKET.
SOCKET SetUpListener(const char *pcHost, const char *pcPort) {
addrinfo *result = NULL;
addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port.
int iResult = getaddrinfo(NULL, pcPort, &hints, &result);
if (iResult != 0) {
cerr << WSAGetLastErrorMessage("getaddrinfo() failed") << endl;
return INVALID_SOCKET;
}
// Create a SOCKET for connecting to server.
SOCKET ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
cerr << WSAGetLastErrorMessage("socket() failed") << endl;
freeaddrinfo(result);
return INVALID_SOCKET;
}
int opt = 1;
setsockopt(ListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char *) &opt, sizeof(opt));
// Setup the TCP listening socket.
iResult = bind(ListenSocket, result->ai_addr, (int) result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
cerr << WSAGetLastErrorMessage("bind() failed") << endl;
freeaddrinfo(result);
closesocket(ListenSocket);
return INVALID_SOCKET;
}
freeaddrinfo(result);
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
cerr << WSAGetLastErrorMessage("listen() failed") << endl;
closesocket(ListenSocket);
WSACleanup();
return INVALID_SOCKET;
}
return ListenSocket;
}
//// EchoHandler ///////////////////////////////////////////////////////
// Handles the incoming data by reflecting it back to the sender.
VOID CALLBACK ProxyHandler(PTP_CALLBACK_INSTANCE, PVOID sd_) {
++g_numThreads;
if (true) {
SOCKET sd = (SOCKET) sd_;
MyProxy proxy(sd);
if (!proxy.HandleBrowser()) {
Logger::LogError(__FUNC__ "Handling browser request failed");
}
if (!ShutdownConnection(sd)) {
proxy.PrintRequest(Logger::OL_ERROR);
Logger::LogError(__FUNC__ "Connection shutdown failed");
}
}
Logger::LogInfo("Threadpool work done!");
--g_numThreads;
}
//// AcceptConnections /////////////////////////////////////////////////
// Spins forever waiting for connections. For each one that comes in,
// we create a thread to handle it and go back to waiting for
// connections. If an error occurs, we return.
void AcceptConnections(SOCKET ListeningSocket) {
Logger::LEVEL = Logger::OL_ERROR;
Logger::CONSOLE = false;
ThreadPool pool;
ostringstream ss;
char szIPv4[24] = {};
sockaddr_in sinRemote;
int nAddrSize = sizeof(sinRemote);
while (true) {
SOCKET sd = accept
(ListeningSocket, (sockaddr *) &sinRemote, &nAddrSize);
if (sd != INVALID_SOCKET) {
inet_ntop(AF_INET, &sinRemote.sin_addr, szIPv4, sizeof(szIPv4));
cout << "Accepted connection from " << szIPv4 << ":" <<
ntohs(sinRemote.sin_port) << endl <<
"-- Threads count: " << (g_numThreads + 1) << endl;
pool.SetThreadMinimum(g_numThreads + 1);
if (!pool.CreateWork(ProxyHandler, (void *) sd)) {
pool.SetThreadMinimum(g_numThreads);
ss << "ThreadPool::CreateWork() failed --" << GetLastError();
Logger::LogError(ss.str());
ss.str(string());
}
}
else {
Logger::LogError(WSAGetLastErrorMessage("accept() failed"));
return;
}
}
}
//// DoWinsock /////////////////////////////////////////////////////////
// The module's driver function -- we just call other functions and
// interpret their results.
int DoWinsock(const char *pcAddr, const char *pcPort) {
cout << "Establishing the listener on port " << pcPort << "..." << endl;
SOCKET ListeningSocket = SetUpListener(pcAddr, pcPort);
if (ListeningSocket == INVALID_SOCKET) {
cout << endl << WSAGetLastErrorMessage("establish listener") << endl;
return 3;
}
cout << "Waiting for connections..." << endl;
AcceptConnections(ListeningSocket);
return 0;
}