2007-11-16 15:24:25 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* 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 <net/netpacket.h>
|
|
|
|
|
#include <net/socket_msg.h>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
2009-08-02 15:11:40 +00:00
|
|
|
NetPacket::NetPacket(MemAllocType alloc)
|
|
|
|
|
: m_msg(NULL)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
if (alloc != NoAlloc)
|
|
|
|
|
m_msg = (PokerTHMessage_t *)calloc(1, sizeof(PokerTHMessage_t));
|
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
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
ASN_STRUCT_FREE(asn_DEF_PokerTHMessage, m_msg);
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boost::shared_ptr<NetPacket>
|
|
|
|
|
NetPacket::Create(char *data, unsigned &dataSize)
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<NetPacket> tmpPacket;
|
|
|
|
|
|
|
|
|
|
// Check minimum requirements.
|
2009-08-02 15:11:40 +00:00
|
|
|
if (data && dataSize > 0)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
PokerTHMessage_t *msg = NULL;
|
2009-08-14 18:56:45 +00:00
|
|
|
asn_dec_rval_t retVal = ber_decode(0, &asn_DEF_PokerTHMessage, (void **)&msg, data, dataSize);
|
2009-08-02 15:11:40 +00:00
|
|
|
if(retVal.code == RC_OK)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
// ASN.1 BER decoding was successful.
|
|
|
|
|
tmpPacket.reset(new NetPacket(NoAlloc));
|
|
|
|
|
tmpPacket->m_msg = msg;
|
|
|
|
|
// Consume the bytes.
|
|
|
|
|
if (retVal.consumed < dataSize)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
dataSize -= retVal.consumed;
|
|
|
|
|
memmove(data, data + retVal.consumed, dataSize);
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
2009-08-02 15:11:40 +00:00
|
|
|
else
|
|
|
|
|
dataSize = 0;
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
else
|
2009-08-02 15:11:40 +00:00
|
|
|
{
|
|
|
|
|
// Free the partially decoded message (if applicable).
|
|
|
|
|
ASN_STRUCT_FREE(asn_DEF_PokerTHMessage, msg);
|
|
|
|
|
}
|
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 &&
|
|
|
|
|
(m_msg->present == PokerTHMessage_PR_initMessage
|
|
|
|
|
|| m_msg->present == PokerTHMessage_PR_joinGameRequestMessage
|
|
|
|
|
|| m_msg->present == PokerTHMessage_PR_kickPlayerRequestMessage
|
|
|
|
|
|| m_msg->present == PokerTHMessage_PR_leaveGameRequestMessage
|
|
|
|
|
|| m_msg->present == PokerTHMessage_PR_startEventMessage
|
|
|
|
|
|| m_msg->present == PokerTHMessage_PR_myActionRequestMessage
|
|
|
|
|
|| m_msg->present == PokerTHMessage_PR_resetTimeoutMessage
|
|
|
|
|
|| m_msg->present == PokerTHMessage_PR_chatRequestMessage))
|
2008-10-16 22:19:10 +00:00
|
|
|
{
|
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
|
|
|
static int
|
|
|
|
|
net_packet_print_to_string(const void *buffer, size_t size, void *packetStr)
|
|
|
|
|
{
|
|
|
|
|
string *tmpString = (string *)packetStr;
|
|
|
|
|
*tmpString += string((const char *)buffer, size);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string
|
|
|
|
|
NetPacket::ToString() const
|
|
|
|
|
{
|
|
|
|
|
string packetString;
|
|
|
|
|
xer_encode(&asn_DEF_PokerTHMessage, m_msg, XER_F_BASIC, &net_packet_print_to_string, &packetString);
|
|
|
|
|
return packetString;
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-16 22:19:10 +00:00
|
|
|
void
|
2009-08-02 15:11:40 +00:00
|
|
|
NetPacket::SetGameData(const GameData &inData, NetGameInfo_t *outData)
|
2008-10-16 22:19:10 +00:00
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
assert(outData);
|
2008-10-16 22:19:10 +00:00
|
|
|
|
2009-08-02 15:11:40 +00:00
|
|
|
int numManualBlinds = (int)inData.manualBlindsList.size();
|
2008-10-16 22:19:10 +00:00
|
|
|
|
2009-08-02 15:11:40 +00:00
|
|
|
outData->maxNumPlayers = inData.maxNumberOfPlayers;
|
|
|
|
|
outData->raiseIntervalMode.present = static_cast<raiseIntervalMode_PR>(inData.raiseIntervalMode);
|
|
|
|
|
if (inData.raiseIntervalMode == RAISE_ON_HANDNUMBER)
|
2008-10-16 22:19:10 +00:00
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
outData->raiseIntervalMode.present = raiseIntervalMode_PR_raiseEveryHands;
|
|
|
|
|
outData->raiseIntervalMode.choice.raiseEveryHands = inData.raiseSmallBlindEveryHandsValue;
|
2008-10-16 22:19:10 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
outData->raiseIntervalMode.present = raiseIntervalMode_PR_raiseEveryMinutes;
|
|
|
|
|
outData->raiseIntervalMode.choice.raiseEveryMinutes = inData.raiseSmallBlindEveryMinutesValue;
|
2008-10-16 22:19:10 +00:00
|
|
|
}
|
2009-08-02 15:11:40 +00:00
|
|
|
outData->endRaiseMode = inData.afterManualBlindsMode;
|
|
|
|
|
outData->proposedGuiSpeed = inData.guiSpeed;
|
2009-10-24 21:49:11 +00:00
|
|
|
outData->delayBetweenHands = inData.delayBetweenHandsSec;
|
2009-08-02 15:11:40 +00:00
|
|
|
outData->playerActionTimeout = inData.playerActionTimeoutSec;
|
|
|
|
|
outData->endRaiseSmallBlindValue = inData.afterMBAlwaysRaiseValue;
|
|
|
|
|
outData->firstSmallBlind = inData.firstSmallBlind;
|
|
|
|
|
outData->startMoney = inData.startMoney;
|
2008-10-16 22:19:10 +00:00
|
|
|
|
2009-08-02 15:11:40 +00:00
|
|
|
if (numManualBlinds)
|
2008-10-16 22:19:10 +00:00
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
list<int>::const_iterator i = inData.manualBlindsList.begin();
|
|
|
|
|
list<int>::const_iterator end = inData.manualBlindsList.end();
|
|
|
|
|
while (i != end)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
long *manualBlind = (long *)calloc(1, sizeof(long));
|
|
|
|
|
*manualBlind = *i;
|
|
|
|
|
ASN_SEQUENCE_ADD(&outData->manualBlinds.list, manualBlind);
|
|
|
|
|
++i;
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2009-08-02 15:11:40 +00:00
|
|
|
NetPacket::GetGameData(const NetGameInfo_t *inData, GameData &outData)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
assert(inData);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-08-18 00:08:14 +00:00
|
|
|
int numManualBlinds = (int)inData->manualBlinds.list.count;
|
2009-08-02 15:11:40 +00:00
|
|
|
|
|
|
|
|
outData.maxNumberOfPlayers = inData->maxNumPlayers;
|
|
|
|
|
outData.raiseIntervalMode = static_cast<RaiseIntervalMode>(inData->raiseIntervalMode.present);
|
|
|
|
|
outData.raiseSmallBlindEveryHandsValue = outData.raiseSmallBlindEveryMinutesValue = 0;
|
|
|
|
|
if (inData->raiseIntervalMode.present == raiseIntervalMode_PR_raiseEveryHands)
|
|
|
|
|
outData.raiseSmallBlindEveryHandsValue = inData->raiseIntervalMode.choice.raiseEveryHands;
|
|
|
|
|
else
|
|
|
|
|
outData.raiseSmallBlindEveryMinutesValue = inData->raiseIntervalMode.choice.raiseEveryMinutes;
|
|
|
|
|
outData.raiseMode = numManualBlinds > 0 ? MANUAL_BLINDS_ORDER : DOUBLE_BLINDS;
|
|
|
|
|
outData.afterManualBlindsMode = static_cast<AfterManualBlindsMode>(inData->endRaiseMode);
|
|
|
|
|
outData.guiSpeed = inData->proposedGuiSpeed;
|
2009-10-24 21:49:11 +00:00
|
|
|
outData.delayBetweenHandsSec = inData->delayBetweenHands;
|
2009-08-02 15:11:40 +00:00
|
|
|
outData.playerActionTimeoutSec = inData->playerActionTimeout;
|
|
|
|
|
outData.firstSmallBlind = inData->firstSmallBlind;
|
|
|
|
|
outData.afterMBAlwaysRaiseValue = inData->endRaiseSmallBlindValue;
|
|
|
|
|
outData.startMoney = inData->startMoney;
|
|
|
|
|
|
|
|
|
|
if (numManualBlinds)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
long **manualBlindsPtr = inData->manualBlinds.list.array;
|
|
|
|
|
int counter = 0;
|
|
|
|
|
for (int i = 0; i < numManualBlinds; i++)
|
|
|
|
|
{
|
|
|
|
|
outData.manualBlindsList.push_back((int)*manualBlindsPtr[i]);
|
|
|
|
|
counter++;
|
|
|
|
|
}
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-02 15:11:40 +00:00
|
|
|
int
|
|
|
|
|
NetPacket::NetErrorToGameError(long netErrorReason)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
int retVal;
|
|
|
|
|
switch(netErrorReason)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
case errorReason_errorInitVersionNotSupported :
|
|
|
|
|
retVal = ERR_NET_VERSION_NOT_SUPPORTED;
|
2007-11-16 15:24:25 +00:00
|
|
|
break;
|
2009-08-02 15:11:40 +00:00
|
|
|
case errorReason_errorInitServerFull :
|
|
|
|
|
retVal = ERR_NET_SERVER_FULL;
|
2007-11-16 15:24:25 +00:00
|
|
|
break;
|
2009-08-02 15:11:40 +00:00
|
|
|
case errorReason_errorInitAuthFailure :
|
|
|
|
|
retVal = ERR_NET_INVALID_PASSWORD;
|
2007-11-16 15:24:25 +00:00
|
|
|
break;
|
2009-08-02 15:11:40 +00:00
|
|
|
case errorReason_errorInitPlayerNameInUse :
|
|
|
|
|
retVal = ERR_NET_PLAYER_NAME_IN_USE;
|
2007-11-16 15:24:25 +00:00
|
|
|
break;
|
2009-08-02 15:11:40 +00:00
|
|
|
case errorReason_errorInitInvalidPlayerName :
|
|
|
|
|
retVal = ERR_NET_INVALID_PLAYER_NAME;
|
2008-03-07 21:45:26 +00:00
|
|
|
break;
|
2009-08-02 15:11:40 +00:00
|
|
|
case errorReason_errorInitServerMaintenance :
|
|
|
|
|
retVal = ERR_NET_SERVER_MAINTENANCE;
|
|
|
|
|
break;
|
2009-12-23 09:53:01 +00:00
|
|
|
case errorReason_errorInitBlocked :
|
|
|
|
|
retVal = ERR_NET_INIT_BLOCKED;
|
|
|
|
|
break;
|
2009-08-02 15:11:40 +00:00
|
|
|
case errorReason_errorAvatarTooLarge :
|
|
|
|
|
retVal = ERR_NET_AVATAR_TOO_LARGE;
|
|
|
|
|
break;
|
|
|
|
|
case errorReason_errorInvalidPacket :
|
|
|
|
|
retVal = ERR_SOCK_INVALID_PACKET;
|
|
|
|
|
break;
|
|
|
|
|
case errorReason_errorInvalidState :
|
|
|
|
|
retVal = ERR_SOCK_INVALID_STATE;
|
|
|
|
|
break;
|
|
|
|
|
case errorReason_errorKickedFromServer :
|
|
|
|
|
retVal = ERR_NET_PLAYER_KICKED;
|
|
|
|
|
break;
|
|
|
|
|
case errorReason_errorBannedFromServer :
|
|
|
|
|
retVal = ERR_NET_PLAYER_BANNED;
|
|
|
|
|
break;
|
|
|
|
|
case errorReason_errorSessionTimeout :
|
|
|
|
|
retVal = ERR_NET_SESSION_TIMED_OUT;
|
2008-04-02 21:41:39 +00:00
|
|
|
break;
|
2007-11-16 15:24:25 +00:00
|
|
|
default :
|
2009-08-02 15:11:40 +00:00
|
|
|
retVal = ERR_SOCK_INTERNAL;
|
2007-11-16 15:24:25 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2009-08-02 15:11:40 +00:00
|
|
|
return retVal;
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-02 15:11:40 +00:00
|
|
|
long
|
|
|
|
|
NetPacket::GameErrorToNetError(int gameErrorReason)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-08-02 15:11:40 +00:00
|
|
|
int retVal;
|
|
|
|
|
switch(gameErrorReason)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
case ERR_NET_VERSION_NOT_SUPPORTED :
|
2009-08-02 15:11:40 +00:00
|
|
|
retVal = errorReason_errorInitVersionNotSupported;
|
2007-11-16 15:24:25 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_SERVER_FULL :
|
2009-08-02 15:11:40 +00:00
|
|
|
retVal = errorReason_errorInitServerFull;
|
2007-11-16 15:24:25 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_INVALID_PASSWORD :
|
2009-08-02 15:11:40 +00:00
|
|
|
retVal = errorReason_errorInitAuthFailure;
|
2007-11-16 15:24:25 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_PLAYER_NAME_IN_USE :
|
2009-08-02 15:11:40 +00:00
|
|
|
retVal = errorReason_errorInitPlayerNameInUse;
|
2007-11-16 15:24:25 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_INVALID_PLAYER_NAME :
|
2009-08-02 15:11:40 +00:00
|
|
|
retVal = errorReason_errorInitInvalidPlayerName;
|
2007-11-16 15:24:25 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_SERVER_MAINTENANCE :
|
2009-08-02 15:11:40 +00:00
|
|
|
retVal = errorReason_errorInitServerMaintenance;
|
2007-11-16 15:24:25 +00:00
|
|
|
break;
|
2009-12-23 09:53:01 +00:00
|
|
|
case ERR_NET_INIT_BLOCKED :
|
|
|
|
|
retVal = errorReason_errorInitBlocked;
|
|
|
|
|
break;
|
2007-11-16 15:24:25 +00:00
|
|
|
case ERR_NET_AVATAR_TOO_LARGE :
|
2009-08-02 15:11:40 +00:00
|
|
|
retVal = errorReason_errorAvatarTooLarge;
|
2007-11-16 15:24:25 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_SOCK_INVALID_PACKET :
|
2009-08-02 15:11:40 +00:00
|
|
|
retVal = errorReason_errorInvalidPacket;
|
2007-11-16 15:24:25 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_SOCK_INVALID_STATE :
|
2009-08-02 15:11:40 +00:00
|
|
|
retVal = errorReason_errorInvalidState;
|
2007-11-16 15:24:25 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_PLAYER_KICKED :
|
2009-08-02 15:11:40 +00:00
|
|
|
retVal = errorReason_errorKickedFromServer;
|
2007-11-16 15:24:25 +00:00
|
|
|
break;
|
2008-03-06 21:48:14 +00:00
|
|
|
case ERR_NET_PLAYER_BANNED :
|
2009-08-02 15:11:40 +00:00
|
|
|
retVal = errorReason_errorBannedFromServer;
|
2008-03-06 21:48:14 +00:00
|
|
|
break;
|
|
|
|
|
case ERR_NET_SESSION_TIMED_OUT :
|
2009-08-02 15:11:40 +00:00
|
|
|
retVal = errorReason_errorSessionTimeout;
|
2008-03-06 21:48:14 +00:00
|
|
|
break;
|
2007-11-16 15:24:25 +00:00
|
|
|
default :
|
2009-08-02 15:11:40 +00:00
|
|
|
retVal = errorReason_errorReserved;
|
2007-11-16 15:24:25 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2009-08-02 15:11:40 +00:00
|
|
|
return retVal;
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|