2010-03-27 08:09:34 +01:00
|
|
|
// ==============================================================
|
|
|
|
// This file is part of Glest (www.glest.org)
|
|
|
|
//
|
2011-12-14 08:40:48 +01:00
|
|
|
// Copyright (C) 2001-2008 Martiño Figueroa
|
2010-03-27 08:09:34 +01:00
|
|
|
//
|
2010-12-14 00:39:24 +01:00
|
|
|
// You can redistribute this code and/or modify it under
|
|
|
|
// the terms of the GNU General Public License as published
|
|
|
|
// by the Free Software Foundation; either version 2 of the
|
2010-03-27 08:09:34 +01:00
|
|
|
// License, or (at your option) any later version
|
|
|
|
// ==============================================================
|
|
|
|
|
|
|
|
#include "network_manager.h"
|
2010-06-24 03:23:18 +02:00
|
|
|
#include "util.h"
|
2010-03-27 08:09:34 +01:00
|
|
|
#include "leak_dumper.h"
|
|
|
|
|
2010-06-24 03:23:18 +02:00
|
|
|
using namespace Shared::Util;
|
|
|
|
|
2010-03-27 08:09:34 +01:00
|
|
|
namespace Glest{ namespace Game{
|
|
|
|
|
|
|
|
// =====================================================
|
|
|
|
// class NetworkManager
|
|
|
|
// =====================================================
|
|
|
|
|
|
|
|
NetworkManager &NetworkManager::getInstance(){
|
|
|
|
static NetworkManager networkManager;
|
|
|
|
return networkManager;
|
|
|
|
}
|
|
|
|
|
2010-06-24 03:23:18 +02:00
|
|
|
NetworkManager::NetworkManager() {
|
2011-03-28 23:04:47 +02:00
|
|
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
2010-06-24 03:23:18 +02:00
|
|
|
|
2010-03-27 08:09:34 +01:00
|
|
|
gameNetworkInterface= NULL;
|
|
|
|
networkRole= nrIdle;
|
2010-06-24 03:23:18 +02:00
|
|
|
|
2011-03-28 05:54:23 +02:00
|
|
|
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface);
|
2010-03-27 08:09:34 +01:00
|
|
|
}
|
|
|
|
|
2013-05-17 04:14:43 +02:00
|
|
|
NetworkManager::~NetworkManager() {
|
|
|
|
end();
|
|
|
|
}
|
|
|
|
|
2011-03-11 13:14:24 +01:00
|
|
|
void NetworkManager::init(NetworkRole networkRole, bool publishEnabled) {
|
2011-03-28 23:04:47 +02:00
|
|
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
2011-03-28 05:54:23 +02:00
|
|
|
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d, networkRole = %d, gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,networkRole,gameNetworkInterface);
|
2010-06-24 03:23:18 +02:00
|
|
|
|
2014-01-01 19:59:55 +01:00
|
|
|
//assert(gameNetworkInterface==NULL);
|
|
|
|
NetworkMessage::resetNetworkPacketStats();
|
2010-03-27 08:09:34 +01:00
|
|
|
this->networkRole = networkRole;
|
|
|
|
|
2011-01-02 10:33:37 +01:00
|
|
|
if(networkRole == nrServer) {
|
2011-03-28 05:54:23 +02:00
|
|
|
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d, networkRole = %d, gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,networkRole,gameNetworkInterface);
|
2014-01-02 22:56:37 +01:00
|
|
|
gameNetworkInterface = new ServerInterface(publishEnabled,NULL);
|
2010-03-27 08:09:34 +01:00
|
|
|
}
|
2010-06-24 03:23:18 +02:00
|
|
|
else {
|
2011-03-28 05:54:23 +02:00
|
|
|
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d, networkRole = %d, gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,networkRole,gameNetworkInterface);
|
2010-03-27 08:09:34 +01:00
|
|
|
gameNetworkInterface = new ClientInterface();
|
|
|
|
}
|
2010-06-24 03:23:18 +02:00
|
|
|
|
2011-01-02 10:33:37 +01:00
|
|
|
//printf("==========] CREATING gameNetworkInterface [%p]\n",gameNetworkInterface);
|
|
|
|
|
2011-03-28 05:54:23 +02:00
|
|
|
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface);
|
2010-03-27 08:09:34 +01:00
|
|
|
}
|
|
|
|
|
2014-01-02 22:56:37 +01:00
|
|
|
void NetworkManager::initServerInterfaces(ClientLagCallbackInterface *intf) {
|
|
|
|
ServerInterface *server = getServerInterface();
|
|
|
|
server->setClientLagCallbackInterface(intf);
|
|
|
|
}
|
|
|
|
|
2010-06-24 03:23:18 +02:00
|
|
|
void NetworkManager::end() {
|
2011-03-28 23:04:47 +02:00
|
|
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
2011-03-28 05:54:23 +02:00
|
|
|
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface);
|
2011-10-23 02:55:34 +02:00
|
|
|
//printf("In [%s::%s] Line: %d gameNetworkInterface = %p\n",__FILE__,__FUNCTION__,__LINE__,gameNetworkInterface);
|
2011-01-02 10:33:37 +01:00
|
|
|
//printf("==========] DELETING gameNetworkInterface [%p]\n",gameNetworkInterface);
|
2010-06-24 03:23:18 +02:00
|
|
|
|
2010-03-27 08:09:34 +01:00
|
|
|
delete gameNetworkInterface;
|
|
|
|
gameNetworkInterface= NULL;
|
|
|
|
networkRole= nrIdle;
|
2010-06-24 03:23:18 +02:00
|
|
|
|
2011-03-28 23:04:47 +02:00
|
|
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
2011-03-28 05:54:23 +02:00
|
|
|
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface);
|
2010-03-27 08:09:34 +01:00
|
|
|
}
|
|
|
|
|
2011-01-02 10:33:37 +01:00
|
|
|
void NetworkManager::update() {
|
|
|
|
if(gameNetworkInterface!=NULL) {
|
2010-03-27 08:09:34 +01:00
|
|
|
gameNetworkInterface->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-02 10:33:37 +01:00
|
|
|
bool NetworkManager::isNetworkGame() {
|
2012-07-07 07:35:25 +02:00
|
|
|
return networkRole==nrClient || (networkRole==nrServer && getServerInterface()->getSlotCount() > 0);
|
2010-03-27 08:09:34 +01:00
|
|
|
}
|
|
|
|
|
2013-02-26 21:48:20 +01:00
|
|
|
bool NetworkManager::isNetworkGameWithConnectedClients() {
|
|
|
|
return networkRole==nrClient || (networkRole==nrServer && getServerInterface()->getConnectedSlotCount(true) > 0);
|
|
|
|
}
|
|
|
|
|
2011-01-02 10:33:37 +01:00
|
|
|
GameNetworkInterface* NetworkManager::getGameNetworkInterface(bool throwErrorOnNull) {
|
|
|
|
if(throwErrorOnNull) {
|
2012-11-15 16:08:30 +01:00
|
|
|
//assert(gameNetworkInterface!=NULL);
|
2011-01-02 10:33:37 +01:00
|
|
|
|
|
|
|
if(gameNetworkInterface==NULL) {
|
2012-04-14 23:21:09 +02:00
|
|
|
throw megaglest_runtime_error("gameNetworkInterface==NULL");
|
2011-01-02 10:33:37 +01:00
|
|
|
}
|
|
|
|
}
|
2010-03-27 08:09:34 +01:00
|
|
|
return gameNetworkInterface;
|
|
|
|
}
|
|
|
|
|
2011-01-02 10:33:37 +01:00
|
|
|
ServerInterface* NetworkManager::getServerInterface(bool throwErrorOnNull) {
|
|
|
|
if(throwErrorOnNull) {
|
2012-11-15 16:08:30 +01:00
|
|
|
//assert(gameNetworkInterface!=NULL);
|
2011-01-02 10:33:37 +01:00
|
|
|
if(gameNetworkInterface==NULL) {
|
2012-04-14 23:21:09 +02:00
|
|
|
throw megaglest_runtime_error("gameNetworkInterface==NULL");
|
2011-01-02 10:33:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(networkRole==nrServer);
|
|
|
|
if(networkRole!=nrServer) {
|
2012-04-14 23:21:09 +02:00
|
|
|
throw megaglest_runtime_error("networkRole!=nrServer");
|
2011-01-02 10:33:37 +01:00
|
|
|
}
|
|
|
|
}
|
2010-08-26 21:01:44 +02:00
|
|
|
return dynamic_cast<ServerInterface*>(gameNetworkInterface);
|
2010-03-27 08:09:34 +01:00
|
|
|
}
|
|
|
|
|
2011-01-02 10:33:37 +01:00
|
|
|
ClientInterface* NetworkManager::getClientInterface(bool throwErrorOnNull) {
|
2011-04-24 06:22:19 +02:00
|
|
|
//if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface);
|
2011-01-02 10:33:37 +01:00
|
|
|
|
|
|
|
if(throwErrorOnNull) {
|
2012-11-15 16:08:30 +01:00
|
|
|
//assert(gameNetworkInterface!=NULL);
|
2011-01-02 10:33:37 +01:00
|
|
|
if(gameNetworkInterface==NULL) {
|
2012-04-14 23:21:09 +02:00
|
|
|
throw megaglest_runtime_error("gameNetworkInterface==NULL");
|
2011-01-02 10:33:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(networkRole==nrClient);
|
|
|
|
if(networkRole!=nrClient) {
|
2012-04-14 23:21:09 +02:00
|
|
|
throw megaglest_runtime_error("networkRole!=nrClient");
|
2011-01-02 10:33:37 +01:00
|
|
|
}
|
|
|
|
}
|
2010-08-26 21:01:44 +02:00
|
|
|
return dynamic_cast<ClientInterface*>(gameNetworkInterface);
|
2010-03-27 08:09:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}}//end namespace
|