+ proper shutdown

+ better thread pausing
+ misc stuff
This commit is contained in:
False.Genesis 2007-01-06 15:29:00 +00:00
parent b701810b15
commit 0962bec886
11 changed files with 106 additions and 85 deletions

View File

@ -17,7 +17,7 @@ VarSet::~VarSet()
std::string VarSet::Get(std::string varname) std::string VarSet::Get(std::string varname)
{ {
for(std::list<Var>::iterator i=buffer.begin();i!=buffer.end();i++) for(std::deque<Var>::iterator i=buffer.begin();i!=buffer.end();i++)
if( i->name==varname ) if( i->name==varname )
return i->value; return i->value;
return ""; // if var has not been set return empty string return ""; // if var has not been set return empty string
@ -27,7 +27,7 @@ void VarSet::Set(std::string varname, std::string varvalue)
{ {
if(varname.empty()) if(varname.empty())
return; return;
for(std::list<Var>::iterator i = buffer.begin();i!=buffer.end();i++) for(std::deque<Var>::iterator i = buffer.begin();i!=buffer.end();i++)
{ {
if( i->name==varname ) if( i->name==varname )
{ {
@ -49,7 +49,7 @@ unsigned int VarSet::Size(void)
bool VarSet::Exists(std::string varname) bool VarSet::Exists(std::string varname)
{ {
for(std::list<Var>::iterator i = buffer.begin();i!=buffer.end();i++) for(std::deque<Var>::iterator i = buffer.begin();i!=buffer.end();i++)
if(i->name==varname) if(i->name==varname)
return true; return true;
return false; return false;
@ -59,7 +59,7 @@ void VarSet::Unset(std::string varname)
{ {
if ( varname.empty() ) if ( varname.empty() )
return; return;
for(std::list<Var>::iterator i = buffer.begin();i!=buffer.end();i++) for(std::deque<Var>::iterator i = buffer.begin();i!=buffer.end();i++)
{ {
if(i->name==varname) if(i->name==varname)
{ {
@ -74,6 +74,11 @@ void VarSet::Clear(void)
buffer.clear(); buffer.clear();
} }
Var VarSet::operator[](unsigned int id)
{
return buffer.at(id);
}
bool VarSet::ReadVarsFromFile(std::string fn) bool VarSet::ReadVarsFromFile(std::string fn)
{ {
std::fstream fh; std::fstream fh;

View File

@ -2,7 +2,7 @@
#define __VARSET_H #define __VARSET_H
#include <string> #include <string>
#include <list> #include <deque>
struct Var { struct Var {
@ -19,12 +19,13 @@ public:
unsigned int Size(void); unsigned int Size(void);
bool Exists(std::string); bool Exists(std::string);
bool ReadVarsFromFile(std::string fn); bool ReadVarsFromFile(std::string fn);
Var operator[](unsigned int id);
VarSet(); VarSet();
~VarSet(); ~VarSet();
// far future: MergeWith(VarSet,bool overwrite); // far future: MergeWith(VarSet,bool overwrite);
private: private:
std::list<Var> buffer; std::deque<Var> buffer;
std::string toLower(std::string); std::string toLower(std::string);
std::string toUpper(std::string); std::string toUpper(std::string);

View File

@ -21,9 +21,13 @@ opcode handler table vervollst
//###### Start of program code ####### //###### Start of program code #######
PseuInstanceRunnable::PseuInstanceRunnable()
{
}
void PseuInstanceRunnable::run(void) void PseuInstanceRunnable::run(void)
{ {
PseuInstance *_i = new PseuInstance(); _i = new PseuInstance(this);
_i->SetConfDir("./conf/"); _i->SetConfDir("./conf/");
_i->SetScpDir("./scripts/"); _i->SetScpDir("./scripts/");
_i->Init(); _i->Init();
@ -32,13 +36,22 @@ void PseuInstanceRunnable::run(void)
delete _i; delete _i;
} }
PseuInstance::PseuInstance() void PseuInstanceRunnable::sleep(uint32 msecs)
{ {
ZThread::Thread::sleep(msecs);
}
PseuInstance::PseuInstance(PseuInstanceRunnable *run)
{
_runnable=run;
_ver="PseuWoW Alpha Build 12 dev 2" DEBUG_APPENDIX; _ver="PseuWoW Alpha Build 12 dev 2" DEBUG_APPENDIX;
_ver_short="A12-dev1" DEBUG_APPENDIX; _ver_short="A12-dev1" DEBUG_APPENDIX;
_wsession=NULL; _wsession=NULL;
_rsession=NULL; _rsession=NULL;
_scp=NULL;
_conf=NULL;
_stop=false; _stop=false;
_fastquit=false;
} }
@ -47,7 +60,7 @@ PseuInstance::~PseuInstance()
{ {
delete _scp; delete _scp;
delete _conf; delete _conf;
delete _rsession; //delete _rsession; // deleted by SocketHandler!!!!!
delete _wsession; delete _wsession;
} }
@ -138,6 +151,16 @@ void PseuInstance::Run(void)
{ {
Update(); Update();
} }
if(_fastquit)
{
printf("Aborting Instance...\n");
return;
}
printf("Shutting down instance...\n");
// save all data here // save all data here
} }
@ -145,12 +168,12 @@ void PseuInstance::Run(void)
void PseuInstance::Update() void PseuInstance::Update()
{ {
if(_sh.GetCount()) if(_sh.GetCount())
_sh.Select(1,0); // update the realmsocket _sh.Select(0,0); // update the realmsocket
//else //else
// socket invalid? // socket invalid?
_wsession->Update(); _wsession->Update(); // update the worldSESSION, which will update the worldsocket itself
this->Sleep(GetConf()->networksleeptime);
} }
void PseuInstance::SaveAllCache(void) void PseuInstance::SaveAllCache(void)
@ -159,6 +182,11 @@ void PseuInstance::SaveAllCache(void)
//... //...
} }
void PseuInstance::Sleep(uint32 msecs)
{
GetRunnable()->sleep(msecs);
}
/* /*

View File

@ -2,15 +2,15 @@
#ifndef _PSEUWOW_H #ifndef _PSEUWOW_H
#define _PSEUWOW_H #define _PSEUWOW_H
#include "../shared/common.h" #include "common.h"
#include "Auth/BigNumber.h" #include "Auth/BigNumber.h"
#include "DefScript/DefScript.h" #include "DefScript/DefScript.h"
#include "../shared/Network/SocketHandler.h" #include "Network/SocketHandler.h"
class RealmSocket; class RealmSocket;
class WorldSession; class WorldSession;
class Sockethandler; class Sockethandler;
class PseuInstanceRunnable;
class PseuInstanceConf class PseuInstanceConf
{ {
@ -45,7 +45,7 @@ class PseuInstance
{ {
public: public:
PseuInstance(); PseuInstance(PseuInstanceRunnable *run);
~PseuInstance(); ~PseuInstance();
@ -53,6 +53,7 @@ class PseuInstance
RealmSocket *GetRSession(void) { return _rsession; } RealmSocket *GetRSession(void) { return _rsession; }
PseuInstanceConf *GetConf(void) { return _conf; } PseuInstanceConf *GetConf(void) { return _conf; }
DefScriptPackage *GetScripts(void) { return _scp; } DefScriptPackage *GetScripts(void) { return _scp; }
PseuInstanceRunnable *GetRunnable(void) { return _runnable; }
void SetConfDir(std::string dir) { _confdir = dir; } void SetConfDir(std::string dir) { _confdir = dir; }
void SetScpDir(std::string dir) { _scpdir = dir; } void SetScpDir(std::string dir) { _scpdir = dir; }
void SetSessionKey(BigNumber key) { _sessionkey = key; } void SetSessionKey(BigNumber key) { _sessionkey = key; }
@ -64,21 +65,24 @@ class PseuInstance
bool Init(); bool Init();
void SaveAllCache(void); void SaveAllCache(void);
void Stop(void) { _stop = true; } void Stop(void) { _stop = true; }
void SetFastQuit(bool q=true) { _fastquit=true; }
void Quit(void); void Quit(void);
void Run(void); void Run(void);
void Update(void); void Update(void);
void Sleep(uint32 msecs);
private: private:
PseuInstanceRunnable *_runnable;
RealmSocket *_rsession; RealmSocket *_rsession;
WorldSession *_wsession; WorldSession *_wsession;
PseuInstanceConf *_conf; PseuInstanceConf *_conf;
DefScriptPackage *_scp; DefScriptPackage *_scp;
std::string _confdir,_scpdir; std::string _confdir,_scpdir;
bool _initialized; bool _initialized;
bool _stop; bool _stop,_fastquit;
BigNumber _sessionkey; BigNumber _sessionkey;
char *_ver,*_ver_short; char *_ver,*_ver_short;
SocketHandler _sh; SocketHandler _sh;
@ -89,60 +93,14 @@ class PseuInstance
class PseuInstanceRunnable : public ZThread::Runnable class PseuInstanceRunnable : public ZThread::Runnable
{ {
public: public:
void run(); PseuInstanceRunnable::PseuInstanceRunnable();
void run(void);
void sleep(uint32);
PseuInstance *GetInstance(void) { return _i; }
private:
PseuInstance *_i;
}; };
// OBOELETE
/*
class SDLTCPConnection;
class DefScriptPackage;
class PlayerNameCache;
class VarSet;
extern char DEBUG;
extern char *ver;
extern char *realmlist,*accname,*accpass,*realmname;
extern bool quit,quitted,exitonerror;
extern unsigned char error;
extern unsigned char clientversion[3];
extern unsigned short clientbuild;
extern char clientlang[4];
extern bool something_went_wrong;
extern unsigned int c_port;
extern bool allowcontroller;
extern char *c_password;
extern unsigned int rs_port;
extern unsigned short clientbuild;
extern char clientlang[4];
extern unsigned int ws_port;
extern std::string worldhost, charname;
extern SDLTCPConnection worldCon,realmCon,ctrlCon;
extern bool inworld;
extern unsigned short idleSleepTime;
extern DefScriptPackage defScp;
extern std::string defScpPath;
extern PlayerNameCache plrNameCache;
extern VarSet playerPermissions;
extern uint64 _myGUID, _targetGUID, _followGUID;
// --- Some Functions ---
void quitproc_error(void);
void quitproc(void);
void _SaveAllCache(void);
*/
#endif #endif

View File

@ -115,7 +115,7 @@ void RealmSocket::Start(void)
void RealmSocket::Stop(void) void RealmSocket::Stop(void)
{ {
_valid=false; _valid=false;
this->SetCloseAndDelete(); this->Close();
memset(_m2,0,20); memset(_m2,0,20);
_key=0; _key=0;
} }

View File

@ -29,6 +29,7 @@ WorldSession::WorldSession(PseuInstance *in)
_instance = in; _instance = in;
_valid=_authed=_logged=false; _valid=_authed=_logged=false;
_socket=new WorldSocket(_sh,this); _socket=new WorldSocket(_sh,this);
_socket->SetDeleteByHandler();
_sh.Add(_socket); _sh.Add(_socket);
_targetGUID=0; // no target _targetGUID=0; // no target
_followGUID=0; // dont follow anything _followGUID=0; // dont follow anything
@ -38,7 +39,7 @@ WorldSession::WorldSession(PseuInstance *in)
WorldSession::~WorldSession() WorldSession::~WorldSession()
{ {
delete _socket; //delete _socket; the socket will be deleted by its handler!!
} }
void WorldSession::AddToDataQueue(uint8 *data, uint32 len) void WorldSession::AddToDataQueue(uint8 *data, uint32 len)

View File

@ -1,10 +1,9 @@
#include "../shared/common.h" #include "common.h"
#include "main.h" #include "main.h"
#include "PseuWoW.h" #include "PseuWoW.h"
#ifndef SIGQUIT std::list<PseuInstanceRunnable*> instanceList; // TODO: move this to a "Master" class later
#define SIGQUIT 3
#endif
void _HookSignals(void) void _HookSignals(void)
{ {
@ -24,28 +23,50 @@ void _OnSignal(int s)
case SIGINT: case SIGINT:
case SIGQUIT: case SIGQUIT:
case SIGTERM: case SIGTERM:
case SIGABRT:
quitproc(); quitproc();
break; break;
case SIGABRT:
#ifndef _DEBUG
case SIGSEGV:
#endif
#ifdef _WIN32 #ifdef _WIN32
case SIGBREAK: case SIGBREAK:
exit(0);
break;
#endif #endif
abortproc();
break;
} }
signal(s, _OnSignal); signal(s, _OnSignal);
} }
void quitproc(void){ void quitproc(void)
exit(0); {
printf("Waiting for all instances to finish... [%u]\n",instanceList.size());
for(std::list<PseuInstanceRunnable*>::iterator i=instanceList.begin();i!=instanceList.end();i++)
{
(*i)->GetInstance()->Stop();
}
} }
void abortproc(void)
{
printf("Terminating all instances... [%u]\n",instanceList.size());
for(std::list<PseuInstanceRunnable*>::iterator i=instanceList.begin();i!=instanceList.end();i++)
{
(*i)->GetInstance()->SetFastQuit(true);
(*i)->GetInstance()->Stop();
}
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
try try
{ {
_HookSignals(); _HookSignals();
ZThread::Thread t(new PseuInstanceRunnable);
// 1 instance is enough for now
PseuInstanceRunnable *r=new PseuInstanceRunnable();
ZThread::Thread t(r);
instanceList.push_back(r);
t.setPriority((ZThread::Priority)2); t.setPriority((ZThread::Priority)2);
//... //...
t.wait(); t.wait();

View File

@ -4,8 +4,7 @@
void _HookSignals(void); void _HookSignals(void);
void _OnSignal(int); void _OnSignal(int);
void quitproc(void); void quitproc(void);
void abortproc(void);
int main(int,char**); int main(int,char**);
// TODO: ZTHread additional includes machen, dass #include <...> möglich ist
#endif #endif

View File

@ -106,7 +106,7 @@
Name="VCLinkerTool" Name="VCLinkerTool"
AdditionalDependencies="odbc32.lib odbccp32.lib sdl.lib sdl_net.lib sdlmain.lib libeay32MT.lib ssleay32MT.lib WS2_32.LIB" AdditionalDependencies="odbc32.lib odbccp32.lib sdl.lib sdl_net.lib sdlmain.lib libeay32MT.lib ssleay32MT.lib WS2_32.LIB"
OutputFile="$(OutDir)/$(InputName).exe" OutputFile="$(OutDir)/$(InputName).exe"
LinkIncremental="1" LinkIncremental="2"
SuppressStartupBanner="TRUE" SuppressStartupBanner="TRUE"
AdditionalLibraryDirectories="" AdditionalLibraryDirectories=""
GenerateDebugInformation="TRUE" GenerateDebugInformation="TRUE"

View File

@ -83,4 +83,9 @@
typedef uint32 DWORD; typedef uint32 DWORD;
#endif #endif
#ifndef SIGQUIT
#define SIGQUIT 3
#endif
#endif #endif

View File

@ -12,6 +12,9 @@
#include <string> #include <string>
#include <string.h> #include <string.h>
#include <sstream> #include <sstream>
#include <list>
#include <deque>
#include <vector>
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include "zthread/FastMutex.h" #include "zthread/FastMutex.h"