Skip to content

Commit ef0975e

Browse files
committed
Removed separate HTTPServer thread
1 parent 6e8d2f7 commit ef0975e

File tree

5 files changed

+23
-45
lines changed

5 files changed

+23
-45
lines changed

HTTPServer.cpp

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
HTTPServer::HTTPServer() {
2626
canRun = false;
27-
thread = NULL;
2827

2928
listenSocket = INVALID_SOCKET;
3029
memset(&serverAddr, 0, sizeof(serverAddr)); // clear the struct
@@ -48,9 +47,6 @@ HTTPServer::~HTTPServer() {
4847
if(listenSocket != INVALID_SOCKET)
4948
closeSockets();
5049
delete clientMap;
51-
52-
if(thread != NULL)
53-
delete thread;
5450
}
5551

5652
/**
@@ -77,7 +73,7 @@ void HTTPServer::start(int port) {
7773
serverAddr.sin_addr.s_addr = INADDR_ANY; // Let OS intelligently select the server's host address
7874

7975
// Bind: Assign the address to the socket
80-
if(bind(listenSocket, (sockaddr*)&serverAddr, sizeof(serverAddr)) != 0) {
76+
if(bind(listenSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) != 0) {
8177
cout << "Failed to bind to the address!" << endl;
8278
return;
8379
}
@@ -99,22 +95,17 @@ void HTTPServer::start(int port) {
9995

10096
cout << "Server started. Listening on port " << port << "..." << endl;
10197

102-
// Spawn thread
98+
// Start processing
10399
canRun = true;
104-
thread = new boost::thread(boost::ref(*this));
100+
process();
105101
}
106102

107103
/**
108104
* Stop
109105
* Signal the server thread to stop running and shut down
110106
*/
111107
void HTTPServer::stop() {
112-
runMutex.lock();
113108
canRun = false;
114-
runMutex.unlock();
115-
116-
if(thread != NULL)
117-
thread->join();
118109

119110
// Safely shutdown the server and close all open connections and sockets
120111
closeSockets();
@@ -183,16 +174,10 @@ void HTTPServer::acceptConnection() {
183174
* Main server processing function that checks for any new connections or data to read on
184175
* the listening socket
185176
*/
186-
void HTTPServer::operator() () {
187-
bool run = true;
177+
void HTTPServer::process() {
188178
int sret = 0;
189179

190-
while(run) {
191-
// Update the running state
192-
runMutex.lock();
193-
run = canRun;
194-
runMutex.unlock();
195-
180+
while(canRun) {
196181
// Copy master set into fd_read for processing
197182
fd_read = fd_master;
198183

@@ -218,8 +203,7 @@ void HTTPServer::operator() () {
218203
cout << "Select failed!" << endl;
219204
break;
220205
} else { // Timeout
221-
// Yield rest of time slice to CPU
222-
boost::this_thread::yield();
206+
usleep(100);
223207
}
224208
}
225209
}

HTTPServer.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include <map>
2323
#include <string>
24-
#include <boost/thread.hpp>
2524

2625
#include <time.h>
2726
#include <unistd.h>
@@ -42,6 +41,8 @@
4241
#define INVALID_SOCKET -1
4342

4443
class HTTPServer {
44+
bool canRun;
45+
4546
// Network
4647
SOCKET listenSocket; // Descriptor for the listening socket
4748
struct sockaddr_in serverAddr; // Structure for the server address
@@ -53,13 +54,9 @@ class HTTPServer {
5354

5455
// Resources / File System
5556
ResourceManager *resMgr;
56-
57-
// Threading
58-
bool canRun;
59-
boost::thread* thread;
60-
boost::mutex runMutex;
6157

6258
// Private methods
59+
void process();
6360
void acceptConnection();
6461
Client *getClient(SOCKET clfd);
6562
void closeSockets();
@@ -79,7 +76,6 @@ class HTTPServer {
7976

8077
void start(int port);
8178
void stop();
82-
void operator() ();
8379
};
8480

8581
#endif

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
CC = g++
55
OBJS = ByteBuffer.o Client.o HTTPMessage.o HTTPRequest.o HTTPResponse.o HTTPServer.o main.o Resource.o ResourceManager.o
66
# Debug Flags
7-
DEBUGFLAGS = -g -O0 -fpermissive -Wall
7+
DEBUGFLAGS = -g -O0 -Wall
88
# Production Flags
99
PRODFLAGS = -Wall -O3
1010
# Active Flags
11-
FLAGS = -Iinclude/ $(DEBUGFLAGS)
12-
LINK = -lpthread -lboost_thread-mt
11+
FLAGS = -Iinclude/ $(DEBUGFLAGS)
12+
# LINK = -lpthread -lboost_thread-mt
1313

1414
all: $(OBJS)
15-
$(CC) $(FLAGS) *.o -o bin/httpserver $(LINK)
15+
$(CC) $(FLAGS) *.o -o bin/httpserver
1616

1717
ByteBuffer.o: ByteBuffer.cpp
1818
$(CC) $(FLAGS) -c ByteBuffer.cpp -o $@

bin/htdocs/test.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<html>
2+
<head>
3+
<title>Test Title</title>
4+
</head>
5+
6+
<body>
7+
Hello World!<br />
8+
</body>
9+
</html>
10+

main.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,6 @@ int main (int argc, const char * argv[])
5050
svr = new HTTPServer();
5151
svr->start(8080);
5252

53-
// Input loop
54-
std::string input;
55-
while(true) {
56-
cin >> input;
57-
58-
if(input == "quit") {
59-
break;
60-
} else {
61-
cout << "Unknown command: " << input << endl;
62-
}
63-
}
64-
6553
// Stop the server thread
6654
svr->stop();
6755
delete svr;

0 commit comments

Comments
 (0)