2007-01-13 02:40:33 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* Copyright (C) 2006 by FThauer FHammer *
|
|
|
|
|
* f.thauer@web.de *
|
|
|
|
|
* *
|
|
|
|
|
* This program is free software; you can redistribute it 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 License, or *
|
|
|
|
|
* (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
#include "session.h"
|
|
|
|
|
#include "game.h"
|
|
|
|
|
#include "guiinterface.h"
|
2007-01-21 14:20:15 +00:00
|
|
|
#include "configfile.h"
|
2007-02-18 13:45:56 +00:00
|
|
|
#include <net/clientthread.h>
|
2007-02-25 23:16:26 +00:00
|
|
|
#include <net/serverthread.h>
|
2007-01-21 14:20:15 +00:00
|
|
|
|
2007-02-18 13:45:56 +00:00
|
|
|
#define NET_CLIENT_TERMINATE_TIMEOUT_MSEC 1000
|
2007-02-25 23:16:26 +00:00
|
|
|
#define NET_SERVER_TERMINATE_TIMEOUT_MSEC 1000
|
2007-01-13 02:40:33 +00:00
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
2007-04-20 23:14:08 +00:00
|
|
|
Session::Session(GuiInterface *g)
|
2007-04-26 21:11:13 +00:00
|
|
|
: currentGameID(0), myNetClient(0), myNetServer(0), actualGame(0), myGui(g)
|
2007-02-18 13:45:56 +00:00
|
|
|
{
|
2007-05-05 19:02:11 +00:00
|
|
|
|
2007-04-20 23:14:08 +00:00
|
|
|
myConfig = new ConfigFile;
|
2007-01-13 02:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Session::~Session()
|
|
|
|
|
{
|
2007-02-18 13:45:56 +00:00
|
|
|
terminateNetworkClient();
|
2007-03-20 14:05:16 +00:00
|
|
|
terminateNetworkServer();
|
2007-02-18 13:45:56 +00:00
|
|
|
deleteGame();
|
|
|
|
|
delete myConfig;
|
2007-04-21 20:42:50 +00:00
|
|
|
myConfig = 0;
|
2007-01-13 02:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-26 23:07:08 +00:00
|
|
|
void Session::startGame(const GameData &gameData) {
|
2007-01-13 02:40:33 +00:00
|
|
|
|
2007-05-02 21:20:54 +00:00
|
|
|
myGui->setGame(0);
|
|
|
|
|
myGui->initGui(gameData.guiSpeed);
|
|
|
|
|
deleteGame();
|
|
|
|
|
|
2007-04-26 21:11:13 +00:00
|
|
|
currentGameID++;
|
2007-01-13 02:40:33 +00:00
|
|
|
|
2007-04-26 21:11:13 +00:00
|
|
|
PlayerDataList playerDataList;
|
2007-04-27 22:55:19 +00:00
|
|
|
for(int i=0; i<gameData.numberOfPlayers; i++) {
|
2007-04-26 21:11:13 +00:00
|
|
|
|
|
|
|
|
//Namen und Avatarpfad abfragen
|
|
|
|
|
ostringstream myName;
|
|
|
|
|
if (i==0) { myName << "MyName"; }
|
|
|
|
|
else { myName << "Opponent" << i << "Name"; }
|
|
|
|
|
ostringstream myAvatar;
|
|
|
|
|
if (i==0) { myAvatar << "MyAvatar"; }
|
|
|
|
|
else { myAvatar << "Opponent" << i << "Avatar"; }
|
|
|
|
|
|
|
|
|
|
//PlayerData erzeugen
|
|
|
|
|
// TODO: PlayerType setzen
|
2007-04-26 23:07:08 +00:00
|
|
|
// UniqueId = PlayerNumber for local games.
|
|
|
|
|
boost::shared_ptr<PlayerData> playerData(new PlayerData(i));
|
2007-04-26 21:11:13 +00:00
|
|
|
playerData->SetName(myConfig->readConfigString(myName.str()));
|
|
|
|
|
playerData->SetAvatarFile(myConfig->readConfigString(myAvatar.str()));
|
|
|
|
|
|
|
|
|
|
playerDataList.push_back(playerData);
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-26 23:07:08 +00:00
|
|
|
actualGame = new Game(myGui, playerDataList, gameData, currentGameID);
|
2007-05-02 21:20:54 +00:00
|
|
|
myGui->setGame(actualGame);
|
2007-01-13 02:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Session::deleteGame() {
|
|
|
|
|
|
|
|
|
|
delete actualGame;
|
|
|
|
|
actualGame = 0;
|
|
|
|
|
|
|
|
|
|
}
|
2007-02-18 13:45:56 +00:00
|
|
|
|
|
|
|
|
void Session::startNetworkClient(const string &serverAddress, unsigned serverPort, bool ipv6, const string &pwd)
|
|
|
|
|
{
|
|
|
|
|
if (myNetClient || !myGui)
|
|
|
|
|
return; // TODO: throw exception
|
|
|
|
|
myNetClient = new ClientThread(*myGui);
|
2007-04-08 16:18:20 +00:00
|
|
|
myNetClient->Init(
|
|
|
|
|
serverAddress,
|
|
|
|
|
serverPort,
|
|
|
|
|
ipv6,
|
|
|
|
|
pwd,
|
|
|
|
|
myConfig->readConfigString("MyName"));
|
2007-02-18 13:45:56 +00:00
|
|
|
myNetClient->Run();
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-20 14:05:16 +00:00
|
|
|
void Session::startNetworkClientForLocalServer()
|
|
|
|
|
{
|
|
|
|
|
if (myNetClient || !myGui)
|
|
|
|
|
return; // TODO: throw exception
|
|
|
|
|
myNetClient = new ClientThread(*myGui);
|
|
|
|
|
myNetClient->Init(
|
|
|
|
|
"localhost",
|
|
|
|
|
myConfig->readConfigInt("ServerPort"),
|
|
|
|
|
myConfig->readConfigInt("ServerUseIpv6") == 1,
|
2007-04-08 16:18:20 +00:00
|
|
|
myConfig->readConfigString("ServerPassword"),
|
|
|
|
|
myConfig->readConfigString("MyName"));
|
2007-03-20 14:05:16 +00:00
|
|
|
myNetClient->Run();
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-18 13:45:56 +00:00
|
|
|
void Session::terminateNetworkClient()
|
|
|
|
|
{
|
|
|
|
|
if (!myNetClient)
|
|
|
|
|
return; // already terminated
|
|
|
|
|
myNetClient->SignalTermination();
|
|
|
|
|
// Give the thread some time to terminate.
|
|
|
|
|
if (myNetClient->Join(NET_CLIENT_TERMINATE_TIMEOUT_MSEC))
|
|
|
|
|
{
|
|
|
|
|
delete myNetClient;
|
|
|
|
|
}
|
|
|
|
|
// If termination fails, leave a memory leak to prevent a crash.
|
|
|
|
|
myNetClient = 0;
|
|
|
|
|
}
|
2007-02-25 23:16:26 +00:00
|
|
|
|
2007-04-26 23:07:08 +00:00
|
|
|
void Session::startNetworkServer(const GameData &gameData)
|
2007-02-25 23:16:26 +00:00
|
|
|
{
|
|
|
|
|
if (myNetServer)
|
|
|
|
|
return; // TODO: throw exception
|
2007-03-20 02:20:50 +00:00
|
|
|
myNetServer = new ServerThread(*myGui);
|
2007-03-13 23:27:17 +00:00
|
|
|
myNetServer->Init(
|
|
|
|
|
myConfig->readConfigInt("ServerPort"),
|
|
|
|
|
myConfig->readConfigInt("ServerUseIpv6") == 1,
|
2007-04-26 23:07:08 +00:00
|
|
|
myConfig->readConfigString("ServerPassword"),
|
|
|
|
|
gameData);
|
2007-02-25 23:16:26 +00:00
|
|
|
myNetServer->Run();
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-20 02:20:50 +00:00
|
|
|
void Session::initiateNetworkServerGame()
|
|
|
|
|
{
|
|
|
|
|
if (!myNetServer)
|
|
|
|
|
return; // TODO: throw exception
|
|
|
|
|
myNetServer->StartGame();
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-25 23:16:26 +00:00
|
|
|
void Session::terminateNetworkServer()
|
|
|
|
|
{
|
|
|
|
|
if (!myNetServer)
|
|
|
|
|
return; // already terminated
|
|
|
|
|
myNetServer->SignalTermination();
|
|
|
|
|
// Give the thread some time to terminate.
|
|
|
|
|
if (myNetServer->Join(NET_SERVER_TERMINATE_TIMEOUT_MSEC))
|
|
|
|
|
{
|
|
|
|
|
delete myNetServer;
|
|
|
|
|
}
|
|
|
|
|
// If termination fails, leave a memory leak to prevent a crash.
|
|
|
|
|
myNetServer = 0;
|
|
|
|
|
}
|
|
|
|
|
|