1
0
Fork 0

Make MultiPlayer a well-behaved subsystem.

This commit is contained in:
James Turner 2010-10-02 23:40:38 +01:00
parent d39841d2df
commit 0c342308eb
8 changed files with 34 additions and 52 deletions

View file

@ -1397,6 +1397,14 @@ bool fgInitSubsystems() {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
globals->add_subsystem("atis", new FGAtisManager, SGSubsystemMgr::POST_FDM); globals->add_subsystem("atis", new FGAtisManager, SGSubsystemMgr::POST_FDM);
#endif #endif
////////////////////////////////////////////////////////////////////
// Initialize multiplayer subsystem
////////////////////////////////////////////////////////////////////
globals->add_subsystem("mp", new FGMultiplayMgr, SGSubsystemMgr::POST_FDM);
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Initialise the AI Model Manager // Initialise the AI Model Manager
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -1465,13 +1473,6 @@ bool fgInitSubsystems() {
globals->get_subsystem_mgr()->bind(); globals->get_subsystem_mgr()->bind();
globals->get_subsystem_mgr()->init(); globals->get_subsystem_mgr()->init();
////////////////////////////////////////////////////////////////////
// Initialize multiplayer subsystem
////////////////////////////////////////////////////////////////////
globals->set_multiplayer_mgr(new FGMultiplayMgr);
globals->get_multiplayer_mgr()->init();
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// Initialize the Nasal interpreter. // Initialize the Nasal interpreter.
// Do this last, so that the loaded scripts see initialized state // Do this last, so that the loaded scripts see initialized state

View file

@ -292,6 +292,10 @@ int fgOSMainLoop()
int fgGetKeyModifiers() int fgGetKeyModifiers()
{ {
if (!globals->get_renderer()) { // happens during shutdown
return 0;
}
return globals->get_renderer()->getEventHandler()->getCurrentModifiers(); return globals->get_renderer()->getEventHandler()->getCurrentModifiers();
} }

View file

@ -150,8 +150,8 @@ FGGlobals::FGGlobals() :
tacanlist( NULL ), tacanlist( NULL ),
carrierlist( NULL ), carrierlist( NULL ),
channellist( NULL ), channellist( NULL ),
airwaynet( NULL ), airwaynet( NULL )
multiplayer_mgr( NULL )
{ {
simgear::ResourceManager::instance()->addProvider(new AircraftResourceProvider()); simgear::ResourceManager::instance()->addProvider(new AircraftResourceProvider());
} }
@ -161,6 +161,8 @@ FGGlobals::FGGlobals() :
FGGlobals::~FGGlobals() FGGlobals::~FGGlobals()
{ {
delete renderer; delete renderer;
renderer = NULL;
// The AIModels manager performs a number of actions upon // The AIModels manager performs a number of actions upon
// Shutdown that implicitly assume that other subsystems // Shutdown that implicitly assume that other subsystems
// are still operational (Due to the dynamic allocation and // are still operational (Due to the dynamic allocation and
@ -168,16 +170,11 @@ FGGlobals::~FGGlobals()
// shut down all subsystems, make sure we take down the // shut down all subsystems, make sure we take down the
// AIModels system first. // AIModels system first.
subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("ai_model"); subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("ai_model");
// FGInput (FGInputEvent) and FGDialog calls get_subsystem() in their destructors,
// which is not safe since some subsystem are already deleted but can be referred.
// So these subsystems must be deleted prior to deleting subsystem_mgr unless
// ~SGSubsystemGroup and SGSubsystemMgr::get_subsystem are changed not to refer to
// deleted subsystems.
subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("input");
subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("gui");
subsystem_mgr->unbind(); subsystem_mgr->unbind();
delete subsystem_mgr; delete subsystem_mgr;
delete event_mgr; delete event_mgr;
delete time_params; delete time_params;
delete mag; delete mag;
delete matlib; delete matlib;
@ -203,7 +200,6 @@ FGGlobals::~FGGlobals()
delete carrierlist; delete carrierlist;
delete channellist; delete channellist;
delete airwaynet; delete airwaynet;
delete multiplayer_mgr;
soundmgr->unbind(); soundmgr->unbind();
delete soundmgr; delete soundmgr;

View file

@ -66,7 +66,6 @@ class FGLight;
class FGModelMgr; class FGModelMgr;
class FGRouteMgr; class FGRouteMgr;
class FGScenery; class FGScenery;
class FGMultiplayMgr;
class FGPanel; class FGPanel;
class FGTileMgr; class FGTileMgr;
class FGViewMgr; class FGViewMgr;
@ -175,9 +174,6 @@ private:
FGTACANList *channellist; FGTACANList *channellist;
FGAirwayNetwork *airwaynet; FGAirwayNetwork *airwaynet;
//Mulitplayer managers
FGMultiplayMgr *multiplayer_mgr;
/// roots of Aircraft trees /// roots of Aircraft trees
string_list fg_aircraft_dirs; string_list fg_aircraft_dirs;
public: public:
@ -289,13 +285,6 @@ public:
model_mgr = mgr; model_mgr = mgr;
} }
inline FGMultiplayMgr *get_multiplayer_mgr () { return multiplayer_mgr; }
inline void set_multiplayer_mgr (FGMultiplayMgr * mgr)
{
multiplayer_mgr = mgr;
}
inline string_list *get_channel_options_list () { inline string_list *get_channel_options_list () {
return channel_options_list; return channel_options_list;
} }

View file

@ -146,11 +146,6 @@ static void fgMainLoop( void ) {
altitude->getDoubleValue() * SG_FEET_TO_METER, altitude->getDoubleValue() * SG_FEET_TO_METER,
globals->get_time_params()->getJD() ); globals->get_time_params()->getJD() );
// Update any multiplayer's network queues, the AIMultiplayer
// implementation is an AI model and depends on that
globals->get_multiplayer_mgr()->Update();
#if ENABLE_ATCDCL #if ENABLE_ATCDCL
// Run ATC subsystem // Run ATC subsystem
if (fgGetBool("/sim/atc/enabled")) if (fgGetBool("/sim/atc/enabled"))

View file

@ -366,7 +366,7 @@ FGMultiplayMgr::~FGMultiplayMgr()
// Initialise object // Initialise object
// //
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
bool void
FGMultiplayMgr::init (void) FGMultiplayMgr::init (void)
{ {
////////////////////////////////////////////////// //////////////////////////////////////////////////
@ -374,7 +374,7 @@ FGMultiplayMgr::init (void)
////////////////////////////////////////////////// //////////////////////////////////////////////////
if (mInitialised) { if (mInitialised) {
SG_LOG(SG_NETWORK, SG_WARN, "FGMultiplayMgr::init - already initialised"); SG_LOG(SG_NETWORK, SG_WARN, "FGMultiplayMgr::init - already initialised");
return false; return;
} }
////////////////////////////////////////////////// //////////////////////////////////////////////////
// Set members from property values // Set members from property values
@ -400,7 +400,7 @@ FGMultiplayMgr::init (void)
if (rxPort <= 0) { if (rxPort <= 0) {
SG_LOG(SG_NETWORK, SG_DEBUG, SG_LOG(SG_NETWORK, SG_DEBUG,
"FGMultiplayMgr - No receiver port, Multiplayermode disabled"); "FGMultiplayMgr - No receiver port, Multiplayermode disabled");
return (false); return;
} }
if (mCallsign.empty()) if (mCallsign.empty())
mCallsign = "JohnDoe"; // FIXME: use getpwuid mCallsign = "JohnDoe"; // FIXME: use getpwuid
@ -415,17 +415,17 @@ FGMultiplayMgr::init (void)
if (!mSocket->open(false)) { if (!mSocket->open(false)) {
SG_LOG( SG_NETWORK, SG_DEBUG, SG_LOG( SG_NETWORK, SG_DEBUG,
"FGMultiplayMgr::init - Failed to create data socket" ); "FGMultiplayMgr::init - Failed to create data socket" );
return false; return;
} }
mSocket->setBlocking(false); mSocket->setBlocking(false);
if (mSocket->bind(rxAddress.c_str(), rxPort) != 0) { if (mSocket->bind(rxAddress.c_str(), rxPort) != 0) {
perror("bind"); perror("bind");
SG_LOG( SG_NETWORK, SG_DEBUG, SG_LOG( SG_NETWORK, SG_DEBUG,
"FGMultiplayMgr::Open - Failed to bind receive socket" ); "FGMultiplayMgr::Open - Failed to bind receive socket" );
return false; return;
} }
mInitialised = true; mInitialised = true;
return true;
} // FGMultiplayMgr::init() } // FGMultiplayMgr::init()
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
@ -710,7 +710,7 @@ FGMultiplayMgr::SendTextMessage(const string &MsgText)
// //
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
void void
FGMultiplayMgr::Update(void) FGMultiplayMgr::update(double)
{ {
if (!mInitialised) if (!mInitialised)
return; return;

View file

@ -33,25 +33,20 @@
#include "mpmessages.hxx" #include "mpmessages.hxx"
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string> #include <string>
using std::string;
#include <vector> #include <vector>
using std::vector;
#include <simgear/compiler.h> #include <simgear/compiler.h>
#include <simgear/props/props.hxx> #include <simgear/props/props.hxx>
#include <plib/netSocket.h> #include <plib/netSocket.h>
#include <Main/globals.hxx> #include <Main/globals.hxx>
#include <simgear/structure/subsystem_mgr.hxx>
#include <AIModel/AIMultiplayer.hxx> #include <AIModel/AIMultiplayer.hxx>
struct FGExternalMotionInfo; struct FGExternalMotionInfo;
class FGMultiplayMgr class FGMultiplayMgr : public SGSubsystem
{ {
public: public:
@ -67,13 +62,15 @@ public:
FGMultiplayMgr(); FGMultiplayMgr();
~FGMultiplayMgr(); ~FGMultiplayMgr();
bool init(void);
virtual void init(void);
virtual void update(double dt);
void Close(void); void Close(void);
// transmitter // transmitter
void SendMyPosition(const FGExternalMotionData& motionInfo); void SendMyPosition(const FGExternalMotionData& motionInfo);
void SendTextMessage(const string &sMsgText); void SendTextMessage(const string &sMsgText);
// receiver // receiver
void Update(void);
private: private:
union MsgBuf; union MsgBuf;
@ -93,7 +90,7 @@ private:
netAddress mServer; netAddress mServer;
bool mHaveServer; bool mHaveServer;
bool mInitialised; bool mInitialised;
string mCallsign; std::string mCallsign;
}; };
#endif #endif

View file

@ -243,7 +243,7 @@ bool FGMultiplay::process() {
motionInfo.properties.push_back(pData); motionInfo.properties.push_back(pData);
} }
FGMultiplayMgr* mpmgr = globals->get_multiplayer_mgr(); FGMultiplayMgr* mpmgr = (FGMultiplayMgr*) globals->get_subsystem("mp");
mpmgr->SendMyPosition(motionInfo); mpmgr->SendMyPosition(motionInfo);
// Now remove the data // Now remove the data
@ -272,7 +272,7 @@ bool FGMultiplay::process() {
******************************************************************/ ******************************************************************/
bool FGMultiplay::close() { bool FGMultiplay::close() {
FGMultiplayMgr *mgr = globals->get_multiplayer_mgr(); FGMultiplayMgr* mgr = (FGMultiplayMgr*) globals->get_subsystem("mp");
if (mgr == 0) { if (mgr == 0) {
return false; return false;