Added console server app. First try. Depends on qt for locale-specific stuff. Cannot start a game yet, only join is possible. Cannot run multiple games yet.

Not yet as service/daemon, because testing is easier with a simple console app.
This commit is contained in:
lotodore
2007-07-30 23:22:58 +00:00
parent 5d13dd7803
commit f11998c28c
12 changed files with 374 additions and 33 deletions
+17 -7
View File
@@ -86,14 +86,24 @@ Thread::Join(unsigned msecTimeout)
if (!IsRunning())
return true;
// Calculate time after timeout
boost::xtime t;
boost::xtime_get(&t, boost::TIME_UTC);
ADD_MSEC_TO_XTIME(t, msecTimeout);
bool tmpIsTerminated;
if (msecTimeout == THREAD_WAIT_INFINITE)
{
// Wait infinitely.
boost::timed_mutex::scoped_lock lock(m_isTerminatedMutex);
tmpIsTerminated = true;
}
else
{
// Calculate time after timeout
boost::xtime t;
boost::xtime_get(&t, boost::TIME_UTC);
ADD_MSEC_TO_XTIME(t, msecTimeout);
// Wait for the termination of the application code.
boost::timed_mutex::scoped_timed_lock lock(m_isTerminatedMutex, t);
bool tmpIsTerminated = lock.locked();
// Wait for the termination of the application code.
boost::timed_mutex::scoped_timed_lock lock(m_isTerminatedMutex, t);
tmpIsTerminated = lock.locked();
}
if (tmpIsTerminated)
{
+2
View File
@@ -29,6 +29,8 @@
#define NANOSECONDS_PER_SECOND 1000000000
#endif
#define THREAD_WAIT_INFINITE 0xFFFFFFFF
class Thread
{
public:
-1
View File
@@ -347,7 +347,6 @@ ClientStateStartSession::Process(ClientThread &client)
NetPacketJoinGame::Data initData;
initData.password = context.GetPassword();
initData.playerName = context.GetPlayerName();
initData.ptype = PLAYER_TYPE_HUMAN; // TODO
boost::shared_ptr<NetPacket> packet(new NetPacketJoinGame);
((NetPacketJoinGame *)packet.get())->SetData(initData);
+21 -6
View File
@@ -51,6 +51,7 @@ using namespace std;
#define NET_TYPE_ERROR 0x0400
#define NET_PLAYER_FLAG_HUMAN 0x01
#define NET_PLAYER_FLAG_ADMIN 0x02
#define NET_ERR_JOIN_GAME_VERSION_NOT_SUPPORTED 0x0001
#define NET_ERR_JOIN_GAME_SERVER_FULL 0x0002
@@ -83,9 +84,8 @@ struct GCC_PACKED NetPacketJoinGameData
u_int16_t requestedVersionMajor;
u_int16_t requestedVersionMinor;
u_int16_t passwordLength;
u_int16_t playerFlags;
u_int16_t playerNameLength;
u_int16_t reserved;
u_int32_t reserved;
};
struct GCC_PACKED NetPacketJoinGameAckData
@@ -93,11 +93,13 @@ struct GCC_PACKED NetPacketJoinGameAckData
NetPacketHeader head;
u_int32_t sessionId;
u_int16_t playerId;
u_int16_t playerFlags;
u_int16_t maxNumberOfPlayers;
u_int16_t smallBlind;
u_int16_t handsBeforeRaise;
u_int16_t proposedGuiSpeed;
u_int16_t playerActionTimeout;
u_int16_t reserved;
u_int32_t startMoney;
};
@@ -670,7 +672,6 @@ NetPacketJoinGame::SetData(const NetPacketJoinGame::Data &inData)
// Set the data.
tmpData->passwordLength = htons(passwordLen);
tmpData->playerFlags = htons((inData.ptype == PLAYER_TYPE_HUMAN) ? NET_PLAYER_FLAG_HUMAN : 0);
tmpData->playerNameLength = htons(playerNameLen);
char *passwordPtr = (char *)tmpData + sizeof(NetPacketJoinGameData);
memcpy(passwordPtr, inData.password.c_str(), passwordLen);
@@ -689,8 +690,6 @@ NetPacketJoinGame::GetData(NetPacketJoinGame::Data &outData) const
outData.versionMajor = ntohs(tmpData->requestedVersionMajor);
outData.versionMinor = ntohs(tmpData->requestedVersionMinor);
outData.ptype = (ntohs(tmpData->playerFlags) & NET_PLAYER_FLAG_HUMAN) ? PLAYER_TYPE_HUMAN : PLAYER_TYPE_COMPUTER;
u_int16_t passwordLen = ntohs(tmpData->passwordLength);
char *passwordPtr = (char *)tmpData + sizeof(NetPacketJoinGameData);
outData.password = string(passwordPtr, passwordLen);
@@ -766,8 +765,15 @@ NetPacketJoinGameAck::SetData(const NetPacketJoinGameAck::Data &inData)
{
NetPacketJoinGameAckData *tmpData = (NetPacketJoinGameAckData *)GetRawData();
u_int16_t tmpPlayerFlags = 0;
if (inData.ptype == PLAYER_TYPE_HUMAN)
tmpPlayerFlags |= NET_PLAYER_FLAG_HUMAN;
if (inData.prights == PLAYER_RIGHTS_ADMIN)
tmpPlayerFlags |= NET_PLAYER_FLAG_ADMIN;
tmpData->sessionId = htonl(inData.sessionId);
tmpData->playerId = htons(inData.yourPlayerUniqueId);
tmpData->playerFlags = htons(tmpPlayerFlags);
tmpData->maxNumberOfPlayers = htons(inData.gameData.maxNumberOfPlayers);
tmpData->smallBlind = htons(inData.gameData.smallBlind);
tmpData->handsBeforeRaise = htons(inData.gameData.handsBeforeRaise);
@@ -786,6 +792,8 @@ NetPacketJoinGameAck::GetData(NetPacketJoinGameAck::Data &outData) const
outData.sessionId = ntohl(tmpData->sessionId);
outData.yourPlayerUniqueId = ntohs(tmpData->playerId);
outData.ptype = (ntohs(tmpData->playerFlags) & NET_PLAYER_FLAG_HUMAN) ? PLAYER_TYPE_HUMAN : PLAYER_TYPE_COMPUTER;
outData.prights = (ntohs(tmpData->playerFlags) & NET_PLAYER_FLAG_ADMIN) ? PLAYER_RIGHTS_ADMIN : PLAYER_RIGHTS_NORMAL;
outData.gameData.maxNumberOfPlayers = ntohs(tmpData->maxNumberOfPlayers);
outData.gameData.smallBlind = ntohs(tmpData->smallBlind);
outData.gameData.handsBeforeRaise = ntohs(tmpData->handsBeforeRaise);
@@ -858,8 +866,14 @@ NetPacketPlayerJoined::SetData(const NetPacketPlayerJoined::Data &inData)
NetPacketPlayerJoinedData *tmpData = (NetPacketPlayerJoinedData *)GetRawData();
u_int16_t tmpPlayerFlags = 0;
if (inData.ptype == PLAYER_TYPE_HUMAN)
tmpPlayerFlags |= NET_PLAYER_FLAG_HUMAN;
if (inData.prights == PLAYER_RIGHTS_ADMIN)
tmpPlayerFlags |= NET_PLAYER_FLAG_ADMIN;
// Set the data.
tmpData->playerFlags = htons((inData.ptype == PLAYER_TYPE_HUMAN) ? NET_PLAYER_FLAG_HUMAN : 0);
tmpData->playerFlags = htons(tmpPlayerFlags);
tmpData->playerId = htons(inData.playerId);
tmpData->playerNameLength = htons(playerNameLen);
char *namePtr = (char *)tmpData + sizeof(NetPacketPlayerJoinedData);
@@ -876,6 +890,7 @@ NetPacketPlayerJoined::GetData(NetPacketPlayerJoined::Data &outData) const
NetPacketPlayerJoinedData *tmpData = (NetPacketPlayerJoinedData *)GetRawData();
outData.ptype = (ntohs(tmpData->playerFlags) & NET_PLAYER_FLAG_HUMAN) ? PLAYER_TYPE_HUMAN : PLAYER_TYPE_COMPUTER;
outData.prights = (ntohs(tmpData->playerFlags) & NET_PLAYER_FLAG_ADMIN) ? PLAYER_RIGHTS_ADMIN : PLAYER_RIGHTS_NORMAL;
outData.playerId = ntohs(tmpData->playerId);
char *namePtr = (char *)tmpData + sizeof(NetPacketPlayerJoinedData);
outData.playerName = string(namePtr, ntohs(tmpData->playerNameLength));
+1 -1
View File
@@ -290,7 +290,7 @@ ServerRecvStateInit::InternalProcess(ServerRecvThread &server, SessionWrapper se
// Create player data object.
boost::shared_ptr<PlayerData> tmpPlayerData(
new PlayerData(m_curUniquePlayerId++, 0, joinGameData.ptype));
new PlayerData(m_curUniquePlayerId++, 0, PLAYER_TYPE_HUMAN));
tmpPlayerData->SetName(joinGameData.playerName);
tmpPlayerData->SetNetSessionData(session.sessionData);
+9 -7
View File
@@ -120,7 +120,6 @@ public:
{
int versionMajor;
int versionMinor;
PlayerType ptype;
std::string playerName;
std::string password;
};
@@ -145,9 +144,11 @@ class NetPacketJoinGameAck : public NetPacket
public:
struct Data
{
u_int32_t sessionId;
u_int16_t yourPlayerUniqueId;
GameData gameData;
u_int32_t sessionId;
u_int16_t yourPlayerUniqueId;
PlayerType ptype;
PlayerRights prights;
GameData gameData;
};
NetPacketJoinGameAck();
@@ -170,9 +171,10 @@ class NetPacketPlayerJoined : public NetPacket
public:
struct Data
{
u_int16_t playerId;
PlayerType ptype;
std::string playerName;
u_int16_t playerId;
PlayerType ptype;
PlayerRights prights;
std::string playerName;
};
NetPacketPlayerJoined();
+7 -1
View File
@@ -31,7 +31,13 @@ class SessionData;
enum PlayerType
{
PLAYER_TYPE_COMPUTER,
PLAYER_TYPE_HUMAN,
PLAYER_TYPE_HUMAN
};
enum PlayerRights
{
PLAYER_RIGHTS_NORMAL,
PLAYER_RIGHTS_ADMIN
};
class PlayerData
+99
View File
@@ -0,0 +1,99 @@
/***************************************************************************
* Copyright (C) 2007 by Lothar May *
* *
* 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 <iostream>
#include "session.h"
#include "configfile.h"
#include <gui/generic/serverguiwrapper.h>
#include <net/socket_startup.h>
#include <QtCore>
#include <csignal>
#ifdef _MSC_VER
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define ENABLE_LEAK_CHECK() \
{ \
int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); \
tmpFlag |= _CRTDBG_LEAK_CHECK_DF; \
_CrtSetDbgFlag(tmpFlag); \
}
#endif
#endif
#ifndef ENABLE_LEAK_CHECK
#define ENABLE_LEAK_CHECK()
#endif
using namespace std;
volatile int g_pokerthTerminate = 0;
void
TerminateHandler(int signal)
{
g_pokerthTerminate = 1;
}
int
main(int argc, char *argv[])
{
ENABLE_LEAK_CHECK();
//_CrtSetBreakAlloc(164);
signal(SIGTERM, TerminateHandler);
signal(SIGINT, TerminateHandler);
//create defaultconfig
ConfigFile *myConfig = new ConfigFile(argc, argv);
socket_startup();
// Create pseudo Gui Wrapper for the server.
boost::shared_ptr<GuiInterface> myServerGuiInterface(new ServerGuiWrapper(myConfig, NULL, NULL));
{
boost::shared_ptr<Session> session(new Session(myServerGuiInterface.get(), myConfig));
myServerGuiInterface->setSession(session);
}
GameData gameData;
gameData.maxNumberOfPlayers = myConfig->readConfigInt("NetNumberOfPlayers");
gameData.startMoney = myConfig->readConfigInt("NetStartCash");
gameData.smallBlind = myConfig->readConfigInt("NetSmallBlind");
gameData.handsBeforeRaise = myConfig->readConfigInt("NetHandsBeforeRaiseSmallBlind");
gameData.guiSpeed = 4;
gameData.playerActionTimeoutSec = myConfig->readConfigInt("NetTimeOutPlayerAction");
myServerGuiInterface->getSession().startNetworkServer(gameData);
while (!g_pokerthTerminate)
{
myServerGuiInterface->getSession().waitForNetworkServer(100);
}
myServerGuiInterface->getSession().terminateNetworkServer();
socket_cleanup();
return 0;
}
+11
View File
@@ -196,6 +196,17 @@ void Session::terminateNetworkServer()
myNetServer = 0;
}
void Session::waitForNetworkServer(unsigned timeoutMsec)
{
if (!myNetServer)
return; // already terminated
if (myNetServer->Join(timeoutMsec))
{
delete myNetServer;
myNetServer = 0;
}
}
void Session::sendClientPlayerAction()
{
if (!myNetClient)
+1
View File
@@ -52,6 +52,7 @@ public:
void startNetworkServer(const GameData &gameData);
void initiateNetworkServerGame();
void terminateNetworkServer();
void waitForNetworkServer(unsigned timeoutMsec);
void sendClientPlayerAction();