2011-07-02 14:45:12 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
|
* PokerTH - The open source texas holdem engine *
|
|
|
|
|
* Copyright (C) 2006-2011 Felix Hammer, Florian Thauer, Lothar May *
|
|
|
|
|
* *
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as *
|
|
|
|
|
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License *
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
|
*****************************************************************************/
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2008-04-16 20:38:11 +00:00
|
|
|
#include <net/netpacket.h>
|
2007-11-16 15:24:25 +00:00
|
|
|
#include "session.h"
|
|
|
|
|
#include "configfile.h"
|
2007-12-11 19:35:02 +00:00
|
|
|
#include <qttoolsinterface.h>
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <gui/generic/serverguiwrapper.h>
|
|
|
|
|
#include <net/socket_startup.h>
|
|
|
|
|
#include <core/loghelper.h>
|
2007-11-16 22:34:38 +00:00
|
|
|
#include <core/thread.h>
|
2008-02-10 12:38:12 +00:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
#include <boost/filesystem.hpp>
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2008-04-16 20:38:11 +00:00
|
|
|
#include <iostream>
|
2008-02-10 13:35:13 +00:00
|
|
|
#include <fstream>
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <csignal>
|
|
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2011-02-11 20:02:08 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
|
#define _CRTDBG_MAP_ALLOC
|
|
|
|
|
#include <crtdbg.h>
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2011-02-11 20:02:08 +00:00
|
|
|
#define ENABLE_LEAK_CHECK() \
|
2007-11-16 15:24:25 +00:00
|
|
|
{ \
|
|
|
|
|
int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); \
|
|
|
|
|
tmpFlag |= _CRTDBG_LEAK_CHECK_DF; \
|
|
|
|
|
_CrtSetDbgFlag(tmpFlag); \
|
|
|
|
|
}
|
2011-02-11 20:02:08 +00:00
|
|
|
#endif
|
2007-11-16 15:24:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef ENABLE_LEAK_CHECK
|
2011-02-11 20:02:08 +00:00
|
|
|
#define ENABLE_LEAK_CHECK()
|
2007-11-16 15:24:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
2008-02-10 12:38:12 +00:00
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
using namespace boost::filesystem;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
volatile int g_pokerthTerminate = 0;
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
TerminateHandler(int /*signal*/)
|
|
|
|
|
{
|
|
|
|
|
g_pokerthTerminate = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Hack
|
2008-02-10 12:38:12 +00:00
|
|
|
#ifdef _WIN32
|
2011-02-11 20:02:08 +00:00
|
|
|
#include <process.h>
|
2008-02-10 12:38:12 +00:00
|
|
|
#else
|
2011-02-11 20:02:08 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#ifndef daemon
|
|
|
|
|
int daemon(int, int);
|
|
|
|
|
#endif
|
2007-11-16 15:24:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
ENABLE_LEAK_CHECK();
|
|
|
|
|
|
2011-02-28 23:06:23 +00:00
|
|
|
//_CrtSetBreakAlloc(10260);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2008-02-10 12:38:12 +00:00
|
|
|
bool readonlyConfig = false;
|
|
|
|
|
string pidFile;
|
2008-03-19 23:39:59 +00:00
|
|
|
int logLevel = 1;
|
2008-02-10 12:38:12 +00:00
|
|
|
{
|
|
|
|
|
// Check command line options.
|
|
|
|
|
po::options_description desc("Allowed options");
|
|
|
|
|
desc.add_options()
|
2011-02-11 20:02:08 +00:00
|
|
|
("help,h", "produce help message")
|
|
|
|
|
("version,v", "print version string")
|
|
|
|
|
("log-level,l", po::value<int>(), "set log level (0=minimal, 1=default, 2=verbose)")
|
|
|
|
|
("pid-file,p", po::value<string>(), "create pid-file in different location")
|
|
|
|
|
("readonly-config", "treat config file as read-only")
|
|
|
|
|
;
|
2008-02-10 12:38:12 +00:00
|
|
|
|
2008-02-10 12:48:49 +00:00
|
|
|
po::variables_map vm;
|
|
|
|
|
po::store(po::parse_command_line(argc, argv, desc), vm);
|
|
|
|
|
po::notify(vm);
|
|
|
|
|
|
2011-02-11 20:02:08 +00:00
|
|
|
if (vm.count("help")) {
|
2008-02-10 12:48:49 +00:00
|
|
|
cout << desc << endl;
|
2008-02-10 12:38:12 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
2011-02-11 20:02:08 +00:00
|
|
|
if (vm.count("version")) {
|
2008-02-10 12:38:12 +00:00
|
|
|
cout << "PokerTH server version " << POKERTH_BETA_RELEASE_STRING << endl
|
|
|
|
|
<< "Network protocol version " << NET_VERSION_MAJOR << "." << NET_VERSION_MINOR << endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2011-02-11 20:02:08 +00:00
|
|
|
if (vm.count("log-level")) {
|
2008-03-19 23:39:59 +00:00
|
|
|
logLevel = vm["log-level"].as<int>();
|
2011-02-11 20:02:08 +00:00
|
|
|
if (logLevel < 0 || logLevel > 2) {
|
2008-05-21 17:31:09 +00:00
|
|
|
cout << "Invalid log-level: \"" << logLevel << "\", allowed range 0-2." << endl;
|
2008-03-19 23:39:59 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-02-10 12:38:12 +00:00
|
|
|
if (vm.count("pid-file"))
|
|
|
|
|
pidFile = vm["pid-file"].as<string>();
|
|
|
|
|
if (vm.count("readonly-config"))
|
|
|
|
|
readonlyConfig = true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-28 23:06:23 +00:00
|
|
|
boost::shared_ptr<QtToolsInterface> myQtToolsInterface(CreateQtToolsWrapper());
|
2007-11-16 15:24:25 +00:00
|
|
|
//create defaultconfig
|
2011-02-28 23:06:23 +00:00
|
|
|
boost::shared_ptr<ConfigFile> myConfig(new ConfigFile(argv[0], readonlyConfig));
|
2008-03-19 23:39:59 +00:00
|
|
|
loghelper_init(myQtToolsInterface->stringFromUtf8(myConfig->readConfigString("LogDir")), logLevel);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
// TODO: Hack
|
|
|
|
|
#ifndef _WIN32
|
2011-12-30 20:56:58 +00:00
|
|
|
#ifdef QT_NO_DEBUG
|
2011-02-11 20:02:08 +00:00
|
|
|
if (daemon(0, 0) != 0) {
|
2011-01-14 23:21:36 +00:00
|
|
|
cout << "Failed to start daemon." << endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2011-12-30 20:56:58 +00:00
|
|
|
#endif
|
2007-11-16 15:24:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
signal(SIGTERM, TerminateHandler);
|
|
|
|
|
signal(SIGINT, TerminateHandler);
|
|
|
|
|
|
|
|
|
|
socket_startup();
|
|
|
|
|
|
|
|
|
|
LOG_MSG("Starting PokerTH dedicated server. Availability: IPv6 "
|
2011-02-11 20:02:08 +00:00
|
|
|
<< socket_has_ipv6() << ", SCTP " << socket_has_sctp() << ", Dual Stack " << socket_has_dual_stack() << ".");
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2008-02-10 12:38:12 +00:00
|
|
|
// Store pid in file.
|
2011-02-11 20:02:08 +00:00
|
|
|
if (pidFile.empty()) {
|
2008-02-10 12:38:12 +00:00
|
|
|
path tmpPidPath(myConfig->readConfigString("LogDir"));
|
|
|
|
|
tmpPidPath /= "pokerth.pid";
|
|
|
|
|
pidFile = tmpPidPath.directory_string();
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
ofstream pidStream(pidFile.c_str(), ios_base::out | ios_base::trunc);
|
|
|
|
|
if (!pidStream.fail())
|
2011-04-10 10:17:30 +00:00
|
|
|
pidStream << getpid();
|
2008-02-10 12:38:12 +00:00
|
|
|
else
|
|
|
|
|
LOG_ERROR("Could not create process id file \"" << pidFile << "\"!");
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
// Create pseudo Gui Wrapper for the server.
|
2011-02-28 23:06:23 +00:00
|
|
|
boost::shared_ptr<GuiInterface> myServerGuiInterface(new ServerGuiWrapper(myConfig.get(), NULL, NULL, NULL));
|
|
|
|
|
boost::shared_ptr<Session> session(new Session(myServerGuiInterface.get(), myConfig.get()));
|
2007-11-16 15:24:25 +00:00
|
|
|
if (!session->init())
|
|
|
|
|
LOG_ERROR("Missing files - please check your directory settings!");
|
|
|
|
|
myServerGuiInterface->setSession(session);
|
|
|
|
|
|
2009-11-08 09:48:01 +00:00
|
|
|
myServerGuiInterface->getSession()->startNetworkServer(true);
|
2011-02-11 20:02:08 +00:00
|
|
|
while (!g_pokerthTerminate) {
|
2007-11-16 22:34:38 +00:00
|
|
|
Thread::Msleep(100);
|
2008-10-07 21:16:27 +00:00
|
|
|
if (myServerGuiInterface->getSession()->pollNetworkServerTerminated())
|
2007-11-16 15:24:25 +00:00
|
|
|
g_pokerthTerminate = true;
|
|
|
|
|
}
|
2008-10-07 21:16:27 +00:00
|
|
|
myServerGuiInterface->getSession()->terminateNetworkServer();
|
2011-02-28 23:06:23 +00:00
|
|
|
session.reset();
|
|
|
|
|
myServerGuiInterface.reset();
|
|
|
|
|
myConfig.reset();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2007-11-22 17:25:55 +00:00
|
|
|
LOG_MSG("Terminating PokerTH dedicated server." << endl);
|
2007-11-16 15:24:25 +00:00
|
|
|
socket_cleanup();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|