VC2008 / VC2010: Enabled precompiled header through Globals.h; the header included in every module in the project. Compilation optimization.
git-svn-id: http://mc-server.googlecode.com/svn/trunk@188 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
10
WebServer/Globals.cpp
Normal file
10
WebServer/Globals.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
// Globals.cpp
|
||||
|
||||
// This file is used for precompiled header generation in MSVC environments
|
||||
|
||||
#include "Globals.h"
|
||||
|
||||
|
||||
|
||||
|
||||
78
WebServer/Globals.h
Normal file
78
WebServer/Globals.h
Normal file
@@ -0,0 +1,78 @@
|
||||
|
||||
// Globals.h
|
||||
|
||||
// This file gets included from every module in the project, so that global symbols may be introduced easily
|
||||
// Also used for precompiled header generation in MSVC environments
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// OS-dependent stuff:
|
||||
#ifdef _WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <Windows.h>
|
||||
#include <winsock.h>
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h> // for mkdir
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <iostream>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// CRT stuff:
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// STL stuff:
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Common headers:
|
||||
#include "../source/cCriticalSection.h"
|
||||
#include "../source/cMCLogger.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Common definitions:
|
||||
|
||||
/// Evaluates to the number of elements in an array (compile-time!)
|
||||
#define ARRAYCOUNT(X) (sizeof(X) / sizeof(*(X)))
|
||||
|
||||
// sprintf_s is the preferred call in MSVC ("secure"); make it *nix-compatible:
|
||||
#ifndef _WIN32
|
||||
#define sprintf_s(dst, size, format, ...) sprintf(dst, format, __VA_ARGS__ )
|
||||
#define vsnprintf_s(buffer, buffer_size, maxcount, stringbuffer, ...) (vsnprintf(buffer, maxcount, stringbuffer, __VA_ARGS__))
|
||||
#endif // _WIN32
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
THIS IS NOT THE ORIGINAL SOURCE1!!1!!!~!!~`1ONE!!`1
|
||||
*/
|
||||
|
||||
#include "../source/cMCLogger.h"
|
||||
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
||||
|
||||
#include "Socket.h"
|
||||
#include <iostream>
|
||||
@@ -38,11 +38,11 @@
|
||||
#include <cstring>
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#define MSG_NOSIGNAL (0)
|
||||
#define MSG_NOSIGNAL (0)
|
||||
#endif
|
||||
|
||||
#ifdef __MAC_NA
|
||||
#define MSG_NOSIGNAL (0)
|
||||
#define MSG_NOSIGNAL (0)
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -29,8 +29,9 @@
|
||||
THIS IS NOT THE ORIGINAL SOURCE1!!1!!!~!!~`1ONE!!`1
|
||||
*/
|
||||
|
||||
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
||||
|
||||
#include "StdHelpers.h"
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
std::string ReplaceInStr(const std::string& in, const std::string& search_for, const std::string& replace_with) {
|
||||
|
||||
@@ -29,14 +29,12 @@
|
||||
THIS IS NOT THE ORIGINAL SOURCE1!!1!!!~!!~`1ONE!!`1
|
||||
*/
|
||||
|
||||
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
||||
|
||||
#include "UrlHelper.h"
|
||||
#include "Tracer.h"
|
||||
#include "StdHelpers.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
@@ -31,24 +31,22 @@
|
||||
THIS IS NOT THE ORIGINAL SOURCE1!!1!!!~!!~`1ONE!!`1
|
||||
*/
|
||||
|
||||
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
||||
|
||||
#include <ctime>
|
||||
#ifdef _WIN32
|
||||
#include <process.h>
|
||||
#include <process.h>
|
||||
#endif
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
#include "WebServer.h"
|
||||
#include "cEvents.h"
|
||||
#include "Socket.h"
|
||||
#include "UrlHelper.h"
|
||||
#include "base64.h"
|
||||
|
||||
#include "cEvent.h"
|
||||
|
||||
|
||||
webserver::request_func webserver::request_func_=0;
|
||||
|
||||
#ifdef _WIN32
|
||||
@@ -178,7 +176,7 @@ void webserver::Stop()
|
||||
{
|
||||
m_bStop = true;
|
||||
m_Socket->Close();
|
||||
m_Event->Wait();
|
||||
m_Events->Wait();
|
||||
}
|
||||
|
||||
void webserver::Begin()
|
||||
@@ -209,18 +207,18 @@ void webserver::Begin()
|
||||
pthread_create( hHandle, NULL, Request, ptr_s);
|
||||
#endif
|
||||
}
|
||||
m_Event->Set();
|
||||
m_Events->Set();
|
||||
}
|
||||
|
||||
webserver::webserver(unsigned int port_to_listen, request_func r) {
|
||||
m_Socket = new SocketServer(port_to_listen,1);
|
||||
|
||||
request_func_ = r;
|
||||
m_Event = new cEvent();
|
||||
m_Events = new cEvents();
|
||||
}
|
||||
|
||||
webserver::~webserver()
|
||||
{
|
||||
delete m_Socket;
|
||||
delete m_Event;
|
||||
delete m_Events;
|
||||
}
|
||||
|
||||
@@ -30,66 +30,64 @@
|
||||
THIS IS NOT THE ORIGINAL SOURCE1!!1!!!~!!~`1ONE!!`1
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
class cEvent;
|
||||
class cEvents;
|
||||
class Socket;
|
||||
class SocketServer;
|
||||
class webserver {
|
||||
public:
|
||||
struct http_request {
|
||||
public:
|
||||
struct http_request {
|
||||
|
||||
http_request()
|
||||
: s_( 0 )
|
||||
, authentication_given_(false)
|
||||
{}
|
||||
http_request()
|
||||
: s_( 0 )
|
||||
, authentication_given_(false)
|
||||
{}
|
||||
|
||||
Socket* s_;
|
||||
std::string method_;
|
||||
std::string path_;
|
||||
std::map<std::string, std::string> params_;
|
||||
Socket* s_;
|
||||
std::string method_;
|
||||
std::string path_;
|
||||
std::map<std::string, std::string> params_;
|
||||
|
||||
std::string accept_;
|
||||
std::string accept_language_;
|
||||
std::string accept_encoding_;
|
||||
std::string user_agent_;
|
||||
std::string accept_;
|
||||
std::string accept_language_;
|
||||
std::string accept_encoding_;
|
||||
std::string user_agent_;
|
||||
|
||||
/* status_: used to transmit server's error status, such as
|
||||
o 202 OK
|
||||
o 404 Not Found
|
||||
and so on */
|
||||
std::string status_;
|
||||
/* status_: used to transmit server's error status, such as
|
||||
o 202 OK
|
||||
o 404 Not Found
|
||||
and so on */
|
||||
std::string status_;
|
||||
|
||||
/* auth_realm_: allows to set the basic realm for an authentication,
|
||||
no need to additionally set status_ if set */
|
||||
std::string auth_realm_;
|
||||
/* auth_realm_: allows to set the basic realm for an authentication,
|
||||
no need to additionally set status_ if set */
|
||||
std::string auth_realm_;
|
||||
|
||||
std::string answer_;
|
||||
std::string answer_;
|
||||
|
||||
/* authentication_given_ is true when the user has entered a username and password.
|
||||
These can then be read from username_ and password_ */
|
||||
bool authentication_given_;
|
||||
std::string username_;
|
||||
std::string password_;
|
||||
};
|
||||
/* authentication_given_ is true when the user has entered a username and password.
|
||||
These can then be read from username_ and password_ */
|
||||
bool authentication_given_;
|
||||
std::string username_;
|
||||
std::string password_;
|
||||
};
|
||||
|
||||
typedef void (*request_func) (http_request*);
|
||||
webserver(unsigned int port_to_listen, request_func);
|
||||
typedef void (*request_func) (http_request*);
|
||||
webserver(unsigned int port_to_listen, request_func);
|
||||
~webserver();
|
||||
|
||||
void Begin();
|
||||
void Stop();
|
||||
|
||||
private:
|
||||
private:
|
||||
bool m_bStop;
|
||||
#ifdef _WIN32
|
||||
static unsigned __stdcall Request(void*);
|
||||
#else
|
||||
static void* Request(void*);
|
||||
#endif
|
||||
static request_func request_func_;
|
||||
#ifdef _WIN32
|
||||
static unsigned __stdcall Request(void*);
|
||||
#else
|
||||
static void* Request(void*);
|
||||
#endif
|
||||
static request_func request_func_;
|
||||
|
||||
cEvent* m_Event;
|
||||
cEvents * m_Events;
|
||||
SocketServer* m_Socket;
|
||||
};
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
|
||||
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
||||
|
||||
#include "base64.h"
|
||||
#include <iostream>
|
||||
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
#include "cEvent.h"
|
||||
#include "../source/cMCLogger.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <Windows.h>
|
||||
#else
|
||||
#include <semaphore.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h> // sprintf()
|
||||
#endif
|
||||
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
||||
|
||||
cEvent::cEvent( unsigned int a_NumEvents /* = 1 */ )
|
||||
#include "cEvents.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
cEvents::cEvents( unsigned int a_NumEvents /* = 1 */ )
|
||||
: m_NumEvents( a_NumEvents )
|
||||
#ifndef _WIN32
|
||||
, m_bNamed( false )
|
||||
@@ -34,24 +31,28 @@ cEvent::cEvent( unsigned int a_NumEvents /* = 1 */ )
|
||||
|
||||
if( sem_init( HandlePtr, 0, 0 ) )
|
||||
{
|
||||
LOG("WARNING cEvent: Could not create unnamed semaphore, fallback to named.");
|
||||
LOG("WARNING cEvents: Could not create unnamed semaphore, fallback to named.");
|
||||
m_bNamed = true;
|
||||
delete HandlePtr; // named semaphores return their own address
|
||||
|
||||
char c_Str[32];
|
||||
sprintf( c_Str, "cEvent%p", &HandlePtr );
|
||||
sprintf( c_Str, "cEvents%p", &HandlePtr );
|
||||
HandlePtr = sem_open( c_Str, O_CREAT, 777, 0 );
|
||||
if( HandlePtr == SEM_FAILED )
|
||||
LOG("ERROR: Could not create Event. (%i)", errno);
|
||||
else
|
||||
if( sem_unlink( c_Str ) != 0 )
|
||||
LOG("ERROR: Could not unlink cEvent. (%i)", errno);
|
||||
LOG("ERROR: Could not unlink cEvents. (%i)", errno);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
cEvent::~cEvent()
|
||||
|
||||
|
||||
|
||||
|
||||
cEvents::~cEvents()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
for( unsigned int i = 0; i < m_NumEvents; i++ )
|
||||
@@ -66,12 +67,12 @@ cEvent::~cEvent()
|
||||
{
|
||||
sem_t* & HandlePtr = ((sem_t**)m_Handle)[i];
|
||||
char c_Str[32];
|
||||
sprintf( c_Str, "cEvent%p", &HandlePtr );
|
||||
sprintf( c_Str, "cEvents%p", &HandlePtr );
|
||||
// LOG("Closing event: %s", c_Str );
|
||||
// LOG("Sem ptr: %p", HandlePtr );
|
||||
if( sem_close( HandlePtr ) != 0 )
|
||||
{
|
||||
LOG("ERROR: Could not close cEvent. (%i)", errno);
|
||||
LOG("ERROR: Could not close cEvents. (%i)", errno);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -84,7 +85,11 @@ cEvent::~cEvent()
|
||||
#endif
|
||||
}
|
||||
|
||||
void cEvent::Wait()
|
||||
|
||||
|
||||
|
||||
|
||||
void cEvents::Wait()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
WaitForMultipleObjects( m_NumEvents, (HANDLE*)m_Handle, true, INFINITE );
|
||||
@@ -93,20 +98,28 @@ void cEvent::Wait()
|
||||
{
|
||||
if( sem_wait( ((sem_t**)m_Handle)[i] ) != 0 )
|
||||
{
|
||||
LOG("ERROR: Could not wait for cEvent. (%i)", errno);
|
||||
LOG("ERROR: Could not wait for cEvents. (%i)", errno);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void cEvent::Set(unsigned int a_EventNum /* = 0 */)
|
||||
|
||||
|
||||
|
||||
|
||||
void cEvents::Set(unsigned int a_EventNum /* = 0 */)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
SetEvent( ((HANDLE*)m_Handle)[a_EventNum] );
|
||||
#else
|
||||
if( sem_post( ((sem_t**)m_Handle)[a_EventNum] ) != 0 )
|
||||
{
|
||||
LOG("ERROR: Could not set cEvent. (%i)", errno);
|
||||
LOG("ERROR: Could not set cEvents. (%i)", errno);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
class cEvent
|
||||
class cEvents
|
||||
{
|
||||
public:
|
||||
cEvent( unsigned int a_NumEvents = 1 );
|
||||
~cEvent();
|
||||
cEvents( unsigned int a_NumEvents = 1 );
|
||||
~cEvents();
|
||||
|
||||
void Wait();
|
||||
void Set(unsigned int a_EventNum = 0);
|
||||
private:
|
||||
unsigned int m_NumEvents;
|
||||
void* m_Handle; // HANDLE[] pointer
|
||||
|
||||
#ifndef _WIN32
|
||||
bool m_bNamed;
|
||||
void* m_Handle; // HANDLE[] pointer
|
||||
|
||||
#ifndef _WIN32
|
||||
bool m_bNamed;
|
||||
#endif
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user