decouple masterserver from network host game settings into two seperate threads

This commit is contained in:
Mark Vejvoda
2013-11-07 18:39:08 +00:00
parent b7a7d3bc4a
commit 9b097fd911
19 changed files with 300 additions and 89 deletions

View File

@@ -71,10 +71,10 @@ typedef void taskFunctionCallback(BaseThread *callingThread);
//
class SimpleTaskCallbackInterface {
public:
virtual void simpleTask(BaseThread *callingThread) = 0;
virtual void simpleTask(BaseThread *callingThread,void *userdata) = 0;
virtual void setupTask(BaseThread *callingThread) { }
virtual void shutdownTask(BaseThread *callingThread) { }
virtual void setupTask(BaseThread *callingThread,void *userdata) { }
virtual void shutdownTask(BaseThread *callingThread,void *userdata) { }
virtual ~SimpleTaskCallbackInterface() {}
};
@@ -97,14 +97,26 @@ protected:
time_t lastExecuteTimestamp;
taskFunctionCallback *overrideShutdownTask;
void *userdata;
bool wantSetupAndShutdown;
public:
SimpleTaskThread(SimpleTaskCallbackInterface *simpleTaskInterface,
unsigned int executionCount=0,
unsigned int millisecsBetweenExecutions=0,
bool needTaskSignal = false);
bool needTaskSignal = false,
void *userdata=NULL,
bool wantSetupAndShutdown=true);
virtual ~SimpleTaskThread();
virtual void * getUserdata() { return userdata; }
virtual int getUserdataAsInt() {
int value = 0;
if(userdata) {
value = *((int*)&userdata);
}
return value;
}
virtual void execute();
virtual bool canShutdown(bool deleteSelfIfShutdownDelayed=false);

View File

@@ -164,8 +164,6 @@ public:
static int DEFAULT_SOCKET_SENDBUF_SIZE;
static int DEFAULT_SOCKET_RECVBUF_SIZE;
//virtual void simpleTask(BaseThread *callingThread);
static int getBroadCastPort() { return broadcast_portno; }
static void setBroadCastPort(int value) { broadcast_portno = value; }
static std::vector<std::string> getLocalIPAddressList();

View File

@@ -345,7 +345,8 @@ vector<Texture2D *> FileCRCPreCacheThread::getPendingTextureList(int maxTextures
SimpleTaskThread::SimpleTaskThread( SimpleTaskCallbackInterface *simpleTaskInterface,
unsigned int executionCount,
unsigned int millisecsBetweenExecutions,
bool needTaskSignal) : BaseThread(),
bool needTaskSignal,
void *userdata, bool wantSetupAndShutdown) : BaseThread(),
simpleTaskInterface(NULL),
overrideShutdownTask(NULL) {
uniqueID = "SimpleTaskThread";
@@ -355,6 +356,11 @@ SimpleTaskThread::SimpleTaskThread( SimpleTaskCallbackInterface *simpleTaskInter
this->millisecsBetweenExecutions = millisecsBetweenExecutions;
this->needTaskSignal = needTaskSignal;
this->overrideShutdownTask = NULL;
this->userdata = userdata;
this->wantSetupAndShutdown = wantSetupAndShutdown;
//if(this->userdata != NULL) {
// printf("IN SImpleThread this->userdata [%p]\n",this->userdata);
//}
setTaskSignalled(false);
@@ -363,15 +369,18 @@ SimpleTaskThread::SimpleTaskThread( SimpleTaskCallbackInterface *simpleTaskInter
mutexLastExecuteTimestamp.setOwnerId(mutexOwnerId);
lastExecuteTimestamp = time(NULL);
string mutexOwnerId1 = CODE_AT_LINE;
MutexSafeWrapper safeMutex1(&mutexSimpleTaskInterfaceValid,mutexOwnerId1);
if(this->simpleTaskInterfaceValid == true) {
safeMutex1.ReleaseLock();
this->simpleTaskInterface->setupTask(this);
if(this->wantSetupAndShutdown == true) {
string mutexOwnerId1 = CODE_AT_LINE;
MutexSafeWrapper safeMutex1(&mutexSimpleTaskInterfaceValid,mutexOwnerId1);
if(this->simpleTaskInterfaceValid == true) {
safeMutex1.ReleaseLock();
this->simpleTaskInterface->setupTask(this,userdata);
}
}
}
SimpleTaskThread::~SimpleTaskThread() {
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
try {
cleanup();
}
@@ -382,22 +391,33 @@ SimpleTaskThread::~SimpleTaskThread() {
throw megaglest_runtime_error(ex.what());
//abort();
}
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
}
void SimpleTaskThread::cleanup() {
if(this->overrideShutdownTask != NULL) {
this->overrideShutdownTask(this);
this->overrideShutdownTask = NULL;
}
else if(this->simpleTaskInterface != NULL) {
string mutexOwnerId1 = CODE_AT_LINE;
MutexSafeWrapper safeMutex1(&mutexSimpleTaskInterfaceValid,mutexOwnerId1);
if(this->simpleTaskInterfaceValid == true) {
safeMutex1.ReleaseLock();
this->simpleTaskInterface->shutdownTask(this);
this->simpleTaskInterface = NULL;
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
if(this->wantSetupAndShutdown == true) {
if(this->overrideShutdownTask != NULL) {
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
this->overrideShutdownTask(this);
this->overrideShutdownTask = NULL;
}
else if(this->simpleTaskInterface != NULL) {
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
string mutexOwnerId1 = CODE_AT_LINE;
MutexSafeWrapper safeMutex1(&mutexSimpleTaskInterfaceValid,mutexOwnerId1);
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
if(this->simpleTaskInterfaceValid == true) {
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
safeMutex1.ReleaseLock();
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
this->simpleTaskInterface->shutdownTask(this,userdata);
this->simpleTaskInterface = NULL;
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
}
}
}
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
}
void SimpleTaskThread::setOverrideShutdownTask(taskFunctionCallback *ptr) {
@@ -479,7 +499,7 @@ void SimpleTaskThread::execute() {
if(getQuitStatus() == false) {
ExecutingTaskSafeWrapper safeExecutingTaskMutex(this);
//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
this->simpleTaskInterface->simpleTask(this);
this->simpleTaskInterface->simpleTask(this,this->userdata);
//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(getQuitStatus() == true) {