2424 */
2525HTTPServer::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 */
111107void 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}
0 commit comments