2011-07-02 14:38:06 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
|
* PokerTH - The open source texas holdem engine *
|
2012-09-18 15:57:58 +02:00
|
|
|
* Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May *
|
2011-07-02 14:38:06 +00:00
|
|
|
* *
|
|
|
|
|
* 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/>. *
|
2012-12-25 15:06:23 +01:00
|
|
|
* *
|
|
|
|
|
* *
|
|
|
|
|
* Additional permission under GNU AGPL version 3 section 7 *
|
|
|
|
|
* *
|
|
|
|
|
* If you modify this program, or any covered work, by linking or *
|
|
|
|
|
* combining it with the OpenSSL project's OpenSSL library (or a *
|
|
|
|
|
* modified version of that library), containing parts covered by the *
|
|
|
|
|
* terms of the OpenSSL or SSLeay licenses, the authors of PokerTH *
|
|
|
|
|
* (Felix Hammer, Florian Thauer, Lothar May) grant you additional *
|
|
|
|
|
* permission to convey the resulting work. *
|
|
|
|
|
* Corresponding Source for a non-source form of such a combination *
|
|
|
|
|
* shall include the source code for the parts of OpenSSL used as well *
|
|
|
|
|
* as that of the covered work. *
|
2011-07-02 14:38:06 +00:00
|
|
|
*****************************************************************************/
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
#include <net/netpacket.h>
|
|
|
|
|
#include <net/socket_msg.h>
|
|
|
|
|
|
2012-09-18 15:57:58 +02:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
2012-09-18 15:57:58 +02:00
|
|
|
NetPacket::NetPacket()
|
|
|
|
|
{
|
|
|
|
|
m_msg = PokerTHMessage::default_instance().New();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetPacket::NetPacket(PokerTHMessage *msg)
|
|
|
|
|
: m_msg(msg)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-02 15:11:40 +00:00
|
|
|
NetPacket::~NetPacket()
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2012-09-18 15:57:58 +02:00
|
|
|
delete m_msg;
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boost::shared_ptr<NetPacket>
|
2013-02-24 16:22:51 +01:00
|
|
|
NetPacket::Create(const char *data, size_t dataSize)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
boost::shared_ptr<NetPacket> tmpPacket;
|
|
|
|
|
|
|
|
|
|
// Check minimum requirements.
|
2011-02-11 20:02:08 +00:00
|
|
|
if (data && dataSize > 0) {
|
2012-09-18 15:57:58 +02:00
|
|
|
PokerTHMessage *msg = PokerTHMessage::default_instance().New();
|
|
|
|
|
if (msg->ParseFromArray(data, static_cast<int>(dataSize))) {
|
|
|
|
|
tmpPacket.reset(new NetPacket(msg));
|
2011-02-11 20:02:08 +00:00
|
|
|
} else {
|
2012-09-18 15:57:58 +02:00
|
|
|
delete msg;
|
2009-08-02 15:11:40 +00:00
|
|
|
}
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
return tmpPacket;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-02 15:11:40 +00:00
|
|
|
bool
|
|
|
|
|
NetPacket::IsClientActivity() const
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
bool retVal = false;
|
|
|
|
|
if (m_msg &&
|
2012-09-18 15:57:58 +02:00
|
|
|
(m_msg->messagetype() == PokerTHMessage::Type_InitMessage
|
|
|
|
|
|| m_msg->messagetype() == PokerTHMessage::Type_JoinNewGameMessage
|
|
|
|
|
|| m_msg->messagetype() == PokerTHMessage::Type_JoinExistingGameMessage
|
|
|
|
|
|| m_msg->messagetype() == PokerTHMessage::Type_RejoinExistingGameMessage
|
|
|
|
|
|| m_msg->messagetype() == PokerTHMessage::Type_KickPlayerRequestMessage
|
|
|
|
|
|| m_msg->messagetype() == PokerTHMessage::Type_LeaveGameRequestMessage
|
|
|
|
|
|| m_msg->messagetype() == PokerTHMessage::Type_StartEventMessage
|
|
|
|
|
|| m_msg->messagetype() == PokerTHMessage::Type_MyActionRequestMessage
|
|
|
|
|
|| m_msg->messagetype() == PokerTHMessage::Type_ResetTimeoutMessage
|
|
|
|
|
|| m_msg->messagetype() == PokerTHMessage::Type_ChatRequestMessage)) {
|
2009-08-02 15:11:40 +00:00
|
|
|
retVal = true;
|
2008-10-16 22:19:10 +00:00
|
|
|
}
|
2009-08-02 15:11:40 +00:00
|
|
|
return retVal;
|
2008-10-16 22:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-03 20:42:32 +00:00
|
|
|
string
|
|
|
|
|
NetPacket::ToString() const
|
|
|
|
|
{
|
2012-09-18 15:57:58 +02:00
|
|
|
return m_msg ? m_msg->SerializeAsString() : "NULL";
|
2009-08-03 20:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
2008-10-16 22:19:10 +00:00
|
|
|
void
|
2012-09-18 15:57:58 +02:00
|
|
|
NetPacket::SetGameData(const GameData &inData, NetGameInfo &outData)
|
2008-10-16 22:19:10 +00:00
|
|
|
{
|
2012-09-18 15:57:58 +02:00
|
|
|
outData.set_netgametype(static_cast<NetGameInfo::NetGameType>(inData.gameType));
|
2013-10-27 00:41:13 +02:00
|
|
|
outData.set_allowspectators(inData.allowSpectators);
|
2012-09-18 15:57:58 +02:00
|
|
|
outData.set_maxnumplayers(inData.maxNumberOfPlayers);
|
|
|
|
|
outData.set_raiseintervalmode(static_cast<NetGameInfo::RaiseIntervalMode>(inData.raiseIntervalMode));
|
2011-02-11 20:02:08 +00:00
|
|
|
if (inData.raiseIntervalMode == RAISE_ON_HANDNUMBER) {
|
2012-09-18 15:57:58 +02:00
|
|
|
outData.set_raiseeveryhands(inData.raiseSmallBlindEveryHandsValue);
|
2011-02-11 20:02:08 +00:00
|
|
|
} else {
|
2012-09-18 15:57:58 +02:00
|
|
|
outData.set_raiseeveryminutes(inData.raiseSmallBlindEveryMinutesValue);
|
2008-10-16 22:19:10 +00:00
|
|
|
}
|
2012-09-18 15:57:58 +02:00
|
|
|
outData.set_endraisemode(static_cast<NetGameInfo::EndRaiseMode>(inData.afterManualBlindsMode));
|
|
|
|
|
outData.set_proposedguispeed(inData.guiSpeed);
|
|
|
|
|
outData.set_delaybetweenhands(inData.delayBetweenHandsSec);
|
|
|
|
|
outData.set_playeractiontimeout(inData.playerActionTimeoutSec);
|
|
|
|
|
outData.set_endraisesmallblindvalue(inData.afterMBAlwaysRaiseValue);
|
|
|
|
|
outData.set_firstsmallblind(inData.firstSmallBlind);
|
|
|
|
|
outData.set_startmoney(inData.startMoney);
|
2008-10-16 22:19:10 +00:00
|
|
|
|
2012-09-18 15:57:58 +02:00
|
|
|
BOOST_FOREACH(int manualBlind, inData.manualBlindsList) {
|
|
|
|
|
outData.add_manualblinds(manualBlind);
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-09-18 15:57:58 +02:00
|
|
|
NetPacket::GetGameData(const NetGameInfo &inData, GameData &outData)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2012-09-18 15:57:58 +02:00
|
|
|
int numManualBlinds = inData.manualblinds_size();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2012-09-18 15:57:58 +02:00
|
|
|
outData.gameType = static_cast<GameType>(inData.netgametype());
|
2013-10-27 00:41:13 +02:00
|
|
|
outData.allowSpectators = inData.allowspectators();
|
2012-09-18 15:57:58 +02:00
|
|
|
outData.maxNumberOfPlayers = inData.maxnumplayers();
|
|
|
|
|
outData.raiseIntervalMode = static_cast<RaiseIntervalMode>(inData.raiseintervalmode());
|
2009-08-02 15:11:40 +00:00
|
|
|
outData.raiseSmallBlindEveryHandsValue = outData.raiseSmallBlindEveryMinutesValue = 0;
|
2012-09-18 15:57:58 +02:00
|
|
|
if (outData.raiseIntervalMode == RAISE_ON_HANDNUMBER)
|
|
|
|
|
outData.raiseSmallBlindEveryHandsValue = inData.raiseeveryhands();
|
2009-08-02 15:11:40 +00:00
|
|
|
else
|
2012-09-18 15:57:58 +02:00
|
|
|
outData.raiseSmallBlindEveryMinutesValue = inData.raiseeveryminutes();
|
2009-08-02 15:11:40 +00:00
|
|
|
outData.raiseMode = numManualBlinds > 0 ? MANUAL_BLINDS_ORDER : DOUBLE_BLINDS;
|
2012-09-18 15:57:58 +02:00
|
|
|
outData.afterManualBlindsMode = static_cast<AfterManualBlindsMode>(inData.endraisemode());
|
|
|
|
|
outData.guiSpeed = inData.proposedguispeed();
|
|
|
|
|
outData.delayBetweenHandsSec = inData.delaybetweenhands();
|
|
|
|
|
outData.playerActionTimeoutSec = inData.playeractiontimeout();
|
|
|
|
|
outData.firstSmallBlind = inData.firstsmallblind();
|
|
|
|
|
outData.afterMBAlwaysRaiseValue = inData.endraisesmallblindvalue();
|
|
|
|
|
outData.startMoney = inData.startmoney();
|
2009-08-02 15:11:40 +00:00
|
|
|
|
2012-09-18 15:57:58 +02:00
|
|
|
for (int i = 0; i < numManualBlinds; i++) {
|
|
|
|
|
outData.manualBlindsList.push_back(static_cast<int>(inData.manualblinds(i)));
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-02 15:11:40 +00:00
|
|
|
int
|
2012-09-18 15:57:58 +02:00
|
|
|
NetPacket::NetErrorToGameError(ErrorMessage::ErrorReason netErrorReason)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
int retVal;
|
2011-02-11 20:02:08 +00:00
|
|
|
switch(netErrorReason) {
|
2012-09-18 15:57:58 +02:00
|
|
|
case ErrorMessage::initVersionNotSupported :
|
2011-02-11 20:02:08 +00:00
|
|
|
retVal = ERR_NET_VERSION_NOT_SUPPORTED;
|
|
|
|
|
break;
|
2012-09-18 15:57:58 +02:00
|
|
|
case ErrorMessage::initServerFull :
|
2011-02-11 20:02:08 +00:00
|
|
|
retVal = ERR_NET_SERVER_FULL;
|
|
|
|
|
break;
|
2012-09-18 15:57:58 +02:00
|
|
|
case ErrorMessage::initAuthFailure :
|
2011-02-11 20:02:08 +00:00
|
|
|
retVal = ERR_NET_INVALID_PASSWORD;
|
|
|
|
|
break;
|
2012-09-18 15:57:58 +02:00
|
|
|
case ErrorMessage::initPlayerNameInUse :
|
2011-02-11 20:02:08 +00:00
|
|
|
retVal = ERR_NET_PLAYER_NAME_IN_USE;
|
|
|
|
|
break;
|
2012-09-18 15:57:58 +02:00
|
|
|
case ErrorMessage::initInvalidPlayerName :
|
2011-02-11 20:02:08 +00:00
|
|
|
retVal = ERR_NET_INVALID_PLAYER_NAME;
|
|
|
|
|
break;
|
2012-09-18 15:57:58 +02:00
|
|
|
case ErrorMessage::initServerMaintenance :
|
2011-02-11 20:02:08 +00:00
|
|
|
retVal = ERR_NET_SERVER_MAINTENANCE;
|
|
|
|
|
break;
|
2012-09-18 15:57:58 +02:00
|
|
|
case ErrorMessage::initBlocked :
|
2011-02-11 20:02:08 +00:00
|
|
|
retVal = ERR_NET_INIT_BLOCKED;
|
|
|
|
|
break;
|
2012-09-18 15:57:58 +02:00
|
|
|
case ErrorMessage::avatarTooLarge :
|
2011-02-11 20:02:08 +00:00
|
|
|
retVal = ERR_NET_AVATAR_TOO_LARGE;
|
|
|
|
|
break;
|
2012-09-18 15:57:58 +02:00
|
|
|
case ErrorMessage::invalidPacket :
|
2011-02-11 20:02:08 +00:00
|
|
|
retVal = ERR_SOCK_INVALID_PACKET;
|
|
|
|
|
break;
|
2012-09-18 15:57:58 +02:00
|
|
|
case ErrorMessage::invalidState :
|
2011-02-11 20:02:08 +00:00
|
|
|
retVal = ERR_SOCK_INVALID_STATE;
|
|
|
|
|
break;
|
2012-09-18 15:57:58 +02:00
|
|
|
case ErrorMessage::kickedFromServer :
|
2011-02-11 20:02:08 +00:00
|
|
|
retVal = ERR_NET_PLAYER_KICKED;
|
|
|
|
|
break;
|
2012-09-18 15:57:58 +02:00
|
|
|
case ErrorMessage::bannedFromServer :
|
2011-02-11 20:02:08 +00:00
|
|
|
retVal = ERR_NET_PLAYER_BANNED;
|
|
|
|
|
break;
|
2012-09-18 15:57:58 +02:00
|
|
|
case ErrorMessage::blockedByServer :
|
2011-02-11 20:02:08 +00:00
|
|
|
retVal = ERR_NET_PLAYER_BLOCKED;
|
|
|
|
|
break;
|
2012-09-18 15:57:58 +02:00
|
|
|
case ErrorMessage::sessionTimeout :
|
2011-02-11 20:02:08 +00:00
|
|
|
retVal = ERR_NET_SESSION_TIMED_OUT;
|
|
|
|
|
break;
|
|
|
|
|
default :
|
|
|
|
|
retVal = ERR_SOCK_INTERNAL;
|
|
|
|
|
break;
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
2009-08-02 15:11:40 +00:00
|
|
|
return retVal;
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-09-18 15:57:58 +02:00
|
|
|
ErrorMessage::ErrorReason
|
2009-08-02 15:11:40 +00:00
|
|
|
NetPacket::GameErrorToNetError(int gameErrorReason)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2012-09-18 15:57:58 +02:00
|
|
|
ErrorMessage::ErrorReason retVal;
|
2011-02-11 20:02:08 +00:00
|
|
|
switch(gameErrorReason) {
|
|
|
|
|
case ERR_NET_VERSION_NOT_SUPPORTED :
|
2012-09-18 15:57:58 +02:00
|
|
|
retVal = ErrorMessage::initVersionNotSupported;
|
2011-02-11 20:02:08 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_SERVER_FULL :
|
2012-09-18 15:57:58 +02:00
|
|
|
retVal = ErrorMessage::initServerFull;
|
2011-02-11 20:02:08 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_INVALID_PASSWORD :
|
2012-09-18 15:57:58 +02:00
|
|
|
retVal = ErrorMessage::initAuthFailure;
|
2011-02-11 20:02:08 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_PLAYER_NAME_IN_USE :
|
2012-09-18 15:57:58 +02:00
|
|
|
retVal = ErrorMessage::initPlayerNameInUse;
|
2011-02-11 20:02:08 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_INVALID_PLAYER_NAME :
|
2012-09-18 15:57:58 +02:00
|
|
|
retVal = ErrorMessage::initInvalidPlayerName;
|
2011-02-11 20:02:08 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_SERVER_MAINTENANCE :
|
2012-09-18 15:57:58 +02:00
|
|
|
retVal = ErrorMessage::initServerMaintenance;
|
2011-02-11 20:02:08 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_INIT_BLOCKED :
|
2012-09-18 15:57:58 +02:00
|
|
|
retVal = ErrorMessage::initBlocked;
|
2011-02-11 20:02:08 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_AVATAR_TOO_LARGE :
|
2012-09-18 15:57:58 +02:00
|
|
|
retVal = ErrorMessage::avatarTooLarge;
|
2011-02-11 20:02:08 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_SOCK_INVALID_PACKET :
|
2012-09-18 15:57:58 +02:00
|
|
|
retVal = ErrorMessage::invalidPacket;
|
2011-02-11 20:02:08 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_SOCK_INVALID_STATE :
|
2012-09-18 15:57:58 +02:00
|
|
|
retVal = ErrorMessage::invalidState;
|
2011-02-11 20:02:08 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_PLAYER_KICKED :
|
2012-09-18 15:57:58 +02:00
|
|
|
retVal = ErrorMessage::kickedFromServer;
|
2011-02-11 20:02:08 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_PLAYER_BLOCKED :
|
2012-09-18 15:57:58 +02:00
|
|
|
retVal = ErrorMessage::blockedByServer;
|
2011-02-11 20:02:08 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_PLAYER_BANNED :
|
2012-09-18 15:57:58 +02:00
|
|
|
retVal = ErrorMessage::bannedFromServer;
|
2011-02-11 20:02:08 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_SESSION_TIMED_OUT :
|
2012-09-18 15:57:58 +02:00
|
|
|
retVal = ErrorMessage::sessionTimeout;
|
2011-02-11 20:02:08 +00:00
|
|
|
break;
|
|
|
|
|
default :
|
2012-09-18 15:57:58 +02:00
|
|
|
retVal = ErrorMessage::reserved;
|
2011-02-11 20:02:08 +00:00
|
|
|
break;
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
2009-08-02 15:11:40 +00:00
|
|
|
return retVal;
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|