From 945c4a102a9889c969138e744afa21055e13df26 Mon Sep 17 00:00:00 2001 From: lotodore Date: Sun, 2 Aug 2009 15:11:40 +0000 Subject: [PATCH] Completely rewriting all NetPacket stuff to use ASN.1. Finally this big chunk of work is completed. Compiles and links fine, but still partly non-functional. PLEASE NOTE: This is NOT COMPATIBLE to 0.7.x, because it is a new network protocol. --- pokerth_server.pro | 1 + src/core/avatarmanager.h | 2 + src/core/common/avatarmanager.cpp | 41 +- src/game_defs.h | 7 +- src/net/clientthread.h | 12 +- src/net/common/clientcontext.cpp | 1 + src/net/common/clientstate.cpp | 856 ++-- src/net/common/clientthread.cpp | 247 +- src/net/common/netpacket.cpp | 5861 +------------------------- src/net/common/receiverhelper.cpp | 2 +- src/net/common/senderhelper.cpp | 56 +- src/net/common/servergame.cpp | 126 +- src/net/common/servergamestate.cpp | 420 +- src/net/common/serverlobbythread.cpp | 531 ++- src/net/common/sessionmanager.cpp | 6 +- src/net/netpacket.h | 1543 +------ src/net/senderhelper.h | 2 + src/net/servergamestate.h | 2 +- src/net/serverlobbythread.h | 22 +- src/net/socket_msg.h | 7 +- src/playerdata.cpp | 10 +- src/playerdata.h | 10 +- src/pokerth.cpp | 16 +- src/session.cpp | 1 + 24 files changed, 1517 insertions(+), 8265 deletions(-) diff --git a/pokerth_server.pro b/pokerth_server.pro index 906de29d..e419fad5 100644 --- a/pokerth_server.pro +++ b/pokerth_server.pro @@ -31,6 +31,7 @@ INCLUDEPATH += . \ src/engine/network_engine \ src/config \ src/third_party/tinyxml \ + src/third_party/asn1 \ src/core \ DEPENDPATH += . \ diff --git a/src/core/avatarmanager.h b/src/core/avatarmanager.h index 36934767..22555a2d 100644 --- a/src/core/avatarmanager.h +++ b/src/core/avatarmanager.h @@ -27,6 +27,8 @@ #include #include +#include +#include #include #define MIN_AVATAR_FILE_SIZE 32 diff --git a/src/core/common/avatarmanager.cpp b/src/core/common/avatarmanager.cpp index 92ffd268..5634b095 100644 --- a/src/core/common/avatarmanager.cpp +++ b/src/core/common/avatarmanager.cpp @@ -194,28 +194,34 @@ AvatarManager::AvatarFileToNetPackets(const string &fileName, unsigned requestId boost::shared_ptr tmpState = OpenAvatarFileForChunkRead(fileName, fileSize, fileType); if (tmpState.get() && fileSize && fileType != AVATAR_FILE_TYPE_UNKNOWN) { - boost::shared_ptr avatarHeader(new NetPacketAvatarHeader); - NetPacketAvatarHeader::Data avatarHeaderData; - avatarHeaderData.requestId = requestId; - avatarHeaderData.avatarFileSize = fileSize; - avatarHeaderData.avatarFileType = fileType; - static_cast(avatarHeader.get())->SetData(avatarHeaderData); + boost::shared_ptr avatarHeader(new NetPacket(NetPacket::Alloc)); + avatarHeader->GetMsg()->present = PokerTHMessage_PR_avatarReplyMessage; + AvatarReplyMessage_t *netHeader = &avatarHeader->GetMsg()->choice.avatarReplyMessage; + netHeader->requestId = requestId; + netHeader->avatarResult.present = avatarResult_PR_avatarHeader; + netHeader->avatarResult.choice.avatarHeader.avatarType = fileType; + netHeader->avatarResult.choice.avatarHeader.avatarSize = fileSize; packets.push_back(avatarHeader); unsigned numBytes = 0; unsigned totalBytesRead = 0; + vector tmpData(MAX_FILE_DATA_SIZE); do { - boost::shared_ptr avatarFile(new NetPacketAvatarFile); - NetPacketAvatarFile::Data avatarFileData; - avatarFileData.fileData.resize(MAX_FILE_DATA_SIZE); - numBytes = ChunkReadAvatarFile(tmpState, &avatarFileData.fileData[0], MAX_FILE_DATA_SIZE); + numBytes = ChunkReadAvatarFile(tmpState, &tmpData[0], MAX_FILE_DATA_SIZE); if (numBytes) { - avatarFileData.fileData.resize(numBytes); - avatarFileData.requestId = requestId; totalBytesRead += numBytes; - static_cast(avatarFile.get())->SetData(avatarFileData); + + boost::shared_ptr avatarFile(new NetPacket(NetPacket::Alloc)); + avatarFile->GetMsg()->present = PokerTHMessage_PR_avatarReplyMessage; + AvatarReplyMessage_t *netFile = &avatarFile->GetMsg()->choice.avatarReplyMessage; + netFile->requestId = requestId; + netFile->avatarResult.present = avatarResult_PR_avatarData; + OCTET_STRING_fromBuf( + &netFile->avatarResult.choice.avatarData.avatarBlock, + (const char *)&tmpData[0], + numBytes); packets.push_back(avatarFile); } } while (numBytes); @@ -224,10 +230,11 @@ AvatarManager::AvatarFileToNetPackets(const string &fileName, unsigned requestId retVal = ERR_NET_WRONG_AVATAR_SIZE; else { - boost::shared_ptr avatarEnd(new NetPacketAvatarEnd); - NetPacketAvatarEnd::Data avatarEndData; - avatarEndData.requestId = requestId; - static_cast(avatarEnd.get())->SetData(avatarEndData); + boost::shared_ptr avatarEnd(new NetPacket(NetPacket::Alloc)); + avatarEnd->GetMsg()->present = PokerTHMessage_PR_avatarReplyMessage; + AvatarReplyMessage_t *netEnd = &avatarEnd->GetMsg()->choice.avatarReplyMessage; + netEnd->requestId = requestId; + netEnd->avatarResult.present = avatarResult_PR_avatarEnd; packets.push_back(avatarEnd); retVal = 0; } diff --git a/src/game_defs.h b/src/game_defs.h index 309406a6..f43ae8ab 100644 --- a/src/game_defs.h +++ b/src/game_defs.h @@ -75,8 +75,7 @@ enum DenyKickPlayerReason KICK_DENIED_TOO_FEW_PLAYERS, KICK_DENIED_TEMPORARY, KICK_DENIED_OTHER_IN_PROGRESS, - KICK_DENIED_INVALID_PLAYER_ID, - KICK_DENIED_OTHER_REASON + KICK_DENIED_INVALID_PLAYER_ID }; enum KickVote @@ -88,9 +87,7 @@ enum KickVote enum DenyVoteReason { VOTE_DENIED_INVALID_PETITION = 0, - VOTE_DENIED_ALREADY_VOTED, - VOTE_DENIED_IMPOSSIBLE, - VOTE_DENIED_OTHER_REASON + VOTE_DENIED_ALREADY_VOTED }; enum EndPetitionReason diff --git a/src/net/clientthread.h b/src/net/clientthread.h index 452c238b..c6f3e7b4 100644 --- a/src/net/clientthread.h +++ b/src/net/clientthread.h @@ -98,7 +98,7 @@ protected: typedef std::map GameInfoMap; typedef std::list > NetPacketList; typedef std::map PlayerInfoMap; - typedef std::map > AvatarDataMap; + typedef std::map > AvatarFileMap; typedef std::map ServerInfoMap; // Main function of the thread. @@ -117,10 +117,10 @@ protected: void SetNewGameAdmin(unsigned id); void RetrieveAvatarIfNeeded(unsigned id, const PlayerInfo &info); - void AddTempAvatarData(unsigned playerId, unsigned avatarSize, AvatarFileType type); - void StoreInTempAvatarData(unsigned playerId, const std::vector &data); - void CompleteTempAvatarData(unsigned playerId); - void PassAvatarDataToManager(unsigned playerId, boost::shared_ptr avatarData); + void AddTempAvatarFile(unsigned playerId, unsigned avatarSize, AvatarFileType type); + void StoreInTempAvatarFile(unsigned playerId, const std::vector &data); + void CompleteTempAvatarFile(unsigned playerId); + void PassAvatarFileToManager(unsigned playerId, boost::shared_ptr AvatarFile); void SetUnknownAvatar(unsigned playerId); void TimerCheckAvatarDownloads(const boost::system::error_code& ec); @@ -234,7 +234,7 @@ private: unsigned m_curPetitionId; mutable boost::mutex m_curPetitionIdMutex; - AvatarDataMap m_tempAvatarMap; + AvatarFileMap m_tempAvatarMap; unsigned m_curGameNum; unsigned m_guiPlayerId; diff --git a/src/net/common/clientcontext.cpp b/src/net/common/clientcontext.cpp index 1bc3cf04..73008e6b 100644 --- a/src/net/common/clientcontext.cpp +++ b/src/net/common/clientcontext.cpp @@ -17,6 +17,7 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include #include diff --git a/src/net/common/clientstate.cpp b/src/net/common/clientstate.cpp index d32b0ff4..c55c9fad 100644 --- a/src/net/common/clientstate.cpp +++ b/src/net/common/clientstate.cpp @@ -667,21 +667,32 @@ ClientStateStartSession::Enter(boost::shared_ptr client) { ClientContext &context = client->GetContext(); - NetPacketInit::Data initData; - initData.password = context.GetPassword(); - initData.playerName = context.GetPlayerName(); + boost::shared_ptr init(new NetPacket(NetPacket::Alloc)); + init->GetMsg()->present = PokerTHMessage_PR_initMessage; + InitMessage_t *netInit = &init->GetMsg()->choice.initMessage; + netInit->requestedVersion.major = NET_VERSION_MAJOR; + netInit->requestedVersion.minor = NET_VERSION_MINOR; + netInit->login.present = login_PR_anonymousLogin; + AnonymousLogin_t *anonLogin = &netInit->login.choice.anonymousLogin; + OCTET_STRING_fromBuf(&anonLogin->playerName, + context.GetPlayerName().c_str(), + context.GetPlayerName().length()); + //context.GetPassword(); string avatarFile = client->GetQtToolsInterface().stringFromUtf8(context.GetAvatarFile()); - initData.showAvatar = false; if (!avatarFile.empty()) { - if (client->GetAvatarManager().GetHashForAvatar(avatarFile, initData.avatar)) - initData.showAvatar = true; + MD5Buf tmpMD5; + if (client->GetAvatarManager().GetHashForAvatar(avatarFile, tmpMD5)) + { + // TODO: use sha1. + anonLogin->avatar = + OCTET_STRING_new_fromBuf( + &asn_DEF_OCTET_STRING, + (const char *)tmpMD5.data, + MD5_DATA_SIZE); + } } - - boost::shared_ptr packet(new NetPacketInit); - ((NetPacketInit *)packet.get())->SetData(initData); - - client->GetSender().Send(context.GetSessionData(), packet); + client->GetSender().Send(context.GetSessionData(), init); client->SetState(ClientStateWaitSession::Instance()); } @@ -704,194 +715,234 @@ AbstractClientStateReceiving::~AbstractClientStateReceiving() void AbstractClientStateReceiving::HandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket) { - if (tmpPacket->ToNetPacketPlayerInfo()) + if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_playerInfoReplyMessage) { - NetPacketPlayerInfo::Data infoData; - tmpPacket->ToNetPacketPlayerInfo()->GetData(infoData); - client->SetPlayerInfo(infoData.playerId, infoData.playerInfo); + unsigned playerId = tmpPacket->GetMsg()->choice.playerInfoReplyMessage.playerId; + if (tmpPacket->GetMsg()->choice.playerInfoReplyMessage.playerInfoResult.present == playerInfoResult_PR_playerInfoData) + { + PlayerInfo tmpInfo; + PlayerInfoData_t *netInfo = &tmpPacket->GetMsg()->choice.playerInfoReplyMessage.playerInfoResult.choice.playerInfoData; + tmpInfo.playerName = STL_STRING_FROM_OCTET_STRING(netInfo->playerName); + if (netInfo->isHuman) + tmpInfo.playerName = PLAYER_TYPE_HUMAN; + else + tmpInfo.ptype = PLAYER_TYPE_COMPUTER; + if (netInfo->avatarData != NULL) + { + tmpInfo.hasAvatar = true; + // TODO: use sha1. + memcpy(tmpInfo.avatar.data, netInfo->avatarData->avatar.buf, MD5_DATA_SIZE); + tmpInfo.avatarType = (AvatarFileType)netInfo->avatarData->avatarType; + } + client->SetPlayerInfo( + playerId, + tmpInfo); + } + else + { + client->SetUnknownPlayer(playerId); + } } - else if (tmpPacket->ToNetPacketUnknownPlayerId()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_gamePlayerMessage) { - NetPacketUnknownPlayerId::Data unknownIdData; - tmpPacket->ToNetPacketUnknownPlayerId()->GetData(unknownIdData); - client->SetUnknownPlayer(unknownIdData.playerId); + GamePlayerMessage_t *netGamePlayer = &tmpPacket->GetMsg()->choice.gamePlayerMessage; +// unsigned gameId = netGamePlayer->gameId; + if (netGamePlayer->gamePlayerNotification.present == gamePlayerNotification_PR_removedFromGame) + { + RemovedFromGame_t *netRemoved = &netGamePlayer->gamePlayerNotification.choice.removedFromGame; + + client->ClearPlayerDataList(); + // Resubscribe Lobby messages. + client->ResubscribeLobbyMsg(); + // Show Lobby. + client->GetCallback().SignalNetClientWaitDialog(); + client->GetCallback().SignalNetClientRemovedFromGame(netRemoved->removedFromGameReason); + client->SetState(ClientStateWaitJoin::Instance()); + } + else if (netGamePlayer->gamePlayerNotification.present == gamePlayerNotification_PR_gamePlayerLeft) + { + // A player left the game. + GamePlayerLeft_t *netLeft = &netGamePlayer->gamePlayerNotification.choice.gamePlayerLeft; + + // Signal to GUI and remove from data list. + client->RemovePlayerData(netLeft->playerId, netLeft->gamePlayerLeftReason); + } + else if (netGamePlayer->gamePlayerNotification.present == gamePlayerNotification_PR_gameAdminChanged) + { + // New admin for the game. + GameAdminChanged_t *netChanged = &netGamePlayer->gamePlayerNotification.choice.gameAdminChanged; + + // Set new game admin and signal to GUI. + client->SetNewGameAdmin(netChanged->newAdminPlayerId); + } } - else if (tmpPacket->ToNetPacketRemovedFromGame()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_timeoutWarningMessage) { - NetPacketRemovedFromGame::Data removedData; - tmpPacket->ToNetPacketRemovedFromGame()->GetData(removedData); - client->ClearPlayerDataList(); - // Resubscribe Lobby messages. - client->ResubscribeLobbyMsg(); - // Show Lobby. - client->GetCallback().SignalNetClientWaitDialog(); - client->GetCallback().SignalNetClientRemovedFromGame(removedData.removeReason); - client->SetState(ClientStateWaitJoin::Instance()); + TimeoutWarningMessage_t *tmpTimeout = &tmpPacket->GetMsg()->choice.timeoutWarningMessage; + client->GetCallback().SignalNetClientShowTimeoutDialog((NetTimeoutReason)tmpTimeout->timeoutReason, tmpTimeout->remainingSeconds); } - else if (tmpPacket->ToNetPacketTimeoutWarning()) - { - NetPacketTimeoutWarning::Data warningData; - tmpPacket->ToNetPacketTimeoutWarning()->GetData(warningData); - client->GetCallback().SignalNetClientShowTimeoutDialog(warningData.timeoutReason, warningData.remainingSeconds); - } - else if (tmpPacket->ToNetPacketChatText()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_chatMessage) { // Chat message - display it in the GUI. - NetPacketChatText::Data chatData; - tmpPacket->ToNetPacketChatText()->GetData(chatData); + ChatMessage_t *netMessage = &tmpPacket->GetMsg()->choice.chatMessage; string playerName; - if (chatData.playerId == 0) + if (netMessage->playerId == 0) { playerName = "(global notice)"; } else { - boost::shared_ptr tmpPlayer = client->GetPlayerDataByUniqueId(chatData.playerId); + boost::shared_ptr tmpPlayer = client->GetPlayerDataByUniqueId(netMessage->playerId); if (tmpPlayer.get()) playerName = tmpPlayer->GetName(); } if (!playerName.empty()) - client->GetCallback().SignalNetClientChatMsg(playerName, chatData.text); + client->GetCallback().SignalNetClientChatMsg(playerName, STL_STRING_FROM_OCTET_STRING(netMessage->chatText)); } - else if (tmpPacket->ToNetPacketMsgBoxText()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_dialogMessage) { // Message box - display it in the GUI. - NetPacketMsgBoxText::Data msgData; - tmpPacket->ToNetPacketMsgBoxText()->GetData(msgData); - client->GetCallback().SignalNetClientMsgBox(msgData.text); + DialogMessage_t *netDialog = &tmpPacket->GetMsg()->choice.dialogMessage; + client->GetCallback().SignalNetClientMsgBox(STL_STRING_FROM_OCTET_STRING(netDialog->notificationText)); } - else if (tmpPacket->ToNetPacketPlayerLeft()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_gameListMessage) { - // A player left the game. - NetPacketPlayerLeft::Data playerLeftData; - tmpPacket->ToNetPacketPlayerLeft()->GetData(playerLeftData); + GameListMessage_t *netGameList = &tmpPacket->GetMsg()->choice.gameListMessage; + unsigned gameId = netGameList->gameId; - // Signal to GUI and remove from data list. - client->RemovePlayerData(playerLeftData.playerId, playerLeftData.removeReason); - } - else if (tmpPacket->ToNetPacketGameAdminChanged()) - { - // New admin for the game. - NetPacketGameAdminChanged::Data adminChangedData; - tmpPacket->ToNetPacketGameAdminChanged()->GetData(adminChangedData); - - // Set new game admin and signal to GUI. - client->SetNewGameAdmin(adminChangedData.playerId); - } - else if (tmpPacket->ToNetPacketGameListNew()) - { - // A new game was created on the server. - NetPacketGameListNew::Data gameListNewData; - tmpPacket->ToNetPacketGameListNew()->GetData(gameListNewData); - - // Request player info for players if needed. - PlayerIdList::const_iterator i = gameListNewData.gameInfo.players.begin(); - PlayerIdList::const_iterator end = gameListNewData.gameInfo.players.end(); - while (i != end) + if (netGameList->gameListNotification.present == gameListNotification_PR_gameListNew) { - PlayerInfo info; - if (!client->GetCachedPlayerInfo(*i, info)) + // A new game was created on the server. + GameListNew_t *netListNew = &netGameList->gameListNotification.choice.gameListNew; + + NonZeroId_t **playerIds = netListNew->playerIds.list.array; + unsigned numPlayers = netListNew->playerIds.list.count; + // Request player info for players if needed. + if (numPlayers && playerIds && *playerIds) { - // Request player info. - client->RequestPlayerInfo(*i); - } - ++i; - } + GameInfo tmpInfo; + for (unsigned i = 0; i < numPlayers; i++) + { + PlayerInfo info; + unsigned playerId = *playerIds[i]; + if (!client->GetCachedPlayerInfo(playerId, info)) + { + // Request player info. + client->RequestPlayerInfo(playerId); + } + tmpInfo.players.push_back(playerId); + ++i; + } + tmpInfo.adminPlayerId = netListNew->adminPlayerId; + tmpInfo.isPasswordProtected = netListNew->isPrivate ? true : false; + tmpInfo.mode = GAME_MODE_CREATED; + tmpInfo.name = STL_STRING_FROM_OCTET_STRING(netListNew->gameInfo.gameName); + NetPacket::GetGameData(&netListNew->gameInfo, tmpInfo.data); - client->AddGameInfo(gameListNewData.gameId, gameListNewData.gameInfo); - } - else if (tmpPacket->ToNetPacketGameListUpdate()) - { - // An existing game was updated on the server. - NetPacketGameListUpdate::Data gameListUpdateData; - tmpPacket->ToNetPacketGameListUpdate()->GetData(gameListUpdateData); - if (gameListUpdateData.gameMode == GAME_MODE_CLOSED) - client->RemoveGameInfo(gameListUpdateData.gameId); - else - client->UpdateGameInfoMode(gameListUpdateData.gameId, gameListUpdateData.gameMode); - } - else if (tmpPacket->ToNetPacketGameListPlayerJoined()) - { - NetPacketGameListPlayerJoined::Data playerJoinedData; - tmpPacket->ToNetPacketGameListPlayerJoined()->GetData(playerJoinedData); - client->ModifyGameInfoAddPlayer(playerJoinedData.gameId, playerJoinedData.playerId); - // Request player info if needed. - PlayerInfo info; - if (!client->GetCachedPlayerInfo(playerJoinedData.playerId, info)) + client->AddGameInfo(gameId, tmpInfo); + } + } + else if (netGameList->gameListNotification.present == gameListNotification_PR_gameListUpdate) { - client->RequestPlayerInfo(playerJoinedData.playerId); + // An existing game was updated on the server. + GameListUpdate_t *netListUpdate = &netGameList->gameListNotification.choice.gameListUpdate; + if (netListUpdate->gameMode == NetGameMode_gameClosed) + client->RemoveGameInfo(gameId); + else + client->UpdateGameInfoMode(gameId, static_cast(netListUpdate->gameMode)); + } + else if (netGameList->gameListNotification.present == gameListNotification_PR_gameListPlayerJoined) + { + GameListPlayerJoined_t *netListJoined = &netGameList->gameListNotification.choice.gameListPlayerJoined; + + client->ModifyGameInfoAddPlayer(gameId, netListJoined->playerId); + // Request player info if needed. + PlayerInfo info; + if (!client->GetCachedPlayerInfo(netListJoined->playerId, info)) + { + client->RequestPlayerInfo(netListJoined->playerId); + } + } + else if (netGameList->gameListNotification.present == gameListNotification_PR_gameListPlayerLeft) + { + GameListPlayerLeft_t *netListLeft = &netGameList->gameListNotification.choice.gameListPlayerLeft; + + client->ModifyGameInfoRemovePlayer(gameId, netListLeft->playerId); + } + else if (netGameList->gameListNotification.present == gameListNotification_PR_gameListAdminChanged) + { + GameListAdminChanged_t *netListAdmin = &netGameList->gameListNotification.choice.gameListAdminChanged; + + client->UpdateGameInfoAdmin(gameId, netListAdmin->newAdminPlayerId); } } - else if (tmpPacket->ToNetPacketGameListPlayerLeft()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_startKickPetitionMessage) { - NetPacketGameListPlayerLeft::Data playerLeftData; - tmpPacket->ToNetPacketGameListPlayerLeft()->GetData(playerLeftData); - client->ModifyGameInfoRemovePlayer(playerLeftData.gameId, playerLeftData.playerId); + StartKickPetitionMessage_t *netStartPetition = &tmpPacket->GetMsg()->choice.startKickPetitionMessage; + client->StartPetition(netStartPetition->petitionId, netStartPetition->proposingPlayerId, + netStartPetition->kickPlayerId, netStartPetition->kickTimeoutSec, netStartPetition->numVotesNeededToKick); } - else if (tmpPacket->ToNetPacketGameListAdminChanged()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_kickPetitionUpdateMessage) { - NetPacketGameListAdminChanged::Data adminChangedData; - tmpPacket->ToNetPacketGameListAdminChanged()->GetData(adminChangedData); - client->UpdateGameInfoAdmin(adminChangedData.gameId, adminChangedData.newAdminplayerId); + KickPetitionUpdateMessage_t *netPetitionUpdate = &tmpPacket->GetMsg()->choice.kickPetitionUpdateMessage; + client->UpdatePetition(netPetitionUpdate->petitionId, netPetitionUpdate->numVotesAgainstKicking, + netPetitionUpdate->numVotesInFavourOfKicking, netPetitionUpdate->numVotesNeededToKick); } - else if (tmpPacket->ToNetPacketStartKickPlayerPetition()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_endKickPetitionMessage) { - NetPacketStartKickPlayerPetition::Data startPetitionData; - tmpPacket->ToNetPacketStartKickPlayerPetition()->GetData(startPetitionData); - client->StartPetition(startPetitionData.petitionId, startPetitionData.proposingPlayerId, - startPetitionData.kickPlayerId, startPetitionData.kickTimeoutSec, startPetitionData.numVotesNeededToKick); + EndKickPetitionMessage_t *netEndPetition = &tmpPacket->GetMsg()->choice.endKickPetitionMessage; + client->EndPetition(netEndPetition->petitionId); } - else if (tmpPacket->ToNetPacketKickPlayerPetitionUpdate()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_avatarReplyMessage) { - NetPacketKickPlayerPetitionUpdate::Data updatePetitionData; - tmpPacket->ToNetPacketKickPlayerPetitionUpdate()->GetData(updatePetitionData); - client->UpdatePetition(updatePetitionData.petitionId, updatePetitionData.numVotesAgainstKicking, - updatePetitionData.numVotesInFavourOfKicking, updatePetitionData.numVotesNeededToKick); + AvatarReplyMessage_t *netAvatarReply = &tmpPacket->GetMsg()->choice.avatarReplyMessage; + unsigned requestId = netAvatarReply->requestId; + if (netAvatarReply->avatarResult.present == avatarResult_PR_avatarHeader) + { + AvatarHeader_t *netAvatarHeader = &netAvatarReply->avatarResult.choice.avatarHeader; + client->AddTempAvatarFile(requestId, netAvatarHeader->avatarSize, static_cast(netAvatarHeader->avatarType)); + } + else if (netAvatarReply->avatarResult.present == avatarResult_PR_avatarData) + { + AvatarData_t *netAvatarData = &netAvatarReply->avatarResult.choice.avatarData; + vector fileData(netAvatarData->avatarBlock.size); + memcpy(&fileData[0], netAvatarData->avatarBlock.buf, netAvatarData->avatarBlock.size); + client->StoreInTempAvatarFile(requestId, fileData); + } + else if (netAvatarReply->avatarResult.present == avatarResult_PR_avatarEnd) + { + client->CompleteTempAvatarFile(requestId); + } + else if (netAvatarReply->avatarResult.present == avatarResult_PR_unknownAvatar) + { + client->SetUnknownAvatar(requestId); + } } - else if (tmpPacket->ToNetPacketEndKickPlayerPetition()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_statisticsMessage) { - NetPacketEndKickPlayerPetition::Data endPetitionData; - tmpPacket->ToNetPacketEndKickPlayerPetition()->GetData(endPetitionData); - client->EndPetition(endPetitionData.petitionId); + StatisticsMessage_t *netStatistics = &tmpPacket->GetMsg()->choice.statisticsMessage; + + StatisticsData **statData = netStatistics->statisticsData.list.array; + unsigned numStats = netStatistics->statisticsData.list.count; + // Request player info for players if needed. + if (numStats && statData && *statData) + { + ServerStats tmpStats; + for (unsigned i = 0; i < numStats; i++) + { + if (statData[i]->statisticsType == statisticsType_statNumberOfPlayers) + tmpStats.numberOfPlayersOnServer = statData[i]->statisticsValue; + } + client->UpdateStatData(tmpStats); + } } - else if (tmpPacket->ToNetPacketAvatarHeader()) - { - NetPacketAvatarHeader::Data headerData; - tmpPacket->ToNetPacketAvatarHeader()->GetData(headerData); - client->AddTempAvatarData(headerData.requestId, headerData.avatarFileSize, headerData.avatarFileType); - } - else if (tmpPacket->ToNetPacketAvatarFile()) - { - NetPacketAvatarFile::Data fileData; - tmpPacket->ToNetPacketAvatarFile()->GetData(fileData); - client->StoreInTempAvatarData(fileData.requestId, fileData.fileData); - } - else if (tmpPacket->ToNetPacketAvatarEnd()) - { - NetPacketAvatarEnd::Data endData; - tmpPacket->ToNetPacketAvatarEnd()->GetData(endData); - client->CompleteTempAvatarData(endData.requestId); - } - else if (tmpPacket->ToNetPacketUnknownAvatar()) - { - NetPacketUnknownAvatar::Data unknownAvatarData; - tmpPacket->ToNetPacketUnknownAvatar()->GetData(unknownAvatarData); - client->SetUnknownAvatar(unknownAvatarData.requestId); - } - else if (tmpPacket->ToNetPacketStatisticsChanged()) - { - NetPacketStatisticsChanged::Data statData; - tmpPacket->ToNetPacketStatisticsChanged()->GetData(statData); - client->UpdateStatData(statData.stats); - } - else if (tmpPacket->ToNetPacketError()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_errorMessage) { // Server reported an error. - NetPacketError::Data errorData; - tmpPacket->ToNetPacketError()->GetData(errorData); + ErrorMessage_t *netError = &tmpPacket->GetMsg()->choice.errorMessage; // Show the error. - throw ClientException(__FILE__, __LINE__, errorData.errorCode, 0); + throw ClientException(__FILE__, __LINE__, NetPacket::NetErrorToGameError(netError->errorReason), 0); } else InternalHandlePacket(client, tmpPacket); @@ -929,33 +980,37 @@ ClientStateWaitSession::Exit(boost::shared_ptr /*client*/) void ClientStateWaitSession::InternalHandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket) { - if (tmpPacket->ToNetPacketInitAck()) + if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_initAckMessage) { // Everything is fine - we are in the lobby. - NetPacketInitAck::Data initAckData; - tmpPacket->ToNetPacketInitAck()->GetData(initAckData); + InitAckMessage_t *netInitAck = &tmpPacket->GetMsg()->choice.initAckMessage; // Check current game version. - if (initAckData.latestGameVersion != POKERTH_VERSION) + if (netInitAck->latestGameVersion.major != POKERTH_VERSION_MAJOR + || netInitAck->latestGameVersion.minor != POKERTH_VERSION_MINOR) + { client->GetCallback().SignalNetClientNotification(NTF_NET_NEW_RELEASE_AVAILABLE); - else if (POKERTH_BETA_REVISION && initAckData.latestBetaRevision != POKERTH_BETA_REVISION) + } + else if (POKERTH_BETA_REVISION && netInitAck->latestBetaRevision != POKERTH_BETA_REVISION) + { client->GetCallback().SignalNetClientNotification(NTF_NET_OUTDATED_BETA); + } - client->SetGuiPlayerId(initAckData.playerId); + client->SetGuiPlayerId(netInitAck->yourPlayerId); client->SetSessionEstablished(true); client->GetCallback().SignalNetClientConnect(MSG_SOCK_SESSION_DONE); client->SetState(ClientStateWaitJoin::Instance()); } - else if (tmpPacket->ToNetPacketRetrieveAvatar()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_avatarRequestMessage) { // Before letting us join the lobby, the server requests our avatar. - NetPacketRetrieveAvatar::Data retrieveAvatarData; - tmpPacket->ToNetPacketRetrieveAvatar()->GetData(retrieveAvatarData); + AvatarRequestMessage_t *netAvatarRequest = &tmpPacket->GetMsg()->choice.avatarRequestMessage; + // TODO compare SHA1. NetPacketList tmpList; int avatarError = client->GetAvatarManager().AvatarFileToNetPackets( client->GetQtToolsInterface().stringFromUtf8(client->GetContext().GetAvatarFile()), - retrieveAvatarData.requestId, + netAvatarRequest->requestId, tmpList); if (!avatarError) @@ -997,30 +1052,57 @@ ClientStateWaitJoin::InternalHandlePacket(boost::shared_ptr client { ClientContext &context = client->GetContext(); - if (tmpPacket->ToNetPacketJoinGameAck()) + if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_joinGameReplyMessage) { - // Successfully joined a game. - NetPacketJoinGameAck::Data joinGameAckData; - tmpPacket->ToNetPacketJoinGameAck()->GetData(joinGameAckData); - client->SetGameId(joinGameAckData.gameId); - client->SetGameData(joinGameAckData.gameData); + JoinGameReplyMessage_t *netJoinReply = &tmpPacket->GetMsg()->choice.joinGameReplyMessage; + unsigned gameId = netJoinReply->gameId; + if (netJoinReply->joinGameResult.present == joinGameResult_PR_joinGameAck) + { + // Successfully joined a game. + JoinGameAck_t *netJoinAck = &netJoinReply->joinGameResult.choice.joinGameAck; + client->SetGameId(gameId); + GameData tmpData; + NetPacket::GetGameData(&netJoinAck->gameInfo, tmpData); + client->SetGameData(tmpData); - // Player number is 0 on init. Will be set when the game starts. - boost::shared_ptr playerData( - new PlayerData(client->GetGuiPlayerId(), 0, PLAYER_TYPE_HUMAN, joinGameAckData.prights)); - playerData->SetName(context.GetPlayerName()); - playerData->SetAvatarFile(context.GetAvatarFile()); - client->AddPlayerData(playerData); + // Player number is 0 on init. Will be set when the game starts. + boost::shared_ptr playerData( + new PlayerData(client->GetGuiPlayerId(), 0, PLAYER_TYPE_HUMAN, + netJoinAck->areYouAdmin ? PLAYER_RIGHTS_ADMIN : PLAYER_RIGHTS_NORMAL)); + playerData->SetName(context.GetPlayerName()); + playerData->SetAvatarFile(context.GetAvatarFile()); + client->AddPlayerData(playerData); - client->GetCallback().SignalNetClientGameInfo(MSG_NET_GAME_CLIENT_JOIN); - client->SetState(ClientStateWaitGame::Instance()); - } - else if (tmpPacket->ToNetPacketJoinGameFailed()) - { - // Failed to join a game. - NetPacketJoinGameFailed::Data joinGameFailedData; - tmpPacket->ToNetPacketJoinGameFailed()->GetData(joinGameFailedData); - client->GetCallback().SignalNetClientNotification(joinGameFailedData.failureCode); + client->GetCallback().SignalNetClientGameInfo(MSG_NET_GAME_CLIENT_JOIN); + client->SetState(ClientStateWaitGame::Instance()); + } + else if (netJoinReply->joinGameResult.present == joinGameResult_PR_joinGameFailed) + { + // Failed to join a game. + JoinGameFailed_t *netJoinFailed = &netJoinReply->joinGameResult.choice.joinGameFailed; + + int failureCode; + switch (netJoinFailed->joinGameFailureReason) + { + case joinGameFailureReason_invalidGame : + failureCode = NTF_NET_JOIN_GAME_INVALID; + break; + case joinGameFailureReason_gameIsFull : + failureCode = NTF_NET_JOIN_GAME_FULL; + break; + case joinGameFailureReason_gameIsRunning : + failureCode = NTF_NET_JOIN_ALREADY_RUNNING; + break; + case joinGameFailureReason_invalidPassword : + failureCode = NTF_NET_JOIN_INVALID_PASSWORD; + break; + default : + failureCode = NTF_NET_INTERNAL; + break; + } + + client->GetCallback().SignalNetClientNotification(failureCode); + } } } @@ -1054,45 +1136,49 @@ ClientStateWaitGame::Exit(boost::shared_ptr /*client*/) void ClientStateWaitGame::InternalHandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket) { - if (tmpPacket->ToNetPacketStartEvent()) + if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_startEventMessage) { client->SetState(ClientStateSynchronizeStart::Instance()); } - else if (tmpPacket->ToNetPacketPlayerJoined()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_gamePlayerMessage) { - // Another player joined the network game. - NetPacketPlayerJoined::Data netPlayerData; - tmpPacket->ToNetPacketPlayerJoined()->GetData(netPlayerData); - - boost::shared_ptr playerData; - PlayerInfo info; - if (client->GetCachedPlayerInfo(netPlayerData.playerId, info)) + GamePlayerMessage_t *netGamePlayer = &tmpPacket->GetMsg()->choice.gamePlayerMessage; +// unsigned gameId = netGamePlayer->gameId; + if (netGamePlayer->gamePlayerNotification.present == gamePlayerNotification_PR_gamePlayerJoined) { - playerData.reset( - new PlayerData(netPlayerData.playerId, 0, info.ptype, netPlayerData.prights)); - playerData->SetName(info.playerName); - if (info.hasAvatar) + // Another player joined the network game. + GamePlayerJoined_t *netPlayerJoined = &netGamePlayer->gamePlayerNotification.choice.gamePlayerJoined; + + boost::shared_ptr playerData; + PlayerInfo info; + if (client->GetCachedPlayerInfo(netPlayerJoined->playerId, info)) { - string avatarFile; - if (client->GetAvatarManager().GetAvatarFileName(info.avatar, avatarFile)) - playerData->SetAvatarFile(client->GetQtToolsInterface().stringToUtf8(avatarFile)); - else - client->RetrieveAvatarIfNeeded(netPlayerData.playerId, info); + playerData.reset( + new PlayerData(netPlayerJoined->playerId, 0, info.ptype, netPlayerJoined->isAdmin ? PLAYER_RIGHTS_ADMIN : PLAYER_RIGHTS_NORMAL)); + playerData->SetName(info.playerName); + if (info.hasAvatar) + { + string avatarFile; + if (client->GetAvatarManager().GetAvatarFileName(info.avatar, avatarFile)) + playerData->SetAvatarFile(client->GetQtToolsInterface().stringToUtf8(avatarFile)); + else + client->RetrieveAvatarIfNeeded(netPlayerJoined->playerId, info); + } } - } - else - { - ostringstream name; - name << "#" << netPlayerData.playerId; + else + { + ostringstream name; + name << "#" << netPlayerJoined->playerId; - // Request player info. - client->RequestPlayerInfo(netPlayerData.playerId, true); - // Use temporary data until the PlayerInfo request is completed. - playerData.reset( - new PlayerData(netPlayerData.playerId, 0, PLAYER_TYPE_HUMAN, netPlayerData.prights)); - playerData->SetName(name.str()); + // Request player info. + client->RequestPlayerInfo(netPlayerJoined->playerId, true); + // Use temporary data until the PlayerInfo request is completed. + playerData.reset( + new PlayerData(netPlayerJoined->playerId, 0, PLAYER_TYPE_HUMAN, netPlayerJoined->isAdmin ? PLAYER_RIGHTS_ADMIN : PLAYER_RIGHTS_NORMAL)); + playerData->SetName(name.str()); + } + client->AddPlayerData(playerData); } - client->AddPlayerData(playerData); } } @@ -1137,7 +1223,11 @@ ClientStateSynchronizeStart::TimerLoop(const boost::system::error_code& ec, boos if (client->IsSynchronized()) { // Acknowledge start. - boost::shared_ptr startAck(new NetPacketStartEventAck); + boost::shared_ptr startAck(new NetPacket(NetPacket::Alloc)); + startAck->GetMsg()->present = PokerTHMessage_PR_startEventAckMessage; + StartEventAckMessage_t *netStartAck = &startAck->GetMsg()->choice.startEventAckMessage; + netStartAck->gameId = client->GetGameId(); + client->GetSender().Send(client->GetContext().GetSessionData(), startAck); // Unsubscribe lobby messages. client->UnsubscribeLobbyMsg(); @@ -1158,7 +1248,7 @@ ClientStateSynchronizeStart::TimerLoop(const boost::system::error_code& ec, boos void ClientStateSynchronizeStart::InternalHandlePacket(boost::shared_ptr /*client*/, boost::shared_ptr tmpPacket) { - if (tmpPacket->ToNetPacketGameStart()) + if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_gameStartMessage) throw ClientException(__FILE__, __LINE__, ERR_NET_START_TIMEOUT, 0); } @@ -1192,30 +1282,35 @@ ClientStateWaitStart::Exit(boost::shared_ptr /*client*/) void ClientStateWaitStart::InternalHandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket) { - if (tmpPacket->ToNetPacketGameStart()) + if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_gameStartMessage) { // Start the network game as client. - NetPacketGameStart::Data gameStartData; - tmpPacket->ToNetPacketGameStart()->GetData(gameStartData); + GameStartMessage_t *netGameStart = &tmpPacket->GetMsg()->choice.gameStartMessage; - client->SetStartData(gameStartData.startData); + StartData startData; + startData.numberOfPlayers = netGameStart->playerSeats.list.count; + startData.startDealerPlayerId = netGameStart->startDealerPlayerId; + client->SetStartData(startData); // Set player numbers using the game start data slots. - NetPacketGameStart::PlayerSlotList::const_iterator slot_i = gameStartData.playerSlots.begin(); - NetPacketGameStart::PlayerSlotList::const_iterator slot_end = gameStartData.playerSlots.end(); - int num = 0; - - while (slot_i != slot_end) + NonZeroId_t **playerIds = netGameStart->playerSeats.list.array; + unsigned numPlayers = netGameStart->playerSeats.list.count; + // Request player info for players if needed. + if (numPlayers && playerIds && *playerIds) { - unsigned playerId = (*slot_i).playerId; - boost::shared_ptr tmpPlayer = client->GetPlayerDataByUniqueId(playerId); - if (!tmpPlayer.get()) - throw ClientException(__FILE__, __LINE__, ERR_NET_UNKNOWN_PLAYER_ID, 0); - tmpPlayer->SetNumber(num); + for (unsigned i = 0; i < numPlayers; i++) + { + unsigned playerId = *playerIds[i]; + boost::shared_ptr tmpPlayer = client->GetPlayerDataByUniqueId(playerId); + if (!tmpPlayer.get()) + throw ClientException(__FILE__, __LINE__, ERR_NET_UNKNOWN_PLAYER_ID, 0); + tmpPlayer->SetNumber(i); - ++num; - ++slot_i; + ++i; + } } + else + throw ClientException(__FILE__, __LINE__, ERR_NET_INVALID_PLAYER_COUNT, 0); client->InitGame(); client->GetCallback().SignalNetClientGameInfo(MSG_NET_GAME_CLIENT_START); @@ -1253,21 +1348,21 @@ ClientStateWaitHand::Exit(boost::shared_ptr /*client*/) void ClientStateWaitHand::InternalHandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket) { - if (tmpPacket->ToNetPacketHandStart()) + if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_handStartMessage) { // Remove all players which left the server. client->RemoveDisconnectedPlayers(); + // Hand was started. // These are the cards. Good luck. - NetPacketHandStart::Data tmpData; - tmpPacket->ToNetPacketHandStart()->GetData(tmpData); + HandStartMessage_t *netHandStart = &tmpPacket->GetMsg()->choice.handStartMessage; int myCards[2]; - myCards[0] = (int)tmpData.yourCards[0]; - myCards[1] = (int)tmpData.yourCards[1]; + myCards[0] = (int)netHandStart->yourCard1; + myCards[1] = (int)netHandStart->yourCard2; client->GetGame()->getSeatsList()->front()->setMyCards(myCards); client->GetGame()->initHand(); - client->GetGame()->getCurrentHand()->setSmallBlind(tmpData.smallBlind); - client->GetGame()->getCurrentHand()->getCurrentBeRo()->setMinimumRaise(2 * tmpData.smallBlind); + client->GetGame()->getCurrentHand()->setSmallBlind(netHandStart->smallBlind); + client->GetGame()->getCurrentHand()->getCurrentBeRo()->setMinimumRaise(2 * netHandStart->smallBlind); client->GetGame()->startHand(); client->GetGui().dealHoleCards(); client->GetGui().refreshGameLabels(GAME_STATE_PREFLOP); @@ -1277,14 +1372,14 @@ ClientStateWaitHand::InternalHandlePacket(boost::shared_ptr client client->GetCallback().SignalNetClientGameInfo(MSG_NET_GAME_CLIENT_HAND_START); client->SetState(ClientStateRunHand::Instance()); } - else if (tmpPacket->ToNetPacketEndOfGame()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_endOfGameMessage) { boost::shared_ptr curGame = client->GetGame(); if (curGame.get()) { - NetPacketEndOfGame::Data endData; - tmpPacket->ToNetPacketEndOfGame()->GetData(endData); - boost::shared_ptr tmpPlayer = curGame->getPlayerByUniqueId(endData.winnerPlayerId); + EndOfGameMessage_t *netEndOfGame = &tmpPacket->GetMsg()->choice.endOfGameMessage; + + boost::shared_ptr tmpPlayer = curGame->getPlayerByUniqueId(netEndOfGame->winnerPlayerId); if (!tmpPlayer) throw ClientException(__FILE__, __LINE__, ERR_NET_UNKNOWN_PLAYER_ID, 0); client->GetGui().logPlayerWinGame(tmpPlayer->getMyName(), curGame->getMyGameID()); @@ -1329,22 +1424,22 @@ void ClientStateRunHand::InternalHandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket) { boost::shared_ptr curGame = client->GetGame(); - if (tmpPacket->ToNetPacketPlayersActionDone()) + if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_playersActionDoneMessage) { - NetPacketPlayersActionDone::Data actionDoneData; - tmpPacket->ToNetPacketPlayersActionDone()->GetData(actionDoneData); - boost::shared_ptr tmpPlayer = curGame->getPlayerByUniqueId(actionDoneData.playerId); + PlayersActionDoneMessage_t *netActionDone = &tmpPacket->GetMsg()->choice.playersActionDoneMessage; + + boost::shared_ptr tmpPlayer = curGame->getPlayerByUniqueId(netActionDone->playerId); if (!tmpPlayer) throw ClientException(__FILE__, __LINE__, ERR_NET_UNKNOWN_PLAYER_ID, 0); bool isBigBlind = false; - if (actionDoneData.gameState == GAME_STATE_PREFLOP_SMALL_BLIND) + if (netActionDone->gameState == NetGameState_statePreflopSmallBlind) { curGame->getCurrentHand()->getCurrentBeRo()->setSmallBlindPositionId(tmpPlayer->getMyUniqueID()); tmpPlayer->setMyButton(BUTTON_SMALL_BLIND); } - else if (actionDoneData.gameState == GAME_STATE_PREFLOP_BIG_BLIND) + else if (netActionDone->gameState == NetGameState_statePreflopBigBlind) { curGame->getCurrentHand()->getCurrentBeRo()->setBigBlindPositionId(tmpPlayer->getMyUniqueID()); tmpPlayer->setMyButton(BUTTON_BIG_BLIND); @@ -1352,29 +1447,33 @@ ClientStateRunHand::InternalHandlePacket(boost::shared_ptr client, } else // no blind -> log { - if (actionDoneData.playerAction) + if (netActionDone->playerAction) { - assert(actionDoneData.totalPlayerBet >= (unsigned)tmpPlayer->getMySet()); + assert(netActionDone->totalPlayerBet >= (long)tmpPlayer->getMySet()); client->GetGui().logPlayerActionMsg( tmpPlayer->getMyName(), - actionDoneData.playerAction, - actionDoneData.totalPlayerBet - tmpPlayer->getMySet()); + netActionDone->playerAction, + netActionDone->totalPlayerBet - tmpPlayer->getMySet()); } // Update last players turn only after the blinds. curGame->getCurrentHand()->setLastPlayersTurn(tmpPlayer->getMyID()); } - tmpPlayer->setMyAction(actionDoneData.playerAction); - tmpPlayer->setMySetAbsolute(actionDoneData.totalPlayerBet); - tmpPlayer->setMyCash(actionDoneData.playerMoney); - curGame->getCurrentHand()->getCurrentBeRo()->setHighestSet(actionDoneData.highestSet); - curGame->getCurrentHand()->getCurrentBeRo()->setMinimumRaise(actionDoneData.minimumRaise); + tmpPlayer->setMyAction(netActionDone->playerAction); + tmpPlayer->setMySetAbsolute(netActionDone->totalPlayerBet); + tmpPlayer->setMyCash(netActionDone->playerMoney); + curGame->getCurrentHand()->getCurrentBeRo()->setHighestSet(netActionDone->highestSet); + curGame->getCurrentHand()->getCurrentBeRo()->setMinimumRaise(netActionDone->minimumRaise); curGame->getCurrentHand()->getBoard()->collectSets(); curGame->getCurrentHand()->switchRounds(); //log blinds sets after setting bigblind-button if (isBigBlind) { - client->GetGui().logNewBlindsSetsMsg(curGame->getPlayerByUniqueId(curGame->getCurrentHand()->getCurrentBeRo()->getSmallBlindPositionId())->getMySet(), curGame->getPlayerByUniqueId(curGame->getCurrentHand()->getCurrentBeRo()->getBigBlindPositionId())->getMySet(), curGame->getPlayerByUniqueId(curGame->getCurrentHand()->getCurrentBeRo()->getSmallBlindPositionId())->getMyName(), curGame->getPlayerByUniqueId(curGame->getCurrentHand()->getCurrentBeRo()->getBigBlindPositionId())->getMyName()); + client->GetGui().logNewBlindsSetsMsg( + curGame->getPlayerByUniqueId(curGame->getCurrentHand()->getCurrentBeRo()->getSmallBlindPositionId())->getMySet(), + curGame->getPlayerByUniqueId(curGame->getCurrentHand()->getCurrentBeRo()->getBigBlindPositionId())->getMySet(), + curGame->getPlayerByUniqueId(curGame->getCurrentHand()->getCurrentBeRo()->getSmallBlindPositionId())->getMyName(), + curGame->getPlayerByUniqueId(curGame->getCurrentHand()->getCurrentBeRo()->getBigBlindPositionId())->getMyName()); client->GetGui().flushLogAtHand(); } @@ -1394,19 +1493,19 @@ ClientStateRunHand::InternalHandlePacket(boost::shared_ptr client, client->GetGui().refreshButton(); client->GetGui().updateMyButtonsState(); } - else if (tmpPacket->ToNetPacketPlayersTurn()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_playersTurnMessage) { - NetPacketPlayersTurn::Data turnData; - tmpPacket->ToNetPacketPlayersTurn()->GetData(turnData); - boost::shared_ptr tmpPlayer = curGame->getPlayerByUniqueId(turnData.playerId); + PlayersTurnMessage_t *netPlayersTurn = &tmpPacket->GetMsg()->choice.playersTurnMessage; + + boost::shared_ptr tmpPlayer = curGame->getPlayerByUniqueId(netPlayersTurn->playerId); if (!tmpPlayer) throw ClientException(__FILE__, __LINE__, ERR_NET_UNKNOWN_PLAYER_ID, 0); // Set round. - if (curGame->getCurrentHand()->getCurrentRound() != turnData.gameState) + if (curGame->getCurrentHand()->getCurrentRound() != netPlayersTurn->gameState) { ResetPlayerActions(*curGame); - curGame->getCurrentHand()->setCurrentRound(turnData.gameState); + curGame->getCurrentHand()->setCurrentRound(netPlayersTurn->gameState); // Refresh actions. client->GetGui().refreshSet(); client->GetGui().refreshAction(); @@ -1431,13 +1530,14 @@ ClientStateRunHand::InternalHandlePacket(boost::shared_ptr client, if (tmpPlayer->getMyID() == 0) // Is this the GUI player? client->GetGui().meInAction(); } - else if (tmpPacket->ToNetPacketDealFlopCards()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_dealFlopCardsMessage) { - NetPacketDealFlopCards::Data cardsData; - tmpPacket->ToNetPacketDealFlopCards()->GetData(cardsData); + DealFlopCardsMessage_t *netDealFlop = &tmpPacket->GetMsg()->choice.dealFlopCardsMessage; + int tmpCards[5]; - for (int num = 0; num < 3; num++) - tmpCards[num] = static_cast(cardsData.flopCards[num]); + tmpCards[0] = static_cast(netDealFlop->flopCard1); + tmpCards[1] = static_cast(netDealFlop->flopCard2); + tmpCards[2] = static_cast(netDealFlop->flopCard3); tmpCards[3] = tmpCards[4] = 0; curGame->getCurrentHand()->getBoard()->setMyCards(tmpCards); curGame->getCurrentHand()->getBoard()->collectPot(); @@ -1449,13 +1549,13 @@ ClientStateRunHand::InternalHandlePacket(boost::shared_ptr client, client->GetGui().refreshSet(); client->GetGui().dealBeRoCards(1); } - else if (tmpPacket->ToNetPacketDealTurnCard()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_dealTurnCardMessage) { - NetPacketDealTurnCard::Data cardsData; - tmpPacket->ToNetPacketDealTurnCard()->GetData(cardsData); + DealTurnCardMessage_t *netDealTurn = &tmpPacket->GetMsg()->choice.dealTurnCardMessage; + int tmpCards[5]; curGame->getCurrentHand()->getBoard()->getMyCards(tmpCards); - tmpCards[3] = static_cast(cardsData.turnCard); + tmpCards[3] = static_cast(netDealTurn->turnCard); curGame->getCurrentHand()->getBoard()->setMyCards(tmpCards); curGame->getCurrentHand()->getBoard()->collectPot(); curGame->getCurrentHand()->setLastPlayersTurn(-1); @@ -1466,13 +1566,13 @@ ClientStateRunHand::InternalHandlePacket(boost::shared_ptr client, client->GetGui().refreshSet(); client->GetGui().dealBeRoCards(2); } - else if (tmpPacket->ToNetPacketDealRiverCard()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_dealRiverCardMessage) { - NetPacketDealRiverCard::Data cardsData; - tmpPacket->ToNetPacketDealRiverCard()->GetData(cardsData); + DealRiverCardMessage_t *netDealRiver = &tmpPacket->GetMsg()->choice.dealRiverCardMessage; + int tmpCards[5]; curGame->getCurrentHand()->getBoard()->getMyCards(tmpCards); - tmpCards[4] = static_cast(cardsData.riverCard); + tmpCards[4] = static_cast(netDealRiver->riverCard); curGame->getCurrentHand()->getBoard()->setMyCards(tmpCards); curGame->getCurrentHand()->getBoard()->collectPot(); curGame->getCurrentHand()->setLastPlayersTurn(-1); @@ -1483,120 +1583,126 @@ ClientStateRunHand::InternalHandlePacket(boost::shared_ptr client, client->GetGui().refreshSet(); client->GetGui().dealBeRoCards(3); } - else if (tmpPacket->ToNetPacketAllInShowCards()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_allInShowCardsMessage) { + AllInShowCardsMessage_t *netAllInShow = &tmpPacket->GetMsg()->choice.allInShowCardsMessage; + curGame->getCurrentHand()->setAllInCondition(true); - NetPacketAllInShowCards::Data allInData; - tmpPacket->ToNetPacketAllInShowCards()->GetData(allInData); - - NetPacketAllInShowCards::PlayerCardsList::const_iterator i - = allInData.playerCards.begin(); - NetPacketAllInShowCards::PlayerCardsList::const_iterator end - = allInData.playerCards.end(); - - while (i != end) + // Set player numbers using the game start data slots. + PlayerAllIn_t **allInPlayers = netAllInShow->playersAllIn.list.array; + unsigned numPlayers = netAllInShow->playersAllIn.list.count; + // Request player info for players if needed. + if (numPlayers && allInPlayers && *allInPlayers) { - boost::shared_ptr tmpPlayer = curGame->getPlayerByUniqueId((*i).playerId); - if (!tmpPlayer) - throw ClientException(__FILE__, __LINE__, ERR_NET_UNKNOWN_PLAYER_ID, 0); + for (unsigned i = 0; i < numPlayers; i++) + { + PlayerAllIn_t *p = allInPlayers[i]; - int tmpCards[2]; - tmpCards[0] = static_cast((*i).cards[0]); - tmpCards[1] = static_cast((*i).cards[1]); - tmpPlayer->setMyCards(tmpCards); - ++i; + boost::shared_ptr tmpPlayer = curGame->getPlayerByUniqueId(p->playerId); + if (!tmpPlayer) + throw ClientException(__FILE__, __LINE__, ERR_NET_UNKNOWN_PLAYER_ID, 0); + + int tmpCards[2]; + tmpCards[0] = static_cast(p->allInCard1); + tmpCards[1] = static_cast(p->allInCard2); + tmpPlayer->setMyCards(tmpCards); + ++i; + } } client->GetGui().flipHolecardsAllIn(); } - else if (tmpPacket->ToNetPacketEndOfHandHideCards()) + else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_endOfHandMessage) { - curGame->getCurrentHand()->getBoard()->collectPot(); - // Reset player sets - ResetPlayerSets(*curGame); - client->GetGui().refreshPot(); - client->GetGui().refreshSet(); - // Synchronize with GUI. - client->GetGui().waitForGuiUpdateDone(); + EndOfHandMessage_t *netEndHand = &tmpPacket->GetMsg()->choice.endOfHandMessage; - // End of Hand, but keep cards hidden. - NetPacketEndOfHandHideCards::Data endHandData; - tmpPacket->ToNetPacketEndOfHandHideCards()->GetData(endHandData); - - boost::shared_ptr tmpPlayer = curGame->getPlayerByUniqueId(endHandData.playerId); - if (!tmpPlayer) - throw ClientException(__FILE__, __LINE__, ERR_NET_UNKNOWN_PLAYER_ID, 0); - - tmpPlayer->setMyCash(endHandData.playerMoney); - tmpPlayer->setLastMoneyWon(endHandData.moneyWon); - list winnerList; - winnerList.push_back(tmpPlayer->getMyUniqueID()); - - curGame->getCurrentHand()->getBoard()->setPot(0); - curGame->getCurrentHand()->getBoard()->setWinners(winnerList); - - client->GetGui().postRiverRunAnimation1(); - - // Wait for next Hand. - client->GetCallback().SignalNetClientGameInfo(MSG_NET_GAME_SERVER_HAND_END); - client->SetState(ClientStateWaitHand::Instance()); - } - else if (tmpPacket->ToNetPacketEndOfHandShowCards()) - { - curGame->getCurrentHand()->getBoard()->collectPot(); - // Reset player sets - ResetPlayerSets(*curGame); - client->GetGui().refreshPot(); - client->GetGui().refreshSet(); - // Synchronize with GUI. - client->GetGui().waitForGuiUpdateDone(); - - // End of Hand, show cards. - NetPacketEndOfHandShowCards::Data endHandData; - tmpPacket->ToNetPacketEndOfHandShowCards()->GetData(endHandData); - - NetPacketEndOfHandShowCards::PlayerResultList::const_iterator i - = endHandData.playerResults.begin(); - NetPacketEndOfHandShowCards::PlayerResultList::const_iterator end - = endHandData.playerResults.end(); - - list winnerList; - int highestValueOfCards = 0; - while (i != end) + if (netEndHand->endOfHandType.present == endOfHandType_PR_endOfHandHideCards) { - boost::shared_ptr tmpPlayer = curGame->getPlayerByUniqueId((*i).playerId); + EndOfHandHideCards_t *hideCards = &netEndHand->endOfHandType.choice.endOfHandHideCards; + curGame->getCurrentHand()->getBoard()->collectPot(); + // Reset player sets + ResetPlayerSets(*curGame); + client->GetGui().refreshPot(); + client->GetGui().refreshSet(); + // Synchronize with GUI. + client->GetGui().waitForGuiUpdateDone(); + + // End of Hand, but keep cards hidden. + boost::shared_ptr tmpPlayer = curGame->getPlayerByUniqueId(hideCards->playerId); if (!tmpPlayer) throw ClientException(__FILE__, __LINE__, ERR_NET_UNKNOWN_PLAYER_ID, 0); - int tmpCards[2]; - int bestHandPos[5]; - tmpCards[0] = static_cast((*i).cards[0]); - tmpCards[1] = static_cast((*i).cards[1]); - tmpPlayer->setMyCards(tmpCards); - for (int num = 0; num < 5; num++) - bestHandPos[num] = (*i).bestHandPos[num]; - tmpPlayer->setMyCardsValueInt((*i).valueOfCards); - tmpPlayer->setMyBestHandPosition(bestHandPos); - if (tmpPlayer->getMyCardsValueInt() > highestValueOfCards) - highestValueOfCards = tmpPlayer->getMyCardsValueInt(); - tmpPlayer->setMyCash((*i).playerMoney); - tmpPlayer->setLastMoneyWon((*i).moneyWon); - if ((*i).moneyWon) - winnerList.push_back((*i).playerId); + tmpPlayer->setMyCash(hideCards->playerMoney); + tmpPlayer->setLastMoneyWon(hideCards->moneyWon); + list winnerList; + winnerList.push_back(tmpPlayer->getMyUniqueID()); - ++i; + curGame->getCurrentHand()->getBoard()->setPot(0); + curGame->getCurrentHand()->getBoard()->setWinners(winnerList); + + client->GetGui().postRiverRunAnimation1(); + + // Wait for next Hand. + client->GetCallback().SignalNetClientGameInfo(MSG_NET_GAME_SERVER_HAND_END); + client->SetState(ClientStateWaitHand::Instance()); } - curGame->getCurrentHand()->getCurrentBeRo()->setHighestCardsValue(highestValueOfCards); - curGame->getCurrentHand()->getBoard()->setPot(0); - curGame->getCurrentHand()->getBoard()->setWinners(winnerList); + else if (netEndHand->endOfHandType.present == endOfHandType_PR_endOfHandShowCards) + { + EndOfHandShowCards_t *showCards = &netEndHand->endOfHandType.choice.endOfHandShowCards; - client->GetGui().postRiverRunAnimation1(); + curGame->getCurrentHand()->getBoard()->collectPot(); + // Reset player sets + ResetPlayerSets(*curGame); + client->GetGui().refreshPot(); + client->GetGui().refreshSet(); + // Synchronize with GUI. + client->GetGui().waitForGuiUpdateDone(); - // Wait for next Hand. - client->GetCallback().SignalNetClientGameInfo(MSG_NET_GAME_CLIENT_HAND_END); - client->SetState(ClientStateWaitHand::Instance()); + // End of Hand, show cards. + list winnerList; + int highestValueOfCards = 0; + PlayerResult_t **playerResults = showCards->playerResults.list.array; + unsigned numResults = showCards->playerResults.list.count; + // Request player info for players if needed. + if (numResults && playerResults && *playerResults) + { + for (unsigned i = 0; i < numResults; i++) + { + PlayerResult_t *r = playerResults[i]; + + boost::shared_ptr tmpPlayer = curGame->getPlayerByUniqueId(r->playerId); + if (!tmpPlayer) + throw ClientException(__FILE__, __LINE__, ERR_NET_UNKNOWN_PLAYER_ID, 0); + + int tmpCards[2]; + int bestHandPos[5]; + tmpCards[0] = static_cast(r->resultCard1); + tmpCards[1] = static_cast(r->resultCard2); + tmpPlayer->setMyCards(tmpCards); + for (int num = 0; num < 5; num++) + bestHandPos[num] = *r->bestHandPosition.list.array[num]; + tmpPlayer->setMyCardsValueInt(r->cardsValue); + tmpPlayer->setMyBestHandPosition(bestHandPos); + if (tmpPlayer->getMyCardsValueInt() > highestValueOfCards) + highestValueOfCards = tmpPlayer->getMyCardsValueInt(); + tmpPlayer->setMyCash(r->playerMoney); + tmpPlayer->setLastMoneyWon(r->moneyWon); + if (r->moneyWon) + winnerList.push_back(r->playerId); + } + } + + curGame->getCurrentHand()->getCurrentBeRo()->setHighestCardsValue(highestValueOfCards); + curGame->getCurrentHand()->getBoard()->setPot(0); + curGame->getCurrentHand()->getBoard()->setWinners(winnerList); + + client->GetGui().postRiverRunAnimation1(); + + // Wait for next Hand. + client->GetCallback().SignalNetClientGameInfo(MSG_NET_GAME_CLIENT_HAND_END); + client->SetState(ClientStateWaitHand::Instance()); + } } - // Synchronize with GUI. client->GetGui().waitForGuiUpdateDone(); } diff --git a/src/net/common/clientthread.cpp b/src/net/common/clientthread.cpp index 302970ef..1076c0bd 100644 --- a/src/net/common/clientthread.cpp +++ b/src/net/common/clientthread.cpp @@ -119,18 +119,20 @@ ClientThread::SignalTermination() void ClientThread::SendKickPlayer(unsigned playerId) { - boost::shared_ptr request(new NetPacketKickPlayer); - NetPacketKickPlayer::Data requestData; - requestData.playerId = playerId; - static_cast(request.get())->SetData(requestData); - m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), request)); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_kickPlayerRequestMessage; + KickPlayerRequestMessage_t *netKick = &packet->GetMsg()->choice.kickPlayerRequestMessage; + netKick->gameId = GetGameId(); + netKick->playerId = playerId; + m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), packet)); } void ClientThread::SendLeaveCurrentGame() { - boost::shared_ptr request(new NetPacketLeaveCurrentGame); - m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), request)); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_leaveGameRequestMessage; + m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), packet)); } void @@ -138,17 +140,12 @@ ClientThread::SendStartEvent(bool fillUpWithCpuPlayers) { // Warning: This function is called in the context of the GUI thread. // Create a network packet for the server start event. - boost::shared_ptr start(new NetPacketStartEvent); - NetPacketStartEvent::Data startData; - startData.fillUpWithCpuPlayers = fillUpWithCpuPlayers; - try - { - static_cast(start.get())->SetData(startData); - m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), start)); - } catch (const NetException &e) - { - LOG_ERROR("ClientThread::SendStartEvent: " << e.what()); - } + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_startEventMessage; + StartEventMessage_t *netStartEvent = &packet->GetMsg()->choice.startEventMessage; + netStartEvent->gameId = GetGameId(); + netStartEvent->fillWithComputerPlayers = fillUpWithCpuPlayers; + m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), packet)); } void @@ -156,25 +153,20 @@ ClientThread::SendPlayerAction() { // Warning: This function is called in the context of the GUI thread. // Create a network packet containing the current player action. - boost::shared_ptr action(new NetPacketPlayersAction); - NetPacketPlayersAction::Data actionData; + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_myActionRequestMessage; + MyActionRequestMessage_t *netMyAction = &packet->GetMsg()->choice.myActionRequestMessage; + netMyAction->gameId = GetGameId(); boost::shared_ptr myPlayer = GetGame()->getSeatsList()->front(); - actionData.gameState = static_cast(GetGame()->getCurrentHand()->getCurrentRound()); - actionData.playerAction = static_cast(myPlayer->getMyAction()); + netMyAction->gameState = GetGame()->getCurrentHand()->getCurrentRound(); + netMyAction->myAction = myPlayer->getMyAction(); // Only send last bet if not fold/checked. - if (actionData.playerAction != PLAYER_ACTION_FOLD && actionData.playerAction != PLAYER_ACTION_CHECK) - actionData.playerBet = myPlayer->getMyLastRelativeSet(); + if (myPlayer->getMyAction() != PLAYER_ACTION_FOLD && myPlayer->getMyAction() != PLAYER_ACTION_CHECK) + netMyAction->myRelativeBet = myPlayer->getMyLastRelativeSet(); else - actionData.playerBet = 0; - try - { - static_cast(action.get())->SetData(actionData); - // Just dump the packet. - m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), action)); - } catch (const NetException &e) - { - LOG_ERROR("ClientThread::SendPlayerAction: " << e.what()); - } + netMyAction->myRelativeBet = 0; + // Just dump the packet. + m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), packet)); } void @@ -182,18 +174,16 @@ ClientThread::SendChatMessage(const std::string &msg) { // Warning: This function is called in the context of the GUI thread. // Create a network packet containing the chat message. - boost::shared_ptr chat(new NetPacketSendChatText); - NetPacketSendChatText::Data chatData; - chatData.text = msg; - try - { - static_cast(chat.get())->SetData(chatData); - // Just dump the packet. - m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), chat)); - } catch (const NetException &e) - { - LOG_ERROR("ClientThread::SendChatMessage: " << e.what()); - } + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_chatRequestMessage; + ChatRequestMessage_t *netChat = &packet->GetMsg()->choice.chatRequestMessage; + netChat->gameId = GetGameId(); + OCTET_STRING_fromBuf(&netChat->chatText, + msg.c_str(), + msg.length()); + + // Just dump the packet. + m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), packet)); } void @@ -201,18 +191,21 @@ ClientThread::SendJoinFirstGame(const std::string &password) { // Warning: This function is called in the context of the GUI thread. // Create a network packet to request joining a game. - boost::shared_ptr join(new NetPacketJoinGame); - NetPacketJoinGame::Data joinData; - joinData.gameId = 1; - joinData.password = password; - try + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_joinGameRequestMessage; + JoinGameRequestMessage_t *netJoinGame = &packet->GetMsg()->choice.joinGameRequestMessage; + if (!password.empty()) { - static_cast(join.get())->SetData(joinData); - m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), join)); - } catch (const NetException &e) - { - LOG_ERROR("ClientThread::SendJoinFirstGame: " << e.what()); + netJoinGame->password = OCTET_STRING_new_fromBuf( + &asn_DEF_UTF8String, + password.c_str(), + password.length()); } + netJoinGame->joinGameAction.present = joinGameAction_PR_joinExistingGame; + + JoinExistingGame_t *joinExisting = &netJoinGame->joinGameAction.choice.joinExistingGame; + joinExisting->gameId = 1; + m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), packet)); } void @@ -220,18 +213,21 @@ ClientThread::SendJoinGame(unsigned gameId, const std::string &password) { // Warning: This function is called in the context of the GUI thread. // Create a network packet to request joining a game. - boost::shared_ptr join(new NetPacketJoinGame); - NetPacketJoinGame::Data joinData; - joinData.password = password; - joinData.gameId = gameId; - try + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_joinGameRequestMessage; + JoinGameRequestMessage_t *netJoinGame = &packet->GetMsg()->choice.joinGameRequestMessage; + if (!password.empty()) { - static_cast(join.get())->SetData(joinData); - m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), join)); - } catch (const NetException &e) - { - LOG_ERROR("ClientThread::SendJoinGame: " << e.what()); + netJoinGame->password = OCTET_STRING_new_fromBuf( + &asn_DEF_UTF8String, + password.c_str(), + password.length()); } + netJoinGame->joinGameAction.present = joinGameAction_PR_joinExistingGame; + + JoinExistingGame_t *joinExisting = &netJoinGame->joinGameAction.choice.joinExistingGame; + joinExisting->gameId = gameId; + m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), packet)); } void @@ -239,50 +235,56 @@ ClientThread::SendCreateGame(const GameData &gameData, const std::string &name, { // Warning: This function is called in the context of the GUI thread. // Create a network packet to request creating a new game. - boost::shared_ptr create(new NetPacketCreateGame); - NetPacketCreateGame::Data createData; - createData.gameData = gameData; - createData.gameName = name; - createData.password = password; - try + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_joinGameRequestMessage; + JoinGameRequestMessage_t *netJoinGame = &packet->GetMsg()->choice.joinGameRequestMessage; + if (!password.empty()) { - static_cast(create.get())->SetData(createData); - m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), create)); - } catch (const NetException &e) - { - LOG_ERROR("ClientThread::SendCreateGame: " << e.what()); + netJoinGame->password = OCTET_STRING_new_fromBuf( + &asn_DEF_UTF8String, + password.c_str(), + password.length()); } + netJoinGame->joinGameAction.present = joinGameAction_PR_joinNewGame; + + JoinNewGame_t *joinNew = &netJoinGame->joinGameAction.choice.joinNewGame; + NetPacket::SetGameData(gameData, &joinNew->gameInfo); + OCTET_STRING_fromBuf(&joinNew->gameInfo.gameName, + name.c_str(), + name.length()); + m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), packet)); } void ClientThread::SendResetTimeout() { - boost::shared_ptr reset(new NetPacketResetTimeout); - m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), reset)); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_resetTimeoutMessage; + m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), packet)); } void ClientThread::SendAskKickPlayer(unsigned playerId) { - boost::shared_ptr ask(new NetPacketAskKickPlayer); - NetPacketAskKickPlayer::Data askData; - askData.playerId = playerId; - static_cast(ask.get())->SetData(askData); - m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), ask)); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_askKickPlayerMessage; + AskKickPlayerMessage_t *netAsk = &packet->GetMsg()->choice.askKickPlayerMessage; + netAsk->playerId = playerId; + m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), packet)); } void ClientThread::SendVoteKick(bool doKick) { - boost::shared_ptr vote(new NetPacketVoteKickPlayer); - NetPacketVoteKickPlayer::Data voteData; + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_voteKickRequestMessage; + VoteKickRequestMessage_t *netVote = &packet->GetMsg()->choice.voteKickRequestMessage; { boost::mutex::scoped_lock lock(m_curPetitionIdMutex); - voteData.petitionId = m_curPetitionId; + netVote->petitionId = m_curPetitionId; } - voteData.vote = doKick ? KICK_VOTE_IN_FAVOUR : KICK_VOTE_AGAINST; - static_cast(vote.get())->SetData(voteData); - m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), vote)); + netVote->voteKick = doKick; + m_ioService->post(boost::bind(&ClientThread::SendSessionPacket, shared_from_this(), packet)); } void @@ -523,11 +525,11 @@ ClientThread::RequestPlayerInfo(unsigned id, bool requestAvatar) { if (find(m_playerInfoRequestList.begin(), m_playerInfoRequestList.end(), id) == m_playerInfoRequestList.end()) { - boost::shared_ptr req(new NetPacketRetrievePlayerInfo); - NetPacketRetrievePlayerInfo::Data reqData; - reqData.playerId = id; - static_cast(req.get())->SetData(reqData); - GetSender().Send(GetContext().GetSessionData(), req); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_playerInfoRequestMessage; + PlayerInfoRequestMessage_t *netPlayerInfo = &packet->GetMsg()->choice.playerInfoRequestMessage; + netPlayerInfo->playerId = id; + GetSender().Send(GetContext().GetSessionData(), packet); m_playerInfoRequestList.push_back(id); @@ -637,21 +639,23 @@ ClientThread::RetrieveAvatarIfNeeded(unsigned id, const PlayerInfo &info) } else { - boost::shared_ptr retrieveAvatar(new NetPacketRetrieveAvatar); - NetPacketRetrieveAvatar::Data retrieveAvatarData; - retrieveAvatarData.requestId = id; - retrieveAvatarData.avatar = info.avatar; - static_cast(retrieveAvatar.get())->SetData(retrieveAvatarData); - GetSender().Send(GetContext().GetSessionData(), retrieveAvatar); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_avatarRequestMessage; + AvatarRequestMessage_t *netAvatar = &packet->GetMsg()->choice.avatarRequestMessage; + netAvatar->requestId = id; + OCTET_STRING_fromBuf(&netAvatar->avatar, + (const char *)info.avatar.data, + MD5_DATA_SIZE); + GetSender().Send(GetContext().GetSessionData(), packet); } } } } void -ClientThread::AddTempAvatarData(unsigned playerId, unsigned avatarSize, AvatarFileType type) +ClientThread::AddTempAvatarFile(unsigned playerId, unsigned avatarSize, AvatarFileType type) { - boost::shared_ptr tmpAvatar(new AvatarData); + boost::shared_ptr tmpAvatar(new AvatarFile); tmpAvatar->fileData.reserve(avatarSize); tmpAvatar->fileType = type; tmpAvatar->reportedSize = avatarSize; @@ -660,9 +664,9 @@ ClientThread::AddTempAvatarData(unsigned playerId, unsigned avatarSize, AvatarFi } void -ClientThread::StoreInTempAvatarData(unsigned playerId, const vector &data) +ClientThread::StoreInTempAvatarFile(unsigned playerId, const vector &data) { - AvatarDataMap::iterator pos = m_tempAvatarMap.find(playerId); + AvatarFileMap::iterator pos = m_tempAvatarMap.find(playerId); if (pos == m_tempAvatarMap.end()) throw ClientException(__FILE__, __LINE__, ERR_NET_INVALID_REQUEST_ID, 0); // We trust the server (concerning size of the data). @@ -670,33 +674,33 @@ ClientThread::StoreInTempAvatarData(unsigned playerId, const vector tmpAvatar = pos->second; + boost::shared_ptr tmpAvatar = pos->second; unsigned avatarSize = (unsigned)tmpAvatar->fileData.size(); if (avatarSize != tmpAvatar->reportedSize) LOG_ERROR("Client received invalid avatar file size!"); else - PassAvatarDataToManager(playerId, tmpAvatar); + PassAvatarFileToManager(playerId, tmpAvatar); // Free memory. m_tempAvatarMap.erase(pos); } void -ClientThread::PassAvatarDataToManager(unsigned playerId, boost::shared_ptr avatarData) +ClientThread::PassAvatarFileToManager(unsigned playerId, boost::shared_ptr AvatarFile) { PlayerInfo tmpPlayerInfo; if (!GetCachedPlayerInfo(playerId, tmpPlayerInfo)) LOG_ERROR("Client received invalid player id!"); else { - if (avatarData->fileType == AVATAR_FILE_TYPE_UNKNOWN) - avatarData->fileType = tmpPlayerInfo.avatarType; - if (!GetAvatarManager().StoreAvatarInCache(tmpPlayerInfo.avatar, avatarData->fileType, &avatarData->fileData[0], avatarData->reportedSize, false)) + if (AvatarFile->fileType == AVATAR_FILE_TYPE_UNKNOWN) + AvatarFile->fileType = tmpPlayerInfo.avatarType; + if (!GetAvatarManager().StoreAvatarInCache(tmpPlayerInfo.avatar, AvatarFile->fileType, &AvatarFile->fileData[0], AvatarFile->reportedSize, false)) LOG_ERROR("Failed to store avatar in cache directory."); // Update player info, but never re-request avatar. @@ -726,10 +730,10 @@ ClientThread::TimerCheckAvatarDownloads(const boost::system::error_code& ec) if (m_avatarDownloader && m_avatarDownloader->HasDownloadResult()) { unsigned playerId; - boost::shared_ptr tmpAvatar(new AvatarData); + boost::shared_ptr tmpAvatar(new AvatarFile); m_avatarDownloader->GetDownloadResult(playerId, tmpAvatar->fileData); tmpAvatar->reportedSize = tmpAvatar->fileData.size(); - PassAvatarDataToManager(playerId, tmpAvatar); + PassAvatarFileToManager(playerId, tmpAvatar); } m_avatarTimer.expires_from_now( boost::posix_time::milliseconds(CLIENT_AVATAR_LOOP_MSEC)); @@ -745,8 +749,11 @@ ClientThread::UnsubscribeLobbyMsg() if (GetContext().GetSubscribeLobbyMsg()) { // Send unsubscribe request. - boost::shared_ptr unsubscr(new NetPacketUnsubscribeGameList); - GetSender().Send(GetContext().GetSessionData(), unsubscr); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_subscriptionRequestMessage; + SubscriptionRequestMessage_t *netRequest = &packet->GetMsg()->choice.subscriptionRequestMessage; + netRequest->subscriptionAction = subscriptionAction_unsubscribeGameList; + GetSender().Send(GetContext().GetSessionData(), packet); GetContext().SetSubscribeLobbyMsg(false); } } @@ -759,8 +766,10 @@ ClientThread::ResubscribeLobbyMsg() // Clear game info map as it is outdated. ClearGameInfoMap(); // Send resubscribe request. - boost::shared_ptr resubscr(new NetPacketResubscribeGameList); - GetSender().Send(GetContext().GetSessionData(), resubscr); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_subscriptionRequestMessage; + SubscriptionRequestMessage_t *netRequest = &packet->GetMsg()->choice.subscriptionRequestMessage; + netRequest->subscriptionAction = subscriptionAction_resubscribeGameList; GetContext().SetSubscribeLobbyMsg(true); } } diff --git a/src/net/common/netpacket.cpp b/src/net/common/netpacket.cpp index 8a79c133..e8c61024 100644 --- a/src/net/common/netpacket.cpp +++ b/src/net/common/netpacket.cpp @@ -17,756 +17,21 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -#include - #include -#include #include -#include - -#include - using namespace std; -#define ADD_PADDING(x) ((((x) + 3) >> 2) << 2) - -#define NET_TYPE_INIT 0x0001 -#define NET_TYPE_INIT_ACK 0x0002 -#define NET_TYPE_RETRIEVE_AVATAR 0x0003 -#define NET_TYPE_AVATAR_HEADER 0x0004 -#define NET_TYPE_AVATAR_FILE 0x0005 -#define NET_TYPE_AVATAR_END 0x0006 -#define NET_TYPE_UNKNOWN_AVATAR 0x0007 -#define NET_TYPE_GAME_LIST_NEW 0x0010 -#define NET_TYPE_GAME_LIST_UPDATE 0x0011 -#define NET_TYPE_GAME_LIST_PLAYER_JOINED 0x0012 -#define NET_TYPE_GAME_LIST_PLAYER_LEFT 0x0013 -#define NET_TYPE_GAME_LIST_ADMIN_CHANGED 0x0014 -#define NET_TYPE_RETRIEVE_PLAYER_INFO 0x0020 -#define NET_TYPE_PLAYER_INFO 0x0021 -#define NET_TYPE_UNKNOWN_PLAYER_ID 0x0022 -#define NET_TYPE_UNSUBSCRIBE_GAME_LIST 0x0023 -#define NET_TYPE_RESUBSCRIBE_GAME_LIST 0x0024 -#define NET_TYPE_CREATE_GAME 0x0030 -#define NET_TYPE_JOIN_GAME 0x0031 -#define NET_TYPE_JOIN_GAME_ACK 0x0032 -#define NET_TYPE_JOIN_GAME_FAILED 0x0033 -#define NET_TYPE_PLAYER_JOINED 0x0034 -#define NET_TYPE_PLAYER_LEFT 0x0035 -#define NET_TYPE_GAME_ADMIN_CHANGED 0x0036 -#define NET_TYPE_KICK_PLAYER 0x0040 -#define NET_TYPE_LEAVE_CURRENT_GAME 0x0041 -#define NET_TYPE_START_EVENT 0x0042 -#define NET_TYPE_START_EVENT_ACK 0x0043 -#define NET_TYPE_GAME_START 0x0050 -#define NET_TYPE_HAND_START 0x0051 -#define NET_TYPE_PLAYERS_TURN 0x0052 -#define NET_TYPE_PLAYERS_ACTION 0x0053 -#define NET_TYPE_PLAYERS_ACTION_DONE 0x0054 -#define NET_TYPE_PLAYERS_ACTION_REJECTED 0x0055 -#define NET_TYPE_DEAL_FLOP_CARDS 0x0060 -#define NET_TYPE_DEAL_TURN_CARD 0x0061 -#define NET_TYPE_DEAL_RIVER_CARD 0x0062 -#define NET_TYPE_ALL_IN_SHOW_CARDS 0x0063 -#define NET_TYPE_END_OF_HAND_SHOW_CARDS 0x0064 -#define NET_TYPE_END_OF_HAND_HIDE_CARDS 0x0065 -#define NET_TYPE_END_OF_GAME 0x0070 -#define NET_TYPE_ASK_KICK_PLAYER 0x0071 -#define NET_TYPE_ASK_KICK_PLAYER_DENIED 0x0072 -#define NET_TYPE_START_KICK_PLAYER_PETITION 0x0073 -#define NET_TYPE_VOTE_KICK_PLAYER 0x0074 -#define NET_TYPE_VOTE_KICK_PLAYER_ACK 0x0075 -#define NET_TYPE_VOTE_KICK_PLAYER_DENIED 0x0076 -#define NET_TYPE_KICK_PLAYER_PETITION_UPDATE 0x0077 -#define NET_TYPE_END_KICK_PLAYER_PETITION 0x0078 - -#define NET_TYPE_STATISTICS_CHANGED 0x0080 - -#define NET_TYPE_REMOVED_FROM_GAME 0x0100 -#define NET_TYPE_TIMEOUT_WARNING 0x0101 -#define NET_TYPE_RESET_TIMEOUT 0x0102 - -#define NET_TYPE_SEND_CHAT_TEXT 0x0200 -#define NET_TYPE_CHAT_TEXT 0x0201 -#define NET_TYPE_MSG_BOX_TEXT 0x0202 - -#define NET_TYPE_ERROR 0x0400 - -#define NET_PRIVACY_FLAG_PASSWORD_PROTECTED 0x01 - -#define NET_PLAYER_FLAG_HUMAN 0x01 -#define NET_PLAYER_FLAG_HAS_AVATAR 0x02 -#define NET_PLAYER_FLAG_AVATAR_TYPE_MASK 0x0c - -#define NET_START_FLAG_FILL_WITH_CPU_PLAYERS 0x01 - -#define NET_PRIVACY_FLAG_SHOW_AVATAR 0x01 - -// Reasons why join game failed. -#define NET_JOIN_FAILED_GAME_FULL 0x0001 -#define NET_JOIN_FAILED_GAME_ALREADY_RUNNING 0x0002 -#define NET_JOIN_FAILED_INVALID_PASSWORD 0x0003 -#define NET_JOIN_FAILED_OTHER 0xFFFF - -// Reasons for being removed from a game. -#define NET_REMOVED_ON_REQUEST 0x0000 -#define NET_REMOVED_GAME_FULL 0x0001 -#define NET_REMOVED_GAME_ALREADY_RUNNING 0x0002 -#define NET_REMOVED_KICKED 0x0003 -#define NET_REMOVED_TIMEOUT 0x0004 -#define NET_REMOVED_START_FAILED 0x0005 -#define NET_REMOVED_OTHER_REASON 0xFFFF - -// Reasons why player left a game. -#define NET_LEFT_ON_REQUEST 0x0000 -#define NET_LEFT_KICKED 0x0001 -#define NET_LEFT_ERROR 0x0002 -#define NET_LEFT_OTHER_REASON 0xFFFF - -// Reasons why ask kick was denied. -#define NET_ASK_KICK_DENIED_INVALID_STATE 0x0000 -#define NET_ASK_KICK_DENIED_TOO_FEW_PLAYERS 0x0001 -#define NET_ASK_KICK_DENIED_TEMPORARY 0x0002 -#define NET_ASK_KICK_DENIED_OTHER_IN_PROGRESS 0x0003 -#define NET_ASK_KICK_DENIED_INVALID_PLAYER_ID 0x0004 -#define NET_ASK_KICK_DENIED_OTHER_REASON 0xFFFF - -// Vote types -#define NET_VOTE_KICK_AGAINST 0x0000 -#define NET_VOTE_KICK_IN_FAVOUR 0x0001 - -// Reasons why vote to kick was denied. -#define NET_VOTE_KICK_DENIED_INVALID_PETITION 0x0000 -#define NET_VOTE_KICK_DENIED_ALREADY_VOTED 0x0001 -#define NET_VOTE_KICK_DENIED_IMPOSSIBLE 0x0002 -#define NET_VOTE_KICK_DENIED_OTHER_REASON 0xFFFF - -// Reasons why a petition to kick a player was ended. -#define NET_PETITION_END_ENOUGH_VOTES 0x0000 -#define NET_PETITION_END_NOT_ENOUGH_PLAYERS 0x0001 -#define NET_PETITION_END_PLAYER_LEFT 0x0002 -#define NET_PETITION_END_TIMEOUT 0x0003 - -// Reasons for timeout warning -#define NET_TIMEOUT_NO_DATA_RECEIVED 0x0000 -#define NET_TIMEOUT_INACTIVE_GAME 0x0001 -#define NET_TIMEOUT_OTHER_REASON 0xFFFF - -// Internal error codes. -#define NET_ERR_RESERVED 0x0000 -#define NET_ERR_INIT_VERSION_NOT_SUPPORTED 0x0001 -#define NET_ERR_INIT_SERVER_FULL 0x0002 -#define NET_ERR_INIT_INVALID_PASSWORD 0x0004 -#define NET_ERR_INIT_PLAYER_NAME_IN_USE 0x0005 -#define NET_ERR_INIT_INVALID_PLAYER_NAME 0x0006 -#define NET_ERR_INIT_SERVER_MAINTENANCE 0x0007 -#define NET_ERR_AVATAR_TOO_LARGE 0x0010 -#define NET_ERR_AVATAR_WRONG_SIZE 0x0011 -#define NET_ERR_AVATAR_UPLOAD_BLOCKED 0x0012 -#define NET_ERR_JOIN_GAME_UNKNOWN_GAME 0x0020 -#define NET_ERR_GENERAL_INVALID_PACKET 0xFF01 -#define NET_ERR_GENERAL_INVALID_STATE 0xFF02 -#define NET_ERR_GENERAL_PLAYER_KICKED 0xFF03 -#define NET_ERR_GENERAL_PLAYER_BANNED 0xFF04 -#define NET_ERR_GENERAL_SESSION_TIMED_OUT 0xFF05 -#define NET_ERR_OTHER 0xFFFF - -// Statistics types -#define NET_STAT_CUR_PLAYERS_ON_SERVER 0x0001 -#define NET_STAT_TOTAL_PLAYERS_EVER_ON_SERVER 0x0002 -#define NET_STAT_TOTAL_GAMES_EVER_ON_SERVER 0x0003 - -#ifdef _MSC_VER - #pragma pack(push, 1) - #define GCC_PACKED -#else - #define GCC_PACKED __attribute__ ((__packed__)) -#endif - - -struct GCC_PACKED NetPacketHeader +NetPacket::NetPacket(MemAllocType alloc) +: m_msg(NULL) { - u_int16_t type; - u_int16_t length; -}; - -struct GCC_PACKED NetPacketInitData -{ - NetPacketHeader head; - u_int16_t requestedVersionMajor; - u_int16_t requestedVersionMinor; - u_int16_t passwordLength; - u_int16_t playerNameLength; - u_int16_t privacyFlags; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketInitAckData -{ - NetPacketHeader head; - u_int16_t latestGameVersion; - u_int16_t latestBetaRevision; - u_int32_t sessionId; - u_int32_t playerId; -}; - -struct GCC_PACKED NetPacketRetrieveAvatarData -{ - NetPacketHeader head; - u_int32_t requestId; - unsigned char avatarMD5[MD5_DATA_SIZE]; -}; - -struct GCC_PACKED NetPacketAvatarHeaderData -{ - NetPacketHeader head; - u_int32_t requestId; - u_int32_t avatarFileSize; - u_int16_t avatarFileType; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketAvatarFileData -{ - NetPacketHeader head; - u_int32_t requestId; - u_int16_t blockSize; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketAvatarEndData -{ - NetPacketHeader head; - u_int32_t requestId; -}; - -struct GCC_PACKED NetPacketUnknownAvatarData -{ - NetPacketHeader head; - u_int32_t requestId; -}; - -struct GCC_PACKED GameInfoData -{ - u_int16_t maxNumberOfPlayers; - u_int16_t raiseIntervalMode; - u_int16_t raiseSmallBlindInterval; - u_int16_t raiseMode; - u_int16_t endRaiseMode; - u_int16_t numberOfManualBlinds; - u_int16_t proposedGuiSpeed; - u_int16_t playerActionTimeout; - u_int32_t firstSmallBlind; - u_int32_t endRaiseSmallBlindValue; - u_int32_t startMoney; -}; - -struct GCC_PACKED NetPacketGameListNewData -{ - NetPacketHeader head; - u_int32_t gameId; - u_int32_t adminPlayerId; - u_int16_t gameMode; - u_int16_t gameNameLength; - u_int16_t curNumberOfPlayers; - u_int16_t privacyFlags; - GameInfoData gameData; -}; - -struct GCC_PACKED NetPacketGameListUpdateData -{ - NetPacketHeader head; - u_int32_t gameId; - u_int16_t gameMode; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketGameListPlayerJoinedData -{ - NetPacketHeader head; - u_int32_t gameId; - u_int32_t playerId; -}; - -struct GCC_PACKED NetPacketGameListPlayerLeftData -{ - NetPacketHeader head; - u_int32_t gameId; - u_int32_t playerId; -}; - -struct GCC_PACKED NetPacketGameListAdminChangedData -{ - NetPacketHeader head; - u_int32_t gameId; - u_int32_t newAdminPlayerId; -}; - -struct GCC_PACKED NetPacketRetrievePlayerInfoData -{ - NetPacketHeader head; - u_int32_t playerId; -}; - -struct GCC_PACKED NetPacketPlayerInfoData -{ - NetPacketHeader head; - u_int32_t playerId; - u_int16_t playerFlags; - u_int16_t playerNameLength; - u_int32_t reserved; -}; - -struct GCC_PACKED NetPacketUnknownPlayerIdData -{ - NetPacketHeader head; - u_int32_t playerId; -}; - -struct GCC_PACKED NetPacketUnsubscribeGameListData -{ - NetPacketHeader head; - u_int32_t reserved; -}; - -struct GCC_PACKED NetPacketResubscribeGameListData -{ - NetPacketHeader head; - u_int32_t reserved; -}; - -struct GCC_PACKED NetPacketCreateGameData -{ - NetPacketHeader head; - u_int16_t passwordLength; - u_int16_t gameNameLength; - GameInfoData gameData; -}; - -struct GCC_PACKED NetPacketJoinGameData -{ - NetPacketHeader head; - u_int32_t gameId; - u_int16_t passwordLength; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketJoinGameAckData -{ - NetPacketHeader head; - u_int32_t gameId; - u_int16_t playerRights; - u_int16_t reserved; - GameInfoData gameData; -}; - -struct GCC_PACKED NetPacketJoinGameFailedData -{ - NetPacketHeader head; - u_int16_t failureReason; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketPlayerJoinedData -{ - NetPacketHeader head; - u_int32_t playerId; - u_int16_t playerRights; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketPlayerLeftData -{ - NetPacketHeader head; - u_int32_t playerId; - u_int16_t leaveReason; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketGameAdminChangedData -{ - NetPacketHeader head; - u_int32_t playerId; -}; - -struct GCC_PACKED NetPacketKickPlayerData -{ - NetPacketHeader head; - u_int32_t playerId; -}; - -struct GCC_PACKED NetPacketLeaveCurrentGameData -{ - NetPacketHeader head; - u_int32_t reserved; -}; - -struct GCC_PACKED NetPacketStartEventData -{ - NetPacketHeader head; - u_int16_t startFlags; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketStartEventAckData -{ - NetPacketHeader head; - u_int32_t reserved; -}; - -struct GCC_PACKED NetPacketGameStartData -{ - NetPacketHeader head; - u_int32_t startDealerPlayerId; - u_int16_t numberOfPlayers; - u_int16_t reserved; -}; - -struct GCC_PACKED PlayerSlotData -{ - u_int32_t playerId; -}; - -struct GCC_PACKED NetPacketHandStartData -{ - NetPacketHeader head; - u_int16_t yourCard1; - u_int16_t yourCard2; - u_int32_t smallBlind; -}; - -struct GCC_PACKED NetPacketPlayersTurnData -{ - NetPacketHeader head; - u_int32_t playerId; - u_int16_t gameState; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketPlayersActionData -{ - NetPacketHeader head; - u_int16_t gameState; - u_int16_t playerAction; - u_int32_t playerBet; -}; - -struct GCC_PACKED NetPacketPlayersActionDoneData -{ - NetPacketHeader head; - u_int32_t playerId; - u_int16_t gameState; - u_int16_t playerAction; - u_int32_t totalPlayerBet; - u_int32_t playerMoney; - u_int32_t highestSet; - u_int32_t minimumRaise; -}; - -struct GCC_PACKED NetPacketPlayersActionRejectedData -{ - NetPacketHeader head; - u_int16_t gameState; - u_int16_t playerAction; - u_int32_t playerBet; - u_int16_t rejectionReason; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketDealFlopCardsData -{ - NetPacketHeader head; - u_int16_t flopCard1; - u_int16_t flopCard2; - u_int16_t flopCard3; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketDealTurnCardData -{ - NetPacketHeader head; - u_int16_t turnCard; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketDealRiverCardData -{ - NetPacketHeader head; - u_int16_t riverCard; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketAllInShowCardsData -{ - NetPacketHeader head; - u_int16_t numberOfPlayerCards; - u_int16_t reserved; -}; - -struct GCC_PACKED PlayerCardsData -{ - u_int32_t playerId; - u_int16_t card1; - u_int16_t card2; -}; - -struct GCC_PACKED NetPacketEndOfHandShowCardsData -{ - NetPacketHeader head; - u_int16_t numberOfPlayerResults; - u_int16_t reserved; -}; - -struct GCC_PACKED PlayerResultData -{ - u_int32_t playerId; - u_int16_t card1; - u_int16_t card2; - u_int16_t bestHandPos1; - u_int16_t bestHandPos2; - u_int16_t bestHandPos3; - u_int16_t bestHandPos4; - u_int16_t bestHandPos5; - u_int16_t reserved; - u_int32_t valueOfCards; - u_int32_t moneyWon; - u_int32_t playerMoney; -}; - -struct GCC_PACKED NetPacketEndOfHandHideCardsData -{ - NetPacketHeader head; - u_int32_t playerId; - u_int32_t moneyWon; - u_int32_t playerMoney; -}; - -struct GCC_PACKED NetPacketEndOfGameData -{ - NetPacketHeader head; - u_int32_t winnerPlayerId; -}; - -struct GCC_PACKED NetPacketAskKickPlayerData -{ - NetPacketHeader head; - u_int32_t playerId; -}; - -struct GCC_PACKED NetPacketAskKickPlayerDeniedData -{ - NetPacketHeader head; - u_int32_t playerId; - u_int16_t denyReason; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketStartKickPlayerPetitionData -{ - NetPacketHeader head; - u_int32_t petitionId; - u_int32_t proposingPlayerId; - u_int32_t kickPlayerId; - u_int16_t kickTimeout; - u_int16_t numVotesNeededToKick; -}; - -struct GCC_PACKED NetPacketVoteKickPlayerData -{ - NetPacketHeader head; - u_int32_t petitionId; - u_int16_t vote; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketVoteKickPlayerAckData -{ - NetPacketHeader head; - u_int32_t petitionId; -}; - -struct GCC_PACKED NetPacketVoteKickPlayerDeniedData -{ - NetPacketHeader head; - u_int32_t petitionId; - u_int16_t denyReason; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketKickPlayerPetitionUpdateData -{ - NetPacketHeader head; - u_int32_t petitionId; - u_int16_t numVotesAgainstKicking; - u_int16_t numVotesInFavourOfKicking; - u_int16_t numVotesNeededToKick; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketEndKickPlayerPetitionData -{ - NetPacketHeader head; - u_int32_t petitionId; - u_int16_t numVotesAgainstKicking; - u_int16_t numVotesInFavourOfKicking; - u_int16_t voteResult; - u_int16_t endReason; -}; - -struct GCC_PACKED StatisticsData -{ - u_int32_t statisticsType; - u_int32_t statisticsValue; -}; - -struct GCC_PACKED NetPacketStatisticsChangedData -{ - NetPacketHeader head; - u_int16_t numberOfStatistics; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketRemovedFromGameData -{ - NetPacketHeader head; - u_int16_t removeReason; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketTimeoutWarningData -{ - NetPacketHeader head; - u_int16_t timeoutReason; - u_int16_t remainingSeconds; - u_int32_t reserved; -}; - -struct GCC_PACKED NetPacketResetTimeoutData -{ - NetPacketHeader head; - u_int32_t reserved; -}; - -struct GCC_PACKED NetPacketSendChatTextData -{ - NetPacketHeader head; - u_int16_t textLength; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketChatTextData -{ - NetPacketHeader head; - u_int32_t playerId; - u_int16_t textLength; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketMsgBoxTextData -{ - NetPacketHeader head; - u_int16_t textLength; - u_int16_t reserved; -}; - -struct GCC_PACKED NetPacketErrorData -{ - NetPacketHeader head; - u_int16_t errorReason; - u_int16_t reserved; -}; - -#ifdef _MSC_VER - #pragma pack(pop) -#endif - -// Helper functions - -void SetGameInfoData(const GameData &inData, GameInfoData *outData) -{ - assert(outData); - - u_int16_t numManualBlinds = (u_int16_t)inData.manualBlindsList.size(); - - outData->maxNumberOfPlayers = htons(inData.maxNumberOfPlayers); - outData->raiseIntervalMode = htons(inData.raiseIntervalMode); - outData->raiseSmallBlindInterval = inData.raiseIntervalMode == RAISE_ON_HANDNUMBER - ? htons(inData.raiseSmallBlindEveryHandsValue) : htons(inData.raiseSmallBlindEveryMinutesValue); - outData->raiseMode = htons(inData.raiseMode); - outData->endRaiseMode = htons(inData.afterManualBlindsMode); - outData->numberOfManualBlinds = htons(numManualBlinds); - outData->proposedGuiSpeed = htons(inData.guiSpeed); - outData->playerActionTimeout = htons(inData.playerActionTimeoutSec); - outData->endRaiseSmallBlindValue = htonl(inData.afterMBAlwaysRaiseValue); - outData->firstSmallBlind = htonl(inData.firstSmallBlind); - outData->startMoney = htonl(inData.startMoney); - - if (numManualBlinds) - { - u_int32_t *manualBlindsPtr = (u_int32_t *)((char *)outData + sizeof(GameInfoData)); - list::const_iterator i = inData.manualBlindsList.begin(); - list::const_iterator end = inData.manualBlindsList.end(); - while (i != end) - { - *manualBlindsPtr = htonl(*i); - ++manualBlindsPtr; - ++i; - } - } + if (alloc != NoAlloc) + m_msg = (PokerTHMessage_t *)calloc(1, sizeof(PokerTHMessage_t)); } -void GetGameInfoData(const GameInfoData *inData, GameData &outData) +NetPacket::~NetPacket() { - assert(inData); - - u_int16_t numManualBlinds = ntohs(inData->numberOfManualBlinds); - - outData.maxNumberOfPlayers = ntohs(inData->maxNumberOfPlayers); - outData.raiseIntervalMode = static_cast(ntohs(inData->raiseIntervalMode)); - outData.raiseSmallBlindEveryHandsValue = outData.raiseSmallBlindEveryMinutesValue = 0; - if (outData.raiseIntervalMode == RAISE_ON_HANDNUMBER) - outData.raiseSmallBlindEveryHandsValue = ntohs(inData->raiseSmallBlindInterval); - else - outData.raiseSmallBlindEveryMinutesValue = ntohs(inData->raiseSmallBlindInterval); - outData.raiseMode = static_cast(ntohs(inData->raiseMode)); - outData.afterManualBlindsMode = static_cast(ntohs(inData->endRaiseMode)); - outData.guiSpeed = ntohs(inData->proposedGuiSpeed); - outData.playerActionTimeoutSec = ntohs(inData->playerActionTimeout); - outData.firstSmallBlind = ntohl(inData->firstSmallBlind); - outData.afterMBAlwaysRaiseValue = ntohl(inData->endRaiseSmallBlindValue); - outData.startMoney = ntohl(inData->startMoney); - - if (numManualBlinds) - { - const u_int32_t *manualBlindsPtr = (const u_int32_t *)((const char *)inData + sizeof(GameInfoData)); - for (u_int16_t i = 0; i < numManualBlinds; i++) - { - outData.manualBlindsList.push_back(ntohl(*manualBlindsPtr)); - ++manualBlindsPtr; - } - } -} - -void -CheckGameInfoData(const GameInfoData *tmpData) -{ - // Semantic checks - int maxNumPlayers = ntohs(tmpData->maxNumberOfPlayers); - int proposedGuiSpeed = ntohs(tmpData->proposedGuiSpeed); - if (maxNumPlayers < MIN_NUMBER_OF_PLAYERS - || maxNumPlayers > MAX_NUMBER_OF_PLAYERS - || !ntohs(tmpData->raiseIntervalMode) - || !ntohs(tmpData->raiseSmallBlindInterval) - || !ntohs(tmpData->raiseMode) - || !ntohs(tmpData->endRaiseMode) - || proposedGuiSpeed < MIN_GUI_SPEED - || proposedGuiSpeed > MAX_GUI_SPEED - || !ntohl(tmpData->firstSmallBlind) - || !ntohl(tmpData->startMoney)) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } + ASN_STRUCT_FREE(asn_DEF_PokerTHMessage, m_msg); } boost::shared_ptr @@ -775,5060 +40,228 @@ NetPacket::Create(char *data, unsigned &dataSize) boost::shared_ptr tmpPacket; // Check minimum requirements. - if (!data || dataSize < sizeof(NetPacketHeader)) + if (data && dataSize > 0) { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - - NetPacketHeader *tmpHeader = (NetPacketHeader *)data; - u_int16_t tmpLen = ntohs(tmpHeader->length); - - // Check size restrictions. - if (tmpLen < sizeof(NetPacketHeader) - || tmpLen > MAX_PACKET_SIZE) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - else if (dataSize >= tmpLen) - { - // OK - we have a complete packet. - // Construct a corresponding object. - try + PokerTHMessage_t *msg = NULL; + asn_dec_rval_t retVal; + retVal = ber_decode(0, &asn_DEF_PokerTHMessage, (void **)&msg, data, dataSize); + if(retVal.code == RC_OK) { - switch(ntohs(tmpHeader->type)) + // ASN.1 BER decoding was successful. + tmpPacket.reset(new NetPacket(NoAlloc)); + tmpPacket->m_msg = msg; + // Consume the bytes. + if (retVal.consumed < dataSize) { - case NET_TYPE_INIT: - tmpPacket = boost::shared_ptr(new NetPacketInit); - break; - case NET_TYPE_INIT_ACK: - tmpPacket = boost::shared_ptr(new NetPacketInitAck); - break; - case NET_TYPE_RETRIEVE_AVATAR: - tmpPacket = boost::shared_ptr(new NetPacketRetrieveAvatar); - break; - case NET_TYPE_AVATAR_HEADER: - tmpPacket = boost::shared_ptr(new NetPacketAvatarHeader); - break; - case NET_TYPE_AVATAR_FILE: - tmpPacket = boost::shared_ptr(new NetPacketAvatarFile); - break; - case NET_TYPE_AVATAR_END: - tmpPacket = boost::shared_ptr(new NetPacketAvatarEnd); - break; - case NET_TYPE_UNKNOWN_AVATAR: - tmpPacket = boost::shared_ptr(new NetPacketUnknownAvatar); - break; - case NET_TYPE_GAME_LIST_NEW: - tmpPacket = boost::shared_ptr(new NetPacketGameListNew); - break; - case NET_TYPE_GAME_LIST_UPDATE: - tmpPacket = boost::shared_ptr(new NetPacketGameListUpdate); - break; - case NET_TYPE_GAME_LIST_PLAYER_JOINED: - tmpPacket = boost::shared_ptr(new NetPacketGameListPlayerJoined); - break; - case NET_TYPE_GAME_LIST_PLAYER_LEFT: - tmpPacket = boost::shared_ptr(new NetPacketGameListPlayerLeft); - break; - case NET_TYPE_GAME_LIST_ADMIN_CHANGED: - tmpPacket = boost::shared_ptr(new NetPacketGameListAdminChanged); - break; - case NET_TYPE_RETRIEVE_PLAYER_INFO: - tmpPacket = boost::shared_ptr(new NetPacketRetrievePlayerInfo); - break; - case NET_TYPE_PLAYER_INFO: - tmpPacket = boost::shared_ptr(new NetPacketPlayerInfo); - break; - case NET_TYPE_UNKNOWN_PLAYER_ID: - tmpPacket = boost::shared_ptr(new NetPacketUnknownPlayerId); - break; - case NET_TYPE_UNSUBSCRIBE_GAME_LIST: - tmpPacket = boost::shared_ptr(new NetPacketUnsubscribeGameList); - break; - case NET_TYPE_RESUBSCRIBE_GAME_LIST: - tmpPacket = boost::shared_ptr(new NetPacketResubscribeGameList); - break; - case NET_TYPE_CREATE_GAME: - tmpPacket = boost::shared_ptr(new NetPacketCreateGame); - break; - case NET_TYPE_JOIN_GAME: - tmpPacket = boost::shared_ptr(new NetPacketJoinGame); - break; - case NET_TYPE_JOIN_GAME_ACK: - tmpPacket = boost::shared_ptr(new NetPacketJoinGameAck); - break; - case NET_TYPE_JOIN_GAME_FAILED: - tmpPacket = boost::shared_ptr(new NetPacketJoinGameFailed); - break; - case NET_TYPE_PLAYER_JOINED: - tmpPacket = boost::shared_ptr(new NetPacketPlayerJoined); - break; - case NET_TYPE_PLAYER_LEFT: - tmpPacket = boost::shared_ptr(new NetPacketPlayerLeft); - break; - case NET_TYPE_GAME_ADMIN_CHANGED: - tmpPacket = boost::shared_ptr(new NetPacketGameAdminChanged); - break; - case NET_TYPE_KICK_PLAYER: - tmpPacket = boost::shared_ptr(new NetPacketKickPlayer); - break; - case NET_TYPE_LEAVE_CURRENT_GAME: - tmpPacket = boost::shared_ptr(new NetPacketLeaveCurrentGame); - break; - case NET_TYPE_START_EVENT: - tmpPacket = boost::shared_ptr(new NetPacketStartEvent); - break; - case NET_TYPE_START_EVENT_ACK: - tmpPacket = boost::shared_ptr(new NetPacketStartEventAck); - break; - case NET_TYPE_GAME_START: - tmpPacket = boost::shared_ptr(new NetPacketGameStart); - break; - case NET_TYPE_HAND_START: - tmpPacket = boost::shared_ptr(new NetPacketHandStart); - break; - case NET_TYPE_PLAYERS_TURN: - tmpPacket = boost::shared_ptr(new NetPacketPlayersTurn); - break; - case NET_TYPE_PLAYERS_ACTION: - tmpPacket = boost::shared_ptr(new NetPacketPlayersAction); - break; - case NET_TYPE_PLAYERS_ACTION_DONE: - tmpPacket = boost::shared_ptr(new NetPacketPlayersActionDone); - break; - case NET_TYPE_PLAYERS_ACTION_REJECTED: - tmpPacket = boost::shared_ptr(new NetPacketPlayersActionRejected); - break; - case NET_TYPE_DEAL_FLOP_CARDS: - tmpPacket = boost::shared_ptr(new NetPacketDealFlopCards); - break; - case NET_TYPE_DEAL_TURN_CARD: - tmpPacket = boost::shared_ptr(new NetPacketDealTurnCard); - break; - case NET_TYPE_DEAL_RIVER_CARD: - tmpPacket = boost::shared_ptr(new NetPacketDealRiverCard); - break; - case NET_TYPE_ALL_IN_SHOW_CARDS: - tmpPacket = boost::shared_ptr(new NetPacketAllInShowCards); - break; - case NET_TYPE_END_OF_HAND_SHOW_CARDS: - tmpPacket = boost::shared_ptr(new NetPacketEndOfHandShowCards); - break; - case NET_TYPE_END_OF_HAND_HIDE_CARDS: - tmpPacket = boost::shared_ptr(new NetPacketEndOfHandHideCards); - break; - case NET_TYPE_END_OF_GAME: - tmpPacket = boost::shared_ptr(new NetPacketEndOfGame); - break; - case NET_TYPE_ASK_KICK_PLAYER: - tmpPacket = boost::shared_ptr(new NetPacketAskKickPlayer); - break; - case NET_TYPE_ASK_KICK_PLAYER_DENIED: - tmpPacket = boost::shared_ptr(new NetPacketAskKickPlayerDenied); - break; - case NET_TYPE_START_KICK_PLAYER_PETITION: - tmpPacket = boost::shared_ptr(new NetPacketStartKickPlayerPetition); - break; - case NET_TYPE_VOTE_KICK_PLAYER: - tmpPacket = boost::shared_ptr(new NetPacketVoteKickPlayer); - break; - case NET_TYPE_VOTE_KICK_PLAYER_ACK: - tmpPacket = boost::shared_ptr(new NetPacketVoteKickPlayerAck); - break; - case NET_TYPE_VOTE_KICK_PLAYER_DENIED: - tmpPacket = boost::shared_ptr(new NetPacketVoteKickPlayerDenied); - break; - case NET_TYPE_KICK_PLAYER_PETITION_UPDATE: - tmpPacket = boost::shared_ptr(new NetPacketKickPlayerPetitionUpdate); - break; - case NET_TYPE_END_KICK_PLAYER_PETITION: - tmpPacket = boost::shared_ptr(new NetPacketEndKickPlayerPetition); - break; - case NET_TYPE_STATISTICS_CHANGED: - tmpPacket = boost::shared_ptr(new NetPacketStatisticsChanged); - break; - case NET_TYPE_REMOVED_FROM_GAME: - tmpPacket = boost::shared_ptr(new NetPacketRemovedFromGame); - break; - case NET_TYPE_TIMEOUT_WARNING: - tmpPacket = boost::shared_ptr(new NetPacketTimeoutWarning); - break; - case NET_TYPE_RESET_TIMEOUT: - tmpPacket = boost::shared_ptr(new NetPacketResetTimeout); - break; - case NET_TYPE_SEND_CHAT_TEXT: - tmpPacket = boost::shared_ptr(new NetPacketSendChatText); - break; - case NET_TYPE_CHAT_TEXT: - tmpPacket = boost::shared_ptr(new NetPacketChatText); - break; - case NET_TYPE_MSG_BOX_TEXT: - tmpPacket = boost::shared_ptr(new NetPacketMsgBoxText); - break; - case NET_TYPE_ERROR: - tmpPacket = boost::shared_ptr(new NetPacketError); - break; - default: - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_TYPE, 0); + dataSize -= retVal.consumed; + memmove(data, data + retVal.consumed, dataSize); } - if (tmpPacket.get()) - tmpPacket->SetRawData(tmpHeader); - } catch (const NetException &) - { - tmpPacket.reset(); - } - - // Consume the bytes. - if (tmpLen < dataSize) - { - dataSize -= tmpLen; - memmove(data, data + tmpLen, dataSize); + else + dataSize = 0; } else - dataSize = 0; + { + // Free the partially decoded message (if applicable). + ASN_STRUCT_FREE(asn_DEF_PokerTHMessage, msg); + } } return tmpPacket; } -NetPacket::NetPacket(u_int16_t type, u_int16_t initialSize, u_int16_t maxSize) -: m_data(NULL), m_initialSize(initialSize), m_maxSize(maxSize) +bool +NetPacket::IsClientActivity() const { - assert(initialSize >= sizeof(NetPacketHeader)); - m_data = static_cast(malloc(initialSize)); - assert(m_data); - memset(m_data, 0, initialSize); - m_data->type = htons(type); - m_data->length = htons(initialSize); -} - -NetPacket::~NetPacket() -{ - if (m_data) - free(m_data); -} - -const NetPacketHeader * -NetPacket::GetRawData() const -{ - assert(m_data); - return m_data; -} - -NetPacketHeader * -NetPacket::GetRawData() -{ - assert(m_data); - return m_data; -} - -void -NetPacket::SetRawData(const NetPacketHeader *p) -{ - if (!p) - return; - assert(m_data); - - u_int16_t tmpLen = ntohs(p->length); - if (ntohs(p->type) != GetType()) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check whether the data is valid. - Check(p); - // Resize makes sure m_data is large enough. - Resize(tmpLen); - memcpy(m_data, p, tmpLen); -} - -u_int16_t -NetPacket::GetType() const -{ - return ntohs(GetRawData()->type); -} - -u_int16_t -NetPacket::GetLen() const -{ - return ntohs(GetRawData()->length); -} - -const NetPacketInit * -NetPacket::ToNetPacketInit() const -{ - return NULL; -} - -const NetPacketInitAck * -NetPacket::ToNetPacketInitAck() const -{ - return NULL; -} - -const NetPacketRetrieveAvatar * -NetPacket::ToNetPacketRetrieveAvatar() const -{ - return NULL; -} - -const NetPacketAvatarHeader * -NetPacket::ToNetPacketAvatarHeader() const -{ - return NULL; -} - -const NetPacketAvatarFile * -NetPacket::ToNetPacketAvatarFile() const -{ - return NULL; -} - -const NetPacketAvatarEnd * -NetPacket::ToNetPacketAvatarEnd() const -{ - return NULL; -} - -const NetPacketUnknownAvatar * -NetPacket::ToNetPacketUnknownAvatar() const -{ - return NULL; -} - -const NetPacketGameListNew * -NetPacket::ToNetPacketGameListNew() const -{ - return NULL; -} - -const NetPacketGameListUpdate * -NetPacket::ToNetPacketGameListUpdate() const -{ - return NULL; -} - -const NetPacketGameListPlayerJoined * -NetPacket::ToNetPacketGameListPlayerJoined() const -{ - return NULL; -} - -const NetPacketGameListPlayerLeft * -NetPacket::ToNetPacketGameListPlayerLeft() const -{ - return NULL; -} - -const NetPacketGameListAdminChanged * -NetPacket::ToNetPacketGameListAdminChanged() const -{ - return NULL; -} - -const NetPacketRetrievePlayerInfo * -NetPacket::ToNetPacketRetrievePlayerInfo() const -{ - return NULL; -} - -const NetPacketPlayerInfo * -NetPacket::ToNetPacketPlayerInfo() const -{ - return NULL; -} - -const NetPacketUnknownPlayerId * -NetPacket::ToNetPacketUnknownPlayerId() const -{ - return NULL; -} - -const NetPacketUnsubscribeGameList * -NetPacket::ToNetPacketUnsubscribeGameList() const -{ - return NULL; -} - -const NetPacketResubscribeGameList * -NetPacket::ToNetPacketResubscribeGameList() const -{ - return NULL; -} - -const NetPacketCreateGame * -NetPacket::ToNetPacketCreateGame() const -{ - return NULL; -} - -const NetPacketJoinGame * -NetPacket::ToNetPacketJoinGame() const -{ - return NULL; -} - -const NetPacketJoinGameAck * -NetPacket::ToNetPacketJoinGameAck() const -{ - return NULL; -} - -const NetPacketJoinGameFailed * -NetPacket::ToNetPacketJoinGameFailed() const -{ - return NULL; -} - -const NetPacketPlayerJoined * -NetPacket::ToNetPacketPlayerJoined() const -{ - return NULL; -} - -const NetPacketPlayerLeft * -NetPacket::ToNetPacketPlayerLeft() const -{ - return NULL; -} - -const NetPacketGameAdminChanged * -NetPacket::ToNetPacketGameAdminChanged() const -{ - return NULL; -} - -const NetPacketKickPlayer * -NetPacket::ToNetPacketKickPlayer() const -{ - return NULL; -} - -const NetPacketLeaveCurrentGame * -NetPacket::ToNetPacketLeaveCurrentGame() const -{ - return NULL; -} - -const NetPacketStartEvent * -NetPacket::ToNetPacketStartEvent() const -{ - return NULL; -} - -const NetPacketStartEventAck * -NetPacket::ToNetPacketStartEventAck() const -{ - return NULL; -} - -const NetPacketGameStart * -NetPacket::ToNetPacketGameStart() const -{ - return NULL; -} - -const NetPacketHandStart * -NetPacket::ToNetPacketHandStart() const -{ - return NULL; -} - -const NetPacketPlayersTurn * -NetPacket::ToNetPacketPlayersTurn() const -{ - return NULL; -} - -const NetPacketPlayersAction * -NetPacket::ToNetPacketPlayersAction() const -{ - return NULL; -} - -const NetPacketPlayersActionDone * -NetPacket::ToNetPacketPlayersActionDone() const -{ - return NULL; -} - -const NetPacketPlayersActionRejected * -NetPacket::ToNetPacketPlayersActionRejected() const -{ - return NULL; -} - -const NetPacketDealFlopCards * -NetPacket::ToNetPacketDealFlopCards() const -{ - return NULL; -} - -const NetPacketDealTurnCard * -NetPacket::ToNetPacketDealTurnCard() const -{ - return NULL; -} - -const NetPacketDealRiverCard * -NetPacket::ToNetPacketDealRiverCard() const -{ - return NULL; -} - -const NetPacketAllInShowCards * -NetPacket::ToNetPacketAllInShowCards() const -{ - return NULL; -} - -const NetPacketEndOfHandShowCards * -NetPacket::ToNetPacketEndOfHandShowCards() const -{ - return NULL; -} - -const NetPacketEndOfHandHideCards * -NetPacket::ToNetPacketEndOfHandHideCards() const -{ - return NULL; -} - -const NetPacketEndOfGame * -NetPacket::ToNetPacketEndOfGame() const -{ - return NULL; -} - -const NetPacketAskKickPlayer * -NetPacket::ToNetPacketAskKickPlayer() const -{ - return NULL; -} - -const NetPacketAskKickPlayerDenied * -NetPacket::ToNetPacketAskKickPlayerDenied() const -{ - return NULL; -} - -const NetPacketStartKickPlayerPetition * -NetPacket::ToNetPacketStartKickPlayerPetition() const -{ - return NULL; -} - -const NetPacketVoteKickPlayer * -NetPacket::ToNetPacketVoteKickPlayer() const -{ - return NULL; -} - -const NetPacketVoteKickPlayerAck * -NetPacket::ToNetPacketVoteKickPlayerAck() const -{ - return NULL; -} - -const NetPacketVoteKickPlayerDenied * -NetPacket::ToNetPacketVoteKickPlayerDenied() const -{ - return NULL; -} - -const NetPacketKickPlayerPetitionUpdate * -NetPacket::ToNetPacketKickPlayerPetitionUpdate() const -{ - return NULL; -} - -const NetPacketEndKickPlayerPetition * -NetPacket::ToNetPacketEndKickPlayerPetition() const -{ - return NULL; -} - -const NetPacketStatisticsChanged * -NetPacket::ToNetPacketStatisticsChanged() const -{ - return NULL; -} - -const NetPacketRemovedFromGame * -NetPacket::ToNetPacketRemovedFromGame() const -{ - return NULL; -} - -const NetPacketTimeoutWarning * -NetPacket::ToNetPacketTimeoutWarning() const -{ - return NULL; -} - -const NetPacketResetTimeout * -NetPacket::ToNetPacketResetTimeout() const -{ - return NULL; -} - -const NetPacketSendChatText * -NetPacket::ToNetPacketSendChatText() const -{ - return NULL; -} - -const NetPacketChatText * -NetPacket::ToNetPacketChatText() const -{ - return NULL; -} - -const NetPacketMsgBoxText * -NetPacket::ToNetPacketMsgBoxText() const -{ - return NULL; -} - -const NetPacketError * -NetPacket::ToNetPacketError() const -{ - return NULL; -} - -void -NetPacket::Resize(u_int16_t newLen) -{ - assert(m_data); - u_int16_t oldLen = GetLen(); - if (newLen != oldLen) - { - if (newLen < sizeof(NetPacketHeader)) - throw NetException(__FILE__, __LINE__, ERR_SOCK_INTERNAL, 0); - else - { - NetPacketHeader *newData = (NetPacketHeader *)malloc(newLen); - if (!newData) - throw NetException(__FILE__, __LINE__, ERR_SOCK_INTERNAL, 0); - else - { - // Copy as much data as possible. - memcpy(newData, m_data, newLen > oldLen ? oldLen : newLen); - // Initialize new data to 0. - if (newLen > oldLen) - memset(((unsigned char *)newData) + oldLen, 0, newLen - oldLen); - // Set new len. - newData->length = htons(newLen); - // Switch over to new data. - free(m_data); - m_data = newData; - } - } - } -} - -void -NetPacket::Check(const NetPacketHeader *data) const -{ - // Check the input data. - assert(data); - - // Check minimum and maximum size. - u_int16_t dataLen = ntohs(data->length); - if (dataLen < m_initialSize || dataLen > m_maxSize) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - - InternalCheck(data); -} - -//----------------------------------------------------------------------------- - -NetPacketInit::NetPacketInit() -: NetPacket(NET_TYPE_INIT, sizeof(NetPacketInitData), MAX_PACKET_SIZE) -{ - NetPacketInitData *tmpData = (NetPacketInitData *)GetRawData(); - tmpData->requestedVersionMajor = htons(NET_VERSION_MAJOR); - tmpData->requestedVersionMinor = htons(NET_VERSION_MINOR); -} - -NetPacketInit::~NetPacketInit() -{ -} - -boost::shared_ptr -NetPacketInit::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketInit); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketInit::SetData(const NetPacketInit::Data &inData) -{ - u_int16_t playerNameLen = (u_int16_t)inData.playerName.length(); - u_int16_t passwordLen = (u_int16_t)inData.password.length(); - - // Some basic checks, so we don't use up too much memory. - // The constructed packet will also be checked. - if (!playerNameLen || playerNameLen > MAX_NAME_SIZE) - throw NetException(__FILE__, __LINE__, ERR_NET_INVALID_PLAYER_NAME, 0); - if (passwordLen > MAX_PASSWORD_SIZE) - throw NetException(__FILE__, __LINE__, ERR_NET_INVALID_PASSWORD_STR, 0); - - int avatarSize = inData.showAvatar ? MD5_DATA_SIZE : 0; - // Resize the packet so that the data fits in. - Resize((u_int16_t) - (sizeof(NetPacketInitData) - + avatarSize - + ADD_PADDING(playerNameLen) - + ADD_PADDING(passwordLen))); - - NetPacketInitData *tmpData = (NetPacketInitData *)GetRawData(); - - // Set the data. - tmpData->passwordLength = htons(passwordLen); - tmpData->playerNameLength = htons(playerNameLen); - - if (inData.showAvatar) - { - // Store MD5 sum of avatar. - tmpData->privacyFlags = htons(NET_PRIVACY_FLAG_SHOW_AVATAR); - char *avatarPtr = (char *)tmpData + sizeof(NetPacketInitData); - memcpy(avatarPtr, inData.avatar.data, MD5_DATA_SIZE); - } - - char *passwordPtr = (char *)tmpData + sizeof(NetPacketInitData) + avatarSize; - memcpy(passwordPtr, inData.password.c_str(), passwordLen); - memcpy(passwordPtr + ADD_PADDING(passwordLen), inData.playerName.c_str(), playerNameLen); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketInit::GetData(NetPacketInit::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketInitData *tmpData = (NetPacketInitData *)GetRawData(); - - outData.versionMajor = ntohs(tmpData->requestedVersionMajor); - outData.versionMinor = ntohs(tmpData->requestedVersionMinor); - - outData.showAvatar = ntohs(tmpData->privacyFlags) & NET_PRIVACY_FLAG_SHOW_AVATAR; - - if (outData.showAvatar) - { - char *avatarPtr = (char *)tmpData + sizeof(NetPacketInitData); - memcpy(outData.avatar.data, avatarPtr, MD5_DATA_SIZE); - } - - int avatarSize = outData.showAvatar ? MD5_DATA_SIZE : 0; - u_int16_t passwordLen = ntohs(tmpData->passwordLength); - char *passwordPtr = (char *)tmpData + sizeof(NetPacketInitData) + avatarSize; - outData.password = string(passwordPtr, passwordLen); - outData.playerName = string(passwordPtr + ADD_PADDING(passwordLen), ntohs(tmpData->playerNameLength)); -} - -const NetPacketInit * -NetPacketInit::ToNetPacketInit() const -{ - return this; -} - -void -NetPacketInit::InternalCheck(const NetPacketHeader* data) const -{ - u_int16_t dataLen = ntohs(data->length); - NetPacketInitData *tmpData = (NetPacketInitData *)data; - int passwordLength = ntohs(tmpData->passwordLength); - int playerNameLength = ntohs(tmpData->playerNameLength); - int avatarSize = ntohs(tmpData->privacyFlags) & NET_PRIVACY_FLAG_SHOW_AVATAR ? MD5_DATA_SIZE : 0; - // Generous checking of dynamic packet size - - // larger packets are allowed. - // This is because the version number is in this packet, - // and later versions might provide larger packets. - if (dataLen < - sizeof(NetPacketInitData) - + avatarSize - + ADD_PADDING(passwordLength) - + ADD_PADDING(playerNameLength)) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check string sizes. - if (passwordLength > MAX_PASSWORD_SIZE - || !playerNameLength - || playerNameLength > MAX_NAME_SIZE) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check name string. - char *namePtr = (char *)tmpData + sizeof(NetPacketInitData) + avatarSize + ADD_PADDING(passwordLength); - if (namePtr[0] == 0) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } -} - -//----------------------------------------------------------------------------- - -NetPacketInitAck::NetPacketInitAck() -: NetPacket(NET_TYPE_INIT_ACK, sizeof(NetPacketInitAckData), sizeof(NetPacketInitAckData)) -{ -} - -NetPacketInitAck::~NetPacketInitAck() -{ -} - -boost::shared_ptr -NetPacketInitAck::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketInitAck); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketInitAck::SetData(const NetPacketInitAck::Data &inData) -{ - NetPacketInitAckData *tmpData = (NetPacketInitAckData *)GetRawData(); - - tmpData->latestGameVersion = htons(inData.latestGameVersion); - tmpData->latestBetaRevision = htons(inData.latestBetaRevision); - tmpData->sessionId = htonl(inData.sessionId); - tmpData->playerId = htonl(inData.playerId); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketInitAck::GetData(NetPacketInitAck::Data &outData) const -{ - NetPacketInitAckData *tmpData = (NetPacketInitAckData *)GetRawData(); - - outData.latestGameVersion = ntohs(tmpData->latestGameVersion); - outData.latestBetaRevision = ntohs(tmpData->latestBetaRevision); - outData.sessionId = ntohl(tmpData->sessionId); - outData.playerId = ntohl(tmpData->playerId); -} - -const NetPacketInitAck * -NetPacketInitAck::ToNetPacketInitAck() const -{ - return this; -} - -void -NetPacketInitAck::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketRetrieveAvatar::NetPacketRetrieveAvatar() -: NetPacket(NET_TYPE_RETRIEVE_AVATAR, sizeof(NetPacketRetrieveAvatarData), sizeof(NetPacketRetrieveAvatarData)) -{ -} - -NetPacketRetrieveAvatar::~NetPacketRetrieveAvatar() -{ -} - -boost::shared_ptr -NetPacketRetrieveAvatar::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketRetrieveAvatar); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketRetrieveAvatar::SetData(const NetPacketRetrieveAvatar::Data &inData) -{ - NetPacketRetrieveAvatarData *tmpData = (NetPacketRetrieveAvatarData *)GetRawData(); - - tmpData->requestId = htonl(inData.requestId); - memcpy(tmpData->avatarMD5, inData.avatar.data, MD5_DATA_SIZE); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketRetrieveAvatar::GetData(NetPacketRetrieveAvatar::Data &outData) const -{ - NetPacketRetrieveAvatarData *tmpData = (NetPacketRetrieveAvatarData *)GetRawData(); - - outData.requestId = ntohl(tmpData->requestId); - memcpy(outData.avatar.data, tmpData->avatarMD5, MD5_DATA_SIZE); -} - -const NetPacketRetrieveAvatar * -NetPacketRetrieveAvatar::ToNetPacketRetrieveAvatar() const -{ - return this; -} - -void -NetPacketRetrieveAvatar::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketAvatarHeader::NetPacketAvatarHeader() -: NetPacket(NET_TYPE_AVATAR_HEADER, sizeof(NetPacketAvatarHeaderData), sizeof(NetPacketAvatarHeaderData)) -{ -} - -NetPacketAvatarHeader::~NetPacketAvatarHeader() -{ -} - -boost::shared_ptr -NetPacketAvatarHeader::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketAvatarHeader); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketAvatarHeader::SetData(const NetPacketAvatarHeader::Data &inData) -{ - NetPacketAvatarHeaderData *tmpData = (NetPacketAvatarHeaderData *)GetRawData(); - - tmpData->requestId = htonl(inData.requestId); - tmpData->avatarFileSize = htonl(inData.avatarFileSize); - tmpData->avatarFileType = htons(inData.avatarFileType); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketAvatarHeader::GetData(NetPacketAvatarHeader::Data &outData) const -{ - NetPacketAvatarHeaderData *tmpData = (NetPacketAvatarHeaderData *)GetRawData(); - - outData.requestId = ntohl(tmpData->requestId); - outData.avatarFileSize = ntohl(tmpData->avatarFileSize); - outData.avatarFileType = static_cast(ntohs(tmpData->avatarFileType)); -} - -const NetPacketAvatarHeader * -NetPacketAvatarHeader::ToNetPacketAvatarHeader() const -{ - return this; -} - -void -NetPacketAvatarHeader::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketAvatarFile::NetPacketAvatarFile() -: NetPacket(NET_TYPE_AVATAR_FILE, sizeof(NetPacketAvatarFileData), MAX_PACKET_SIZE) -{ -} - -NetPacketAvatarFile::~NetPacketAvatarFile() -{ -} - -boost::shared_ptr -NetPacketAvatarFile::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketAvatarFile); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketAvatarFile::SetData(const NetPacketAvatarFile::Data &inData) -{ - int fileDataSize = static_cast(inData.fileData.size()); - - if (fileDataSize > MAX_FILE_DATA_SIZE) - throw NetException(__FILE__, __LINE__, ERR_NET_BUF_INVALID_SIZE, 0); - - Resize((u_int16_t) - (sizeof(NetPacketAvatarFileData) - + ADD_PADDING(fileDataSize))); - - NetPacketAvatarFileData *tmpData = (NetPacketAvatarFileData *)GetRawData(); - - tmpData->requestId = htonl(inData.requestId); - tmpData->blockSize = htons(fileDataSize); - - char *avatarDataPtr = (char *)tmpData + sizeof(NetPacketAvatarFileData); - memcpy(avatarDataPtr, &inData.fileData[0], fileDataSize); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketAvatarFile::GetData(NetPacketAvatarFile::Data &outData) const -{ - NetPacketAvatarFileData *tmpData = (NetPacketAvatarFileData *)GetRawData(); - - outData.requestId = ntohl(tmpData->requestId); - int fileDataSize = ntohs(tmpData->blockSize); - - char *avatarDataPtr = (char *)tmpData + sizeof(NetPacketAvatarFileData); - outData.fileData.resize(fileDataSize); - memcpy(&outData.fileData[0], avatarDataPtr, fileDataSize); -} - -const NetPacketAvatarFile * -NetPacketAvatarFile::ToNetPacketAvatarFile() const -{ - return this; -} - -void -NetPacketAvatarFile::InternalCheck(const NetPacketHeader *data) const -{ - u_int16_t dataLen = ntohs(data->length); - NetPacketAvatarFileData *tmpData = (NetPacketAvatarFileData *)data; - int fileDataSize = ntohs(tmpData->blockSize); - - // Exact checking of dynamic packet size. - if (dataLen != - sizeof(NetPacketAvatarFileData) - + ADD_PADDING(fileDataSize)) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - if (!fileDataSize || fileDataSize > MAX_FILE_DATA_SIZE) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } -} - -//----------------------------------------------------------------------------- - -NetPacketAvatarEnd::NetPacketAvatarEnd() -: NetPacket(NET_TYPE_AVATAR_END, sizeof(NetPacketAvatarEndData), sizeof(NetPacketAvatarEndData)) -{ -} - -NetPacketAvatarEnd::~NetPacketAvatarEnd() -{ -} - -boost::shared_ptr -NetPacketAvatarEnd::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketAvatarEnd); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketAvatarEnd::SetData(const NetPacketAvatarEnd::Data &inData) -{ - NetPacketAvatarEndData *tmpData = (NetPacketAvatarEndData *)GetRawData(); - - tmpData->requestId = htonl(inData.requestId); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketAvatarEnd::GetData(NetPacketAvatarEnd::Data &outData) const -{ - NetPacketAvatarEndData *tmpData = (NetPacketAvatarEndData *)GetRawData(); - - outData.requestId = ntohl(tmpData->requestId); -} - -const NetPacketAvatarEnd * -NetPacketAvatarEnd::ToNetPacketAvatarEnd() const -{ - return this; -} - -void -NetPacketAvatarEnd::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketUnknownAvatar::NetPacketUnknownAvatar() -: NetPacket(NET_TYPE_UNKNOWN_AVATAR, sizeof(NetPacketUnknownAvatarData), sizeof(NetPacketUnknownAvatarData)) -{ -} - -NetPacketUnknownAvatar::~NetPacketUnknownAvatar() -{ -} - -boost::shared_ptr -NetPacketUnknownAvatar::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketUnknownAvatar); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketUnknownAvatar::SetData(const NetPacketUnknownAvatar::Data &inData) -{ - NetPacketUnknownAvatarData *tmpData = (NetPacketUnknownAvatarData *)GetRawData(); - - tmpData->requestId = htonl(inData.requestId); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketUnknownAvatar::GetData(NetPacketUnknownAvatar::Data &outData) const -{ - NetPacketUnknownAvatarData *tmpData = (NetPacketUnknownAvatarData *)GetRawData(); - - outData.requestId = ntohl(tmpData->requestId); -} - -const NetPacketUnknownAvatar * -NetPacketUnknownAvatar::ToNetPacketUnknownAvatar() const -{ - return this; -} - -void -NetPacketUnknownAvatar::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketGameListNew::NetPacketGameListNew() -: NetPacket(NET_TYPE_GAME_LIST_NEW, sizeof(NetPacketGameListNewData), MAX_PACKET_SIZE) -{ -} - -NetPacketGameListNew::~NetPacketGameListNew() -{ -} - -boost::shared_ptr -NetPacketGameListNew::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketGameListNew); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketGameListNew::SetData(const NetPacketGameListNew::Data &inData) -{ - u_int16_t gameNameLen = (u_int16_t)inData.gameInfo.name.length(); - u_int16_t curNumPlayers = (u_int16_t)inData.gameInfo.players.size(); - u_int16_t numManualBlinds = (u_int16_t)inData.gameInfo.data.manualBlindsList.size(); - - int manualBlindsSize = numManualBlinds * sizeof(u_int32_t); - - // Some basic checks, so we don't use up too much memory. - // The constructed packet will also be checked. - if (!gameNameLen || gameNameLen > MAX_NAME_SIZE) - throw NetException(__FILE__, __LINE__, ERR_NET_INVALID_GAME_NAME, 0); - if (numManualBlinds > MAX_NUM_MANUAL_BLINDS) - throw NetException(__FILE__, __LINE__, ERR_NET_TOO_MANY_MANUAL_BLINDS, 0); - - // Resize the packet so that the data fits in. - Resize((u_int16_t) - (sizeof(NetPacketGameListNewData) - + manualBlindsSize - + ADD_PADDING(gameNameLen) - + curNumPlayers * sizeof(unsigned) - )); - - NetPacketGameListNewData *tmpData = (NetPacketGameListNewData *)GetRawData(); - - // Set the data. - tmpData->gameId = htonl(inData.gameId); - tmpData->adminPlayerId = htonl(inData.gameInfo.adminPlayerId); - tmpData->gameMode = htons(inData.gameInfo.mode); - tmpData->gameNameLength = htons(gameNameLen); - tmpData->curNumberOfPlayers = htons(curNumPlayers); - tmpData->privacyFlags = htons(inData.gameInfo.isPasswordProtected ? NET_PRIVACY_FLAG_PASSWORD_PROTECTED : 0); - - SetGameInfoData(inData.gameInfo.data, &tmpData->gameData); - - char *gameNamePtr = (char *)tmpData + sizeof(NetPacketGameListNewData) + manualBlindsSize; - memcpy(gameNamePtr, inData.gameInfo.name.c_str(), gameNameLen); - - PlayerIdList::const_iterator i = inData.gameInfo.players.begin(); - PlayerIdList::const_iterator end = inData.gameInfo.players.end(); - - // Copy the player list to continous memory - unsigned *tmpPlayer = - (unsigned *)(gameNamePtr + ADD_PADDING(gameNameLen)); - while (i != end) - { - *tmpPlayer = htonl(*i); - ++tmpPlayer; - ++i; - } - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketGameListNew::GetData(NetPacketGameListNew::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketGameListNewData *tmpData = (NetPacketGameListNewData *)GetRawData(); - - u_int16_t numManualBlinds = ntohs(tmpData->gameData.numberOfManualBlinds); - int manualBlindsSize = numManualBlinds * sizeof(u_int32_t); - - outData.gameId = ntohl(tmpData->gameId); - outData.gameInfo.adminPlayerId = ntohl(tmpData->adminPlayerId); - outData.gameInfo.mode = static_cast(ntohs(tmpData->gameMode)); - u_int16_t gameNameLen = ntohs(tmpData->gameNameLength); - u_int16_t curNumPlayers = ntohs(tmpData->curNumberOfPlayers); - outData.gameInfo.isPasswordProtected = (ntohs(tmpData->privacyFlags) & NET_PRIVACY_FLAG_PASSWORD_PROTECTED) == NET_PRIVACY_FLAG_PASSWORD_PROTECTED; - - GetGameInfoData(&tmpData->gameData, outData.gameInfo.data); - - char *gameNamePtr = (char *)tmpData + sizeof(NetPacketGameListNewData) + manualBlindsSize; - outData.gameInfo.name = string(gameNamePtr, gameNameLen); - - unsigned *tmpPlayer = (unsigned *)(gameNamePtr + ADD_PADDING(gameNameLen)); - - // Store all available players. - for (int i = 0; i < curNumPlayers; i++) - { - outData.gameInfo.players.push_back(ntohl(*tmpPlayer)); - ++tmpPlayer; - } -} - -const NetPacketGameListNew * -NetPacketGameListNew::ToNetPacketGameListNew() const -{ - return this; -} - -void -NetPacketGameListNew::InternalCheck(const NetPacketHeader* data) const -{ - u_int16_t dataLen = ntohs(data->length); - NetPacketGameListNewData *tmpData = (NetPacketGameListNewData *)data; - int gameNameLength = ntohs(tmpData->gameNameLength); - int curNumPlayers = ntohs(tmpData->curNumberOfPlayers); - u_int16_t numManualBlinds = ntohs(tmpData->gameData.numberOfManualBlinds); - int manualBlindsLength = numManualBlinds * sizeof(u_int32_t); - // Exact checking of dynamic packet size. - if (dataLen != - sizeof(NetPacketGameListNewData) - + manualBlindsLength - + ADD_PADDING(gameNameLength) - + curNumPlayers * sizeof(unsigned)) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check manual blind list size - if (numManualBlinds > MAX_NUM_MANUAL_BLINDS) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check string size. - if (!gameNameLength - || gameNameLength > MAX_NAME_SIZE) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check name string. - char *namePtr = (char *)tmpData + sizeof(NetPacketGameListNewData) + manualBlindsLength; - if (namePtr[0] == 0) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } -} - -//----------------------------------------------------------------------------- - -NetPacketGameListUpdate::NetPacketGameListUpdate() -: NetPacket(NET_TYPE_GAME_LIST_UPDATE, sizeof(NetPacketGameListUpdateData), sizeof(NetPacketGameListUpdateData)) -{ -} - -NetPacketGameListUpdate::~NetPacketGameListUpdate() -{ -} - -boost::shared_ptr -NetPacketGameListUpdate::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketGameListUpdate); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketGameListUpdate::SetData(const NetPacketGameListUpdate::Data &inData) -{ - NetPacketGameListUpdateData *tmpData = (NetPacketGameListUpdateData *)GetRawData(); - - // Set the data. - tmpData->gameId = htonl(inData.gameId); - tmpData->gameMode = htons(inData.gameMode); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketGameListUpdate::GetData(NetPacketGameListUpdate::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketGameListUpdateData *tmpData = (NetPacketGameListUpdateData *)GetRawData(); - - outData.gameId = ntohl(tmpData->gameId); - outData.gameMode = static_cast(ntohs(tmpData->gameMode)); -} - -const NetPacketGameListUpdate * -NetPacketGameListUpdate::ToNetPacketGameListUpdate() const -{ - return this; -} - -void -NetPacketGameListUpdate::InternalCheck(const NetPacketHeader*) const -{ -} - -//----------------------------------------------------------------------------- - -NetPacketGameListPlayerJoined::NetPacketGameListPlayerJoined() -: NetPacket(NET_TYPE_GAME_LIST_PLAYER_JOINED, sizeof(NetPacketGameListPlayerJoinedData), sizeof(NetPacketGameListPlayerJoinedData)) -{ -} - -NetPacketGameListPlayerJoined::~NetPacketGameListPlayerJoined() -{ -} - -boost::shared_ptr -NetPacketGameListPlayerJoined::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketGameListPlayerJoined); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketGameListPlayerJoined::SetData(const NetPacketGameListPlayerJoined::Data &inData) -{ - NetPacketGameListPlayerJoinedData *tmpData = (NetPacketGameListPlayerJoinedData *)GetRawData(); - - // Set the data. - tmpData->gameId = htonl(inData.gameId); - tmpData->playerId = htonl(inData.playerId); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketGameListPlayerJoined::GetData(NetPacketGameListPlayerJoined::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketGameListPlayerJoinedData *tmpData = (NetPacketGameListPlayerJoinedData *)GetRawData(); - - outData.gameId = ntohl(tmpData->gameId); - outData.playerId = ntohl(tmpData->playerId); -} - -const NetPacketGameListPlayerJoined * -NetPacketGameListPlayerJoined::ToNetPacketGameListPlayerJoined() const -{ - return this; -} - -void -NetPacketGameListPlayerJoined::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketGameListPlayerLeft::NetPacketGameListPlayerLeft() -: NetPacket(NET_TYPE_GAME_LIST_PLAYER_LEFT, sizeof(NetPacketGameListPlayerLeftData), sizeof(NetPacketGameListPlayerLeftData)) -{ -} - -NetPacketGameListPlayerLeft::~NetPacketGameListPlayerLeft() -{ -} - -boost::shared_ptr -NetPacketGameListPlayerLeft::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketGameListPlayerLeft); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketGameListPlayerLeft::SetData(const NetPacketGameListPlayerLeft::Data &inData) -{ - NetPacketGameListPlayerLeftData *tmpData = (NetPacketGameListPlayerLeftData *)GetRawData(); - - // Set the data. - tmpData->gameId = htonl(inData.gameId); - tmpData->playerId = htonl(inData.playerId); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketGameListPlayerLeft::GetData(NetPacketGameListPlayerLeft::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketGameListPlayerLeftData *tmpData = (NetPacketGameListPlayerLeftData *)GetRawData(); - - outData.gameId = ntohl(tmpData->gameId); - outData.playerId = ntohl(tmpData->playerId); -} - -const NetPacketGameListPlayerLeft * -NetPacketGameListPlayerLeft::ToNetPacketGameListPlayerLeft() const -{ - return this; -} - -void -NetPacketGameListPlayerLeft::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketGameListAdminChanged::NetPacketGameListAdminChanged() -: NetPacket(NET_TYPE_GAME_LIST_ADMIN_CHANGED, sizeof(NetPacketGameListAdminChangedData), sizeof(NetPacketGameListAdminChangedData)) -{ -} - -NetPacketGameListAdminChanged::~NetPacketGameListAdminChanged() -{ -} - -boost::shared_ptr -NetPacketGameListAdminChanged::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketGameListAdminChanged); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketGameListAdminChanged::SetData(const NetPacketGameListAdminChanged::Data &inData) -{ - NetPacketGameListAdminChangedData *tmpData = (NetPacketGameListAdminChangedData *)GetRawData(); - - // Set the data. - tmpData->gameId = htonl(inData.gameId); - tmpData->newAdminPlayerId = htonl(inData.newAdminplayerId); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketGameListAdminChanged::GetData(NetPacketGameListAdminChanged::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketGameListAdminChangedData *tmpData = (NetPacketGameListAdminChangedData *)GetRawData(); - - outData.gameId = ntohl(tmpData->gameId); - outData.newAdminplayerId = ntohl(tmpData->newAdminPlayerId); -} - -const NetPacketGameListAdminChanged * -NetPacketGameListAdminChanged::ToNetPacketGameListAdminChanged() const -{ - return this; -} - -void -NetPacketGameListAdminChanged::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketRetrievePlayerInfo::NetPacketRetrievePlayerInfo() -: NetPacket(NET_TYPE_RETRIEVE_PLAYER_INFO, sizeof(NetPacketRetrievePlayerInfoData), sizeof(NetPacketRetrievePlayerInfoData)) -{ -} - -NetPacketRetrievePlayerInfo::~NetPacketRetrievePlayerInfo() -{ -} - -boost::shared_ptr -NetPacketRetrievePlayerInfo::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketRetrievePlayerInfo); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketRetrievePlayerInfo::SetData(const NetPacketRetrievePlayerInfo::Data &inData) -{ - NetPacketRetrievePlayerInfoData *tmpData = (NetPacketRetrievePlayerInfoData *)GetRawData(); - - // Set the data. - tmpData->playerId = htonl(inData.playerId); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketRetrievePlayerInfo::GetData(NetPacketRetrievePlayerInfo::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketRetrievePlayerInfoData *tmpData = (NetPacketRetrievePlayerInfoData *)GetRawData(); - - outData.playerId = ntohl(tmpData->playerId); -} - -const NetPacketRetrievePlayerInfo * -NetPacketRetrievePlayerInfo::ToNetPacketRetrievePlayerInfo() const -{ - return this; -} - -void -NetPacketRetrievePlayerInfo::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketPlayerInfo::NetPacketPlayerInfo() -: NetPacket(NET_TYPE_PLAYER_INFO, sizeof(NetPacketPlayerInfoData), MAX_PACKET_SIZE) -{ -} - -NetPacketPlayerInfo::~NetPacketPlayerInfo() -{ -} - -boost::shared_ptr -NetPacketPlayerInfo::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketPlayerInfo); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketPlayerInfo::SetData(const NetPacketPlayerInfo::Data &inData) -{ - u_int16_t playerNameLen = (u_int16_t)inData.playerInfo.playerName.length(); - - if (!playerNameLen || playerNameLen > MAX_NAME_SIZE) - throw NetException(__FILE__, __LINE__, ERR_NET_INVALID_PLAYER_NAME, 0); - - int avatarSize = inData.playerInfo.hasAvatar ? MD5_DATA_SIZE : 0; - // Resize the packet so that the data fits in. - Resize((u_int16_t) - (sizeof(NetPacketPlayerInfoData) + avatarSize + ADD_PADDING(playerNameLen))); - - NetPacketPlayerInfoData *tmpData = (NetPacketPlayerInfoData *)GetRawData(); - - // Set the data. - tmpData->playerId = htonl(inData.playerId); - tmpData->playerNameLength = htons(playerNameLen); - - u_int16_t tmpPlayerFlags = inData.playerInfo.ptype == PLAYER_TYPE_HUMAN ? NET_PLAYER_FLAG_HUMAN : 0; - if (inData.playerInfo.hasAvatar) - { - tmpPlayerFlags |= NET_PLAYER_FLAG_HAS_AVATAR; - tmpPlayerFlags |= ((inData.playerInfo.avatarType) << 2); - char *avatarPtr = (char *)tmpData + sizeof(NetPacketPlayerInfoData); - memcpy(avatarPtr, inData.playerInfo.avatar.data, MD5_DATA_SIZE); - } - tmpData->playerFlags = htons(tmpPlayerFlags); - - char *namePtr = (char *)tmpData + sizeof(NetPacketPlayerInfoData) + avatarSize; - memcpy(namePtr, inData.playerInfo.playerName.c_str(), playerNameLen); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketPlayerInfo::GetData(NetPacketPlayerInfo::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketPlayerInfoData *tmpData = (NetPacketPlayerInfoData *)GetRawData(); - - outData.playerId = ntohl(tmpData->playerId); - u_int16_t tmpPlayerFlags = ntohs(tmpData->playerFlags); - outData.playerInfo.ptype = (tmpPlayerFlags & NET_PLAYER_FLAG_HUMAN) ? PLAYER_TYPE_HUMAN : PLAYER_TYPE_COMPUTER; - outData.playerInfo.hasAvatar = (tmpPlayerFlags & NET_PLAYER_FLAG_HAS_AVATAR) ? true : false; - - if (outData.playerInfo.hasAvatar) - { - switch ((tmpPlayerFlags & NET_PLAYER_FLAG_AVATAR_TYPE_MASK) >> 2) - { - case AVATAR_FILE_TYPE_PNG : - outData.playerInfo.avatarType = AVATAR_FILE_TYPE_PNG; - break; - case AVATAR_FILE_TYPE_JPG : - outData.playerInfo.avatarType = AVATAR_FILE_TYPE_JPG; - break; - case AVATAR_FILE_TYPE_GIF : - outData.playerInfo.avatarType = AVATAR_FILE_TYPE_GIF; - break; - default: - outData.playerInfo.avatarType = AVATAR_FILE_TYPE_UNKNOWN; - break; - } - char *avatarPtr = (char *)tmpData + sizeof(NetPacketPlayerInfoData); - memcpy(outData.playerInfo.avatar.data, avatarPtr, MD5_DATA_SIZE); - } - - int avatarSize = outData.playerInfo.hasAvatar ? MD5_DATA_SIZE : 0; - - char *namePtr = (char *)tmpData + avatarSize + sizeof(NetPacketPlayerInfoData); - outData.playerInfo.playerName = string(namePtr, ntohs(tmpData->playerNameLength)); -} - -const NetPacketPlayerInfo * -NetPacketPlayerInfo::ToNetPacketPlayerInfo() const -{ - return this; -} - -void -NetPacketPlayerInfo::InternalCheck(const NetPacketHeader* data) const -{ - u_int16_t dataLen = ntohs(data->length); - NetPacketPlayerInfoData *tmpData = (NetPacketPlayerInfoData *)data; - int playerNameLength = ntohs(tmpData->playerNameLength); - int avatarSize = (ntohs(tmpData->playerFlags) & NET_PLAYER_FLAG_HAS_AVATAR) ? MD5_DATA_SIZE : 0; - // Exact checking this time. - if (dataLen != - sizeof(NetPacketPlayerInfoData) - + avatarSize - + ADD_PADDING(playerNameLength)) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check string sizes. - if (!playerNameLength - || playerNameLength > MAX_NAME_SIZE) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } -} - -//----------------------------------------------------------------------------- - -NetPacketUnknownPlayerId::NetPacketUnknownPlayerId() -: NetPacket(NET_TYPE_UNKNOWN_PLAYER_ID, sizeof(NetPacketUnknownPlayerIdData), sizeof(NetPacketUnknownPlayerIdData)) -{ -} - -NetPacketUnknownPlayerId::~NetPacketUnknownPlayerId() -{ -} - -boost::shared_ptr -NetPacketUnknownPlayerId::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketUnknownPlayerId); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketUnknownPlayerId::SetData(const NetPacketUnknownPlayerId::Data &inData) -{ - NetPacketUnknownPlayerIdData *tmpData = (NetPacketUnknownPlayerIdData *)GetRawData(); - - // Set the data. - tmpData->playerId = htonl(inData.playerId); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketUnknownPlayerId::GetData(NetPacketUnknownPlayerId::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketUnknownPlayerIdData *tmpData = (NetPacketUnknownPlayerIdData *)GetRawData(); - - outData.playerId = ntohl(tmpData->playerId); -} - -const NetPacketUnknownPlayerId * -NetPacketUnknownPlayerId::ToNetPacketUnknownPlayerId() const -{ - return this; -} - -void -NetPacketUnknownPlayerId::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketUnsubscribeGameList::NetPacketUnsubscribeGameList() -: NetPacket(NET_TYPE_UNSUBSCRIBE_GAME_LIST, sizeof(NetPacketUnsubscribeGameListData), sizeof(NetPacketUnsubscribeGameListData)) -{ -} - -NetPacketUnsubscribeGameList::~NetPacketUnsubscribeGameList() -{ -} - -boost::shared_ptr -NetPacketUnsubscribeGameList::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketUnsubscribeGameList); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -const NetPacketUnsubscribeGameList * -NetPacketUnsubscribeGameList::ToNetPacketUnsubscribeGameList() const -{ - return this; -} - -void -NetPacketUnsubscribeGameList::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketResubscribeGameList::NetPacketResubscribeGameList() -: NetPacket(NET_TYPE_RESUBSCRIBE_GAME_LIST, sizeof(NetPacketResubscribeGameListData), sizeof(NetPacketResubscribeGameListData)) -{ -} - -NetPacketResubscribeGameList::~NetPacketResubscribeGameList() -{ -} - -boost::shared_ptr -NetPacketResubscribeGameList::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketResubscribeGameList); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -const NetPacketResubscribeGameList * -NetPacketResubscribeGameList::ToNetPacketResubscribeGameList() const -{ - return this; -} - -void -NetPacketResubscribeGameList::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketCreateGame::NetPacketCreateGame() -: NetPacket(NET_TYPE_CREATE_GAME, sizeof(NetPacketCreateGameData), MAX_PACKET_SIZE) -{ -} - -NetPacketCreateGame::~NetPacketCreateGame() -{ -} - -boost::shared_ptr -NetPacketCreateGame::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketCreateGame); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketCreateGame::SetData(const NetPacketCreateGame::Data &inData) -{ - u_int16_t gameNameLen = (u_int16_t)inData.gameName.length(); - u_int16_t passwordLen = (u_int16_t)inData.password.length(); - u_int16_t numManualBlinds = (u_int16_t)inData.gameData.manualBlindsList.size(); - - // Some basic checks, so we don't use up too much memory. - // The constructed packet will also be checked. - if (!gameNameLen || gameNameLen > MAX_NAME_SIZE) - throw NetException(__FILE__, __LINE__, ERR_NET_INVALID_PLAYER_NAME, 0); - if (passwordLen > MAX_PASSWORD_SIZE) - throw NetException(__FILE__, __LINE__, ERR_NET_INVALID_PASSWORD_STR, 0); - if (numManualBlinds > MAX_NUM_MANUAL_BLINDS) - throw NetException(__FILE__, __LINE__, ERR_NET_TOO_MANY_MANUAL_BLINDS, 0); - - int manualBlindsSize = numManualBlinds * sizeof(u_int32_t); - // Resize the packet so that the data fits in. - Resize((u_int16_t) - (sizeof(NetPacketCreateGameData) + manualBlindsSize + ADD_PADDING(gameNameLen) + ADD_PADDING(passwordLen))); - - NetPacketCreateGameData *tmpData = (NetPacketCreateGameData *)GetRawData(); - - // Set the data. - tmpData->passwordLength = htons(passwordLen); - tmpData->gameNameLength = htons(gameNameLen); - - SetGameInfoData(inData.gameData, &tmpData->gameData); - - char *passwordPtr = (char *)tmpData + sizeof(NetPacketCreateGameData) + manualBlindsSize; - memcpy(passwordPtr, inData.password.c_str(), passwordLen); - memcpy(passwordPtr + ADD_PADDING(passwordLen), inData.gameName.c_str(), gameNameLen); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketCreateGame::GetData(NetPacketCreateGame::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketCreateGameData *tmpData = (NetPacketCreateGameData *)GetRawData(); - - u_int16_t numManualBlinds = ntohs(tmpData->gameData.numberOfManualBlinds); - int manualBlindsSize = numManualBlinds * sizeof(u_int32_t); - - GetGameInfoData(&tmpData->gameData, outData.gameData); - - u_int16_t passwordLen = ntohs(tmpData->passwordLength); - char *passwordPtr = (char *)tmpData + manualBlindsSize + sizeof(NetPacketCreateGameData); - outData.password = string(passwordPtr, passwordLen); - outData.gameName = string(passwordPtr + ADD_PADDING(passwordLen), ntohs(tmpData->gameNameLength)); -} - -const NetPacketCreateGame * -NetPacketCreateGame::ToNetPacketCreateGame() const -{ - return this; -} - -void -NetPacketCreateGame::InternalCheck(const NetPacketHeader* data) const -{ - u_int16_t dataLen = ntohs(data->length); - NetPacketCreateGameData *tmpData = (NetPacketCreateGameData *)data; - int passwordLength = ntohs(tmpData->passwordLength); - int gameNameLength = ntohs(tmpData->gameNameLength); - int numManualBlinds = ntohs(tmpData->gameData.numberOfManualBlinds); - int manualBlindsLength = numManualBlinds * sizeof(u_int32_t); - // Exact checking of dynamic packet size. - if (dataLen < - sizeof(NetPacketCreateGameData) - + manualBlindsLength - + ADD_PADDING(passwordLength) - + ADD_PADDING(gameNameLength)) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check manual blind list size - if (numManualBlinds > MAX_NUM_MANUAL_BLINDS) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check string sizes. - if (passwordLength > MAX_PASSWORD_SIZE - || !gameNameLength - || gameNameLength > MAX_NAME_SIZE) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check name string. - char *namePtr = (char *)tmpData + sizeof(NetPacketCreateGameData) + manualBlindsLength + ADD_PADDING(passwordLength); - if (namePtr[0] == 0) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - CheckGameInfoData(&tmpData->gameData); -} - -//----------------------------------------------------------------------------- - -NetPacketJoinGame::NetPacketJoinGame() -: NetPacket(NET_TYPE_JOIN_GAME, sizeof(NetPacketJoinGameData), MAX_PACKET_SIZE) -{ -} - -NetPacketJoinGame::~NetPacketJoinGame() -{ -} - -boost::shared_ptr -NetPacketJoinGame::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketJoinGame); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketJoinGame::SetData(const NetPacketJoinGame::Data &inData) -{ - u_int16_t passwordLen = (u_int16_t)inData.password.length(); - - // Some basic checks, so we don't use up too much memory. - // The constructed packet will also be checked. - if (passwordLen > MAX_PASSWORD_SIZE) - throw NetException(__FILE__, __LINE__, ERR_NET_INVALID_PASSWORD_STR, 0); - - // Resize the packet so that the data fits in. - Resize((u_int16_t) - (sizeof(NetPacketJoinGameData) + ADD_PADDING(passwordLen))); - - NetPacketJoinGameData *tmpData = (NetPacketJoinGameData *)GetRawData(); - - // Set the data. - tmpData->gameId = htonl(inData.gameId); - tmpData->passwordLength = htons(passwordLen); - - char *passwordPtr = (char *)tmpData + sizeof(NetPacketJoinGameData); - memcpy(passwordPtr, inData.password.c_str(), passwordLen); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketJoinGame::GetData(NetPacketJoinGame::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketJoinGameData *tmpData = (NetPacketJoinGameData *)GetRawData(); - - outData.gameId = ntohl(tmpData->gameId); - - u_int16_t passwordLen = ntohs(tmpData->passwordLength); - char *passwordPtr = (char *)tmpData + sizeof(NetPacketJoinGameData); - outData.password = string(passwordPtr, passwordLen); -} - -const NetPacketJoinGame * -NetPacketJoinGame::ToNetPacketJoinGame() const -{ - return this; -} - -void -NetPacketJoinGame::InternalCheck(const NetPacketHeader* data) const -{ - u_int16_t dataLen = ntohs(data->length); - NetPacketJoinGameData *tmpData = (NetPacketJoinGameData *)data; - int passwordLength = ntohs(tmpData->passwordLength); - // Exact checking of dynamic packet size. - if (dataLen < - sizeof(NetPacketJoinGameData) - + ADD_PADDING(passwordLength)) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check string size. - if (passwordLength > MAX_PASSWORD_SIZE) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } -} - -//----------------------------------------------------------------------------- - -NetPacketJoinGameAck::NetPacketJoinGameAck() -: NetPacket(NET_TYPE_JOIN_GAME_ACK, sizeof(NetPacketJoinGameAckData), MAX_PACKET_SIZE) -{ -} - -NetPacketJoinGameAck::~NetPacketJoinGameAck() -{ -} - -boost::shared_ptr -NetPacketJoinGameAck::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketJoinGameAck); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketJoinGameAck::SetData(const NetPacketJoinGameAck::Data &inData) -{ - u_int16_t numManualBlinds = (u_int16_t)inData.gameData.manualBlindsList.size(); - - // Some basic checks, so we don't use up too much memory. - // The constructed packet will also be checked. - if (numManualBlinds > MAX_NUM_MANUAL_BLINDS) - throw NetException(__FILE__, __LINE__, ERR_NET_TOO_MANY_MANUAL_BLINDS, 0); - - int manualBlindsSize = numManualBlinds * sizeof(u_int32_t); - // Resize the packet so that the data fits in. - Resize((u_int16_t) - (sizeof(NetPacketJoinGameAckData) + manualBlindsSize)); - - NetPacketJoinGameAckData *tmpData = (NetPacketJoinGameAckData *)GetRawData(); - - // Set the data. - tmpData->gameId = htonl(inData.gameId); - tmpData->playerRights = htons(inData.prights); - - SetGameInfoData(inData.gameData, &tmpData->gameData); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketJoinGameAck::GetData(NetPacketJoinGameAck::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketJoinGameAckData *tmpData = (NetPacketJoinGameAckData *)GetRawData(); - - outData.gameId = ntohl(tmpData->gameId); - outData.prights = static_cast(ntohs(tmpData->playerRights)); - - GetGameInfoData(&tmpData->gameData, outData.gameData); -} - -const NetPacketJoinGameAck * -NetPacketJoinGameAck::ToNetPacketJoinGameAck() const -{ - return this; -} - -void -NetPacketJoinGameAck::InternalCheck(const NetPacketHeader* data) const -{ - u_int16_t dataLen = ntohs(data->length); - NetPacketJoinGameAckData *tmpData = (NetPacketJoinGameAckData *)data; - int numManualBlinds = ntohs(tmpData->gameData.numberOfManualBlinds); - int manualBlindsLength = numManualBlinds * sizeof(u_int32_t); - // Exact checking of dynamic packet size. - if (dataLen < - sizeof(NetPacketJoinGameAckData) - + manualBlindsLength) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - - // Check manual blind list size - if (numManualBlinds > MAX_NUM_MANUAL_BLINDS) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Semantic checks - CheckGameInfoData(&tmpData->gameData); -} - -//----------------------------------------------------------------------------- - -NetPacketJoinGameFailed::NetPacketJoinGameFailed() -: NetPacket(NET_TYPE_JOIN_GAME_FAILED, sizeof(NetPacketJoinGameFailedData), sizeof(NetPacketJoinGameFailedData)) -{ -} - -NetPacketJoinGameFailed::~NetPacketJoinGameFailed() -{ -} - -boost::shared_ptr -NetPacketJoinGameFailed::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketJoinGameFailed); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketJoinGameFailed::SetData(const NetPacketJoinGameFailed::Data &inData) -{ - NetPacketJoinGameFailedData *tmpData = (NetPacketJoinGameFailedData *)GetRawData(); - - switch (inData.failureCode) - { - // Join Game Errors. - case NTF_NET_JOIN_GAME_FULL : - tmpData->failureReason = htons(NET_JOIN_FAILED_GAME_FULL); - break; - case NTF_NET_JOIN_ALREADY_RUNNING : - tmpData->failureReason = htons(NET_JOIN_FAILED_GAME_ALREADY_RUNNING); - break; - case NTF_NET_JOIN_INVALID_PASSWORD : - tmpData->failureReason = htons(NET_JOIN_FAILED_INVALID_PASSWORD); - break; - default : - tmpData->failureReason = htons(NET_JOIN_FAILED_OTHER); - break; - } - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketJoinGameFailed::GetData(NetPacketJoinGameFailed::Data &outData) const -{ - NetPacketJoinGameFailedData *tmpData = (NetPacketJoinGameFailedData *)GetRawData(); - - switch (ntohs(tmpData->failureReason)) - { - // Join Game Errors. - case NET_JOIN_FAILED_GAME_FULL : - outData.failureCode = NTF_NET_JOIN_GAME_FULL; - break; - case NET_JOIN_FAILED_GAME_ALREADY_RUNNING : - outData.failureCode = NTF_NET_JOIN_ALREADY_RUNNING; - break; - case NET_JOIN_FAILED_INVALID_PASSWORD : - outData.failureCode = NTF_NET_JOIN_INVALID_PASSWORD; - break; - default : - outData.failureCode = NTF_NET_INTERNAL; - break; - } -} - -const NetPacketJoinGameFailed * -NetPacketJoinGameFailed::ToNetPacketJoinGameFailed() const -{ - return this; -} - -void -NetPacketJoinGameFailed::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketPlayerJoined::NetPacketPlayerJoined() -: NetPacket(NET_TYPE_PLAYER_JOINED, sizeof(NetPacketPlayerJoinedData), sizeof(NetPacketPlayerJoinedData)) -{ -} - -NetPacketPlayerJoined::~NetPacketPlayerJoined() -{ -} - -boost::shared_ptr -NetPacketPlayerJoined::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketPlayerJoined); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketPlayerJoined::SetData(const NetPacketPlayerJoined::Data &inData) -{ - NetPacketPlayerJoinedData *tmpData = (NetPacketPlayerJoinedData *)GetRawData(); - - // Set the data. - tmpData->playerId = htonl(inData.playerId); - tmpData->playerRights = htons(inData.prights); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketPlayerJoined::GetData(NetPacketPlayerJoined::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketPlayerJoinedData *tmpData = (NetPacketPlayerJoinedData *)GetRawData(); - - outData.playerId = ntohl(tmpData->playerId); - outData.prights = static_cast(ntohs(tmpData->playerRights)); -} - -const NetPacketPlayerJoined * -NetPacketPlayerJoined::ToNetPacketPlayerJoined() const -{ - return this; -} - -void -NetPacketPlayerJoined::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketPlayerLeft::NetPacketPlayerLeft() -: NetPacket(NET_TYPE_PLAYER_LEFT, sizeof(NetPacketPlayerLeftData), sizeof(NetPacketPlayerLeftData)) -{ -} - -NetPacketPlayerLeft::~NetPacketPlayerLeft() -{ -} - -boost::shared_ptr -NetPacketPlayerLeft::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketPlayerLeft); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketPlayerLeft::SetData(const NetPacketPlayerLeft::Data &inData) -{ - NetPacketPlayerLeftData *tmpData = (NetPacketPlayerLeftData *)GetRawData(); - - // Set the data. - tmpData->playerId = htonl(inData.playerId); - tmpData->leaveReason = htons(inData.removeReason); - - switch(inData.removeReason) - { - case NTF_NET_REMOVED_ON_REQUEST : - tmpData->leaveReason = htons(NET_LEFT_ON_REQUEST); - break; - case NTF_NET_REMOVED_KICKED : - tmpData->leaveReason = htons(NET_LEFT_KICKED); - break; - case NTF_NET_INTERNAL : - tmpData->leaveReason = htons(NET_LEFT_ERROR); - break; - default : - tmpData->leaveReason = htons(NET_LEFT_OTHER_REASON); - break; - } - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketPlayerLeft::GetData(NetPacketPlayerLeft::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketPlayerLeftData *tmpData = (NetPacketPlayerLeftData *)GetRawData(); - - outData.playerId = ntohl(tmpData->playerId); - - switch(ntohs(tmpData->leaveReason)) - { - case NET_LEFT_ON_REQUEST : - outData.removeReason = NTF_NET_REMOVED_ON_REQUEST; - break; - case NET_LEFT_KICKED : - outData.removeReason = NTF_NET_REMOVED_KICKED; - break; - default : - outData.removeReason = NTF_NET_INTERNAL; - break; - } -} - -const NetPacketPlayerLeft * -NetPacketPlayerLeft::ToNetPacketPlayerLeft() const -{ - return this; -} - -void -NetPacketPlayerLeft::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - - -//----------------------------------------------------------------------------- - -NetPacketGameAdminChanged::NetPacketGameAdminChanged() -: NetPacket(NET_TYPE_GAME_ADMIN_CHANGED, sizeof(NetPacketGameAdminChangedData), sizeof(NetPacketGameAdminChangedData)) -{ -} - -NetPacketGameAdminChanged::~NetPacketGameAdminChanged() -{ -} - -boost::shared_ptr -NetPacketGameAdminChanged::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketGameAdminChanged); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketGameAdminChanged::SetData(const NetPacketGameAdminChanged::Data &inData) -{ - NetPacketGameAdminChangedData *tmpData = (NetPacketGameAdminChangedData *)GetRawData(); - - // Set the data. - tmpData->playerId = htonl(inData.playerId); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketGameAdminChanged::GetData(NetPacketGameAdminChanged::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketGameAdminChangedData *tmpData = (NetPacketGameAdminChangedData *)GetRawData(); - - outData.playerId = ntohl(tmpData->playerId); -} - -const NetPacketGameAdminChanged * -NetPacketGameAdminChanged::ToNetPacketGameAdminChanged() const -{ - return this; -} - -void -NetPacketGameAdminChanged::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketKickPlayer::NetPacketKickPlayer() -: NetPacket(NET_TYPE_KICK_PLAYER, sizeof(NetPacketKickPlayerData), sizeof(NetPacketKickPlayerData)) -{ -} - -NetPacketKickPlayer::~NetPacketKickPlayer() -{ -} - -boost::shared_ptr -NetPacketKickPlayer::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketKickPlayer); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketKickPlayer::SetData(const NetPacketKickPlayer::Data &inData) -{ - NetPacketKickPlayerData *tmpData = (NetPacketKickPlayerData *)GetRawData(); - - // Set the data. - tmpData->playerId = htonl(inData.playerId); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketKickPlayer::GetData(NetPacketKickPlayer::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketKickPlayerData *tmpData = (NetPacketKickPlayerData *)GetRawData(); - - outData.playerId = ntohl(tmpData->playerId); -} - -const NetPacketKickPlayer * -NetPacketKickPlayer::ToNetPacketKickPlayer() const -{ - return this; -} - -void -NetPacketKickPlayer::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketLeaveCurrentGame::NetPacketLeaveCurrentGame() -: NetPacket(NET_TYPE_LEAVE_CURRENT_GAME, sizeof(NetPacketLeaveCurrentGameData), sizeof(NetPacketLeaveCurrentGameData)) -{ -} - -NetPacketLeaveCurrentGame::~NetPacketLeaveCurrentGame() -{ -} - -boost::shared_ptr -NetPacketLeaveCurrentGame::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketLeaveCurrentGame); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -const NetPacketLeaveCurrentGame * -NetPacketLeaveCurrentGame::ToNetPacketLeaveCurrentGame() const -{ - return this; -} - -void -NetPacketLeaveCurrentGame::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketStartEvent::NetPacketStartEvent() -: NetPacket(NET_TYPE_START_EVENT, sizeof(NetPacketStartEventData), sizeof(NetPacketStartEventData)) -{ -} - -NetPacketStartEvent::~NetPacketStartEvent() -{ -} - -boost::shared_ptr -NetPacketStartEvent::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketStartEvent); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketStartEvent::SetData(const NetPacketStartEvent::Data &inData) -{ - NetPacketStartEventData *tmpData = (NetPacketStartEventData *)GetRawData(); - - // Set the data. - tmpData->startFlags = htons(inData.fillUpWithCpuPlayers ? NET_START_FLAG_FILL_WITH_CPU_PLAYERS : 0); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketStartEvent::GetData(NetPacketStartEvent::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketStartEventData *tmpData = (NetPacketStartEventData *)GetRawData(); - - outData.fillUpWithCpuPlayers = (ntohs(tmpData->startFlags) & NET_START_FLAG_FILL_WITH_CPU_PLAYERS); -} - -const NetPacketStartEvent * -NetPacketStartEvent::ToNetPacketStartEvent() const -{ - return this; -} - -void -NetPacketStartEvent::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketStartEventAck::NetPacketStartEventAck() -: NetPacket(NET_TYPE_START_EVENT_ACK, sizeof(NetPacketStartEventAckData), sizeof(NetPacketStartEventAckData)) -{ -} - -NetPacketStartEventAck::~NetPacketStartEventAck() -{ -} - -boost::shared_ptr -NetPacketStartEventAck::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketStartEventAck); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -const NetPacketStartEventAck * -NetPacketStartEventAck::ToNetPacketStartEventAck() const -{ - return this; -} - -void -NetPacketStartEventAck::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketGameStart::NetPacketGameStart() -: NetPacket(NET_TYPE_GAME_START, sizeof(NetPacketGameStartData), MAX_PACKET_SIZE) -{ -} - -NetPacketGameStart::~NetPacketGameStart() -{ -} - -boost::shared_ptr -NetPacketGameStart::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketGameStart); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketGameStart::SetData(const NetPacketGameStart::Data &inData) -{ - u_int16_t numPlayers = (u_int16_t)inData.playerSlots.size(); - - // Basic checking. - if (numPlayers < MIN_NUMBER_OF_PLAYERS || numPlayers > MAX_NUMBER_OF_PLAYERS || numPlayers != inData.startData.numberOfPlayers) - { - LOG_VERBOSE("Invalid number of players. Slots: " << numPlayers << ", Players: " << inData.startData.numberOfPlayers << "."); - throw NetException(__FILE__, __LINE__, ERR_NET_INVALID_PLAYER_COUNT, 0); - } - - // Resize the packet so that the data fits in. - Resize((u_int16_t) - (sizeof(NetPacketGameStartData) + ADD_PADDING(numPlayers * sizeof(PlayerSlotData)))); - - NetPacketGameStartData *tmpData = (NetPacketGameStartData *)GetRawData(); - - tmpData->startDealerPlayerId = htonl(inData.startData.startDealerPlayerId); - tmpData->numberOfPlayers = htons(numPlayers); - - PlayerSlotList::const_iterator i = inData.playerSlots.begin(); - PlayerSlotList::const_iterator end = inData.playerSlots.end(); - - // Copy the player slot data to continous memory - PlayerSlotData *curPlayerSlotData = - (PlayerSlotData *)((char *)tmpData + sizeof(NetPacketGameStartData)); - while (i != end) - { - curPlayerSlotData->playerId = htonl((*i).playerId); - ++curPlayerSlotData; - ++i; - } - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketGameStart::GetData(NetPacketGameStart::Data &outData) const -{ - NetPacketGameStartData *tmpData = (NetPacketGameStartData *)GetRawData(); - - outData.startData.startDealerPlayerId = ntohl(tmpData->startDealerPlayerId); - u_int16_t numPlayers = ntohs(tmpData->numberOfPlayers); - outData.startData.numberOfPlayers = numPlayers; - - PlayerSlotData *curPlayerSlotData = - (PlayerSlotData *)((char *)tmpData + sizeof(NetPacketGameStartData)); - - // Store all available player slots. - for (int i = 0; i < numPlayers; i++) - { - PlayerSlot tmpPlayerSlot; - tmpPlayerSlot.playerId = ntohl(curPlayerSlotData->playerId); - - outData.playerSlots.push_back(tmpPlayerSlot); - ++curPlayerSlotData; - } -} - -const NetPacketGameStart * -NetPacketGameStart::ToNetPacketGameStart() const -{ - return this; -} - -void -NetPacketGameStart::InternalCheck(const NetPacketHeader* data) const -{ - u_int16_t dataLen = ntohs(data->length); - NetPacketGameStartData *tmpData = (NetPacketGameStartData *)data; - int numPlayers = ntohs(tmpData->numberOfPlayers); - // Check exact packet size. - if (dataLen != - sizeof(NetPacketGameStartData) - + numPlayers * sizeof(PlayerSlotData)) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Semantic checks not needed. -} - -//----------------------------------------------------------------------------- - -NetPacketHandStart::NetPacketHandStart() -: NetPacket(NET_TYPE_HAND_START, sizeof(NetPacketHandStartData), sizeof(NetPacketHandStartData)) -{ -} - -NetPacketHandStart::~NetPacketHandStart() -{ -} - -boost::shared_ptr -NetPacketHandStart::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketHandStart); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketHandStart::SetData(const NetPacketHandStart::Data &inData) -{ - NetPacketHandStartData *tmpData = (NetPacketHandStartData *)GetRawData(); - - tmpData->yourCard1 = htons(inData.yourCards[0]); - tmpData->yourCard2 = htons(inData.yourCards[1]); - tmpData->smallBlind = htonl(inData.smallBlind); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketHandStart::GetData(NetPacketHandStart::Data &outData) const -{ - NetPacketHandStartData *tmpData = (NetPacketHandStartData *)GetRawData(); - - outData.yourCards[0] = ntohs(tmpData->yourCard1); - outData.yourCards[1] = ntohs(tmpData->yourCard2); - outData.smallBlind = ntohl(tmpData->smallBlind); -} - -const NetPacketHandStart * -NetPacketHandStart::ToNetPacketHandStart() const -{ - return this; -} - -void -NetPacketHandStart::InternalCheck(const NetPacketHeader* data) const -{ - NetPacketHandStartData *tmpData = (NetPacketHandStartData *)data; - if (ntohs(tmpData->yourCard1) > 51 || ntohs(tmpData->yourCard2) > 51) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } -} - -//----------------------------------------------------------------------------- - -NetPacketPlayersTurn::NetPacketPlayersTurn() -: NetPacket(NET_TYPE_PLAYERS_TURN, sizeof(NetPacketPlayersTurnData), sizeof(NetPacketPlayersTurnData)) -{ -} - -NetPacketPlayersTurn::~NetPacketPlayersTurn() -{ -} - -boost::shared_ptr -NetPacketPlayersTurn::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketPlayersTurn); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketPlayersTurn::SetData(const NetPacketPlayersTurn::Data &inData) -{ - NetPacketPlayersTurnData *tmpData = (NetPacketPlayersTurnData *)GetRawData(); - - tmpData->playerId = htonl(inData.playerId); - tmpData->gameState = htons(inData.gameState); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketPlayersTurn::GetData(NetPacketPlayersTurn::Data &outData) const -{ - NetPacketPlayersTurnData *tmpData = (NetPacketPlayersTurnData *)GetRawData(); - - outData.playerId = ntohl(tmpData->playerId); - outData.gameState = static_cast(ntohs(tmpData->gameState)); -} - -const NetPacketPlayersTurn * -NetPacketPlayersTurn::ToNetPacketPlayersTurn() const -{ - return this; -} - -void -NetPacketPlayersTurn::InternalCheck(const NetPacketHeader* data) const -{ - // Check whether the state is valid. - NetPacketPlayersTurnData *tmpData = (NetPacketPlayersTurnData *)data; - if (ntohs(tmpData->gameState) > GAME_STATE_RIVER) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } -} - -//----------------------------------------------------------------------------- - -NetPacketPlayersAction::NetPacketPlayersAction() -: NetPacket(NET_TYPE_PLAYERS_ACTION, sizeof(NetPacketPlayersActionData), sizeof(NetPacketPlayersActionData)) -{ -} - -NetPacketPlayersAction::~NetPacketPlayersAction() -{ -} - -boost::shared_ptr -NetPacketPlayersAction::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketPlayersAction); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketPlayersAction::SetData(const NetPacketPlayersAction::Data &inData) -{ - NetPacketPlayersActionData *tmpData = (NetPacketPlayersActionData *)GetRawData(); - - tmpData->gameState = htons(inData.gameState); - tmpData->playerAction = htons(inData.playerAction); - tmpData->playerBet = htonl(inData.playerBet); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketPlayersAction::GetData(NetPacketPlayersAction::Data &outData) const -{ - NetPacketPlayersActionData *tmpData = (NetPacketPlayersActionData *)GetRawData(); - - outData.gameState = static_cast(ntohs(tmpData->gameState)); - outData.playerAction = static_cast(ntohs(tmpData->playerAction)); - outData.playerBet = ntohl(tmpData->playerBet); -} - -const NetPacketPlayersAction * -NetPacketPlayersAction::ToNetPacketPlayersAction() const -{ - return this; -} - -void -NetPacketPlayersAction::InternalCheck(const NetPacketHeader* data) const -{ - NetPacketPlayersActionData *tmpData = (NetPacketPlayersActionData *)data; - // Check whether the state is valid. - if (ntohs(tmpData->gameState) > GAME_STATE_RIVER) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check whether the player action is valid. - if (ntohs(tmpData->playerAction) > PLAYER_ACTION_ALLIN) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Semantic checks are done by the server process. -} - -//----------------------------------------------------------------------------- - -NetPacketPlayersActionDone::NetPacketPlayersActionDone() -: NetPacket(NET_TYPE_PLAYERS_ACTION_DONE, sizeof(NetPacketPlayersActionDoneData), sizeof(NetPacketPlayersActionDoneData)) -{ -} - -NetPacketPlayersActionDone::~NetPacketPlayersActionDone() -{ -} - -boost::shared_ptr -NetPacketPlayersActionDone::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketPlayersActionDone); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketPlayersActionDone::SetData(const NetPacketPlayersActionDone::Data &inData) -{ - NetPacketPlayersActionDoneData *tmpData = (NetPacketPlayersActionDoneData *)GetRawData(); - - tmpData->playerId = htonl(inData.playerId); - tmpData->gameState = htons(inData.gameState); - tmpData->playerAction = htons(inData.playerAction); - tmpData->totalPlayerBet = htonl(inData.totalPlayerBet); - tmpData->playerMoney = htonl(inData.playerMoney); - tmpData->highestSet = htonl(inData.highestSet); - tmpData->minimumRaise = htonl(inData.minimumRaise); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketPlayersActionDone::GetData(NetPacketPlayersActionDone::Data &outData) const -{ - NetPacketPlayersActionDoneData *tmpData = (NetPacketPlayersActionDoneData *)GetRawData(); - - outData.playerId = ntohl(tmpData->playerId); - outData.gameState = static_cast(ntohs(tmpData->gameState)); - outData.playerAction = static_cast(ntohs(tmpData->playerAction)); - outData.totalPlayerBet = ntohl(tmpData->totalPlayerBet); - outData.playerMoney = ntohl(tmpData->playerMoney); - outData.highestSet = ntohl(tmpData->highestSet); - outData.minimumRaise = ntohl(tmpData->minimumRaise); -} - -const NetPacketPlayersActionDone * -NetPacketPlayersActionDone::ToNetPacketPlayersActionDone() const -{ - return this; -} - -void -NetPacketPlayersActionDone::InternalCheck(const NetPacketHeader* data) const -{ - NetPacketPlayersActionDoneData *tmpData = (NetPacketPlayersActionDoneData *)data; - // Check whether the state is valid. - int gameState = ntohs(tmpData->gameState); - if (gameState > GAME_STATE_RIVER - && gameState != GAME_STATE_PREFLOP_SMALL_BLIND - && gameState != GAME_STATE_PREFLOP_BIG_BLIND) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check whether the player action is valid. - if (ntohs(tmpData->playerAction) > PLAYER_ACTION_ALLIN) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } -} - -//----------------------------------------------------------------------------- - -NetPacketPlayersActionRejected::NetPacketPlayersActionRejected() -: NetPacket(NET_TYPE_PLAYERS_ACTION_REJECTED, sizeof(NetPacketPlayersActionRejectedData), sizeof(NetPacketPlayersActionRejectedData)) -{ -} - -NetPacketPlayersActionRejected::~NetPacketPlayersActionRejected() -{ -} - -boost::shared_ptr -NetPacketPlayersActionRejected::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketPlayersActionRejected); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketPlayersActionRejected::SetData(const NetPacketPlayersActionRejected::Data &inData) -{ - NetPacketPlayersActionRejectedData *tmpData = (NetPacketPlayersActionRejectedData *)GetRawData(); - - tmpData->gameState = htons(inData.gameState); - tmpData->playerAction = htons(inData.playerAction); - tmpData->playerBet = htonl(inData.playerBet); - tmpData->rejectionReason = htons(inData.rejectionReason); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketPlayersActionRejected::GetData(NetPacketPlayersActionRejected::Data &outData) const -{ - NetPacketPlayersActionRejectedData *tmpData = (NetPacketPlayersActionRejectedData *)GetRawData(); - - outData.gameState = static_cast(ntohs(tmpData->gameState)); - outData.playerAction = static_cast(ntohs(tmpData->playerAction)); - outData.playerBet = ntohl(tmpData->playerBet); - outData.rejectionReason = static_cast(ntohs(tmpData->rejectionReason)); -} - -const NetPacketPlayersActionRejected * -NetPacketPlayersActionRejected::ToNetPacketPlayersActionRejected() const -{ - return this; -} - -void -NetPacketPlayersActionRejected::InternalCheck(const NetPacketHeader* data) const -{ - // Check whether the state is valid. - NetPacketPlayersActionRejectedData *tmpData = (NetPacketPlayersActionRejectedData *)data; - if (ntohs(tmpData->gameState) > GAME_STATE_RIVER) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check whether the player action is valid. - if (ntohs(tmpData->playerAction) > PLAYER_ACTION_ALLIN) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - if (!ntohs(tmpData->rejectionReason)) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } -} - -//----------------------------------------------------------------------------- - -NetPacketDealFlopCards::NetPacketDealFlopCards() -: NetPacket(NET_TYPE_DEAL_FLOP_CARDS, sizeof(NetPacketDealFlopCardsData), sizeof(NetPacketDealFlopCardsData)) -{ -} - -NetPacketDealFlopCards::~NetPacketDealFlopCards() -{ -} - -boost::shared_ptr -NetPacketDealFlopCards::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketDealFlopCards); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketDealFlopCards::SetData(const NetPacketDealFlopCards::Data &inData) -{ - NetPacketDealFlopCardsData *tmpData = (NetPacketDealFlopCardsData *)GetRawData(); - - tmpData->flopCard1 = htons(inData.flopCards[0]); - tmpData->flopCard2 = htons(inData.flopCards[1]); - tmpData->flopCard3 = htons(inData.flopCards[2]); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketDealFlopCards::GetData(NetPacketDealFlopCards::Data &outData) const -{ - NetPacketDealFlopCardsData *tmpData = (NetPacketDealFlopCardsData *)GetRawData(); - - outData.flopCards[0] = ntohs(tmpData->flopCard1); - outData.flopCards[1] = ntohs(tmpData->flopCard2); - outData.flopCards[2] = ntohs(tmpData->flopCard3); -} - -const NetPacketDealFlopCards * -NetPacketDealFlopCards::ToNetPacketDealFlopCards() const -{ - return this; -} - -void -NetPacketDealFlopCards::InternalCheck(const NetPacketHeader* data) const -{ - NetPacketDealFlopCardsData *tmpData = (NetPacketDealFlopCardsData *)data; - if (ntohs(tmpData->flopCard1) > 51 || ntohs(tmpData->flopCard2) > 51 || ntohs(tmpData->flopCard3) > 51) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } -} - -//----------------------------------------------------------------------------- - -NetPacketDealTurnCard::NetPacketDealTurnCard() -: NetPacket(NET_TYPE_DEAL_TURN_CARD, sizeof(NetPacketDealTurnCardData), sizeof(NetPacketDealTurnCardData)) -{ -} - -NetPacketDealTurnCard::~NetPacketDealTurnCard() -{ -} - -boost::shared_ptr -NetPacketDealTurnCard::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketDealTurnCard); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketDealTurnCard::SetData(const NetPacketDealTurnCard::Data &inData) -{ - NetPacketDealTurnCardData *tmpData = (NetPacketDealTurnCardData *)GetRawData(); - - tmpData->turnCard = htons(inData.turnCard); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketDealTurnCard::GetData(NetPacketDealTurnCard::Data &outData) const -{ - NetPacketDealTurnCardData *tmpData = (NetPacketDealTurnCardData *)GetRawData(); - - outData.turnCard = ntohs(tmpData->turnCard); -} - -const NetPacketDealTurnCard * -NetPacketDealTurnCard::ToNetPacketDealTurnCard() const -{ - return this; -} - -void -NetPacketDealTurnCard::InternalCheck(const NetPacketHeader* data) const -{ - NetPacketDealTurnCardData *tmpData = (NetPacketDealTurnCardData *)data; - if (ntohs(tmpData->turnCard) > 51) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } -} - -//----------------------------------------------------------------------------- - -NetPacketDealRiverCard::NetPacketDealRiverCard() -: NetPacket(NET_TYPE_DEAL_RIVER_CARD, sizeof(NetPacketDealRiverCardData), sizeof(NetPacketDealRiverCardData)) -{ -} - -NetPacketDealRiverCard::~NetPacketDealRiverCard() -{ -} - -boost::shared_ptr -NetPacketDealRiverCard::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketDealRiverCard); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketDealRiverCard::SetData(const NetPacketDealRiverCard::Data &inData) -{ - NetPacketDealRiverCardData *tmpData = (NetPacketDealRiverCardData *)GetRawData(); - - tmpData->riverCard = htons(inData.riverCard); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketDealRiverCard::GetData(NetPacketDealRiverCard::Data &outData) const -{ - NetPacketDealRiverCardData *tmpData = (NetPacketDealRiverCardData *)GetRawData(); - - outData.riverCard = ntohs(tmpData->riverCard); -} - -const NetPacketDealRiverCard * -NetPacketDealRiverCard::ToNetPacketDealRiverCard() const -{ - return this; -} - -void -NetPacketDealRiverCard::InternalCheck(const NetPacketHeader* data) const -{ - NetPacketDealRiverCardData *tmpData = (NetPacketDealRiverCardData *)data; - if (ntohs(tmpData->riverCard) > 51) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } -} - -//----------------------------------------------------------------------------- - -NetPacketAllInShowCards::NetPacketAllInShowCards() -: NetPacket(NET_TYPE_ALL_IN_SHOW_CARDS, sizeof(NetPacketAllInShowCardsData), MAX_PACKET_SIZE) -{ -} - -NetPacketAllInShowCards::~NetPacketAllInShowCards() -{ -} - -boost::shared_ptr -NetPacketAllInShowCards::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketAllInShowCards); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketAllInShowCards::SetData(const NetPacketAllInShowCards::Data &inData) -{ - u_int16_t numPlayerCards = (u_int16_t)inData.playerCards.size(); - - if (!numPlayerCards || numPlayerCards > MAX_NUM_PLAYER_CARDS) - throw NetException(__FILE__, __LINE__, ERR_NET_INVALID_PLAYER_CARDS, 0); - - // Resize the packet so that the data fits in. - Resize((u_int16_t) - (sizeof(NetPacketAllInShowCardsData) + numPlayerCards * sizeof(PlayerCardsData))); - - NetPacketAllInShowCardsData *tmpData = (NetPacketAllInShowCardsData *)GetRawData(); - - tmpData->numberOfPlayerCards = htons(numPlayerCards); - - PlayerCardsList::const_iterator i = inData.playerCards.begin(); - PlayerCardsList::const_iterator end = inData.playerCards.end(); - - // Copy the player cards data to continous memory - PlayerCardsData *curPlayerCardsData = - (PlayerCardsData *)((char *)tmpData + sizeof(NetPacketAllInShowCardsData)); - while (i != end) - { - curPlayerCardsData->playerId = htonl((*i).playerId); - curPlayerCardsData->card1 = htons((*i).cards[0]); - curPlayerCardsData->card2 = htons((*i).cards[1]); - ++curPlayerCardsData; - ++i; - } - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketAllInShowCards::GetData(NetPacketAllInShowCards::Data &outData) const -{ - NetPacketAllInShowCardsData *tmpData = (NetPacketAllInShowCardsData *)GetRawData(); - - outData.playerCards.clear(); - - u_int16_t numPlayerCards = ntohs(tmpData->numberOfPlayerCards); - PlayerCardsData *curPlayerCardsData = - (PlayerCardsData *)((char *)tmpData + sizeof(NetPacketAllInShowCardsData)); - - // Store all available player cards. - for (int i = 0; i < numPlayerCards; i++) - { - PlayerCards tmpPlayerCards; - tmpPlayerCards.playerId = ntohl(curPlayerCardsData->playerId); - tmpPlayerCards.cards[0] = ntohs(curPlayerCardsData->card1); - tmpPlayerCards.cards[1] = ntohs(curPlayerCardsData->card2); - - outData.playerCards.push_back(tmpPlayerCards); - ++curPlayerCardsData; - } -} - -const NetPacketAllInShowCards * -NetPacketAllInShowCards::ToNetPacketAllInShowCards() const -{ - return this; -} - -void -NetPacketAllInShowCards::InternalCheck(const NetPacketHeader* data) const -{ - u_int16_t dataLen = ntohs(data->length); - NetPacketAllInShowCardsData *tmpData = (NetPacketAllInShowCardsData *)data; - int numPlayerCards = ntohs(tmpData->numberOfPlayerCards); - - if (dataLen != - sizeof(NetPacketAllInShowCardsData) - + numPlayerCards * sizeof(PlayerCardsData)) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - if (ntohs(tmpData->numberOfPlayerCards) > MAX_NUMBER_OF_PLAYERS) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Semantic checks not needed, this packet is sent by the server. -} - -//----------------------------------------------------------------------------- - -NetPacketEndOfHandShowCards::NetPacketEndOfHandShowCards() -: NetPacket(NET_TYPE_END_OF_HAND_SHOW_CARDS, sizeof(NetPacketEndOfHandShowCardsData), MAX_PACKET_SIZE) -{ -} - -NetPacketEndOfHandShowCards::~NetPacketEndOfHandShowCards() -{ -} - -boost::shared_ptr -NetPacketEndOfHandShowCards::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketEndOfHandShowCards); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketEndOfHandShowCards::SetData(const NetPacketEndOfHandShowCards::Data &inData) -{ - u_int16_t numPlayerResults = (u_int16_t)inData.playerResults.size(); - - if (!numPlayerResults || numPlayerResults > MAX_NUM_PLAYER_RESULTS) - throw NetException(__FILE__, __LINE__, ERR_NET_INVALID_PLAYER_RESULTS, 0); - - // Resize the packet so that the data fits in. - Resize((u_int16_t) - (sizeof(NetPacketEndOfHandShowCardsData) + numPlayerResults * sizeof(PlayerResultData))); - - NetPacketEndOfHandShowCardsData *tmpData = (NetPacketEndOfHandShowCardsData *)GetRawData(); - - tmpData->numberOfPlayerResults = htons(numPlayerResults); - - PlayerResultList::const_iterator i = inData.playerResults.begin(); - PlayerResultList::const_iterator end = inData.playerResults.end(); - - // Copy the player result data to continous memory - PlayerResultData *curPlayerResultData = - (PlayerResultData *)((char *)tmpData + sizeof(NetPacketEndOfHandShowCardsData)); - while (i != end) - { - curPlayerResultData->playerId = htonl((*i).playerId); - curPlayerResultData->card1 = htons((*i).cards[0]); - curPlayerResultData->card2 = htons((*i).cards[1]); - curPlayerResultData->bestHandPos1 = htons((*i).bestHandPos[0]); - curPlayerResultData->bestHandPos2 = htons((*i).bestHandPos[1]); - curPlayerResultData->bestHandPos3 = htons((*i).bestHandPos[2]); - curPlayerResultData->bestHandPos4 = htons((*i).bestHandPos[3]); - curPlayerResultData->bestHandPos5 = htons((*i).bestHandPos[4]); - curPlayerResultData->valueOfCards = htonl((*i).valueOfCards); - curPlayerResultData->moneyWon = htonl((*i).moneyWon); - curPlayerResultData->playerMoney = htonl((*i).playerMoney); - ++curPlayerResultData; - ++i; - } - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketEndOfHandShowCards::GetData(NetPacketEndOfHandShowCards::Data &outData) const -{ - NetPacketEndOfHandShowCardsData *tmpData = (NetPacketEndOfHandShowCardsData *)GetRawData(); - - outData.playerResults.clear(); - - u_int16_t numPlayerResults = ntohs(tmpData->numberOfPlayerResults); - PlayerResultData *curPlayerResultData = - (PlayerResultData *)((char *)tmpData + sizeof(NetPacketEndOfHandShowCardsData)); - - // Store all available player results. - for (int i = 0; i < numPlayerResults; i++) - { - PlayerResult tmpPlayerResult; - tmpPlayerResult.playerId = ntohl(curPlayerResultData->playerId); - tmpPlayerResult.cards[0] = ntohs(curPlayerResultData->card1); - tmpPlayerResult.cards[1] = ntohs(curPlayerResultData->card2); - tmpPlayerResult.bestHandPos[0] = ntohs(curPlayerResultData->bestHandPos1); - tmpPlayerResult.bestHandPos[1] = ntohs(curPlayerResultData->bestHandPos2); - tmpPlayerResult.bestHandPos[2] = ntohs(curPlayerResultData->bestHandPos3); - tmpPlayerResult.bestHandPos[3] = ntohs(curPlayerResultData->bestHandPos4); - tmpPlayerResult.bestHandPos[4] = ntohs(curPlayerResultData->bestHandPos5); - tmpPlayerResult.valueOfCards = ntohl(curPlayerResultData->valueOfCards); - tmpPlayerResult.moneyWon = ntohl(curPlayerResultData->moneyWon); - tmpPlayerResult.playerMoney = ntohl(curPlayerResultData->playerMoney); - - outData.playerResults.push_back(tmpPlayerResult); - ++curPlayerResultData; - } -} - -const NetPacketEndOfHandShowCards * -NetPacketEndOfHandShowCards::ToNetPacketEndOfHandShowCards() const -{ - return this; -} - -void -NetPacketEndOfHandShowCards::InternalCheck(const NetPacketHeader* data) const -{ - u_int16_t dataLen = ntohs(data->length); - NetPacketEndOfHandShowCardsData *tmpData = (NetPacketEndOfHandShowCardsData *)data; - int numPlayerResults = ntohs(tmpData->numberOfPlayerResults); - - if (dataLen != - sizeof(NetPacketEndOfHandShowCardsData) - + numPlayerResults * sizeof(PlayerResultData)) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - if (ntohs(tmpData->numberOfPlayerResults) > MAX_NUMBER_OF_PLAYERS) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Semantic checks not needed, this packet is sent by the server. -} - -//----------------------------------------------------------------------------- - -NetPacketEndOfHandHideCards::NetPacketEndOfHandHideCards() -: NetPacket(NET_TYPE_END_OF_HAND_HIDE_CARDS, sizeof(NetPacketEndOfHandHideCardsData), sizeof(NetPacketEndOfHandHideCardsData)) -{ -} - -NetPacketEndOfHandHideCards::~NetPacketEndOfHandHideCards() -{ -} - -boost::shared_ptr -NetPacketEndOfHandHideCards::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketEndOfHandHideCards); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketEndOfHandHideCards::SetData(const NetPacketEndOfHandHideCards::Data &inData) -{ - NetPacketEndOfHandHideCardsData *tmpData = (NetPacketEndOfHandHideCardsData *)GetRawData(); - - tmpData->playerId = htonl(inData.playerId); - tmpData->moneyWon = htonl(inData.moneyWon); - tmpData->playerMoney = htonl(inData.playerMoney); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketEndOfHandHideCards::GetData(NetPacketEndOfHandHideCards::Data &outData) const -{ - NetPacketEndOfHandHideCardsData *tmpData = (NetPacketEndOfHandHideCardsData *)GetRawData(); - - outData.playerId = ntohl(tmpData->playerId); - outData.moneyWon = ntohl(tmpData->moneyWon); - outData.playerMoney = ntohl(tmpData->playerMoney); -} - -const NetPacketEndOfHandHideCards * -NetPacketEndOfHandHideCards::ToNetPacketEndOfHandHideCards() const -{ - return this; -} - -void -NetPacketEndOfHandHideCards::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketEndOfGame::NetPacketEndOfGame() -: NetPacket(NET_TYPE_END_OF_GAME, sizeof(NetPacketEndOfGameData), sizeof(NetPacketEndOfGameData)) -{ -} - -NetPacketEndOfGame::~NetPacketEndOfGame() -{ -} - -boost::shared_ptr -NetPacketEndOfGame::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketEndOfGame); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketEndOfGame::SetData(const NetPacketEndOfGame::Data &inData) -{ - NetPacketEndOfGameData *tmpData = (NetPacketEndOfGameData *)GetRawData(); - - tmpData->winnerPlayerId = htonl(inData.winnerPlayerId); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketEndOfGame::GetData(NetPacketEndOfGame::Data &outData) const -{ - NetPacketEndOfGameData *tmpData = (NetPacketEndOfGameData *)GetRawData(); - - outData.winnerPlayerId = ntohl(tmpData->winnerPlayerId); -} - -const NetPacketEndOfGame * -NetPacketEndOfGame::ToNetPacketEndOfGame() const -{ - return this; -} - -void -NetPacketEndOfGame::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketAskKickPlayer::NetPacketAskKickPlayer() -: NetPacket(NET_TYPE_ASK_KICK_PLAYER, sizeof(NetPacketAskKickPlayerData), sizeof(NetPacketAskKickPlayerData)) -{ -} - -NetPacketAskKickPlayer::~NetPacketAskKickPlayer() -{ -} - -boost::shared_ptr -NetPacketAskKickPlayer::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketAskKickPlayer); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketAskKickPlayer::SetData(const NetPacketAskKickPlayer::Data &inData) -{ - NetPacketAskKickPlayerData *tmpData = (NetPacketAskKickPlayerData *)GetRawData(); - - tmpData->playerId = htonl(inData.playerId); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketAskKickPlayer::GetData(NetPacketAskKickPlayer::Data &outData) const -{ - NetPacketAskKickPlayerData *tmpData = (NetPacketAskKickPlayerData *)GetRawData(); - - outData.playerId = ntohl(tmpData->playerId); -} - -const NetPacketAskKickPlayer * -NetPacketAskKickPlayer::ToNetPacketAskKickPlayer() const -{ - return this; -} - -void -NetPacketAskKickPlayer::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketAskKickPlayerDenied::NetPacketAskKickPlayerDenied() -: NetPacket(NET_TYPE_ASK_KICK_PLAYER_DENIED, sizeof(NetPacketAskKickPlayerDeniedData), sizeof(NetPacketAskKickPlayerDeniedData)) -{ -} - -NetPacketAskKickPlayerDenied::~NetPacketAskKickPlayerDenied() -{ -} - -boost::shared_ptr -NetPacketAskKickPlayerDenied::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketAskKickPlayerDenied); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketAskKickPlayerDenied::SetData(const NetPacketAskKickPlayerDenied::Data &inData) -{ - NetPacketAskKickPlayerDeniedData *tmpData = (NetPacketAskKickPlayerDeniedData *)GetRawData(); - - tmpData->playerId = htonl(inData.playerId); - switch (inData.denyReason) - { - case KICK_DENIED_INVALID_STATE: - tmpData->denyReason = htons(NET_ASK_KICK_DENIED_INVALID_STATE); - break; - case KICK_DENIED_TOO_FEW_PLAYERS: - tmpData->denyReason = htons(NET_ASK_KICK_DENIED_TOO_FEW_PLAYERS); - break; - case KICK_DENIED_TEMPORARY: - tmpData->denyReason = htons(NET_ASK_KICK_DENIED_TEMPORARY); - break; - case KICK_DENIED_OTHER_IN_PROGRESS: - tmpData->denyReason = htons(NET_ASK_KICK_DENIED_OTHER_IN_PROGRESS); - break; - case KICK_DENIED_INVALID_PLAYER_ID: - tmpData->denyReason = htons(NET_ASK_KICK_DENIED_INVALID_PLAYER_ID); - break; - default: - tmpData->denyReason = htons(NET_ASK_KICK_DENIED_OTHER_REASON); - break; - } - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketAskKickPlayerDenied::GetData(NetPacketAskKickPlayerDenied::Data &outData) const -{ - NetPacketAskKickPlayerDeniedData *tmpData = (NetPacketAskKickPlayerDeniedData *)GetRawData(); - - outData.playerId = ntohl(tmpData->playerId); - switch (ntohs(tmpData->denyReason)) - { - case NET_ASK_KICK_DENIED_INVALID_STATE: - outData.denyReason = KICK_DENIED_INVALID_STATE; - break; - case NET_ASK_KICK_DENIED_TOO_FEW_PLAYERS: - outData.denyReason = KICK_DENIED_TOO_FEW_PLAYERS; - break; - case NET_ASK_KICK_DENIED_TEMPORARY: - outData.denyReason = KICK_DENIED_TEMPORARY; - break; - case NET_ASK_KICK_DENIED_OTHER_IN_PROGRESS: - outData.denyReason = KICK_DENIED_OTHER_IN_PROGRESS; - break; - case NET_ASK_KICK_DENIED_INVALID_PLAYER_ID: - outData.denyReason = KICK_DENIED_INVALID_PLAYER_ID; - break; - default: - outData.denyReason = KICK_DENIED_OTHER_REASON; - break; - } -} - -const NetPacketAskKickPlayerDenied * -NetPacketAskKickPlayerDenied::ToNetPacketAskKickPlayerDenied() const -{ - return this; -} - -void -NetPacketAskKickPlayerDenied::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketStartKickPlayerPetition::NetPacketStartKickPlayerPetition() -: NetPacket(NET_TYPE_START_KICK_PLAYER_PETITION, sizeof(NetPacketStartKickPlayerPetitionData), sizeof(NetPacketStartKickPlayerPetitionData)) -{ -} - -NetPacketStartKickPlayerPetition::~NetPacketStartKickPlayerPetition() -{ -} - -boost::shared_ptr -NetPacketStartKickPlayerPetition::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketStartKickPlayerPetition); - try + 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)) { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. + retVal = true; } - return newPacket; -} - -void -NetPacketStartKickPlayerPetition::SetData(const NetPacketStartKickPlayerPetition::Data &inData) -{ - NetPacketStartKickPlayerPetitionData *tmpData = (NetPacketStartKickPlayerPetitionData *)GetRawData(); - - tmpData->petitionId = htonl(inData.petitionId); - tmpData->proposingPlayerId = htonl(inData.proposingPlayerId); - tmpData->kickPlayerId = htonl(inData.kickPlayerId); - tmpData->kickTimeout = htons(inData.kickTimeoutSec); - tmpData->numVotesNeededToKick = htons(inData.numVotesNeededToKick); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketStartKickPlayerPetition::GetData(NetPacketStartKickPlayerPetition::Data &outData) const -{ - NetPacketStartKickPlayerPetitionData *tmpData = (NetPacketStartKickPlayerPetitionData *)GetRawData(); - - outData.petitionId = ntohl(tmpData->petitionId); - outData.proposingPlayerId = ntohl(tmpData->proposingPlayerId); - outData.kickPlayerId = ntohl(tmpData->kickPlayerId); - outData.kickTimeoutSec = ntohs(tmpData->kickTimeout); - outData.numVotesNeededToKick = ntohs(tmpData->numVotesNeededToKick); -} - -const NetPacketStartKickPlayerPetition * -NetPacketStartKickPlayerPetition::ToNetPacketStartKickPlayerPetition() const -{ - return this; + return retVal; } void -NetPacketStartKickPlayerPetition::InternalCheck(const NetPacketHeader *data) const -{ - NetPacketStartKickPlayerPetitionData *tmpData = (NetPacketStartKickPlayerPetitionData *)data; - - if (ntohs(tmpData->numVotesNeededToKick) > MAX_NUMBER_OF_PLAYERS - 1) - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); -} - -//----------------------------------------------------------------------------- - -NetPacketVoteKickPlayer::NetPacketVoteKickPlayer() -: NetPacket(NET_TYPE_VOTE_KICK_PLAYER, sizeof(NetPacketVoteKickPlayerData), sizeof(NetPacketVoteKickPlayerData)) +NetPacket::SetGameData(const GameData &inData, NetGameInfo_t *outData) { -} + assert(outData); -NetPacketVoteKickPlayer::~NetPacketVoteKickPlayer() -{ -} + int numManualBlinds = (int)inData.manualBlindsList.size(); -boost::shared_ptr -NetPacketVoteKickPlayer::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketVoteKickPlayer); - try + outData->maxNumPlayers = inData.maxNumberOfPlayers; + outData->raiseIntervalMode.present = static_cast(inData.raiseIntervalMode); + if (inData.raiseIntervalMode == RAISE_ON_HANDNUMBER) { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. + outData->raiseIntervalMode.present = raiseIntervalMode_PR_raiseEveryHands; + outData->raiseIntervalMode.choice.raiseEveryHands = inData.raiseSmallBlindEveryHandsValue; } - return newPacket; -} - -void -NetPacketVoteKickPlayer::SetData(const NetPacketVoteKickPlayer::Data &inData) -{ - NetPacketVoteKickPlayerData *tmpData = (NetPacketVoteKickPlayerData *)GetRawData(); - - tmpData->petitionId = htonl(inData.petitionId); - tmpData->vote = htons(static_cast(inData.vote)); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketVoteKickPlayer::GetData(NetPacketVoteKickPlayer::Data &outData) const -{ - NetPacketVoteKickPlayerData *tmpData = (NetPacketVoteKickPlayerData *)GetRawData(); - - outData.petitionId = ntohl(tmpData->petitionId); - if (ntohs(tmpData->vote) == NET_VOTE_KICK_IN_FAVOUR) - outData.vote = KICK_VOTE_IN_FAVOUR; else - outData.vote = KICK_VOTE_AGAINST; -} - -const NetPacketVoteKickPlayer * -NetPacketVoteKickPlayer::ToNetPacketVoteKickPlayer() const -{ - return this; -} - -void -NetPacketVoteKickPlayer::InternalCheck(const NetPacketHeader *data) const -{ - NetPacketVoteKickPlayerData *tmpData = (NetPacketVoteKickPlayerData *)data; - u_int16_t tmpVote = ntohs(tmpData->vote); - if (tmpVote != 0 && tmpVote != 1) - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); -} - -//----------------------------------------------------------------------------- - -NetPacketVoteKickPlayerAck::NetPacketVoteKickPlayerAck() -: NetPacket(NET_TYPE_VOTE_KICK_PLAYER_ACK, sizeof(NetPacketVoteKickPlayerAckData), sizeof(NetPacketVoteKickPlayerAckData)) -{ -} - -NetPacketVoteKickPlayerAck::~NetPacketVoteKickPlayerAck() -{ -} - -boost::shared_ptr -NetPacketVoteKickPlayerAck::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketVoteKickPlayerAck); - try { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. + outData->raiseIntervalMode.present = raiseIntervalMode_PR_raiseEveryMinutes; + outData->raiseIntervalMode.choice.raiseEveryMinutes = inData.raiseSmallBlindEveryMinutesValue; } - return newPacket; -} + outData->endRaiseMode = inData.afterManualBlindsMode; + outData->proposedGuiSpeed = inData.guiSpeed; + outData->playerActionTimeout = inData.playerActionTimeoutSec; + outData->endRaiseSmallBlindValue = inData.afterMBAlwaysRaiseValue; + outData->firstSmallBlind = inData.firstSmallBlind; + outData->startMoney = inData.startMoney; -void -NetPacketVoteKickPlayerAck::SetData(const NetPacketVoteKickPlayerAck::Data &inData) -{ - NetPacketVoteKickPlayerAckData *tmpData = (NetPacketVoteKickPlayerAckData *)GetRawData(); - - tmpData->petitionId = htonl(inData.petitionId); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketVoteKickPlayerAck::GetData(NetPacketVoteKickPlayerAck::Data &outData) const -{ - NetPacketVoteKickPlayerAckData *tmpData = (NetPacketVoteKickPlayerAckData *)GetRawData(); - - outData.petitionId = ntohl(tmpData->petitionId); -} - -const NetPacketVoteKickPlayerAck * -NetPacketVoteKickPlayerAck::ToNetPacketVoteKickPlayerAck() const -{ - return this; -} - -void -NetPacketVoteKickPlayerAck::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketVoteKickPlayerDenied::NetPacketVoteKickPlayerDenied() -: NetPacket(NET_TYPE_VOTE_KICK_PLAYER_DENIED, sizeof(NetPacketVoteKickPlayerDeniedData), sizeof(NetPacketVoteKickPlayerDeniedData)) -{ -} - -NetPacketVoteKickPlayerDenied::~NetPacketVoteKickPlayerDenied() -{ -} - -boost::shared_ptr -NetPacketVoteKickPlayerDenied::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketVoteKickPlayerDenied); - try + if (numManualBlinds) { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketVoteKickPlayerDenied::SetData(const NetPacketVoteKickPlayerDenied::Data &inData) -{ - NetPacketVoteKickPlayerDeniedData *tmpData = (NetPacketVoteKickPlayerDeniedData *)GetRawData(); - - tmpData->petitionId = htonl(inData.petitionId); - switch (inData.denyReason) - { - case VOTE_DENIED_INVALID_PETITION: - tmpData->denyReason = htons(NET_VOTE_KICK_DENIED_INVALID_PETITION); - break; - case VOTE_DENIED_ALREADY_VOTED: - tmpData->denyReason = htons(NET_VOTE_KICK_DENIED_ALREADY_VOTED); - break; - case VOTE_DENIED_IMPOSSIBLE: - tmpData->denyReason = htons(NET_VOTE_KICK_DENIED_IMPOSSIBLE); - break; - default: - tmpData->denyReason = htons(NET_VOTE_KICK_DENIED_OTHER_REASON); - break; - } - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketVoteKickPlayerDenied::GetData(NetPacketVoteKickPlayerDenied::Data &outData) const -{ - NetPacketVoteKickPlayerDeniedData *tmpData = (NetPacketVoteKickPlayerDeniedData *)GetRawData(); - - outData.petitionId = ntohl(tmpData->petitionId); - switch (ntohs(tmpData->denyReason)) - { - case NET_VOTE_KICK_DENIED_INVALID_PETITION: - outData.denyReason = VOTE_DENIED_INVALID_PETITION; - break; - case NET_VOTE_KICK_DENIED_ALREADY_VOTED: - outData.denyReason = VOTE_DENIED_ALREADY_VOTED; - break; - case NET_VOTE_KICK_DENIED_IMPOSSIBLE: - outData.denyReason = VOTE_DENIED_IMPOSSIBLE; - break; - default: - outData.denyReason = VOTE_DENIED_OTHER_REASON; - break; - } -} - -const NetPacketVoteKickPlayerDenied * -NetPacketVoteKickPlayerDenied::ToNetPacketVoteKickPlayerDenied() const -{ - return this; -} - -void -NetPacketVoteKickPlayerDenied::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketKickPlayerPetitionUpdate::NetPacketKickPlayerPetitionUpdate() -: NetPacket(NET_TYPE_KICK_PLAYER_PETITION_UPDATE, sizeof(NetPacketKickPlayerPetitionUpdateData), sizeof(NetPacketKickPlayerPetitionUpdateData)) -{ -} - -NetPacketKickPlayerPetitionUpdate::~NetPacketKickPlayerPetitionUpdate() -{ -} - -boost::shared_ptr -NetPacketKickPlayerPetitionUpdate::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketKickPlayerPetitionUpdate); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketKickPlayerPetitionUpdate::SetData(const NetPacketKickPlayerPetitionUpdate::Data &inData) -{ - NetPacketKickPlayerPetitionUpdateData *tmpData = (NetPacketKickPlayerPetitionUpdateData *)GetRawData(); - - tmpData->petitionId = htonl(inData.petitionId); - tmpData->numVotesAgainstKicking = htons(inData.numVotesAgainstKicking); - tmpData->numVotesInFavourOfKicking = htons(inData.numVotesInFavourOfKicking); - tmpData->numVotesNeededToKick = htons(inData.numVotesNeededToKick); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketKickPlayerPetitionUpdate::GetData(NetPacketKickPlayerPetitionUpdate::Data &outData) const -{ - NetPacketKickPlayerPetitionUpdateData *tmpData = (NetPacketKickPlayerPetitionUpdateData *)GetRawData(); - - outData.petitionId = ntohl(tmpData->petitionId); - outData.numVotesAgainstKicking = ntohs(tmpData->numVotesAgainstKicking); - outData.numVotesInFavourOfKicking = ntohs(tmpData->numVotesInFavourOfKicking); - outData.numVotesNeededToKick = ntohs(tmpData->numVotesNeededToKick); -} - -const NetPacketKickPlayerPetitionUpdate * -NetPacketKickPlayerPetitionUpdate::ToNetPacketKickPlayerPetitionUpdate() const -{ - return this; -} - -void -NetPacketKickPlayerPetitionUpdate::InternalCheck(const NetPacketHeader *data) const -{ - NetPacketKickPlayerPetitionUpdateData *tmpData = (NetPacketKickPlayerPetitionUpdateData *)data; - - if (ntohs(tmpData->numVotesAgainstKicking) > MAX_NUMBER_OF_PLAYERS) - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - if (ntohs(tmpData->numVotesInFavourOfKicking) > MAX_NUMBER_OF_PLAYERS) - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - if (ntohs(tmpData->numVotesNeededToKick) > MAX_NUMBER_OF_PLAYERS) - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); -} - -//----------------------------------------------------------------------------- - -NetPacketEndKickPlayerPetition::NetPacketEndKickPlayerPetition() -: NetPacket(NET_TYPE_END_KICK_PLAYER_PETITION, sizeof(NetPacketEndKickPlayerPetitionData), sizeof(NetPacketEndKickPlayerPetitionData)) -{ -} - -NetPacketEndKickPlayerPetition::~NetPacketEndKickPlayerPetition() -{ -} - -boost::shared_ptr -NetPacketEndKickPlayerPetition::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketEndKickPlayerPetition); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketEndKickPlayerPetition::SetData(const NetPacketEndKickPlayerPetition::Data &inData) -{ - NetPacketEndKickPlayerPetitionData *tmpData = (NetPacketEndKickPlayerPetitionData *)GetRawData(); - - tmpData->petitionId = htonl(inData.petitionId); - tmpData->numVotesAgainstKicking = htons(inData.numVotesAgainstKicking); - tmpData->numVotesInFavourOfKicking = htons(inData.numVotesInFavourOfKicking); - tmpData->voteResult = htons(inData.playerKicked ? 1 : 0); - - switch (inData.endReason) - { - case PETITION_END_ENOUGH_VOTES: - tmpData->endReason = htons(NET_PETITION_END_ENOUGH_VOTES); - break; - case PETITION_END_NOT_ENOUGH_PLAYERS: - tmpData->endReason = htons(NET_PETITION_END_NOT_ENOUGH_PLAYERS); - break; - case PETITION_END_PLAYER_LEFT: - tmpData->endReason = htons(NET_PETITION_END_PLAYER_LEFT); - break; - case PETITION_END_TIMEOUT: - tmpData->endReason = htons(NET_PETITION_END_TIMEOUT); - break; - default: - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketEndKickPlayerPetition::GetData(NetPacketEndKickPlayerPetition::Data &outData) const -{ - NetPacketEndKickPlayerPetitionData *tmpData = (NetPacketEndKickPlayerPetitionData *)GetRawData(); - - outData.petitionId = ntohl(tmpData->petitionId); - outData.numVotesAgainstKicking = ntohs(tmpData->numVotesAgainstKicking); - outData.numVotesInFavourOfKicking = ntohs(tmpData->numVotesInFavourOfKicking); - outData.playerKicked = ntohs(tmpData->voteResult) == 1; - - switch (ntohs(tmpData->endReason)) - { - case NET_PETITION_END_ENOUGH_VOTES: - outData.endReason = PETITION_END_ENOUGH_VOTES; - break; - case NET_PETITION_END_NOT_ENOUGH_PLAYERS: - outData.endReason = PETITION_END_NOT_ENOUGH_PLAYERS; - break; - case NET_PETITION_END_PLAYER_LEFT: - outData.endReason = PETITION_END_PLAYER_LEFT; - break; - case NET_PETITION_END_TIMEOUT: - outData.endReason = PETITION_END_TIMEOUT; - break; - default: - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } -} - -const NetPacketEndKickPlayerPetition * -NetPacketEndKickPlayerPetition::ToNetPacketEndKickPlayerPetition() const -{ - return this; -} - -void -NetPacketEndKickPlayerPetition::InternalCheck(const NetPacketHeader *data) const -{ - NetPacketEndKickPlayerPetitionData *tmpData = (NetPacketEndKickPlayerPetitionData *)data; - - if (ntohs(tmpData->numVotesAgainstKicking) > MAX_NUMBER_OF_PLAYERS) - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - if (ntohs(tmpData->numVotesInFavourOfKicking) > MAX_NUMBER_OF_PLAYERS) - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - u_int16_t tmpVoteResult = ntohs(tmpData->voteResult); - if (tmpVoteResult != 0 && tmpVoteResult != 1) - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); -} - -//----------------------------------------------------------------------------- - -NetPacketStatisticsChanged::NetPacketStatisticsChanged() -: NetPacket(NET_TYPE_STATISTICS_CHANGED, sizeof(NetPacketStatisticsChangedData), MAX_PACKET_SIZE) -{ -} - -NetPacketStatisticsChanged::~NetPacketStatisticsChanged() -{ -} - -boost::shared_ptr -NetPacketStatisticsChanged::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketStatisticsChanged); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketStatisticsChanged::SetData(const NetPacketStatisticsChanged::Data &inData) -{ - // Only send number of players. -// u_int16_t numValues = 0; - u_int16_t numValues = 1; -/* if (inData.stats.numberOfPlayersOnServer) - ++numValues; - if (inData.stats.totalPlayersEverLoggedIn) - ++numValues; - if (inData.stats.totalGamesEverCreated) - ++numValues;*/ - - // Resize the packet so that the data fits in. - Resize((u_int16_t) - (sizeof(NetPacketStatisticsChangedData) + numValues * sizeof(StatisticsData))); - - NetPacketStatisticsChangedData *tmpData = (NetPacketStatisticsChangedData *)GetRawData(); - - tmpData->numberOfStatistics = htons(numValues); - - StatisticsData *curStatisticsData = - (StatisticsData *)((char *)tmpData + sizeof(NetPacketStatisticsChangedData)); - -// if (inData.stats.numberOfPlayersOnServer) -// { - curStatisticsData->statisticsType = htonl(NET_STAT_CUR_PLAYERS_ON_SERVER); - curStatisticsData->statisticsValue = htonl(inData.stats.numberOfPlayersOnServer); - ++curStatisticsData; -// } -/* if (inData.stats.totalPlayersEverLoggedIn) - { - curStatisticsData->statisticsType = htonl(NET_STAT_TOTAL_PLAYERS_EVER_ON_SERVER); - curStatisticsData->statisticsValue = htonl(inData.stats.totalPlayersEverLoggedIn); - ++curStatisticsData; - } - if (inData.stats.totalGamesEverCreated) - { - curStatisticsData->statisticsType = htonl(NET_STAT_TOTAL_GAMES_EVER_ON_SERVER); - curStatisticsData->statisticsValue = htonl(inData.stats.totalGamesEverCreated); - }*/ - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketStatisticsChanged::GetData(NetPacketStatisticsChanged::Data &outData) const -{ - NetPacketStatisticsChangedData *tmpData = (NetPacketStatisticsChangedData *)GetRawData(); - - u_int16_t numStatistics = ntohs(tmpData->numberOfStatistics); - StatisticsData *curStatisticsData = - (StatisticsData *)((char *)tmpData + sizeof(NetPacketStatisticsChangedData)); - - for (int i = 0; i < numStatistics; i++) - { - switch(ntohl(curStatisticsData->statisticsType)) + list::const_iterator i = inData.manualBlindsList.begin(); + list::const_iterator end = inData.manualBlindsList.end(); + while (i != end) { - case NET_STAT_CUR_PLAYERS_ON_SERVER: - outData.stats.numberOfPlayersOnServer = ntohl(curStatisticsData->statisticsValue); - break; - case NET_STAT_TOTAL_PLAYERS_EVER_ON_SERVER: - outData.stats.totalPlayersEverLoggedIn = ntohl(curStatisticsData->statisticsValue); - break; - case NET_STAT_TOTAL_GAMES_EVER_ON_SERVER: - outData.stats.totalGamesEverCreated = ntohl(curStatisticsData->statisticsValue); - break; + long *manualBlind = (long *)calloc(1, sizeof(long)); + *manualBlind = *i; + ASN_SEQUENCE_ADD(&outData->manualBlinds.list, manualBlind); + ++i; } } } -const NetPacketStatisticsChanged * -NetPacketStatisticsChanged::ToNetPacketStatisticsChanged() const -{ - return this; -} - void -NetPacketStatisticsChanged::InternalCheck(const NetPacketHeader *data) const +NetPacket::GetGameData(const NetGameInfo_t *inData, GameData &outData) { - u_int16_t dataLen = ntohs(data->length); - NetPacketStatisticsChangedData *tmpData = (NetPacketStatisticsChangedData *)data; - int numStatistics = ntohs(tmpData->numberOfStatistics); + assert(inData); - if (dataLen != - sizeof(NetPacketStatisticsChangedData) - + numStatistics * sizeof(StatisticsData)) + int numManualBlinds = (int)inData->manualBlinds.list.size; + + outData.maxNumberOfPlayers = inData->maxNumPlayers; + outData.raiseIntervalMode = static_cast(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(inData->endRaiseMode); + outData.guiSpeed = inData->proposedGuiSpeed; + outData.playerActionTimeoutSec = inData->playerActionTimeout; + outData.firstSmallBlind = inData->firstSmallBlind; + outData.afterMBAlwaysRaiseValue = inData->endRaiseSmallBlindValue; + outData.startMoney = inData->startMoney; + + if (numManualBlinds) { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); + long **manualBlindsPtr = inData->manualBlinds.list.array; + int counter = 0; + for (int i = 0; i < numManualBlinds; i++) + { + outData.manualBlindsList.push_back((int)*manualBlindsPtr[i]); + counter++; + } } - if (!numStatistics) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Semantic checks not needed, this packet is sent by the server. } -//----------------------------------------------------------------------------- - -NetPacketRemovedFromGame::NetPacketRemovedFromGame() -: NetPacket(NET_TYPE_REMOVED_FROM_GAME, sizeof(NetPacketRemovedFromGameData), sizeof(NetPacketRemovedFromGameData)) +int +NetPacket::NetErrorToGameError(long netErrorReason) { -} - -NetPacketRemovedFromGame::~NetPacketRemovedFromGame() -{ -} - -boost::shared_ptr -NetPacketRemovedFromGame::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketRemovedFromGame); - try + int retVal; + switch(netErrorReason) { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketRemovedFromGame::SetData(const NetPacketRemovedFromGame::Data &inData) -{ - NetPacketRemovedFromGameData *tmpData = (NetPacketRemovedFromGameData *)GetRawData(); - - switch (inData.removeReason) - { - case NTF_NET_REMOVED_ON_REQUEST : - tmpData->removeReason = htons(NET_REMOVED_ON_REQUEST); + case errorReason_errorInitVersionNotSupported : + retVal = ERR_NET_VERSION_NOT_SUPPORTED; break; - case NTF_NET_REMOVED_GAME_FULL : - tmpData->removeReason = htons(NET_REMOVED_GAME_FULL); + case errorReason_errorInitServerFull : + retVal = ERR_NET_SERVER_FULL; break; - case NTF_NET_REMOVED_ALREADY_RUNNING : - tmpData->removeReason = htons(NET_REMOVED_GAME_ALREADY_RUNNING); + case errorReason_errorInitAuthFailure : + retVal = ERR_NET_INVALID_PASSWORD; break; - case NTF_NET_REMOVED_KICKED : - tmpData->removeReason = htons(NET_REMOVED_KICKED); + case errorReason_errorInitPlayerNameInUse : + retVal = ERR_NET_PLAYER_NAME_IN_USE; break; - case NTF_NET_REMOVED_TIMEOUT : - tmpData->removeReason = htons(NET_REMOVED_TIMEOUT); + case errorReason_errorInitInvalidPlayerName : + retVal = ERR_NET_INVALID_PLAYER_NAME; break; - case NTF_NET_REMOVED_START_FAILED : - tmpData->removeReason = htons(NET_REMOVED_START_FAILED); + case errorReason_errorInitServerMaintenance : + retVal = ERR_NET_SERVER_MAINTENANCE; + break; + case errorReason_errorAvatarTooLarge : + retVal = ERR_NET_AVATAR_TOO_LARGE; + break; + case errorReason_errorAvatarUploadBlocked : + retVal = ERR_NET_AVATAR_UPLOAD_BLOCKED; + 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; break; default : - tmpData->removeReason = htons(NET_REMOVED_OTHER_REASON); + retVal = ERR_SOCK_INTERNAL; break; } - - // Check the packet - just in case. - Check(GetRawData()); + return retVal; } -void -NetPacketRemovedFromGame::GetData(NetPacketRemovedFromGame::Data &outData) const +long +NetPacket::GameErrorToNetError(int gameErrorReason) { - NetPacketRemovedFromGameData *tmpData = (NetPacketRemovedFromGameData *)GetRawData(); - - switch (ntohs(tmpData->removeReason)) + int retVal; + switch(gameErrorReason) { - // Removed Errors. - case NET_REMOVED_ON_REQUEST : - outData.removeReason = NTF_NET_REMOVED_ON_REQUEST; - break; - case NET_REMOVED_GAME_FULL : - outData.removeReason = NTF_NET_REMOVED_GAME_FULL; - break; - case NET_REMOVED_GAME_ALREADY_RUNNING : - outData.removeReason = NTF_NET_REMOVED_ALREADY_RUNNING; - break; - case NET_REMOVED_KICKED : - outData.removeReason = NTF_NET_REMOVED_KICKED; - break; - case NET_REMOVED_TIMEOUT : - outData.removeReason = NTF_NET_REMOVED_TIMEOUT; - break; - case NET_REMOVED_START_FAILED : - outData.removeReason = NTF_NET_REMOVED_START_FAILED; - break; - default : - outData.removeReason = NTF_NET_INTERNAL; - break; - } -} - -const NetPacketRemovedFromGame * -NetPacketRemovedFromGame::ToNetPacketRemovedFromGame() const -{ - return this; -} - -void -NetPacketRemovedFromGame::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketTimeoutWarning::NetPacketTimeoutWarning() -: NetPacket(NET_TYPE_TIMEOUT_WARNING, sizeof(NetPacketTimeoutWarningData), sizeof(NetPacketTimeoutWarningData)) -{ -} - -NetPacketTimeoutWarning::~NetPacketTimeoutWarning() -{ -} - -boost::shared_ptr -NetPacketTimeoutWarning::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketTimeoutWarning); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketTimeoutWarning::SetData(const NetPacketTimeoutWarning::Data &inData) -{ - NetPacketTimeoutWarningData *tmpData = (NetPacketTimeoutWarningData *)GetRawData(); - - switch (inData.timeoutReason) - { - case NETWORK_TIMEOUT_GENERIC: - tmpData->timeoutReason = htons(NET_TIMEOUT_NO_DATA_RECEIVED); - break; - case NETWORK_TIMEOUT_GAME_ADMIN_IDLE: - tmpData->timeoutReason = htons(NET_TIMEOUT_INACTIVE_GAME); - break; - default : - tmpData->timeoutReason = htons(NET_TIMEOUT_OTHER_REASON); - break; - } - tmpData->remainingSeconds = htons(inData.remainingSeconds); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketTimeoutWarning::GetData(NetPacketTimeoutWarning::Data &outData) const -{ - NetPacketTimeoutWarningData *tmpData = (NetPacketTimeoutWarningData *)GetRawData(); - - switch (ntohs(tmpData->timeoutReason)) - { - // Removed Errors. - case NET_TIMEOUT_INACTIVE_GAME : - outData.timeoutReason = NETWORK_TIMEOUT_GAME_ADMIN_IDLE; - break; - default : - outData.timeoutReason = NETWORK_TIMEOUT_GENERIC; - break; - } - outData.remainingSeconds = ntohs(tmpData->remainingSeconds); -} - -const NetPacketTimeoutWarning * -NetPacketTimeoutWarning::ToNetPacketTimeoutWarning() const -{ - return this; -} - -void -NetPacketTimeoutWarning::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketResetTimeout::NetPacketResetTimeout() -: NetPacket(NET_TYPE_RESET_TIMEOUT, sizeof(NetPacketResetTimeoutData), sizeof(NetPacketResetTimeoutData)) -{ -} - -NetPacketResetTimeout::~NetPacketResetTimeout() -{ -} - -boost::shared_ptr -NetPacketResetTimeout::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketResetTimeout); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -const NetPacketResetTimeout * -NetPacketResetTimeout::ToNetPacketResetTimeout() const -{ - return this; -} - -void -NetPacketResetTimeout::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - -NetPacketSendChatText::NetPacketSendChatText() -: NetPacket(NET_TYPE_SEND_CHAT_TEXT, sizeof(NetPacketSendChatTextData), MAX_PACKET_SIZE) -{ -} - -NetPacketSendChatText::~NetPacketSendChatText() -{ -} - -boost::shared_ptr -NetPacketSendChatText::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketSendChatText); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketSendChatText::SetData(const NetPacketSendChatText::Data &inData) -{ - u_int16_t textLen = (u_int16_t)inData.text.length(); - - if (!textLen || textLen > MAX_CHAT_TEXT_SIZE) - throw NetException(__FILE__, __LINE__, ERR_NET_INVALID_CHAT_TEXT, 0); - - // Resize the packet so that the data fits in. - Resize((u_int16_t) - (sizeof(NetPacketSendChatTextData) + ADD_PADDING(textLen))); - - NetPacketSendChatTextData *tmpData = (NetPacketSendChatTextData *)GetRawData(); - - // Set the data. - tmpData->textLength = htons(textLen); - char *textPtr = (char *)tmpData + sizeof(NetPacketSendChatTextData); - memcpy(textPtr, inData.text.c_str(), textLen); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketSendChatText::GetData(NetPacketSendChatText::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketSendChatTextData *tmpData = (NetPacketSendChatTextData *)GetRawData(); - - char *textPtr = (char *)tmpData + sizeof(NetPacketSendChatTextData); - outData.text = string(textPtr, ntohs(tmpData->textLength)); -} - -const NetPacketSendChatText * -NetPacketSendChatText::ToNetPacketSendChatText() const -{ - return this; -} - -void -NetPacketSendChatText::InternalCheck(const NetPacketHeader* data) const -{ - u_int16_t dataLen = ntohs(data->length); - NetPacketSendChatTextData *tmpData = (NetPacketSendChatTextData *)data; - int textLength = ntohs(tmpData->textLength); - // Check exact packet length. - if (dataLen != - sizeof(NetPacketSendChatTextData) - + ADD_PADDING(textLength)) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check string size. - if (!textLength - || textLength > MAX_CHAT_TEXT_SIZE) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check text string. - char *textPtr = (char *)tmpData + sizeof(NetPacketSendChatTextData); - if (textPtr[0] == 0) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } -} - -//----------------------------------------------------------------------------- - -NetPacketChatText::NetPacketChatText() -: NetPacket(NET_TYPE_CHAT_TEXT, sizeof(NetPacketChatTextData), MAX_PACKET_SIZE) -{ -} - -NetPacketChatText::~NetPacketChatText() -{ -} - -boost::shared_ptr -NetPacketChatText::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketChatText); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketChatText::SetData(const NetPacketChatText::Data &inData) -{ - u_int16_t textLen = (u_int16_t)inData.text.length(); - - if (!textLen || textLen > MAX_CHAT_TEXT_SIZE) - throw NetException(__FILE__, __LINE__, ERR_NET_INVALID_CHAT_TEXT, 0); - - // Resize the packet so that the data fits in. - Resize((u_int16_t) - (sizeof(NetPacketChatTextData) + ADD_PADDING(textLen))); - - NetPacketChatTextData *tmpData = (NetPacketChatTextData *)GetRawData(); - - // Set the data. - tmpData->playerId = htonl(inData.playerId); - tmpData->textLength = htons(textLen); - char *textPtr = (char *)tmpData + sizeof(NetPacketChatTextData); - memcpy(textPtr, inData.text.c_str(), textLen); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketChatText::GetData(NetPacketChatText::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketChatTextData *tmpData = (NetPacketChatTextData *)GetRawData(); - - outData.playerId = ntohl(tmpData->playerId); - char *textPtr = (char *)tmpData + sizeof(NetPacketChatTextData); - outData.text = string(textPtr, ntohs(tmpData->textLength)); -} - -const NetPacketChatText * -NetPacketChatText::ToNetPacketChatText() const -{ - return this; -} - -void -NetPacketChatText::InternalCheck(const NetPacketHeader* data) const -{ - u_int16_t dataLen = ntohs(data->length); - NetPacketChatTextData *tmpData = (NetPacketChatTextData *)data; - int textLength = ntohs(tmpData->textLength); - // Check exact packet length. - if (dataLen != - sizeof(NetPacketChatTextData) - + ADD_PADDING(textLength)) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check string size. - if (!textLength - || textLength > MAX_CHAT_TEXT_SIZE) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // No more checks required - this packet is sent by the server. -} - -//----------------------------------------------------------------------------- - -NetPacketMsgBoxText::NetPacketMsgBoxText() -: NetPacket(NET_TYPE_MSG_BOX_TEXT, sizeof(NetPacketMsgBoxTextData), MAX_PACKET_SIZE) -{ -} - -NetPacketMsgBoxText::~NetPacketMsgBoxText() -{ -} - -boost::shared_ptr -NetPacketMsgBoxText::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketMsgBoxText); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketMsgBoxText::SetData(const NetPacketMsgBoxText::Data &inData) -{ - u_int16_t textLen = (u_int16_t)inData.text.length(); - - if (!textLen || textLen > MAX_CHAT_TEXT_SIZE) - throw NetException(__FILE__, __LINE__, ERR_NET_INVALID_CHAT_TEXT, 0); - - // Resize the packet so that the data fits in. - Resize((u_int16_t) - (sizeof(NetPacketMsgBoxTextData) + ADD_PADDING(textLen))); - - NetPacketMsgBoxTextData *tmpData = (NetPacketMsgBoxTextData *)GetRawData(); - - // Set the data. - tmpData->textLength = htons(textLen); - char *textPtr = (char *)tmpData + sizeof(NetPacketMsgBoxTextData); - memcpy(textPtr, inData.text.c_str(), textLen); - - // Check the packet - just in case. - Check(GetRawData()); -} - -void -NetPacketMsgBoxText::GetData(NetPacketMsgBoxText::Data &outData) const -{ - // We assume that the data is valid. Validity has already been checked. - NetPacketMsgBoxTextData *tmpData = (NetPacketMsgBoxTextData *)GetRawData(); - - char *textPtr = (char *)tmpData + sizeof(NetPacketMsgBoxTextData); - outData.text = string(textPtr, ntohs(tmpData->textLength)); -} - -const NetPacketMsgBoxText * -NetPacketMsgBoxText::ToNetPacketMsgBoxText() const -{ - return this; -} - -void -NetPacketMsgBoxText::InternalCheck(const NetPacketHeader* data) const -{ - u_int16_t dataLen = ntohs(data->length); - NetPacketMsgBoxTextData *tmpData = (NetPacketMsgBoxTextData *)data; - int textLength = ntohs(tmpData->textLength); - // Check exact packet length. - if (dataLen != - sizeof(NetPacketMsgBoxTextData) - + ADD_PADDING(textLength)) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // Check string size. - if (!textLength - || textLength > MAX_CHAT_TEXT_SIZE) - { - throw NetException(__FILE__, __LINE__, ERR_SOCK_INVALID_PACKET, 0); - } - // No more checks required - this packet is sent by the server. -} - -//----------------------------------------------------------------------------- - -NetPacketError::NetPacketError() -: NetPacket(NET_TYPE_ERROR, sizeof(NetPacketErrorData), sizeof(NetPacketErrorData)) -{ -} - -NetPacketError::~NetPacketError() -{ -} - -boost::shared_ptr -NetPacketError::Clone() const -{ - boost::shared_ptr newPacket(new NetPacketError); - try - { - newPacket->SetRawData(GetRawData()); - } catch (const NetException &) - { - // Need to return the new packet anyway. - } - return newPacket; -} - -void -NetPacketError::SetData(const NetPacketError::Data &inData) -{ - NetPacketErrorData *tmpData = (NetPacketErrorData *)GetRawData(); - - switch (inData.errorCode) - { - // Init Errors. case ERR_NET_VERSION_NOT_SUPPORTED : - tmpData->errorReason = htons(NET_ERR_INIT_VERSION_NOT_SUPPORTED); + retVal = errorReason_errorInitVersionNotSupported; break; case ERR_NET_SERVER_FULL : - tmpData->errorReason = htons(NET_ERR_INIT_SERVER_FULL); + retVal = errorReason_errorInitServerFull; break; case ERR_NET_INVALID_PASSWORD : - tmpData->errorReason = htons(NET_ERR_INIT_INVALID_PASSWORD); + retVal = errorReason_errorInitAuthFailure; break; case ERR_NET_PLAYER_NAME_IN_USE : - tmpData->errorReason = htons(NET_ERR_INIT_PLAYER_NAME_IN_USE); + retVal = errorReason_errorInitPlayerNameInUse; break; case ERR_NET_INVALID_PLAYER_NAME : - tmpData->errorReason = htons(NET_ERR_INIT_INVALID_PLAYER_NAME); + retVal = errorReason_errorInitInvalidPlayerName; break; case ERR_NET_SERVER_MAINTENANCE : - tmpData->errorReason = htons(NET_ERR_INIT_SERVER_MAINTENANCE); + retVal = errorReason_errorInitServerMaintenance; break; case ERR_NET_AVATAR_TOO_LARGE : - tmpData->errorReason = htons(NET_ERR_AVATAR_TOO_LARGE); - break; - case ERR_NET_WRONG_AVATAR_SIZE : - tmpData->errorReason = htons(NET_ERR_AVATAR_WRONG_SIZE); + retVal = errorReason_errorAvatarTooLarge; break; case ERR_NET_AVATAR_UPLOAD_BLOCKED : - tmpData->errorReason = htons(NET_ERR_AVATAR_UPLOAD_BLOCKED); + retVal = errorReason_errorAvatarUploadBlocked; break; - case ERR_NET_UNKNOWN_GAME : - tmpData->errorReason = htons(NET_ERR_JOIN_GAME_UNKNOWN_GAME); - break; - - // General Errors. case ERR_SOCK_INVALID_PACKET : - tmpData->errorReason = htons(NET_ERR_GENERAL_INVALID_PACKET); + retVal = errorReason_errorInvalidPacket; break; case ERR_SOCK_INVALID_STATE : - tmpData->errorReason = htons(NET_ERR_GENERAL_INVALID_STATE); + retVal = errorReason_errorInvalidState; break; case ERR_NET_PLAYER_KICKED : - tmpData->errorReason = htons(NET_ERR_GENERAL_PLAYER_KICKED); + retVal = errorReason_errorKickedFromServer; break; case ERR_NET_PLAYER_BANNED : - tmpData->errorReason = htons(NET_ERR_GENERAL_PLAYER_BANNED); + retVal = errorReason_errorBannedFromServer; break; case ERR_NET_SESSION_TIMED_OUT : - tmpData->errorReason = htons(NET_ERR_GENERAL_SESSION_TIMED_OUT); + retVal = errorReason_errorSessionTimeout; break; default : - tmpData->errorReason = htons(NET_ERR_OTHER); + retVal = errorReason_errorReserved; break; } - - // Check the packet - just in case. - Check(GetRawData()); + return retVal; } -void -NetPacketError::GetData(NetPacketError::Data &outData) const -{ - NetPacketErrorData *tmpData = (NetPacketErrorData *)GetRawData(); - - switch (ntohs(tmpData->errorReason)) - { - // Init Errors. - case NET_ERR_INIT_VERSION_NOT_SUPPORTED : - outData.errorCode = ERR_NET_VERSION_NOT_SUPPORTED; - break; - case NET_ERR_INIT_SERVER_FULL : - outData.errorCode = ERR_NET_SERVER_FULL; - break; - case NET_ERR_INIT_INVALID_PASSWORD : - outData.errorCode = ERR_NET_INVALID_PASSWORD; - break; - case NET_ERR_INIT_PLAYER_NAME_IN_USE : - outData.errorCode = ERR_NET_PLAYER_NAME_IN_USE; - break; - case NET_ERR_INIT_INVALID_PLAYER_NAME : - outData.errorCode = ERR_NET_INVALID_PLAYER_NAME; - break; - case NET_ERR_INIT_SERVER_MAINTENANCE : - outData.errorCode = ERR_NET_SERVER_MAINTENANCE; - break; - case NET_ERR_AVATAR_TOO_LARGE : - outData.errorCode = ERR_NET_AVATAR_TOO_LARGE; - break; - case NET_ERR_AVATAR_WRONG_SIZE : - outData.errorCode = ERR_NET_WRONG_AVATAR_SIZE; - break; - case NET_ERR_AVATAR_UPLOAD_BLOCKED : - outData.errorCode = ERR_NET_AVATAR_UPLOAD_BLOCKED; - break; - case NET_ERR_JOIN_GAME_UNKNOWN_GAME : - outData.errorCode = ERR_NET_UNKNOWN_GAME; - break; - - // General Errors. - case NET_ERR_GENERAL_INVALID_PACKET : - outData.errorCode = ERR_SOCK_INVALID_PACKET; - break; - case NET_ERR_GENERAL_INVALID_STATE : - outData.errorCode = ERR_SOCK_INVALID_STATE; - break; - case NET_ERR_GENERAL_PLAYER_KICKED : - outData.errorCode = ERR_NET_PLAYER_KICKED; - break; - case NET_ERR_GENERAL_PLAYER_BANNED : - outData.errorCode = ERR_NET_PLAYER_BANNED; - break; - case NET_ERR_GENERAL_SESSION_TIMED_OUT : - outData.errorCode = ERR_NET_SESSION_TIMED_OUT; - break; - default : - outData.errorCode = ERR_SOCK_INTERNAL; - break; - } -} - -const NetPacketError * -NetPacketError::ToNetPacketError() const -{ - return this; -} - -void -NetPacketError::InternalCheck(const NetPacketHeader*) const -{ - // Nothing to do. -} - -//----------------------------------------------------------------------------- - diff --git a/src/net/common/receiverhelper.cpp b/src/net/common/receiverhelper.cpp index b17d7842..3d257f86 100644 --- a/src/net/common/receiverhelper.cpp +++ b/src/net/common/receiverhelper.cpp @@ -47,7 +47,7 @@ ReceiverHelper::ScanPackets(ReceiveBuffer &buf) // This is necessary, because we use TCP. // Packets may be received in multiple chunks or // several packets may be received at once. - if (buf.recvBufUsed >= MIN_PACKET_SIZE) + if (buf.recvBufUsed) { try { diff --git a/src/net/common/senderhelper.cpp b/src/net/common/senderhelper.cpp index 82785b13..8a2226d2 100644 --- a/src/net/common/senderhelper.cpp +++ b/src/net/common/senderhelper.cpp @@ -17,8 +17,9 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include #include -#include \ +#include #include #include @@ -37,8 +38,37 @@ using boost::asio::ip::tcp; #define SEND_QUEUE_SIZE 10000000 #define SEND_LOG_INTERVAL_SEC 60 +class EncodedPacket +{ +public: + EncodedPacket(const unsigned char *data, unsigned size) + : m_size(size) + { + m_data = new unsigned char[size]; + memcpy(m_data, data, size); + } -typedef std::list > SendDataList; + ~EncodedPacket() + { + delete[] m_data; + } + + unsigned GetSize() const + { + return m_size; + } + + const unsigned char *GetData() const + { + return m_data; + } + +private: + unsigned m_size; + unsigned char *m_data; +}; + +typedef std::list > SendDataList; class SendDataManager : public boost::enable_shared_from_this { @@ -87,7 +117,7 @@ SendDataManager::AsyncSendNextPacket(boost::shared_ptrGetLen(); + bufSize += (*i)->GetSize(); ++i; } if (bufSize) @@ -97,8 +127,8 @@ SendDataManager::AsyncSendNextPacket(boost::shared_ptrGetRawData(), (*i)->GetLen()); - bufPos += (*i)->GetLen(); + memcpy(sendBuf + bufPos, (*i)->GetData(), (*i)->GetSize()); + bufPos += (*i)->GetSize(); ++i; } list.clear(); @@ -144,7 +174,7 @@ SenderHelper::Send(boost::shared_ptr session, boost::shared_ptrdataMutex); if (tmpManager->list.size() < SEND_QUEUE_SIZE) { - tmpManager->list.push_back(packet); + InternalStorePacket(*tmpManager, packet); } } { @@ -178,7 +208,7 @@ SenderHelper::Send(boost::shared_ptr session, const NetPacketList & while (i != end) { if (*i) - tmpManager->list.push_back(*i); + InternalStorePacket(*tmpManager, *i); ++i; } } @@ -200,3 +230,15 @@ SenderHelper::SignalSessionTerminated(unsigned sessionId) m_sendQueueMap.erase(pos); } +void +SenderHelper::InternalStorePacket(SendDataManager &tmpManager, boost::shared_ptr packet) +{ + unsigned char buf[MAX_PACKET_SIZE]; + asn_enc_rval_t e; + e = der_encode_to_buffer(&asn_DEF_PokerTHMessage, packet->GetMsg(), buf, MAX_PACKET_SIZE); + if (e.encoded == -1) + LOG_ERROR("Failed to encode NetPacket: " << packet->GetMsg()->present); + else + tmpManager.list.push_back(boost::shared_ptr(new EncodedPacket(buf, e.encoded))); +} + diff --git a/src/net/common/servergame.cpp b/src/net/common/servergame.cpp index 23cee38d..c500b138 100644 --- a/src/net/common/servergame.cpp +++ b/src/net/common/servergame.cpp @@ -17,6 +17,9 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include +#include + #include #include #include @@ -29,8 +32,6 @@ #include #include -#include - #define SERVER_CHECK_VOTE_KICK_INTERVAL_MSEC 500 #define SERVER_KICK_TIMEOUT_ADD_DELAY_SEC 2 @@ -181,16 +182,15 @@ ServerGame::TimerVoteKick(const boost::system::error_code &ec) } if (abortPetition) { - boost::shared_ptr endPetition(new NetPacketEndKickPlayerPetition); - NetPacketEndKickPlayerPetition::Data endPetitionData; - endPetitionData.petitionId = m_voteKickData->petitionId; - endPetitionData.numVotesAgainstKicking = m_voteKickData->numVotesAgainstKicking; - endPetitionData.numVotesInFavourOfKicking = m_voteKickData->numVotesInFavourOfKicking; - endPetitionData.playerKicked = doKick; - endPetitionData.endReason = reason; - - static_cast(endPetition.get())->SetData(endPetitionData); - SendToAllPlayers(endPetition, SessionData::Game); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_endKickPetitionMessage; + EndKickPetitionMessage_t *netEndPetition = &packet->GetMsg()->choice.endKickPetitionMessage; + netEndPetition->petitionId = m_voteKickData->petitionId; + netEndPetition->numVotesAgainstKicking = m_voteKickData->numVotesAgainstKicking; + netEndPetition->numVotesInFavourOfKicking = m_voteKickData->numVotesInFavourOfKicking; + netEndPetition->resultPlayerKicked = doKick; + netEndPetition->petitionEndReason = reason; + SendToAllPlayers(packet, SessionData::Game); // Perform kick. if (doKick) @@ -300,16 +300,15 @@ ServerGame::InternalAskVoteKick(SessionWrapper byWhom, unsigned playerIdWho, uns m_voteKickData->numVotesInFavourOfKicking = 1; m_voteKickData->votedPlayerIds.push_back(playerIdByWhom); - boost::shared_ptr startPetition(new NetPacketStartKickPlayerPetition); - NetPacketStartKickPlayerPetition::Data startPetitionData; - startPetitionData.petitionId = m_voteKickData->petitionId; - startPetitionData.proposingPlayerId = playerIdByWhom; - startPetitionData.kickPlayerId = m_voteKickData->kickPlayerId; - startPetitionData.kickTimeoutSec = timeoutSec; - startPetitionData.numVotesNeededToKick = m_voteKickData->numVotesToKick; - - static_cast(startPetition.get())->SetData(startPetitionData); - SendToAllPlayers(startPetition, SessionData::Game); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_startKickPetitionMessage; + StartKickPetitionMessage_t *netStartPetition = &packet->GetMsg()->choice.startKickPetitionMessage; + netStartPetition->petitionId = m_voteKickData->petitionId; + netStartPetition->proposingPlayerId = playerIdByWhom; + netStartPetition->kickPlayerId = m_voteKickData->kickPlayerId; + netStartPetition->kickTimeoutSec = timeoutSec; + netStartPetition->numVotesNeededToKick = m_voteKickData->numVotesToKick; + SendToAllPlayers(packet, SessionData::Game); } else InternalDenyAskVoteKick(byWhom, playerIdWho, KICK_DENIED_OTHER_IN_PROGRESS); @@ -327,12 +326,12 @@ ServerGame::InternalAskVoteKick(SessionWrapper byWhom, unsigned playerIdWho, uns void ServerGame::InternalDenyAskVoteKick(SessionWrapper byWhom, unsigned playerIdWho, DenyKickPlayerReason reason) { - boost::shared_ptr denyPetition(new NetPacketAskKickPlayerDenied); - NetPacketAskKickPlayerDenied::Data denyPetitionData; - denyPetitionData.playerId = playerIdWho; - denyPetitionData.denyReason = reason; - static_cast(denyPetition.get())->SetData(denyPetitionData); - GetLobbyThread().GetSender().Send(byWhom.sessionData, denyPetition); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_askKickDeniedMessage; + AskKickDeniedMessage_t *netKickDenied = &packet->GetMsg()->choice.askKickDeniedMessage; + netKickDenied->playerId = playerIdWho; + netKickDenied->kickDeniedReason = reason; + GetLobbyThread().GetSender().Send(byWhom.sessionData, packet); } void @@ -353,15 +352,14 @@ ServerGame::InternalVoteKick(SessionWrapper byWhom, unsigned petitionId, KickVot else m_voteKickData->numVotesAgainstKicking++; // Send update notification. - boost::shared_ptr updatePetition(new NetPacketKickPlayerPetitionUpdate); - NetPacketKickPlayerPetitionUpdate::Data updatePetitionData; - updatePetitionData.petitionId = m_voteKickData->petitionId; - updatePetitionData.numVotesAgainstKicking = m_voteKickData->numVotesAgainstKicking; - updatePetitionData.numVotesInFavourOfKicking = m_voteKickData->numVotesInFavourOfKicking; - updatePetitionData.numVotesNeededToKick = m_voteKickData->numVotesToKick; - - static_cast(updatePetition.get())->SetData(updatePetitionData); - SendToAllPlayers(updatePetition, SessionData::Game); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_kickPetitionUpdateMessage; + KickPetitionUpdateMessage_t *netKickUpdate = &packet->GetMsg()->choice.kickPetitionUpdateMessage; + netKickUpdate->petitionId = m_voteKickData->petitionId; + netKickUpdate->numVotesAgainstKicking = m_voteKickData->numVotesAgainstKicking; + netKickUpdate->numVotesInFavourOfKicking = m_voteKickData->numVotesInFavourOfKicking; + netKickUpdate->numVotesNeededToKick = m_voteKickData->numVotesToKick; + SendToAllPlayers(packet, SessionData::Game); } else InternalDenyVoteKick(byWhom, petitionId, VOTE_DENIED_ALREADY_VOTED); @@ -370,18 +368,21 @@ ServerGame::InternalVoteKick(SessionWrapper byWhom, unsigned petitionId, KickVot InternalDenyVoteKick(byWhom, petitionId, VOTE_DENIED_INVALID_PETITION); } else - InternalDenyVoteKick(byWhom, petitionId, VOTE_DENIED_IMPOSSIBLE); + InternalDenyVoteKick(byWhom, petitionId, VOTE_DENIED_INVALID_PETITION); } void ServerGame::InternalDenyVoteKick(SessionWrapper byWhom, unsigned petitionId, DenyVoteReason reason) { - boost::shared_ptr denyVote(new NetPacketVoteKickPlayerDenied); - NetPacketVoteKickPlayerDenied::Data denyVoteData; - denyVoteData.petitionId = petitionId; - denyVoteData.denyReason = reason; - static_cast(denyVote.get())->SetData(denyVoteData); - GetLobbyThread().GetSender().Send(byWhom.sessionData, denyVote); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_voteKickReplyMessage; + VoteKickReplyMessage_t *netVoteReply = &packet->GetMsg()->choice.voteKickReplyMessage; + netVoteReply->petitionId = petitionId; + netVoteReply->voteKickReplyType.present = voteKickReplyType_PR_voteKickDenied; + + VoteKickDenied_t *kickDenied = &netVoteReply->voteKickReplyType.choice.voteKickDenied; + kickDenied->voteKickDeniedReason = reason; + GetLobbyThread().GetSender().Send(byWhom.sessionData, packet); } PlayerDataList @@ -557,10 +558,14 @@ ServerGame::RemovePlayerData(boost::shared_ptr player, int reason) // Notify game state on admin change GetState().NotifyGameAdminChanged(shared_from_this()); // Send "Game Admin Changed" to clients. - boost::shared_ptr adminChanged(new NetPacketGameAdminChanged); - NetPacketGameAdminChanged::Data adminChangedData; - adminChangedData.playerId = newAdmin->GetUniqueId(); // Choose next player as admin. - static_cast(adminChanged.get())->SetData(adminChangedData); + boost::shared_ptr adminChanged(new NetPacket(NetPacket::Alloc)); + adminChanged->GetMsg()->present = PokerTHMessage_PR_gamePlayerMessage; + GamePlayerMessage_t *netGameAdmin = &adminChanged->GetMsg()->choice.gamePlayerMessage; + netGameAdmin->gameId = GetId(); + + netGameAdmin->gamePlayerNotification.present = gamePlayerNotification_PR_gameAdminChanged; + GameAdminChanged_t *gameAdmin = &netGameAdmin->gamePlayerNotification.choice.gameAdminChanged; + gameAdmin->newAdminPlayerId = newAdmin->GetUniqueId(); // Choose next player as admin. GetSessionManager().SendToAllSessions(GetLobbyThread().GetSender(), adminChanged, SessionData::Game); GetLobbyThread().NotifyGameAdminChanged(GetId(), newAdmin->GetUniqueId()); @@ -570,11 +575,26 @@ ServerGame::RemovePlayerData(boost::shared_ptr player, int reason) player->SetRights(PLAYER_RIGHTS_NORMAL); // Send "Player Left" to clients. - boost::shared_ptr thisPlayerLeft(new NetPacketPlayerLeft); - NetPacketPlayerLeft::Data thisPlayerLeftData; - thisPlayerLeftData.playerId = player->GetUniqueId(); - thisPlayerLeftData.removeReason = reason; - static_cast(thisPlayerLeft.get())->SetData(thisPlayerLeftData); + boost::shared_ptr thisPlayerLeft(new NetPacket(NetPacket::Alloc)); + thisPlayerLeft->GetMsg()->present = PokerTHMessage_PR_gamePlayerMessage; + GamePlayerMessage_t *netPlayerLeft = &thisPlayerLeft->GetMsg()->choice.gamePlayerMessage; + netPlayerLeft->gameId = GetId(); + + netPlayerLeft->gamePlayerNotification.present = gamePlayerNotification_PR_gamePlayerLeft; + GamePlayerLeft_t *gamePlayer = &netPlayerLeft->gamePlayerNotification.choice.gamePlayerLeft; + gamePlayer->playerId = player->GetUniqueId(); + switch (reason) + { + case NTF_NET_REMOVED_ON_REQUEST : + gamePlayer->gamePlayerLeftReason = gamePlayerLeftReason_leftOnRequest; + break; + case NTF_NET_REMOVED_KICKED : + gamePlayer->gamePlayerLeftReason = gamePlayerLeftReason_leftKicked; + break; + default : + gamePlayer->gamePlayerLeftReason = gamePlayerLeftReason_leftError; + break; + } GetSessionManager().SendToAllSessions(GetLobbyThread().GetSender(), thisPlayerLeft, SessionData::Game); GetLobbyThread().NotifyPlayerLeftGame(GetId(), player->GetUniqueId()); diff --git a/src/net/common/servergamestate.cpp b/src/net/common/servergamestate.cpp index d7b9a7d9..0a84e181 100644 --- a/src/net/common/servergamestate.cpp +++ b/src/net/common/servergamestate.cpp @@ -73,55 +73,58 @@ static void SendPlayerAction(ServerGame &server, boost::shared_ptr notifyActionDone(new NetPacketPlayersActionDone); - NetPacketPlayersActionDone::Data actionDoneData; - actionDoneData.gameState = server.GetCurRound(); - actionDoneData.playerId = player->getMyUniqueID(); - actionDoneData.playerAction = static_cast(player->getMyAction()); - actionDoneData.totalPlayerBet = player->getMySet(); - actionDoneData.playerMoney = player->getMyCash(); - actionDoneData.highestSet = server.GetGame().getCurrentHand()->getCurrentBeRo()->getHighestSet(); - actionDoneData.minimumRaise = server.GetGame().getCurrentHand()->getCurrentBeRo()->getMinimumRaise(); - static_cast(notifyActionDone.get())->SetData(actionDoneData); - server.SendToAllPlayers(notifyActionDone, SessionData::Game); + + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_playersActionDoneMessage; + PlayersActionDoneMessage_t *netActionDone = &packet->GetMsg()->choice.playersActionDoneMessage; + + netActionDone->gameId = server.GetId(); + netActionDone->gameState = server.GetCurRound(); + netActionDone->highestSet = server.GetGame().getCurrentHand()->getCurrentBeRo()->getHighestSet(); + netActionDone->minimumRaise = server.GetGame().getCurrentHand()->getCurrentBeRo()->getMinimumRaise(); + netActionDone->playerAction = static_cast(player->getMyAction()); + netActionDone->playerId = player->getMyUniqueID(); + netActionDone->playerMoney = player->getMyCash(); + netActionDone->totalPlayerBet = player->getMySet(); + server.SendToAllPlayers(packet, SessionData::Game); } static void SendNewRoundCards(ServerGame &server, Game &curGame, int state) { + int cards[5]; + curGame.getCurrentHand()->getBoard()->getMyCards(cards); switch(state) { case GAME_STATE_PREFLOP: { // nothing to do } break; case GAME_STATE_FLOP: { // deal flop cards - int cards[5]; - curGame.getCurrentHand()->getBoard()->getMyCards(cards); - boost::shared_ptr notifyCards(new NetPacketDealFlopCards); - NetPacketDealFlopCards::Data notifyCardsData; - for (int num = 0; num < 3; num++) - notifyCardsData.flopCards[num] = static_cast(cards[num]); - static_cast(notifyCards.get())->SetData(notifyCardsData); - server.SendToAllPlayers(notifyCards, SessionData::Game); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_dealFlopCardsMessage; + DealFlopCardsMessage_t *netDealFlop = &packet->GetMsg()->choice.dealFlopCardsMessage; + netDealFlop->gameId = server.GetId(); + netDealFlop->flopCard1 = cards[0]; + netDealFlop->flopCard2 = cards[1]; + netDealFlop->flopCard3 = cards[2]; + server.SendToAllPlayers(packet, SessionData::Game); } break; case GAME_STATE_TURN: { // deal turn card - int cards[5]; - curGame.getCurrentHand()->getBoard()->getMyCards(cards); - boost::shared_ptr notifyCards(new NetPacketDealTurnCard); - NetPacketDealTurnCard::Data notifyCardsData; - notifyCardsData.turnCard = static_cast(cards[3]); - static_cast(notifyCards.get())->SetData(notifyCardsData); - server.SendToAllPlayers(notifyCards, SessionData::Game); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_dealTurnCardMessage; + DealTurnCardMessage_t *netDealTurn = &packet->GetMsg()->choice.dealTurnCardMessage; + netDealTurn->gameId = server.GetId(); + netDealTurn->turnCard = cards[3]; + server.SendToAllPlayers(packet, SessionData::Game); } break; case GAME_STATE_RIVER: { // deal river card - int cards[5]; - curGame.getCurrentHand()->getBoard()->getMyCards(cards); - boost::shared_ptr notifyCards(new NetPacketDealRiverCard); - NetPacketDealRiverCard::Data notifyCardsData; - notifyCardsData.riverCard = static_cast(cards[4]); - static_cast(notifyCards.get())->SetData(notifyCardsData); - server.SendToAllPlayers(notifyCards, SessionData::Game); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_dealRiverCardMessage; + DealRiverCardMessage_t *netDealRiver = &packet->GetMsg()->choice.dealRiverCardMessage; + netDealRiver->gameId = server.GetId(); + netDealRiver->riverCard = cards[4]; + server.SendToAllPlayers(packet, SessionData::Game); } break; default: { // @@ -186,81 +189,76 @@ AbstractServerGameStateReceiving::ProcessPacket(boost::shared_ptr se { if (packet->IsClientActivity()) session.sessionData->ResetActivityTimer(); - if (packet->ToNetPacketRetrievePlayerInfo()) + if (packet->GetMsg()->present == PokerTHMessage_PR_playerInfoRequestMessage) { // Delegate to Lobby. - server->GetLobbyThread().HandleGameRetrievePlayerInfo(session, *packet->ToNetPacketRetrievePlayerInfo()); + server->GetLobbyThread().HandleGameRetrievePlayerInfo(session, packet->GetMsg()->choice.playerInfoRequestMessage); } - else if (packet->ToNetPacketRetrieveAvatar()) + else if (packet->GetMsg()->present == PokerTHMessage_PR_avatarRequestMessage) { // Delegate to Lobby. - server->GetLobbyThread().HandleGameRetrieveAvatar(session, *packet->ToNetPacketRetrieveAvatar()); + server->GetLobbyThread().HandleGameRetrieveAvatar(session, packet->GetMsg()->choice.avatarRequestMessage); } - else if (packet->ToNetPacketLeaveCurrentGame()) + else if (packet->GetMsg()->present == PokerTHMessage_PR_leaveGameRequestMessage) { server->MoveSessionToLobby(session, NTF_NET_REMOVED_ON_REQUEST); } - else if (packet->ToNetPacketKickPlayer()) + else if (packet->GetMsg()->present == PokerTHMessage_PR_kickPlayerRequestMessage) { // Only admins are allowed to kick, and only in the lobby. // After leaving the lobby, a vote needs to be initiated to kick. - if (session.playerData->GetRights() == PLAYER_RIGHTS_ADMIN && !server->IsRunning()) + KickPlayerRequestMessage_t *netKickRequest = &packet->GetMsg()->choice.kickPlayerRequestMessage; + if (session.playerData->GetRights() == PLAYER_RIGHTS_ADMIN && !server->IsRunning() && netKickRequest->gameId == server->GetId()) { - NetPacketKickPlayer::Data kickPlayerData; - packet->ToNetPacketKickPlayer()->GetData(kickPlayerData); - - server->InternalKickPlayer(kickPlayerData.playerId); + server->InternalKickPlayer(netKickRequest->playerId); } } - else if (packet->ToNetPacketAskKickPlayer()) + else if (packet->GetMsg()->present == PokerTHMessage_PR_askKickPlayerMessage) { if (session.playerData) { - NetPacketAskKickPlayer::Data askKickData; - packet->ToNetPacketAskKickPlayer()->GetData(askKickData); - - server->InternalAskVoteKick(session, askKickData.playerId, SERVER_VOTE_KICK_TIMEOUT_SEC); + AskKickPlayerMessage_t *netAskKick = &packet->GetMsg()->choice.askKickPlayerMessage; + server->InternalAskVoteKick(session, netAskKick->playerId, SERVER_VOTE_KICK_TIMEOUT_SEC); } } - else if (packet->ToNetPacketVoteKickPlayer()) + else if (packet->GetMsg()->present == PokerTHMessage_PR_voteKickRequestMessage) { if (session.playerData) { - NetPacketVoteKickPlayer::Data voteData; - packet->ToNetPacketVoteKickPlayer()->GetData(voteData); - - server->InternalVoteKick(session, voteData.petitionId, voteData.vote); + VoteKickRequestMessage_t *netVoteKick = &packet->GetMsg()->choice.voteKickRequestMessage; + server->InternalVoteKick(session, netVoteKick->petitionId, netVoteKick->voteKick ? KICK_VOTE_AGAINST : KICK_VOTE_IN_FAVOUR); } } // Chat text is always allowed. - else if (packet->ToNetPacketSendChatText()) + else if (packet->GetMsg()->present == PokerTHMessage_PR_chatRequestMessage) { if (session.playerData) // Only forward if this player is known. { // Forward chat text to all players. // TODO: Some limitation needed. - NetPacketSendChatText::Data inChatData; - packet->ToNetPacketSendChatText()->GetData(inChatData); - - boost::shared_ptr outChat(new NetPacketChatText); - NetPacketChatText::Data outChatData; - outChatData.playerId = session.playerData->GetUniqueId(); - outChatData.text = inChatData.text; - static_cast(outChat.get())->SetData(outChatData); - server->SendToAllPlayers(outChat, SessionData::Game); + ChatRequestMessage_t *netChatRequest = &packet->GetMsg()->choice.chatRequestMessage; + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_chatMessage; + ChatMessage_t *netChat = &packet->GetMsg()->choice.chatMessage; + netChat->gameId = server->GetId(); + netChat->playerId = session.playerData->GetUniqueId(); + OCTET_STRING_fromBuf( + &netChat->chatText, + (char *)netChatRequest->chatText.buf, + netChatRequest->chatText.size); + server->SendToAllPlayers(packet, SessionData::Game); } } - else if (packet->ToNetPacketUnsubscribeGameList()) + else if (packet->GetMsg()->present == PokerTHMessage_PR_subscriptionRequestMessage) { - // We can do this directly in this thread. - session.sessionData->ResetWantsLobbyMsg(); - } - else if (packet->ToNetPacketResubscribeGameList()) - { - // This needs to be performed in the lobby thread, - // because a new game list needs to be sent. - if (!session.sessionData->WantsLobbyMsg()) - server->GetLobbyThread().ResubscribeLobbyMsg(session); + SubscriptionRequestMessage_t *netSubscription = &packet->GetMsg()->choice.subscriptionRequestMessage; + if (netSubscription->subscriptionAction == subscriptionAction_resubscribeGameList) + { + if (!session.sessionData->WantsLobbyMsg()) + server->GetLobbyThread().ResubscribeLobbyMsg(session); + } + else + session.sessionData->ResetWantsLobbyMsg(); } else { @@ -332,13 +330,19 @@ ServerGameStateInit::HandleNewSession(boost::shared_ptr server, Sess } // Send ack to client. - boost::shared_ptr joinGameAck(new NetPacketJoinGameAck); - NetPacketJoinGameAck::Data joinGameAckData; - joinGameAckData.gameId = server->GetId(); - joinGameAckData.prights = session.playerData->GetRights(); - joinGameAckData.gameData = server->GetGameData(); - static_cast(joinGameAck.get())->SetData(joinGameAckData); - server->GetLobbyThread().GetSender().Send(session.sessionData, joinGameAck); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_joinGameReplyMessage; + JoinGameReplyMessage_t *netJoinReply = &packet->GetMsg()->choice.joinGameReplyMessage; + netJoinReply->gameId = server->GetId(); + netJoinReply->joinGameResult.present = joinGameResult_PR_joinGameAck; + JoinGameAck_t *joinAck = &netJoinReply->joinGameResult.choice.joinGameAck; + joinAck->areYouAdmin = session.playerData->GetRights() == PLAYER_RIGHTS_ADMIN; + NetPacket::SetGameData(server->GetGameData(), &joinAck->gameInfo); + OCTET_STRING_fromBuf( + &joinAck->gameInfo.gameName, + session.playerData->GetName().c_str(), + session.playerData->GetName().length()); + server->GetLobbyThread().GetSender().Send(session.sessionData, packet); // Send notifications for connected players to client. PlayerDataList tmpPlayerList = server->GetFullPlayerDataList(); @@ -346,12 +350,12 @@ ServerGameStateInit::HandleNewSession(boost::shared_ptr server, Sess PlayerDataList::iterator player_end = tmpPlayerList.end(); while (player_i != player_end) { - server->GetLobbyThread().GetSender().Send(session.sessionData, CreateNetPacketPlayerJoined(*(*player_i))); + server->GetLobbyThread().GetSender().Send(session.sessionData, CreateNetPacketPlayerJoined(server->GetId(), *(*player_i))); ++player_i; } // Send "Player Joined" to other fully connected clients. - server->SendToAllPlayers(CreateNetPacketPlayerJoined(*session.playerData), SessionData::Game); + server->SendToAllPlayers(CreateNetPacketPlayerJoined(server->GetId(), *session.playerData), SessionData::Game); // Accept session. server->GetSessionManager().AddSession(session); @@ -388,12 +392,12 @@ ServerGameStateInit::TimerAdminWarning(const boost::system::error_code &ec, boos if (session.sessionData.get()) { // Send him a warning. - boost::shared_ptr warning(new NetPacketTimeoutWarning); - NetPacketTimeoutWarning::Data warningData; - warningData.timeoutReason = NETWORK_TIMEOUT_GAME_ADMIN_IDLE; - warningData.remainingSeconds = SERVER_GAME_ADMIN_WARNING_REMAINING_SEC; - static_cast(warning.get())->SetData(warningData); - server->GetLobbyThread().GetSender().Send(session.sessionData, warning); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_timeoutWarningMessage; + TimeoutWarningMessage_t *netWarning = &packet->GetMsg()->choice.timeoutWarningMessage; + netWarning->timeoutReason = NETWORK_TIMEOUT_GAME_ADMIN_IDLE; + netWarning->remainingSeconds = SERVER_GAME_ADMIN_WARNING_REMAINING_SEC; + server->GetLobbyThread().GetSender().Send(session.sessionData, packet); } // Start timeout timer. server->GetStateTimer().expires_from_now( @@ -422,18 +426,16 @@ ServerGameStateInit::TimerAdminTimeout(const boost::system::error_code &ec, boos void ServerGameStateInit::InternalProcessPacket(boost::shared_ptr server, SessionWrapper session, boost::shared_ptr packet) { - if (packet->ToNetPacketStartEvent()) + if (packet->GetMsg()->present == PokerTHMessage_PR_startEventMessage) { + StartEventMessage_t *netStartEvent = &packet->GetMsg()->choice.startEventMessage; // Only admins are allowed to start the game. - if (session.playerData->GetRights() == PLAYER_RIGHTS_ADMIN) + if (session.playerData->GetRights() == PLAYER_RIGHTS_ADMIN && netStartEvent->gameId == server->GetId()) { - NetPacketStartEvent::Data startData; - packet->ToNetPacketStartEvent()->GetData(startData); - // Fill up with computer players. server->ResetComputerPlayerList(); - if (startData.fillUpWithCpuPlayers) + if (netStartEvent->fillWithComputerPlayers) { int remainingSlots = server->GetGameData().maxNumberOfPlayers - server->GetCurNumberOfPlayers(); for (int i = 1; i <= remainingSlots; i++) @@ -447,19 +449,19 @@ ServerGameStateInit::InternalProcessPacket(boost::shared_ptr server, server->AddComputerPlayer(tmpPlayerData); // Send "Player Joined" to other fully connected clients. - server->SendToAllPlayers(CreateNetPacketPlayerJoined(*tmpPlayerData), SessionData::Game); + server->SendToAllPlayers(CreateNetPacketPlayerJoined(server->GetId(), *tmpPlayerData), SessionData::Game); // Notify lobby. server->GetLobbyThread().NotifyPlayerJoinedGame(server->GetId(), tmpPlayerData->GetUniqueId()); } } // Wait for all players to confirm start of game. - server->SendToAllPlayers(boost::shared_ptr(packet->Clone()), SessionData::Game); + server->SendToAllPlayers(packet, SessionData::Game); server->SetState(ServerGameStateStartGame::Instance()); } } - else if (packet->ToNetPacketResetTimeout()) + else if (packet->GetMsg()->present == PokerTHMessage_PR_resetTimeoutMessage) { if (session.playerData->GetRights() == PLAYER_RIGHTS_ADMIN) { @@ -473,14 +475,17 @@ ServerGameStateInit::InternalProcessPacket(boost::shared_ptr server, } boost::shared_ptr -ServerGameStateInit::CreateNetPacketPlayerJoined(const PlayerData &playerData) +ServerGameStateInit::CreateNetPacketPlayerJoined(unsigned gameId, const PlayerData &playerData) { - boost::shared_ptr thisPlayerJoined(new NetPacketPlayerJoined); - NetPacketPlayerJoined::Data thisPlayerJoinedData; - thisPlayerJoinedData.playerId = playerData.GetUniqueId(); - thisPlayerJoinedData.prights = playerData.GetRights(); - static_cast(thisPlayerJoined.get())->SetData(thisPlayerJoinedData); - return thisPlayerJoined; + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_gamePlayerMessage; + GamePlayerMessage_t *netGamePlayer = &packet->GetMsg()->choice.gamePlayerMessage; + netGamePlayer->gameId = gameId; + netGamePlayer->gamePlayerNotification.present = gamePlayerNotification_PR_gamePlayerJoined; + GamePlayerJoined_t *playerJoined = &netGamePlayer->gamePlayerNotification.choice.gamePlayerJoined; + playerJoined->playerId = playerData.GetUniqueId(); + playerJoined->isAdmin = playerData.GetRights() == PLAYER_RIGHTS_ADMIN; + return packet; } //----------------------------------------------------------------------------- @@ -527,7 +532,7 @@ ServerGameStateStartGame::HandleNewSession(boost::shared_ptr server, void ServerGameStateStartGame::InternalProcessPacket(boost::shared_ptr server, SessionWrapper session, boost::shared_ptr packet) { - if (packet->ToNetPacketStartEventAck()) + if (packet->GetMsg()->present == PokerTHMessage_PR_startEventAckMessage) { session.sessionData->SetReadyFlag(); if (server->GetSessionManager().CountReadySessions() == server->GetSessionManager().GetRawSessionCount()) @@ -570,10 +575,11 @@ ServerGameStateStartGame::DoStart(boost::shared_ptr server) { server->InternalStartGame(); - boost::shared_ptr answer(new NetPacketGameStart); - - NetPacketGameStart::Data gameStartData; - gameStartData.startData = server->GetStartData(); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_gameStartMessage; + GameStartMessage_t *netGameStart = &packet->GetMsg()->choice.gameStartMessage; + netGameStart->gameId = server->GetId(); + netGameStart->startDealerPlayerId = server->GetStartData().startDealerPlayerId; // Send player order to clients. // Assume player list is sorted by number. @@ -581,15 +587,14 @@ ServerGameStateStartGame::DoStart(boost::shared_ptr server) PlayerDataList::iterator player_end = tmpPlayerList.end(); while (player_i != player_end) { - NetPacketGameStart::PlayerSlot tmpPlayerSlot; - tmpPlayerSlot.playerId = (*player_i)->GetUniqueId(); - gameStartData.playerSlots.push_back(tmpPlayerSlot); + NonZeroId_t *playerSlot = (NonZeroId_t *)calloc(1, sizeof(NonZeroId_t)); + *playerSlot = (*player_i)->GetUniqueId(); + ASN_SEQUENCE_ADD(&netGameStart->playerSeats.list, playerSlot); ++player_i; } - static_cast(answer.get())->SetData(gameStartData); - server->SendToAllPlayers(answer, SessionData::Game); + server->SendToAllPlayers(packet, SessionData::Game); // Start the first hand. ServerGameStateHand::StartNewHand(server); @@ -688,26 +693,26 @@ ServerGameStateHand::EngineLoop(boost::shared_ptr server) && nonFoldPlayers.size() > 1) { // Send cards of all active players to all players (all in). - boost::shared_ptr allIn(new NetPacketAllInShowCards); - NetPacketAllInShowCards::Data allInData; + boost::shared_ptr allIn(new NetPacket(NetPacket::Alloc)); + allIn->GetMsg()->present = PokerTHMessage_PR_allInShowCardsMessage; + AllInShowCardsMessage_t *netAllInShow = &allIn->GetMsg()->choice.allInShowCardsMessage; + netAllInShow->gameId = server->GetId(); PlayerListConstIterator i = nonFoldPlayers.begin(); PlayerListConstIterator end = nonFoldPlayers.end(); while (i != end) { - NetPacketAllInShowCards::PlayerCards tmpPlayerCards; - tmpPlayerCards.playerId = (*i)->getMyUniqueID(); - + PlayerAllIn_t *playerAllIn = (PlayerAllIn_t *)calloc(1, sizeof(PlayerAllIn_t)); + playerAllIn->playerId = (*i)->getMyUniqueID(); int tmpCards[2]; (*i)->getMyCards(tmpCards); - tmpPlayerCards.cards[0] = static_cast(tmpCards[0]); - tmpPlayerCards.cards[1] = static_cast(tmpCards[1]); + playerAllIn->allInCard1 = tmpCards[0]; + playerAllIn->allInCard2 = tmpCards[1]; + ASN_SEQUENCE_ADD(&netAllInShow->playersAllIn.list, playerAllIn); - allInData.playerCards.push_back(tmpPlayerCards); ++i; } - static_cast(allIn.get())->SetData(allInData); server->SendToAllPlayers(allIn, SessionData::Game); curGame.getCurrentHand()->setCardsShown(true); @@ -742,12 +747,12 @@ ServerGameStateHand::EngineLoop(boost::shared_ptr server) if (!curPlayer->getMyActiveStatus()) throw ServerException(__FILE__, __LINE__, ERR_NET_PLAYER_NOT_ACTIVE, 0); - boost::shared_ptr notification(new NetPacketPlayersTurn); - NetPacketPlayersTurn::Data playersTurnData; - playersTurnData.gameState = (GameState)curGame.getCurrentHand()->getCurrentRound(); - playersTurnData.playerId = curPlayer->getMyUniqueID(); - static_cast(notification.get())->SetData(playersTurnData); - + boost::shared_ptr notification(new NetPacket(NetPacket::Alloc)); + notification->GetMsg()->present = PokerTHMessage_PR_playersTurnMessage; + PlayersTurnMessage_t *netPlayersTurn = ¬ification->GetMsg()->choice.playersTurnMessage; + netPlayersTurn->gameId = server->GetId(); + netPlayersTurn->gameState = curGame.getCurrentHand()->getCurrentRound(); + netPlayersTurn->playerId = curPlayer->getMyUniqueID(); server->SendToAllPlayers(notification, SessionData::Game); // If the player is computer controlled, let the engine act. @@ -788,48 +793,57 @@ ServerGameStateHand::EngineLoop(boost::shared_ptr server) { // End of Hand, but keep cards hidden. boost::shared_ptr player = nonFoldPlayers.front(); - boost::shared_ptr endHand(new NetPacketEndOfHandHideCards); - NetPacketEndOfHandHideCards::Data endHandData; - endHandData.playerId = player->getMyUniqueID(); - endHandData.moneyWon = player->getLastMoneyWon(); - endHandData.playerMoney = player->getMyCash(); - static_cast(endHand.get())->SetData(endHandData); + boost::shared_ptr endHand(new NetPacket(NetPacket::Alloc)); + endHand->GetMsg()->present = PokerTHMessage_PR_endOfHandMessage; + EndOfHandMessage_t *netEndHand = &endHand->GetMsg()->choice.endOfHandMessage; + netEndHand->gameId = server->GetId(); + netEndHand->endOfHandType.present = endOfHandType_PR_endOfHandHideCards; + EndOfHandHideCards_t *endHandHide = &netEndHand->endOfHandType.choice.endOfHandHideCards; + endHandHide->playerId = player->getMyUniqueID(); + endHandHide->moneyWon = player->getLastMoneyWon(); + endHandHide->playerMoney = player->getMyCash(); server->SendToAllPlayers(endHand, SessionData::Game); } else { // End of Hand - show cards of active players. - boost::shared_ptr endHand(new NetPacketEndOfHandShowCards); - NetPacketEndOfHandShowCards::Data endHandData; + boost::shared_ptr player = nonFoldPlayers.front(); + boost::shared_ptr endHand(new NetPacket(NetPacket::Alloc)); + endHand->GetMsg()->present = PokerTHMessage_PR_endOfHandMessage; + EndOfHandMessage_t *netEndHand = &endHand->GetMsg()->choice.endOfHandMessage; + netEndHand->gameId = server->GetId(); + + netEndHand->endOfHandType.present = endOfHandType_PR_endOfHandShowCards; + EndOfHandShowCards_t *endHandShow = &netEndHand->endOfHandType.choice.endOfHandShowCards; PlayerListConstIterator i = nonFoldPlayers.begin(); PlayerListConstIterator end = nonFoldPlayers.end(); while (i != end) { - NetPacketEndOfHandShowCards::PlayerResult tmpPlayerResult; - tmpPlayerResult.playerId = (*i)->getMyUniqueID(); - + PlayerResult_t *playerResult = (PlayerResult_t *)calloc(1, sizeof(PlayerResult_t)); + playerResult->playerId = (*i)->getMyUniqueID(); int tmpCards[2]; int bestHandPos[5]; (*i)->getMyCards(tmpCards); - tmpPlayerResult.cards[0] = static_cast(tmpCards[0]); - tmpPlayerResult.cards[1] = static_cast(tmpCards[1]); - + playerResult->resultCard1 = tmpCards[0]; + playerResult->resultCard2 = tmpCards[1]; (*i)->getMyBestHandPosition(bestHandPos); for (int num = 0; num < 5; num++) - tmpPlayerResult.bestHandPos[num] = bestHandPos[num]; + { + long *handPos = (long *)calloc(1, sizeof(long)); + *handPos = bestHandPos[num]; + ASN_SEQUENCE_ADD(&playerResult->bestHandPosition.list, handPos); + } - tmpPlayerResult.valueOfCards = (*i)->getMyCardsValueInt(); - tmpPlayerResult.moneyWon = (*i)->getLastMoneyWon(); - tmpPlayerResult.playerMoney = (*i)->getMyCash(); + playerResult->cardsValue = (*i)->getMyCardsValueInt(); + playerResult->moneyWon = (*i)->getLastMoneyWon(); + playerResult->playerMoney = (*i)->getMyCash(); - endHandData.playerResults.push_back(tmpPlayerResult); + ASN_SEQUENCE_ADD(&endHandShow->playerResults.list, playerResult); ++i; } - static_cast(endHand.get())->SetData(endHandData); - server->SendToAllPlayers(endHand, SessionData::Game); } @@ -934,11 +948,11 @@ ServerGameStateHand::TimerNextGame(const boost::system::error_code &ec, boost::s if (winnerPlayer) { - boost::shared_ptr endGame(new NetPacketEndOfGame); - NetPacketEndOfGame::Data endGameData; - endGameData.winnerPlayerId = winnerPlayer->getMyUniqueID(); - static_cast(endGame.get())->SetData(endGameData); - + boost::shared_ptr endGame(new NetPacket(NetPacket::Alloc)); + endGame->GetMsg()->present = PokerTHMessage_PR_endOfGameMessage; + EndOfGameMessage_t *netEndGame = &endGame->GetMsg()->choice.endOfGameMessage; + netEndGame->gameId = server->GetId(); + netEndGame->winnerPlayerId = winnerPlayer->getMyUniqueID(); server->SendToAllPlayers(endGame, SessionData::Game); // Wait for the start of a new game. @@ -1002,13 +1016,14 @@ ServerGameStateHand::StartNewHand(boost::shared_ptr server) { int cards[2]; tmpPlayer->getMyCards(cards); - boost::shared_ptr notifyCards(new NetPacketHandStart); - NetPacketHandStart::Data handStartData; - handStartData.yourCards[0] = static_cast(cards[0]); - handStartData.yourCards[1] = static_cast(cards[1]); - handStartData.smallBlind = curGame.getCurrentHand()->getSmallBlind(); - static_cast(notifyCards.get())->SetData(handStartData); + boost::shared_ptr notifyCards(new NetPacket(NetPacket::Alloc)); + notifyCards->GetMsg()->present = PokerTHMessage_PR_handStartMessage; + HandStartMessage_t *netHandStart = ¬ifyCards->GetMsg()->choice.handStartMessage; + netHandStart->gameId = server->GetId(); + netHandStart->yourCard1 = cards[0]; + netHandStart->yourCard2 = cards[1]; + netHandStart->smallBlind = curGame.getCurrentHand()->getSmallBlind(); server->GetLobbyThread().GetSender().Send(tmpPlayer->getNetSessionData(), notifyCards); } ++i; @@ -1026,16 +1041,17 @@ ServerGameStateHand::StartNewHand(boost::shared_ptr server) boost::shared_ptr tmpPlayer = *i; if (tmpPlayer->getMyButton() == BUTTON_SMALL_BLIND) { - boost::shared_ptr notifySmallBlind(new NetPacketPlayersActionDone); - NetPacketPlayersActionDone::Data actionDoneData; - actionDoneData.gameState = GAME_STATE_PREFLOP_SMALL_BLIND; - actionDoneData.playerId = tmpPlayer->getMyUniqueID(); - actionDoneData.playerAction = (PlayerAction)tmpPlayer->getMyAction(); - actionDoneData.totalPlayerBet = tmpPlayer->getMySet(); - actionDoneData.playerMoney = tmpPlayer->getMyCash(); - actionDoneData.highestSet = server->GetGame().getCurrentHand()->getCurrentBeRo()->getHighestSet(); - actionDoneData.minimumRaise = server->GetGame().getCurrentHand()->getCurrentBeRo()->getMinimumRaise(); - static_cast(notifySmallBlind.get())->SetData(actionDoneData); + boost::shared_ptr notifySmallBlind(new NetPacket(NetPacket::Alloc)); + notifySmallBlind->GetMsg()->present = PokerTHMessage_PR_playersActionDoneMessage; + PlayersActionDoneMessage_t *netSmallBlind = ¬ifySmallBlind->GetMsg()->choice.playersActionDoneMessage; + netSmallBlind->gameId = server->GetId(); + netSmallBlind->gameState = NetGameState_statePreflopSmallBlind; + netSmallBlind->playerId = tmpPlayer->getMyUniqueID(); + netSmallBlind->playerAction = tmpPlayer->getMyAction(); + netSmallBlind->totalPlayerBet = tmpPlayer->getMySet(); + netSmallBlind->playerMoney = tmpPlayer->getMyCash(); + netSmallBlind->highestSet = server->GetGame().getCurrentHand()->getCurrentBeRo()->getHighestSet(); + netSmallBlind->minimumRaise = server->GetGame().getCurrentHand()->getCurrentBeRo()->getMinimumRaise(); server->SendToAllPlayers(notifySmallBlind, SessionData::Game); break; } @@ -1049,16 +1065,17 @@ ServerGameStateHand::StartNewHand(boost::shared_ptr server) boost::shared_ptr tmpPlayer = *i; if (tmpPlayer->getMyButton() == BUTTON_BIG_BLIND) { - boost::shared_ptr notifyBigBlind(new NetPacketPlayersActionDone); - NetPacketPlayersActionDone::Data actionDoneData; - actionDoneData.gameState = GAME_STATE_PREFLOP_BIG_BLIND; - actionDoneData.playerId = tmpPlayer->getMyUniqueID(); - actionDoneData.playerAction = (PlayerAction)tmpPlayer->getMyAction(); - actionDoneData.totalPlayerBet = tmpPlayer->getMySet(); - actionDoneData.playerMoney = tmpPlayer->getMyCash(); - actionDoneData.highestSet = server->GetGame().getCurrentHand()->getCurrentBeRo()->getHighestSet(); - actionDoneData.minimumRaise = server->GetGame().getCurrentHand()->getCurrentBeRo()->getMinimumRaise(); - static_cast(notifyBigBlind.get())->SetData(actionDoneData); + boost::shared_ptr notifyBigBlind(new NetPacket(NetPacket::Alloc)); + notifyBigBlind->GetMsg()->present = PokerTHMessage_PR_playersActionDoneMessage; + PlayersActionDoneMessage_t *netBigBlind = ¬ifyBigBlind->GetMsg()->choice.playersActionDoneMessage; + netBigBlind->gameId = server->GetId(); + netBigBlind->gameState = NetGameState_statePreflopBigBlind; + netBigBlind->playerId = tmpPlayer->getMyUniqueID(); + netBigBlind->playerAction = tmpPlayer->getMyAction(); + netBigBlind->totalPlayerBet = tmpPlayer->getMySet(); + netBigBlind->playerMoney = tmpPlayer->getMyCash(); + netBigBlind->highestSet = server->GetGame().getCurrentHand()->getCurrentBeRo()->getHighestSet(); + netBigBlind->minimumRaise = server->GetGame().getCurrentHand()->getCurrentBeRo()->getMinimumRaise(); server->SendToAllPlayers(notifyBigBlind, SessionData::Game); break; } @@ -1116,11 +1133,11 @@ ServerGameStateWaitPlayerAction::HandleNewSession(boost::shared_ptr void ServerGameStateWaitPlayerAction::InternalProcessPacket(boost::shared_ptr server, SessionWrapper session, boost::shared_ptr packet) { - if (packet->ToNetPacketPlayersAction()) + if (packet->GetMsg()->present == PokerTHMessage_PR_myActionRequestMessage) { - NetPacketPlayersAction::Data actionData; - packet->ToNetPacketPlayersAction()->GetData(actionData); + MyActionRequestMessage_t *netMyAction = &packet->GetMsg()->choice.myActionRequestMessage; + // TODO consider game id. Game &curGame = server->GetGame(); boost::shared_ptr tmpPlayer = curGame.getPlayerByUniqueId(session.playerData->GetUniqueId()); if (!tmpPlayer.get()) @@ -1128,7 +1145,7 @@ ServerGameStateWaitPlayerAction::InternalProcessPacket(boost::shared_ptrgetCurrentRound() != actionData.gameState) + if (curGame.getCurrentHand()->getCurrentRound() != netMyAction->gameState) code = ACTION_CODE_INVALID_STATE; // Check whether this is the correct player. @@ -1140,21 +1157,21 @@ ServerGameStateWaitPlayerAction::InternalProcessPacket(boost::shared_ptrmyAction == PLAYER_ACTION_CALL && netMyAction->myRelativeBet == 0) { if (curGame.getCurrentHand()->getCurrentBeRo()->getHighestSet() >= tmpPlayer->getMySet() + tmpPlayer->getMyCash()) - actionData.playerAction = PLAYER_ACTION_ALLIN; + netMyAction->myAction = PLAYER_ACTION_ALLIN; else - actionData.playerBet = curGame.getCurrentHand()->getCurrentBeRo()->getHighestSet() - tmpPlayer->getMySet(); + netMyAction->myRelativeBet = curGame.getCurrentHand()->getCurrentBeRo()->getHighestSet() - tmpPlayer->getMySet(); } - if (actionData.playerAction == PLAYER_ACTION_ALLIN && actionData.playerBet == 0) - actionData.playerBet = tmpPlayer->getMyCash(); + if (netMyAction->myAction == PLAYER_ACTION_ALLIN && netMyAction->myRelativeBet == 0) + netMyAction->myRelativeBet = tmpPlayer->getMyCash(); // Check whether the action is valid. if (code == ACTION_CODE_VALID && (tmpPlayer->checkMyAction( - actionData.playerAction, - actionData.playerBet, + netMyAction->myAction, + netMyAction->myRelativeBet, curGame.getCurrentHand()->getCurrentBeRo()->getHighestSet(), curGame.getCurrentHand()->getCurrentBeRo()->getMinimumRaise(), curGame.getCurrentHand()->getSmallBlind()) != 0)) @@ -1164,19 +1181,20 @@ ServerGameStateWaitPlayerAction::InternalProcessPacket(boost::shared_ptr(netMyAction->myAction), netMyAction->myRelativeBet); server->SetState(ServerGameStateHand::Instance()); } else { // Send reject message. - boost::shared_ptr reject(new NetPacketPlayersActionRejected); - NetPacketPlayersActionRejected::Data rejectData; - rejectData.gameState = actionData.gameState; - rejectData.playerAction = actionData.playerAction; - rejectData.playerBet = actionData.playerBet; - rejectData.rejectionReason = code; - static_cast(reject.get())->SetData(rejectData); + boost::shared_ptr reject(new NetPacket(NetPacket::Alloc)); + reject->GetMsg()->present = PokerTHMessage_PR_yourActionRejectedMessage; + YourActionRejectedMessage_t *netActionRejected = &reject->GetMsg()->choice.yourActionRejectedMessage; + netActionRejected->gameId = server->GetId(); + netActionRejected->gameState = netMyAction->gameState; + netActionRejected->yourAction = netMyAction->myAction; + netActionRejected->yourRelativeBet = netMyAction->myRelativeBet; + netActionRejected->rejectionReason = code; server->GetLobbyThread().GetSender().Send(session.sessionData, reject); } } diff --git a/src/net/common/serverlobbythread.cpp b/src/net/common/serverlobbythread.cpp index ff526260..12276a59 100644 --- a/src/net/common/serverlobbythread.cpp +++ b/src/net/common/serverlobbythread.cpp @@ -192,10 +192,33 @@ ServerLobbyThread::ReAddSession(SessionWrapper session, int reason) { if (session.sessionData.get() && session.playerData.get()) { - boost::shared_ptr packet(new NetPacketRemovedFromGame); - NetPacketRemovedFromGame::Data removedData; - removedData.removeReason = reason; - static_cast(packet.get())->SetData(removedData); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_gamePlayerMessage; + GamePlayerMessage_t *netPlayerMsg = &packet->GetMsg()->choice.gamePlayerMessage; + netPlayerMsg->gamePlayerNotification.present = gamePlayerNotification_PR_removedFromGame; + RemovedFromGame_t *removed = &netPlayerMsg->gamePlayerNotification.choice.removedFromGame; + + switch (reason) + { + case NTF_NET_REMOVED_GAME_FULL : + removed->removedFromGameReason = removedFromGameReason_gameIsFull; + break; + case NTF_NET_REMOVED_ALREADY_RUNNING : + removed->removedFromGameReason = removedFromGameReason_gameIsRunning; + break; + case NTF_NET_REMOVED_KICKED : + removed->removedFromGameReason = removedFromGameReason_kickedFromGame; + break; + case NTF_NET_REMOVED_TIMEOUT : + removed->removedFromGameReason = removedFromGameReason_gameTimeout; + break; + case NTF_NET_REMOVED_START_FAILED : + removed->removedFromGameReason = removedFromGameReason_removedStartFailed; + break; + default : + removed->removedFromGameReason = removedFromGameReason_removedOnRequest; + break; + } GetSender().Send(session.sessionData, packet); HandleReAddedSession(session); @@ -251,11 +274,15 @@ void ServerLobbyThread::NotifyPlayerJoinedGame(unsigned gameId, unsigned playerId) { // Send notification to players in lobby. - boost::shared_ptr packet(new NetPacketGameListPlayerJoined); - NetPacketGameListPlayerJoined::Data packetData; - packetData.gameId = gameId; - packetData.playerId = playerId; - static_cast(packet.get())->SetData(packetData); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_gameListMessage; + GameListMessage_t *netListMsg = &packet->GetMsg()->choice.gameListMessage; + netListMsg->gameId = gameId; + + netListMsg->gameListNotification.present = gameListNotification_PR_gameListPlayerJoined; + GameListPlayerJoined_t *playerJoined = &netListMsg->gameListNotification.choice.gameListPlayerJoined; + playerJoined->playerId = playerId; + m_sessionManager.SendLobbyMsgToAllSessions(GetSender(), packet, SessionData::Established); m_gameSessionManager.SendLobbyMsgToAllSessions(GetSender(), packet, SessionData::Game); } @@ -264,11 +291,15 @@ void ServerLobbyThread::NotifyPlayerLeftGame(unsigned gameId, unsigned playerId) { // Send notification to players in lobby. - boost::shared_ptr packet(new NetPacketGameListPlayerLeft); - NetPacketGameListPlayerLeft::Data packetData; - packetData.gameId = gameId; - packetData.playerId = playerId; - static_cast(packet.get())->SetData(packetData); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_gameListMessage; + GameListMessage_t *netListMsg = &packet->GetMsg()->choice.gameListMessage; + netListMsg->gameId = gameId; + + netListMsg->gameListNotification.present = gameListNotification_PR_gameListPlayerLeft; + GameListPlayerLeft_t *playerLeft = &netListMsg->gameListNotification.choice.gameListPlayerLeft; + playerLeft->playerId = playerId; + m_sessionManager.SendLobbyMsgToAllSessions(GetSender(), packet, SessionData::Established); m_gameSessionManager.SendLobbyMsgToAllSessions(GetSender(), packet, SessionData::Game); } @@ -277,11 +308,15 @@ void ServerLobbyThread::NotifyGameAdminChanged(unsigned gameId, unsigned newAdminPlayerId) { // Send notification to players in lobby. - boost::shared_ptr packet(new NetPacketGameListAdminChanged); - NetPacketGameListAdminChanged::Data packetData; - packetData.gameId = gameId; - packetData.newAdminplayerId = newAdminPlayerId; - static_cast(packet.get())->SetData(packetData); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_gameListMessage; + GameListMessage_t *netListMsg = &packet->GetMsg()->choice.gameListMessage; + netListMsg->gameId = gameId; + + netListMsg->gameListNotification.present = gameListNotification_PR_gameListAdminChanged; + GameListAdminChanged_t *adminChanged = &netListMsg->gameListNotification.choice.gameListAdminChanged; + adminChanged->newAdminPlayerId = newAdminPlayerId; + m_sessionManager.SendLobbyMsgToAllSessions(GetSender(), packet, SessionData::Established); m_gameSessionManager.SendLobbyMsgToAllSessions(GetSender(), packet, SessionData::Game); } @@ -303,17 +338,17 @@ ServerLobbyThread::NotifyReopeningGame(unsigned gameId) } void -ServerLobbyThread::HandleGameRetrievePlayerInfo(SessionWrapper session, const NetPacketRetrievePlayerInfo &tmpPacket) +ServerLobbyThread::HandleGameRetrievePlayerInfo(SessionWrapper session, const PlayerInfoRequestMessage_t &playerInfoRequest) { // Someone within a game requested player info. - HandleNetPacketRetrievePlayerInfo(session, tmpPacket); + HandleNetPacketRetrievePlayerInfo(session, playerInfoRequest); } void -ServerLobbyThread::HandleGameRetrieveAvatar(SessionWrapper session, const NetPacketRetrieveAvatar &tmpPacket) +ServerLobbyThread::HandleGameRetrieveAvatar(SessionWrapper session, const AvatarRequestMessage_t &retrieveAvatar) { // Someone within a game requested an avatar. - HandleNetPacketRetrieveAvatar(session, tmpPacket); + HandleNetPacketRetrieveAvatar(session, retrieveAvatar); } bool @@ -357,22 +392,32 @@ ServerLobbyThread::RemovePlayer(unsigned playerId, unsigned errorCode) void ServerLobbyThread::SendGlobalChat(const string &message) { - boost::shared_ptr outChat(new NetPacketChatText); - NetPacketChatText::Data outChatData; - outChatData.playerId = 0; - outChatData.text = message; - static_cast(outChat.get())->SetData(outChatData); - m_gameSessionManager.SendToAllSessions(GetSender(), outChat, SessionData::Game); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_chatMessage; + ChatMessage_t *netChat = &packet->GetMsg()->choice.chatMessage; + // TODO proper game id handling. + netChat->gameId = 0; + netChat->playerId = 0; + OCTET_STRING_fromBuf( + &netChat->chatText, + message.c_str(), + message.length()); + + m_gameSessionManager.SendToAllSessions(GetSender(), packet, SessionData::Game); } void ServerLobbyThread::SendGlobalMsgBox(const string &message) { - boost::shared_ptr outMsg(new NetPacketMsgBoxText); - NetPacketMsgBoxText::Data outMsgData; - outMsgData.text = message; - static_cast(outMsg.get())->SetData(outMsgData); - m_gameSessionManager.SendToAllSessions(GetSender(), outMsg, SessionData::Game); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_dialogMessage; + DialogMessage_t *netDialog = &packet->GetMsg()->choice.dialogMessage; + + OCTET_STRING_fromBuf( + &netDialog->notificationText, + message.c_str(), + message.length()); + m_gameSessionManager.SendToAllSessions(GetSender(), packet, SessionData::Game); } void @@ -626,40 +671,63 @@ ServerLobbyThread::HandlePacket(SessionWrapper session, boost::shared_ptrGetState() == SessionData::Init) { - if (packet->ToNetPacketInit()) - HandleNetPacketInit(session, *packet->ToNetPacketInit()); - else if (packet->ToNetPacketAvatarHeader()) - HandleNetPacketAvatarHeader(session, *packet->ToNetPacketAvatarHeader()); - else if (packet->ToNetPacketUnknownAvatar()) - HandleNetPacketUnknownAvatar(session, *packet->ToNetPacketUnknownAvatar()); + if (packet->GetMsg()->present == PokerTHMessage_PR_initMessage) + HandleNetPacketInit(session, packet->GetMsg()->choice.initMessage); + else if (packet->GetMsg()->present == PokerTHMessage_PR_avatarReplyMessage) + { + AvatarReplyMessage_t *avatarReply = &packet->GetMsg()->choice.avatarReplyMessage; + if (avatarReply->avatarResult.present == avatarResult_PR_avatarHeader) + HandleNetPacketAvatarHeader(session, avatarReply->requestId, avatarReply->avatarResult.choice.avatarHeader); + else if (avatarReply->avatarResult.present == avatarResult_PR_unknownAvatar) + HandleNetPacketUnknownAvatar(session, avatarReply->requestId, avatarReply->avatarResult.choice.unknownAvatar); + else + SessionError(session, ERR_SOCK_INVALID_STATE); + } else SessionError(session, ERR_SOCK_INVALID_STATE); } else if (session.sessionData->GetState() == SessionData::ReceivingAvatar) { - if (packet->ToNetPacketAvatarFile()) - HandleNetPacketAvatarFile(session, *packet->ToNetPacketAvatarFile()); - else if (packet->ToNetPacketAvatarEnd()) - HandleNetPacketAvatarEnd(session, *packet->ToNetPacketAvatarEnd()); + if (packet->GetMsg()->present == PokerTHMessage_PR_avatarReplyMessage) + { + AvatarReplyMessage_t *avatarReply = &packet->GetMsg()->choice.avatarReplyMessage; + if (avatarReply->avatarResult.present == avatarResult_PR_avatarData) + HandleNetPacketAvatarFile(session, avatarReply->requestId, avatarReply->avatarResult.choice.avatarData); + else if (avatarReply->avatarResult.present == avatarResult_PR_avatarEnd) + HandleNetPacketAvatarEnd(session, avatarReply->requestId, avatarReply->avatarResult.choice.avatarEnd); + else + SessionError(session, ERR_SOCK_INVALID_STATE); + } else SessionError(session, ERR_SOCK_INVALID_STATE); } else { - if (packet->ToNetPacketRetrievePlayerInfo()) - HandleNetPacketRetrievePlayerInfo(session, *packet->ToNetPacketRetrievePlayerInfo()); - else if (packet->ToNetPacketRetrieveAvatar()) - HandleNetPacketRetrieveAvatar(session, *packet->ToNetPacketRetrieveAvatar()); - else if (packet->ToNetPacketResetTimeout()) + if (packet->GetMsg()->present == PokerTHMessage_PR_playerInfoRequestMessage) + HandleNetPacketRetrievePlayerInfo(session, packet->GetMsg()->choice.playerInfoRequestMessage); + else if (packet->GetMsg()->present == PokerTHMessage_PR_avatarRequestMessage) + HandleNetPacketRetrieveAvatar(session, packet->GetMsg()->choice.avatarRequestMessage); + else if (packet->GetMsg()->present == PokerTHMessage_PR_resetTimeoutMessage) {} - else if (packet->ToNetPacketUnsubscribeGameList()) - session.sessionData->ResetWantsLobbyMsg(); - else if (packet->ToNetPacketResubscribeGameList()) - InternalResubscribeMsg(session); - else if (packet->ToNetPacketCreateGame()) - HandleNetPacketCreateGame(session, *packet->ToNetPacketCreateGame()); - else if (packet->ToNetPacketJoinGame()) - HandleNetPacketJoinGame(session, *packet->ToNetPacketJoinGame()); + else if (packet->GetMsg()->present == PokerTHMessage_PR_subscriptionRequestMessage) + { + SubscriptionRequestMessage_t *subscriptionRequest = &packet->GetMsg()->choice.subscriptionRequestMessage; + if (subscriptionRequest->subscriptionAction == subscriptionAction_resubscribeGameList) + InternalResubscribeMsg(session); + else + session.sessionData->ResetWantsLobbyMsg(); + } + else if (packet->GetMsg()->present == PokerTHMessage_PR_joinGameRequestMessage) + { + JoinGameRequestMessage_t *joinRequest = &packet->GetMsg()->choice.joinGameRequestMessage; + string password; + if (joinRequest->password) + password = string((char *)joinRequest->password->buf, joinRequest->password->size); + if (joinRequest->joinGameAction.present == joinGameAction_PR_joinNewGame) + HandleNetPacketCreateGame(session, password, joinRequest->joinGameAction.choice.joinNewGame); + else if (joinRequest->joinGameAction.present == joinGameAction_PR_joinExistingGame) + HandleNetPacketJoinGame(session, password, joinRequest->joinGameAction.choice.joinExistingGame); + } else SessionError(session, ERR_SOCK_INVALID_STATE); } @@ -667,52 +735,61 @@ ServerLobbyThread::HandlePacket(SessionWrapper session, boost::shared_ptrGetId() << "."); - NetPacketInit::Data initData; - tmpPacket.GetData(initData); - // Check the protocol version. - if (initData.versionMajor != NET_VERSION_MAJOR) + if (initMessage.requestedVersion.major != NET_VERSION_MAJOR) { SessionError(session, ERR_NET_VERSION_NOT_SUPPORTED); return; } - if (initData.versionMajor == 5 && (initData.versionMinor == 0 || initData.versionMinor == 1)) + if (initMessage.requestedVersion.major == 5 && (initMessage.requestedVersion.minor == 0 || initMessage.requestedVersion.minor == 1)) session.sessionData->SetMaxNumPlayers(7); else session.sessionData->SetMaxNumPlayers(MAX_NUMBER_OF_PLAYERS); + string playerName; + MD5Buf avatarMD5; + if (initMessage.login.present == login_PR_anonymousLogin) + { + const AnonymousLogin_t *anonLogin = &initMessage.login.choice.anonymousLogin; + playerName = string((const char *)anonLogin->playerName.buf, anonLogin->playerName.size); + if (anonLogin->avatar) + memcpy(avatarMD5.data, anonLogin->avatar->buf, MD5_DATA_SIZE); + } + else + SessionError(session, ERR_NET_INVALID_PASSWORD); // TODO not yet supported + // Check the server password. - if (!CheckPassword(initData.password)) +/* if (!CheckPassword(initData.password)) { SessionError(session, ERR_NET_INVALID_PASSWORD); return; - } + }*/ // Check whether the player name is correct. // Partly, this is also done in netpacket. // However, some disallowed names are checked only here. - if (initData.playerName.empty() || initData.playerName.size() > MAX_NAME_SIZE - || initData.playerName[0] == '#' - || initData.playerName[0] == ' ' - || initData.playerName.substr(0, sizeof(SERVER_COMPUTER_PLAYER_NAME) - 1) == SERVER_COMPUTER_PLAYER_NAME) + if (playerName.empty() + || playerName[0] == '#' + || playerName[0] == ' ' + || playerName.substr(0, sizeof(SERVER_COMPUTER_PLAYER_NAME) - 1) == SERVER_COMPUTER_PLAYER_NAME) { SessionError(session, ERR_NET_INVALID_PLAYER_NAME); return; } // Check whether this player is already connected. - if (IsPlayerConnected(initData.playerName)) + if (IsPlayerConnected(playerName)) { SessionError(session, ERR_NET_PLAYER_NAME_IN_USE); return; } // Check whether the player name is banned. - if (GetBanManager().IsPlayerBanned(initData.playerName)) + if (GetBanManager().IsPlayerBanned(playerName)) { SessionError(session, ERR_NET_PLAYER_BANNED); return; @@ -727,19 +804,17 @@ ServerLobbyThread::HandleNetPacketInit(SessionWrapper session, const NetPacketIn // Create player data object. boost::shared_ptr tmpPlayerData( new PlayerData(GetNextUniquePlayerId(), 0, PLAYER_TYPE_HUMAN, PLAYER_RIGHTS_NORMAL)); - tmpPlayerData->SetName(initData.playerName); + tmpPlayerData->SetName(playerName); tmpPlayerData->SetNetSessionData(session.sessionData); - if (initData.showAvatar) - tmpPlayerData->SetAvatarMD5(initData.avatar); + tmpPlayerData->SetAvatarMD5(avatarMD5); // Set player data for session. m_sessionManager.SetSessionPlayerData(session.sessionData->GetId(), tmpPlayerData); session.playerData = tmpPlayerData; string avatarFileName; - if (initData.showAvatar - && !initData.avatar.IsZero() - && !GetAvatarManager().GetAvatarFileName(initData.avatar, avatarFileName)) + if (!avatarMD5.IsZero() + && !GetAvatarManager().GetAvatarFileName(avatarMD5, avatarFileName)) { bool avatarRecentlyRequested = false; { @@ -761,22 +836,19 @@ ServerLobbyThread::HandleNetPacketInit(SessionWrapper session, const NetPacketIn } void -ServerLobbyThread::HandleNetPacketAvatarHeader(SessionWrapper session, const NetPacketAvatarHeader &tmpPacket) +ServerLobbyThread::HandleNetPacketAvatarHeader(SessionWrapper session, unsigned /*requestId*/, const AvatarHeader_t &avatarHeader) { if (session.playerData.get()) { - NetPacketAvatarHeader::Data headerData; - tmpPacket.GetData(headerData); - - if (headerData.avatarFileSize >= MIN_AVATAR_FILE_SIZE && headerData.avatarFileSize <= MAX_AVATAR_FILE_SIZE) + if (avatarHeader.avatarSize >= MIN_AVATAR_FILE_SIZE && avatarHeader.avatarSize <= MAX_AVATAR_FILE_SIZE) { - boost::shared_ptr tmpAvatarData(new AvatarData); - tmpAvatarData->fileData.reserve(headerData.avatarFileSize); - tmpAvatarData->fileType = headerData.avatarFileType; - tmpAvatarData->reportedSize = headerData.avatarFileSize; + boost::shared_ptr tmpAvatarFile(new AvatarFile); + tmpAvatarFile->fileData.reserve(avatarHeader.avatarSize); + tmpAvatarFile->fileType = static_cast(avatarHeader.avatarType); + tmpAvatarFile->reportedSize = avatarHeader.avatarSize; // Ignore request id for now. - session.playerData->SetNetAvatarData(tmpAvatarData); + session.playerData->SetNetAvatarFile(tmpAvatarFile); // Session is now receiving an avatar. session.sessionData->SetState(SessionData::ReceivingAvatar); @@ -787,12 +859,12 @@ ServerLobbyThread::HandleNetPacketAvatarHeader(SessionWrapper session, const Net } void -ServerLobbyThread::HandleNetPacketUnknownAvatar(SessionWrapper session, const NetPacketUnknownAvatar &/*tmpPacket*/) +ServerLobbyThread::HandleNetPacketUnknownAvatar(SessionWrapper session, unsigned /*requestId*/, const UnknownAvatar_t &/*unknownAvatar*/) { if (session.playerData.get()) { // Free memory (just in case). - session.playerData->SetNetAvatarData(boost::shared_ptr()); + session.playerData->SetNetAvatarFile(boost::shared_ptr()); session.playerData->SetAvatarMD5(MD5Buf()); // Start session. EstablishSession(session); @@ -800,27 +872,24 @@ ServerLobbyThread::HandleNetPacketUnknownAvatar(SessionWrapper session, const Ne } void -ServerLobbyThread::HandleNetPacketAvatarFile(SessionWrapper session, const NetPacketAvatarFile &tmpPacket) +ServerLobbyThread::HandleNetPacketAvatarFile(SessionWrapper session, unsigned /*requestId*/, const AvatarData_t &avatarData) { if (session.playerData.get()) { - NetPacketAvatarFile::Data data; - tmpPacket.GetData(data); - - boost::shared_ptr tmpAvatar = session.playerData->GetNetAvatarData(); - if (tmpAvatar.get() && tmpAvatar->fileData.size() + data.fileData.size() <= tmpAvatar->reportedSize) + boost::shared_ptr tmpAvatar = session.playerData->GetNetAvatarFile(); + if (tmpAvatar.get() && tmpAvatar->fileData.size() + avatarData.avatarBlock.size <= tmpAvatar->reportedSize) { - std::copy(data.fileData.begin(), data.fileData.end(), back_inserter(tmpAvatar->fileData)); + std::copy(&avatarData.avatarBlock.buf[0], &avatarData.avatarBlock.buf[avatarData.avatarBlock.size], back_inserter(tmpAvatar->fileData)); } } } void -ServerLobbyThread::HandleNetPacketAvatarEnd(SessionWrapper session, const NetPacketAvatarEnd &/*tmpPacket*/) +ServerLobbyThread::HandleNetPacketAvatarEnd(SessionWrapper session, unsigned /*requestId*/, const AvatarEnd_t &/*avatarEnd*/) { if (session.playerData.get()) { - boost::shared_ptr tmpAvatar = session.playerData->GetNetAvatarData(); + boost::shared_ptr tmpAvatar = session.playerData->GetNetAvatarFile(); MD5Buf avatarMD5 = session.playerData->GetAvatarMD5(); if (!avatarMD5.IsZero() && tmpAvatar.get()) { @@ -834,7 +903,7 @@ ServerLobbyThread::HandleNetPacketAvatarEnd(SessionWrapper session, const NetPac } // Free memory. - session.playerData->SetNetAvatarData(boost::shared_ptr()); + session.playerData->SetNetAvatarFile(boost::shared_ptr()); // Set avatar file name. string avatarFileName; if (GetAvatarManager().GetAvatarFileName(avatarMD5, avatarFileName)) @@ -851,63 +920,66 @@ ServerLobbyThread::HandleNetPacketAvatarEnd(SessionWrapper session, const NetPac } void -ServerLobbyThread::HandleNetPacketRetrievePlayerInfo(SessionWrapper session, const NetPacketRetrievePlayerInfo &tmpPacket) +ServerLobbyThread::HandleNetPacketRetrievePlayerInfo(SessionWrapper session, const PlayerInfoRequestMessage_t &playerInfoRequest) { - NetPacketRetrievePlayerInfo::Data request; - tmpPacket.GetData(request); - // Find player in lobby or in a game. - boost::shared_ptr tmpPlayer = m_sessionManager.GetSessionByUniquePlayerId(request.playerId).playerData; + boost::shared_ptr tmpPlayer = m_sessionManager.GetSessionByUniquePlayerId(playerInfoRequest.playerId).playerData; if (!tmpPlayer.get()) - tmpPlayer = m_gameSessionManager.GetSessionByUniquePlayerId(request.playerId).playerData; + tmpPlayer = m_gameSessionManager.GetSessionByUniquePlayerId(playerInfoRequest.playerId).playerData; if (!tmpPlayer.get()) { boost::mutex::scoped_lock lock(m_computerPlayersMutex); - PlayerDataMap::const_iterator pos = m_computerPlayers.find(request.playerId); + PlayerDataMap::const_iterator pos = m_computerPlayers.find(playerInfoRequest.playerId); if (pos != m_computerPlayers.end()) tmpPlayer = pos->second; } + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_playerInfoReplyMessage; + PlayerInfoReplyMessage_t *netPlayerInfoReply = &packet->GetMsg()->choice.playerInfoReplyMessage; + netPlayerInfoReply->playerId = tmpPlayer->GetUniqueId(); + if (tmpPlayer.get()) { // Send player info to client. - boost::shared_ptr info(new NetPacketPlayerInfo); - NetPacketPlayerInfo::Data infoData; - infoData.playerId = tmpPlayer->GetUniqueId(); - infoData.playerInfo.ptype = tmpPlayer->GetType(); - infoData.playerInfo.playerName = tmpPlayer->GetName(); - infoData.playerInfo.hasAvatar = !tmpPlayer->GetAvatarMD5().IsZero(); - if (infoData.playerInfo.hasAvatar) + netPlayerInfoReply->playerInfoResult.present = playerInfoResult_PR_playerInfoData; + PlayerInfoData *data = &netPlayerInfoReply->playerInfoResult.choice.playerInfoData; + + data->isHuman = tmpPlayer->GetType() == PLAYER_TYPE_HUMAN; + OCTET_STRING_fromBuf( + &data->playerName, + tmpPlayer->GetName().c_str(), + tmpPlayer->GetName().length()); + if (!tmpPlayer->GetAvatarMD5().IsZero()) { - infoData.playerInfo.avatar = tmpPlayer->GetAvatarMD5(); - infoData.playerInfo.avatarType = AvatarManager::GetAvatarFileType(tmpPlayer->GetAvatarFile()); + data->avatarData = (struct PlayerInfoData::avatarData *)calloc(1, sizeof(struct PlayerInfoData::avatarData)); + data->avatarData->avatarType = static_cast(AvatarManager::GetAvatarFileType(tmpPlayer->GetAvatarFile())); + OCTET_STRING_fromBuf( + &data->avatarData->avatar, + (char *)tmpPlayer->GetAvatarMD5().data, + MD5_DATA_SIZE); } - static_cast(info.get())->SetData(infoData); - GetSender().Send(session.sessionData, info); } else { // Unknown player id - notify client. - boost::shared_ptr unknown(new NetPacketUnknownPlayerId); - NetPacketUnknownPlayerId::Data unknownData; - unknownData.playerId = request.playerId; - static_cast(unknown.get())->SetData(unknownData); - GetSender().Send(session.sessionData, unknown); + netPlayerInfoReply->playerInfoResult.present = playerInfoResult_PR_unknownPlayerInfo; } + GetSender().Send(session.sessionData, packet); } void -ServerLobbyThread::HandleNetPacketRetrieveAvatar(SessionWrapper session, const NetPacketRetrieveAvatar &tmpPacket) +ServerLobbyThread::HandleNetPacketRetrieveAvatar(SessionWrapper session, const AvatarRequestMessage_t &retrieveAvatar) { bool avatarFound = false; - NetPacketRetrieveAvatar::Data request; - tmpPacket.GetData(request); string tmpFile; - if (GetAvatarManager().GetAvatarFileName(request.avatar, tmpFile)) + MD5Buf tmpMD5; + memcpy(tmpMD5.data, retrieveAvatar.avatar.buf, MD5_DATA_SIZE); + if (GetAvatarManager().GetAvatarFileName(tmpMD5, tmpFile)) { NetPacketList tmpPackets; - if (GetAvatarManager().AvatarFileToNetPackets(tmpFile, request.requestId, tmpPackets) == 0) + if (GetAvatarManager().AvatarFileToNetPackets(tmpFile, retrieveAvatar.requestId, tmpPackets) == 0) { avatarFound = true; GetSender().Send(session.sessionData, tmpPackets); @@ -919,30 +991,31 @@ ServerLobbyThread::HandleNetPacketRetrieveAvatar(SessionWrapper session, const N if (!avatarFound) { // Notify client we didn't find the avatar. - boost::shared_ptr unknown(new NetPacketUnknownAvatar); - NetPacketUnknownAvatar::Data unknownData; - unknownData.requestId = request.requestId; - static_cast(unknown.get())->SetData(unknownData); - GetSender().Send(session.sessionData, unknown); + boost::shared_ptr unknownAvatar(new NetPacket(NetPacket::Alloc)); + unknownAvatar->GetMsg()->present = PokerTHMessage_PR_avatarReplyMessage; + AvatarReplyMessage_t *netAvatarReply = &unknownAvatar->GetMsg()->choice.avatarReplyMessage; + netAvatarReply->requestId = retrieveAvatar.requestId; + netAvatarReply->avatarResult.present = avatarResult_PR_unknownAvatar; + + GetSender().Send(session.sessionData, unknownAvatar); } } void -ServerLobbyThread::HandleNetPacketCreateGame(SessionWrapper session, const NetPacketCreateGame &tmpPacket) +ServerLobbyThread::HandleNetPacketCreateGame(SessionWrapper session, const std::string &password, const JoinNewGame_t &newGame) { LOG_VERBOSE("Creating new game, initiated by session #" << session.sessionData->GetId() << "."); // Create a new game. - NetPacketCreateGame::Data createGameData; - tmpPacket.GetData(createGameData); - + GameData tmpData; + NetPacket::GetGameData(&newGame.gameInfo, tmpData); boost::shared_ptr game( new ServerGame( shared_from_this(), GetNextGameId(), - createGameData.gameName, - createGameData.password, - createGameData.gameData, + string((char *)newGame.gameInfo.gameName.buf, newGame.gameInfo.gameName.size), + password, + tmpData, session.playerData->GetUniqueId(), GetGui(), m_playerConfig)); @@ -954,18 +1027,15 @@ ServerLobbyThread::HandleNetPacketCreateGame(SessionWrapper session, const NetPa } void -ServerLobbyThread::HandleNetPacketJoinGame(SessionWrapper session, const NetPacketJoinGame &tmpPacket) +ServerLobbyThread::HandleNetPacketJoinGame(SessionWrapper session, const std::string &password, const JoinExistingGame_t &joinGame) { // Join an existing game. - NetPacketJoinGame::Data joinGameData; - tmpPacket.GetData(joinGameData); - - GameMap::iterator pos = m_gameMap.find(joinGameData.gameId); + GameMap::iterator pos = m_gameMap.find(joinGame.gameId); if (pos != m_gameMap.end()) { ServerGame &game = *pos->second; - if (game.CheckPassword(joinGameData.password)) + if (game.CheckPassword(password)) { MoveSessionToGame(game, session); } @@ -976,6 +1046,7 @@ ServerLobbyThread::HandleNetPacketJoinGame(SessionWrapper session, const NetPack } else { + // TODO do not remove session SessionError(session, ERR_NET_UNKNOWN_GAME); } } @@ -986,14 +1057,15 @@ ServerLobbyThread::EstablishSession(SessionWrapper session) if (!session.playerData.get()) throw ServerException(__FILE__, __LINE__, ERR_NET_INVALID_SESSION, 0); // Send ACK to client. - boost::shared_ptr initAck(new NetPacketInitAck); - NetPacketInitAck::Data initAckData; - initAckData.latestGameVersion = POKERTH_VERSION; - initAckData.latestBetaRevision = POKERTH_BETA_REVISION; - initAckData.sessionId = session.sessionData->GetId(); // TODO: currently unused. - initAckData.playerId = session.playerData->GetUniqueId(); - static_cast(initAck.get())->SetData(initAckData); - GetSender().Send(session.sessionData, initAck); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_initAckMessage; + InitAckMessage_t *netInitAck = &packet->GetMsg()->choice.initAckMessage; + netInitAck->latestGameVersion.major = POKERTH_VERSION_MAJOR; + netInitAck->latestGameVersion.minor = POKERTH_VERSION_MINOR; + netInitAck->latestBetaRevision = POKERTH_BETA_REVISION; +// initAckData.sessionId = session.sessionData->GetId(); // TODO: currently unused. + netInitAck->yourPlayerId = session.playerData->GetUniqueId(); + GetSender().Send(session.sessionData, packet); // Send the game list to the client. SendGameList(session.sessionData); @@ -1020,12 +1092,15 @@ ServerLobbyThread::RequestPlayerAvatar(SessionWrapper session) m_timerAvatarClientAddressMap[session.sessionData->GetClientAddr()] = boost::timers::portable::microsec_timer(); } // Ask the client to send its avatar. - boost::shared_ptr retrieveAvatar(new NetPacketRetrieveAvatar); - NetPacketRetrieveAvatar::Data retrieveAvatarData; - retrieveAvatarData.requestId = session.playerData->GetUniqueId(); - retrieveAvatarData.avatar = session.playerData->GetAvatarMD5(); - static_cast(retrieveAvatar.get())->SetData(retrieveAvatarData); - GetSender().Send(session.sessionData, retrieveAvatar); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_avatarRequestMessage; + AvatarRequestMessage_t *netAvatarRequest = &packet->GetMsg()->choice.avatarRequestMessage; + netAvatarRequest->requestId = session.playerData->GetUniqueId(); + OCTET_STRING_fromBuf( + &netAvatarRequest->avatar, + (char *)session.playerData->GetAvatarMD5().data, + MD5_DATA_SIZE); + GetSender().Send(session.sessionData, packet); } void @@ -1236,17 +1311,16 @@ ServerLobbyThread::InternalResubscribeMsg(SessionWrapper session) session.sessionData->SetWantsLobbyMsg(); SendGameList(session.sessionData); // Send new statistics information. - boost::shared_ptr packet(new NetPacketStatisticsChanged); - NetPacketStatisticsChanged::Data statData; - statData.stats.numberOfPlayersOnServer = m_sessionManager.GetRawSessionCount() + m_gameSessionManager.GetRawSessionCount(); - try { - static_cast(packet.get())->SetData(statData); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_statisticsMessage; + StatisticsMessage_t *netStatistics = &packet->GetMsg()->choice.statisticsMessage; - GetSender().Send(session.sessionData, packet); - } catch (const NetException &) - { - // Ignore errors for now. - } + StatisticsData_t *data = (StatisticsData_t *)calloc(1, sizeof(struct StatisticsData)); + data->statisticsType = statisticsType_statNumberOfPlayers; + data->statisticsValue = m_sessionManager.GetRawSessionCount() + m_gameSessionManager.GetRawSessionCount(); + ASN_SEQUENCE_ADD(&netStatistics->statisticsData.list, data); + + GetSender().Send(session.sessionData, packet); } } @@ -1286,11 +1360,12 @@ ServerLobbyThread::InternalCheckSessionTimeouts(SessionWrapper session) && !session.sessionData->HasActivityNoticeBeenSent()) { session.sessionData->MarkActivityNotice(); - boost::shared_ptr packet(new NetPacketTimeoutWarning); - NetPacketTimeoutWarning::Data warningData; - warningData.timeoutReason = NETWORK_TIMEOUT_GENERIC; - warningData.remainingSeconds = SERVER_TIMEOUT_WARNING_REMAINING_SEC; - static_cast(packet.get())->SetData(warningData); + + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_timeoutWarningMessage; + TimeoutWarningMessage_t *netWarning = &packet->GetMsg()->choice.timeoutWarningMessage; + netWarning->timeoutReason = timeoutReason_timeoutNoDataReceived; + netWarning->remainingSeconds = SERVER_TIMEOUT_WARNING_REMAINING_SEC; GetSender().Send(session.sessionData, packet); } else if (session.sessionData->GetActivityTimerElapsedSec() >= SERVER_SESSION_ACTIVITY_TIMEOUT_SEC) @@ -1324,20 +1399,39 @@ void ServerLobbyThread::SendError(boost::shared_ptr s, int errorCode) { LOG_VERBOSE("Sending error code " << errorCode << " to session #" << s->GetId() << "."); - boost::shared_ptr packet(new NetPacketError); - NetPacketError::Data errorData; - errorData.errorCode = errorCode; - static_cast(packet.get())->SetData(errorData); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_errorMessage; + ErrorMessage_t *netError = &packet->GetMsg()->choice.errorMessage; + netError->errorReason = NetPacket::GameErrorToNetError(errorCode); GetSender().Send(s, packet); } void ServerLobbyThread::SendJoinGameFailed(boost::shared_ptr s, int reason) { - boost::shared_ptr packet(new NetPacketJoinGameFailed); - NetPacketJoinGameFailed::Data failedData; - failedData.failureCode = reason; - static_cast(packet.get())->SetData(failedData); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_joinGameReplyMessage; + JoinGameReplyMessage_t *netJoinReply = &packet->GetMsg()->choice.joinGameReplyMessage; + netJoinReply->gameId = s->GetGameId(); + + netJoinReply->joinGameResult.present = joinGameResult_PR_joinGameFailed; + JoinGameFailed_t *joinFailed = &netJoinReply->joinGameResult.choice.joinGameFailed; + + switch (reason) + { + case NTF_NET_JOIN_GAME_FULL : + joinFailed->joinGameFailureReason = joinGameFailureReason_gameIsFull; + break; + case NTF_NET_JOIN_ALREADY_RUNNING : + joinFailed->joinGameFailureReason = joinGameFailureReason_gameIsRunning; + break; + case NTF_NET_JOIN_INVALID_PASSWORD : + joinFailed->joinGameFailureReason = joinGameFailureReason_invalidPassword; + break; + default : + joinFailed->joinGameFailureReason = joinGameFailureReason_invalidGame; + break; + } GetSender().Send(s, packet); } @@ -1377,19 +1471,17 @@ ServerLobbyThread::BroadcastStatisticsUpdate(const ServerStats &stats) { if (stats.numberOfPlayersOnServer) { - boost::shared_ptr packet(new NetPacketStatisticsChanged); - NetPacketStatisticsChanged::Data statData; - statData.stats = stats; - try { - static_cast(packet.get())->SetData(statData); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_statisticsMessage; + StatisticsMessage_t *netStatistics = &packet->GetMsg()->choice.statisticsMessage; - m_sessionManager.SendLobbyMsgToAllSessions(GetSender(), packet, SessionData::Established); - m_gameSessionManager.SendLobbyMsgToAllSessions(GetSender(), packet, SessionData::Game); - } catch (const NetException &) - { - // Ignore errors for now. - //LOG_ERROR("ServerLobbyThread::BroadcastStatisticsUpdate: " << e.what()); - } + StatisticsData_t *data = (StatisticsData_t *)calloc(1, sizeof(struct StatisticsData)); + data->statisticsType = statisticsType_statNumberOfPlayers; + data->statisticsValue = m_sessionManager.GetRawSessionCount() + m_gameSessionManager.GetRawSessionCount(); + ASN_SEQUENCE_ADD(&netStatistics->statisticsData.list, data); + + m_sessionManager.SendLobbyMsgToAllSessions(GetSender(), packet, SessionData::Established); + m_gameSessionManager.SendLobbyMsgToAllSessions(GetSender(), packet, SessionData::Game); } } @@ -1499,27 +1591,48 @@ ServerLobbyThread::IsPlayerConnected(const string &name) const boost::shared_ptr ServerLobbyThread::CreateNetPacketGameListNew(const ServerGame &game) { - boost::shared_ptr packet(new NetPacketGameListNew); - NetPacketGameListNew::Data packetData; - packetData.gameId = game.GetId(); - packetData.gameInfo.adminPlayerId = game.GetAdminPlayerId(); - packetData.gameInfo.mode = game.IsRunning() ? GAME_MODE_STARTED : GAME_MODE_CREATED; - packetData.gameInfo.name = game.GetName(); - packetData.gameInfo.data = game.GetGameData(); - packetData.gameInfo.players = game.GetPlayerIdList(); - packetData.gameInfo.isPasswordProtected = game.IsPasswordProtected(); - static_cast(packet.get())->SetData(packetData); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_gameListMessage; + GameListMessage_t *netGameList = &packet->GetMsg()->choice.gameListMessage; + netGameList->gameId = game.GetId(); + + netGameList->gameListNotification.present = gameListNotification_PR_gameListNew; + GameListNew_t *gameNew = &netGameList->gameListNotification.choice.gameListNew; + gameNew->adminPlayerId = game.GetAdminPlayerId(); + gameNew->gameMode = game.IsRunning() ? NetGameMode_gameStarted : NetGameMode_gameCreated; + NetPacket::SetGameData(game.GetGameData(), &gameNew->gameInfo); + OCTET_STRING_fromBuf( + &gameNew->gameInfo.gameName, + game.GetName().c_str(), + game.GetName().length()); + gameNew->isPrivate = game.IsPasswordProtected(); + + PlayerIdList tmpList = game.GetPlayerIdList(); + PlayerIdList::const_iterator i = tmpList.begin(); + PlayerIdList::const_iterator end = tmpList.end(); + while (i != end) + { + NonZeroId_t *playerId = (NonZeroId_t *)calloc(1, sizeof(NonZeroId_t)); + *playerId = *i; + ASN_SEQUENCE_ADD(&gameNew->playerIds.list, playerId); + ++i; + } + return packet; } boost::shared_ptr ServerLobbyThread::CreateNetPacketGameListUpdate(unsigned gameId, GameMode mode) { - boost::shared_ptr packet(new NetPacketGameListUpdate); - NetPacketGameListUpdate::Data packetData; - packetData.gameId = gameId; - packetData.gameMode = mode; - static_cast(packet.get())->SetData(packetData); + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_gameListMessage; + GameListMessage_t *netGameList = &packet->GetMsg()->choice.gameListMessage; + netGameList->gameId = gameId; + + netGameList->gameListNotification.present = gameListNotification_PR_gameListUpdate; + GameListUpdate_t *gameUpdate = &netGameList->gameListNotification.choice.gameListUpdate; + + gameUpdate->gameMode = mode; return packet; } diff --git a/src/net/common/sessionmanager.cpp b/src/net/common/sessionmanager.cpp index 081a160c..9aac68cd 100644 --- a/src/net/common/sessionmanager.cpp +++ b/src/net/common/sessionmanager.cpp @@ -307,7 +307,7 @@ SessionManager::SendToAllSessions(SenderHelper &sender, boost::shared_ptrsecond.sessionData->GetState() == state) - sender.Send(i->second.sessionData, boost::shared_ptr(packet->Clone())); + sender.Send(i->second.sessionData, packet); ++i; } } @@ -327,7 +327,7 @@ SessionManager::SendLobbyMsgToAllSessions(SenderHelper &sender, boost::shared_pt // Send each client (with a certain state) a copy of the packet. if (i->second.sessionData->GetState() == state && i->second.sessionData->WantsLobbyMsg()) - sender.Send(i->second.sessionData, boost::shared_ptr(packet->Clone())); + sender.Send(i->second.sessionData, packet); ++i; } } @@ -345,7 +345,7 @@ SessionManager::SendToAllButOneSessions(SenderHelper &sender, boost::shared_ptr< // Send each fully connected client but one a copy of the packet. if (i->second.sessionData->GetState() == state) if (i->first != except) - sender.Send(i->second.sessionData, boost::shared_ptr(packet->Clone())); + sender.Send(i->second.sessionData, packet); ++i; } } diff --git a/src/net/netpacket.h b/src/net/netpacket.h index 24b248f0..1cd27ad1 100644 --- a/src/net/netpacket.h +++ b/src/net/netpacket.h @@ -21,1554 +21,53 @@ #ifndef _NETPACKET_H_ #define _NETPACKET_H_ -#include #include -#include -#include -#include +#include #include -#include #define NET_VERSION_MAJOR 5 #define NET_VERSION_MINOR 2 -#define MIN_PACKET_SIZE 4 -#define MAX_PACKET_SIZE 268 +#define MAX_FILE_DATA_SIZE 256 +#define MAX_PACKET_SIZE 384 +#define MAX_CHAT_TEXT_SIZE 128 + +/*#define MIN_PACKET_SIZE 4 #define MAX_NAME_SIZE 64 #define MAX_PASSWORD_SIZE 64 #define MAX_NUM_MANUAL_BLINDS 30 -#define MAX_CHAT_TEXT_SIZE 128 -#define MAX_FILE_DATA_SIZE 256 #define MAX_NUM_PLAYER_RESULTS MAX_NUMBER_OF_PLAYERS -#define MAX_NUM_PLAYER_CARDS MAX_NUMBER_OF_PLAYERS +#define MAX_NUM_PLAYER_CARDS MAX_NUMBER_OF_PLAYERS*/ +#define STL_STRING_FROM_OCTET_STRING(_a) (string((const char *)_a.buf, _a.size)) -struct NetPacketHeader; - -class NetPacketInit; -class NetPacketInitAck; -class NetPacketRetrieveAvatar; -class NetPacketAvatarHeader; -class NetPacketAvatarFile; -class NetPacketAvatarEnd; -class NetPacketUnknownAvatar; -class NetPacketGameListNew; -class NetPacketGameListUpdate; -class NetPacketGameListPlayerJoined; -class NetPacketGameListPlayerLeft; -class NetPacketGameListAdminChanged; -class NetPacketRetrievePlayerInfo; -class NetPacketPlayerInfo; -class NetPacketUnknownPlayerId; -class NetPacketUnsubscribeGameList; -class NetPacketResubscribeGameList; -class NetPacketCreateGame; -class NetPacketJoinGame; -class NetPacketJoinGameAck; -class NetPacketJoinGameFailed; -class NetPacketPlayerJoined; -class NetPacketPlayerLeft; -class NetPacketGameAdminChanged; -class NetPacketKickPlayer; -class NetPacketLeaveCurrentGame; -class NetPacketStartEvent; -class NetPacketStartEventAck; -class NetPacketGameStart; -class NetPacketHandStart; -class NetPacketPlayersTurn; -class NetPacketPlayersAction; -class NetPacketPlayersActionDone; -class NetPacketPlayersActionRejected; -class NetPacketDealFlopCards; -class NetPacketDealTurnCard; -class NetPacketDealRiverCard; -class NetPacketAllInShowCards; -class NetPacketEndOfHandShowCards; -class NetPacketEndOfHandHideCards; -class NetPacketEndOfGame; -class NetPacketAskKickPlayer; -class NetPacketAskKickPlayerDenied; -class NetPacketStartKickPlayerPetition; -class NetPacketVoteKickPlayer; -class NetPacketVoteKickPlayerAck; -class NetPacketVoteKickPlayerDenied; -class NetPacketKickPlayerPetitionUpdate; -class NetPacketEndKickPlayerPetition; -class NetPacketStatisticsChanged; -class NetPacketRemovedFromGame; -class NetPacketTimeoutWarning; -class NetPacketResetTimeout; -class NetPacketSendChatText; -class NetPacketChatText; -class NetPacketMsgBoxText; -class NetPacketError; - +// This is just a wrapper class for the ASN.1 structure. class NetPacket { public: + enum MemAllocType { NoAlloc = 0, Alloc }; + NetPacket(MemAllocType alloc = NoAlloc); + ~NetPacket(); + static boost::shared_ptr Create(char *data, unsigned &dataSize); - NetPacket(u_int16_t type, u_int16_t initialSize, u_int16_t maxSize); - virtual ~NetPacket(); + const PokerTHMessage_t *GetMsg() const {return m_msg;} + PokerTHMessage_t *GetMsg() {return m_msg;} - virtual boost::shared_ptr Clone() const = 0; + bool IsClientActivity() const; - const NetPacketHeader *GetRawData() const; - NetPacketHeader *GetRawData(); - void SetRawData(const NetPacketHeader *p); + static void SetGameData(const GameData &inData, NetGameInfo_t *outData); + static void GetGameData(const NetGameInfo_t *inData, GameData &outData); - u_int16_t GetType() const; - u_int16_t GetLen() const; - - virtual const NetPacketInit *ToNetPacketInit() const; - virtual const NetPacketInitAck *ToNetPacketInitAck() const; - virtual const NetPacketRetrieveAvatar *ToNetPacketRetrieveAvatar() const; - virtual const NetPacketAvatarHeader *ToNetPacketAvatarHeader() const; - virtual const NetPacketAvatarFile *ToNetPacketAvatarFile() const; - virtual const NetPacketAvatarEnd *ToNetPacketAvatarEnd() const; - virtual const NetPacketUnknownAvatar *ToNetPacketUnknownAvatar() const; - virtual const NetPacketGameListNew *ToNetPacketGameListNew() const; - virtual const NetPacketGameListUpdate *ToNetPacketGameListUpdate() const; - virtual const NetPacketGameListPlayerJoined *ToNetPacketGameListPlayerJoined() const; - virtual const NetPacketGameListPlayerLeft *ToNetPacketGameListPlayerLeft() const; - virtual const NetPacketGameListAdminChanged *ToNetPacketGameListAdminChanged() const; - virtual const NetPacketRetrievePlayerInfo *ToNetPacketRetrievePlayerInfo() const; - virtual const NetPacketPlayerInfo *ToNetPacketPlayerInfo() const; - virtual const NetPacketUnknownPlayerId *ToNetPacketUnknownPlayerId() const; - virtual const NetPacketUnsubscribeGameList *ToNetPacketUnsubscribeGameList() const; - virtual const NetPacketResubscribeGameList *ToNetPacketResubscribeGameList() const; - virtual const NetPacketCreateGame *ToNetPacketCreateGame() const; - virtual const NetPacketJoinGame *ToNetPacketJoinGame() const; - virtual const NetPacketJoinGameAck *ToNetPacketJoinGameAck() const; - virtual const NetPacketJoinGameFailed *ToNetPacketJoinGameFailed() const; - virtual const NetPacketPlayerJoined *ToNetPacketPlayerJoined() const; - virtual const NetPacketPlayerLeft *ToNetPacketPlayerLeft() const; - virtual const NetPacketGameAdminChanged *ToNetPacketGameAdminChanged() const; - virtual const NetPacketKickPlayer *ToNetPacketKickPlayer() const; - virtual const NetPacketLeaveCurrentGame *ToNetPacketLeaveCurrentGame() const; - virtual const NetPacketStartEvent *ToNetPacketStartEvent() const; - virtual const NetPacketStartEventAck *ToNetPacketStartEventAck() const; - virtual const NetPacketGameStart *ToNetPacketGameStart() const; - virtual const NetPacketHandStart *ToNetPacketHandStart() const; - virtual const NetPacketPlayersTurn *ToNetPacketPlayersTurn() const; - virtual const NetPacketPlayersAction *ToNetPacketPlayersAction() const; - virtual const NetPacketPlayersActionDone *ToNetPacketPlayersActionDone() const; - virtual const NetPacketPlayersActionRejected *ToNetPacketPlayersActionRejected() const; - virtual const NetPacketDealFlopCards *ToNetPacketDealFlopCards() const; - virtual const NetPacketDealTurnCard *ToNetPacketDealTurnCard() const; - virtual const NetPacketDealRiverCard *ToNetPacketDealRiverCard() const; - virtual const NetPacketAllInShowCards *ToNetPacketAllInShowCards() const; - virtual const NetPacketEndOfHandShowCards *ToNetPacketEndOfHandShowCards() const; - virtual const NetPacketEndOfHandHideCards *ToNetPacketEndOfHandHideCards() const; - virtual const NetPacketEndOfGame *ToNetPacketEndOfGame() const; - virtual const NetPacketAskKickPlayer *ToNetPacketAskKickPlayer() const; - virtual const NetPacketAskKickPlayerDenied *ToNetPacketAskKickPlayerDenied() const; - virtual const NetPacketStartKickPlayerPetition *ToNetPacketStartKickPlayerPetition() const; - virtual const NetPacketVoteKickPlayer *ToNetPacketVoteKickPlayer() const; - virtual const NetPacketVoteKickPlayerAck *ToNetPacketVoteKickPlayerAck() const; - virtual const NetPacketVoteKickPlayerDenied *ToNetPacketVoteKickPlayerDenied() const; - virtual const NetPacketKickPlayerPetitionUpdate *ToNetPacketKickPlayerPetitionUpdate() const; - virtual const NetPacketEndKickPlayerPetition *ToNetPacketEndKickPlayerPetition() const; - virtual const NetPacketStatisticsChanged *ToNetPacketStatisticsChanged() const; - virtual const NetPacketRemovedFromGame *ToNetPacketRemovedFromGame() const; - virtual const NetPacketTimeoutWarning *ToNetPacketTimeoutWarning() const; - virtual const NetPacketResetTimeout *ToNetPacketResetTimeout() const; - virtual const NetPacketSendChatText *ToNetPacketSendChatText() const; - virtual const NetPacketChatText *ToNetPacketChatText() const; - virtual const NetPacketMsgBoxText *ToNetPacketMsgBoxText() const; - virtual const NetPacketError *ToNetPacketError() const; - - virtual bool IsClientActivity() const {return false;} - -protected: - - NetPacket &operator=(const NetPacket& right); // not allowed - - void Check(const NetPacketHeader *data) const; - virtual void InternalCheck(const NetPacketHeader *data) const = 0; - - void Resize(u_int16_t newLen); + static int NetErrorToGameError(long netErrorReason); + static long GameErrorToNetError(int gameErrorReason); private: - - NetPacketHeader *m_data; - const u_int16_t m_initialSize; - const u_int16_t m_maxSize; + PokerTHMessage_t *m_msg; }; typedef std::list > NetPacketList; -class NetPacketInit : public NetPacket -{ -public: - struct Data - { - int versionMajor; - int versionMinor; - std::string playerName; - std::string password; - bool showAvatar; - MD5Buf avatar; - }; - - NetPacketInit(); - virtual ~NetPacketInit(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketInit *ToNetPacketInit() const; - virtual bool IsClientActivity() const {return true;} - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketInitAck : public NetPacket -{ -public: - struct Data - { - u_int16_t latestGameVersion; - u_int16_t latestBetaRevision; - u_int32_t sessionId; - u_int32_t playerId; - }; - - NetPacketInitAck(); - virtual ~NetPacketInitAck(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketInitAck *ToNetPacketInitAck() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketRetrieveAvatar : public NetPacket -{ -public: - struct Data - { - u_int32_t requestId; - MD5Buf avatar; - }; - - NetPacketRetrieveAvatar(); - virtual ~NetPacketRetrieveAvatar(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketRetrieveAvatar *ToNetPacketRetrieveAvatar() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketAvatarHeader : public NetPacket -{ -public: - struct Data - { - u_int32_t requestId; - u_int32_t avatarFileSize; - AvatarFileType avatarFileType; - }; - - NetPacketAvatarHeader(); - virtual ~NetPacketAvatarHeader(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketAvatarHeader *ToNetPacketAvatarHeader() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketAvatarFile : public NetPacket -{ -public: - struct Data - { - u_int32_t requestId; - std::vector fileData; - }; - - NetPacketAvatarFile(); - virtual ~NetPacketAvatarFile(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketAvatarFile *ToNetPacketAvatarFile() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketAvatarEnd : public NetPacket -{ -public: - struct Data - { - u_int32_t requestId; - }; - - NetPacketAvatarEnd(); - virtual ~NetPacketAvatarEnd(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketAvatarEnd *ToNetPacketAvatarEnd() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketUnknownAvatar : public NetPacket -{ -public: - struct Data - { - u_int32_t requestId; - }; - - NetPacketUnknownAvatar(); - virtual ~NetPacketUnknownAvatar(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketUnknownAvatar *ToNetPacketUnknownAvatar() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketGameListNew : public NetPacket -{ -public: - struct Data - { - u_int32_t gameId; - GameInfo gameInfo; - }; - - NetPacketGameListNew(); - virtual ~NetPacketGameListNew(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketGameListNew *ToNetPacketGameListNew() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketGameListUpdate : public NetPacket -{ -public: - struct Data - { - u_int32_t gameId; - GameMode gameMode; - }; - - NetPacketGameListUpdate(); - virtual ~NetPacketGameListUpdate(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketGameListUpdate *ToNetPacketGameListUpdate() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketGameListPlayerJoined : public NetPacket -{ -public: - struct Data - { - u_int32_t gameId; - u_int32_t playerId; - }; - - NetPacketGameListPlayerJoined(); - virtual ~NetPacketGameListPlayerJoined(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketGameListPlayerJoined *ToNetPacketGameListPlayerJoined() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketGameListPlayerLeft : public NetPacket -{ -public: - struct Data - { - u_int32_t gameId; - u_int32_t playerId; - }; - - NetPacketGameListPlayerLeft(); - virtual ~NetPacketGameListPlayerLeft(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketGameListPlayerLeft *ToNetPacketGameListPlayerLeft() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketGameListAdminChanged : public NetPacket -{ -public: - struct Data - { - u_int32_t gameId; - u_int32_t newAdminplayerId; - }; - - NetPacketGameListAdminChanged(); - virtual ~NetPacketGameListAdminChanged(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketGameListAdminChanged *ToNetPacketGameListAdminChanged() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketRetrievePlayerInfo : public NetPacket -{ -public: - struct Data - { - u_int32_t playerId; - }; - - NetPacketRetrievePlayerInfo(); - virtual ~NetPacketRetrievePlayerInfo(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketRetrievePlayerInfo *ToNetPacketRetrievePlayerInfo() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketPlayerInfo : public NetPacket -{ -public: - struct Data - { - u_int32_t playerId; - PlayerInfo playerInfo; - }; - - NetPacketPlayerInfo(); - virtual ~NetPacketPlayerInfo(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketPlayerInfo *ToNetPacketPlayerInfo() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketUnknownPlayerId : public NetPacket -{ -public: - struct Data - { - u_int32_t playerId; - }; - - NetPacketUnknownPlayerId(); - virtual ~NetPacketUnknownPlayerId(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketUnknownPlayerId *ToNetPacketUnknownPlayerId() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketUnsubscribeGameList : public NetPacket -{ -public: - - NetPacketUnsubscribeGameList(); - virtual ~NetPacketUnsubscribeGameList(); - - virtual boost::shared_ptr Clone() const; - - virtual const NetPacketUnsubscribeGameList *ToNetPacketUnsubscribeGameList() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketResubscribeGameList : public NetPacket -{ -public: - - NetPacketResubscribeGameList(); - virtual ~NetPacketResubscribeGameList(); - - virtual boost::shared_ptr Clone() const; - - virtual const NetPacketResubscribeGameList *ToNetPacketResubscribeGameList() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketCreateGame : public NetPacket -{ -public: - struct Data - { - std::string gameName; - std::string password; - GameData gameData; - }; - - NetPacketCreateGame(); - virtual ~NetPacketCreateGame(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketCreateGame *ToNetPacketCreateGame() const; - virtual bool IsClientActivity() const {return true;} - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketJoinGame : public NetPacket -{ -public: - struct Data - { - u_int32_t gameId; - std::string password; - }; - - NetPacketJoinGame(); - virtual ~NetPacketJoinGame(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketJoinGame *ToNetPacketJoinGame() const; - virtual bool IsClientActivity() const {return true;} - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketJoinGameAck : public NetPacket -{ -public: - struct Data - { - u_int32_t gameId; - GameData gameData; - PlayerRights prights; - }; - - NetPacketJoinGameAck(); - virtual ~NetPacketJoinGameAck(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketJoinGameAck *ToNetPacketJoinGameAck() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketJoinGameFailed : public NetPacket -{ -public: - struct Data - { - int failureCode; - }; - - NetPacketJoinGameFailed(); - virtual ~NetPacketJoinGameFailed(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketJoinGameFailed *ToNetPacketJoinGameFailed() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketPlayerJoined : public NetPacket -{ -public: - struct Data - { - u_int32_t playerId; - PlayerRights prights; - }; - - NetPacketPlayerJoined(); - virtual ~NetPacketPlayerJoined(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketPlayerJoined *ToNetPacketPlayerJoined() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketPlayerLeft : public NetPacket -{ -public: - struct Data - { - u_int32_t playerId; - u_int16_t removeReason; - }; - - NetPacketPlayerLeft(); - virtual ~NetPacketPlayerLeft(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketPlayerLeft *ToNetPacketPlayerLeft() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketGameAdminChanged : public NetPacket -{ -public: - struct Data - { - u_int32_t playerId; - }; - - NetPacketGameAdminChanged(); - virtual ~NetPacketGameAdminChanged(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketGameAdminChanged *ToNetPacketGameAdminChanged() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketKickPlayer : public NetPacket -{ -public: - struct Data - { - u_int32_t playerId; - }; - - NetPacketKickPlayer(); - virtual ~NetPacketKickPlayer(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketKickPlayer *ToNetPacketKickPlayer() const; - virtual bool IsClientActivity() const {return true;} - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketLeaveCurrentGame : public NetPacket -{ -public: - - NetPacketLeaveCurrentGame(); - virtual ~NetPacketLeaveCurrentGame(); - - virtual boost::shared_ptr Clone() const; - - virtual const NetPacketLeaveCurrentGame *ToNetPacketLeaveCurrentGame() const; - virtual bool IsClientActivity() const {return true;} - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketStartEvent : public NetPacket -{ -public: - struct Data - { - bool fillUpWithCpuPlayers; - }; - - NetPacketStartEvent(); - virtual ~NetPacketStartEvent(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketStartEvent *ToNetPacketStartEvent() const; - virtual bool IsClientActivity() const {return true;} - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketStartEventAck : public NetPacket -{ -public: - - NetPacketStartEventAck(); - virtual ~NetPacketStartEventAck(); - - virtual boost::shared_ptr Clone() const; - - virtual const NetPacketStartEventAck *ToNetPacketStartEventAck() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketGameStart : public NetPacket -{ -public: - - struct PlayerSlot - { - u_int32_t playerId; - }; - - typedef std::list PlayerSlotList; - - struct Data - { - StartData startData; - PlayerSlotList playerSlots; - }; - - NetPacketGameStart(); - virtual ~NetPacketGameStart(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketGameStart *ToNetPacketGameStart() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketHandStart : public NetPacket -{ -public: - struct Data - { - u_int16_t yourCards[2]; - u_int32_t smallBlind; - }; - - NetPacketHandStart(); - virtual ~NetPacketHandStart(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketHandStart *ToNetPacketHandStart() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketPlayersTurn : public NetPacket -{ -public: - struct Data - { - GameState gameState; - u_int32_t playerId; - }; - - NetPacketPlayersTurn(); - virtual ~NetPacketPlayersTurn(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketPlayersTurn *ToNetPacketPlayersTurn() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketPlayersAction : public NetPacket -{ -public: - struct Data - { - GameState gameState; - PlayerAction playerAction; - u_int32_t playerBet; - }; - - NetPacketPlayersAction(); - virtual ~NetPacketPlayersAction(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketPlayersAction *ToNetPacketPlayersAction() const; - virtual bool IsClientActivity() const {return true;} - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketPlayersActionDone : public NetPacket -{ -public: - struct Data - { - GameState gameState; - u_int32_t playerId; - PlayerAction playerAction; - u_int32_t totalPlayerBet; - u_int32_t playerMoney; - u_int32_t highestSet; - u_int32_t minimumRaise; - }; - - NetPacketPlayersActionDone(); - virtual ~NetPacketPlayersActionDone(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketPlayersActionDone *ToNetPacketPlayersActionDone() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketPlayersActionRejected : public NetPacket -{ -public: - - struct Data - { - GameState gameState; - PlayerAction playerAction; - u_int32_t playerBet; - PlayerActionCode rejectionReason; - }; - - NetPacketPlayersActionRejected(); - virtual ~NetPacketPlayersActionRejected(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketPlayersActionRejected *ToNetPacketPlayersActionRejected() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketDealFlopCards : public NetPacket -{ -public: - struct Data - { - u_int16_t flopCards[3]; - }; - - NetPacketDealFlopCards(); - virtual ~NetPacketDealFlopCards(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketDealFlopCards *ToNetPacketDealFlopCards() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketDealTurnCard : public NetPacket -{ -public: - struct Data - { - u_int16_t turnCard; - }; - - NetPacketDealTurnCard(); - virtual ~NetPacketDealTurnCard(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketDealTurnCard *ToNetPacketDealTurnCard() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketDealRiverCard : public NetPacket -{ -public: - struct Data - { - u_int16_t riverCard; - }; - - NetPacketDealRiverCard(); - virtual ~NetPacketDealRiverCard(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketDealRiverCard *ToNetPacketDealRiverCard() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketAllInShowCards : public NetPacket -{ -public: - struct PlayerCards - { - u_int32_t playerId; - u_int16_t cards[2]; - }; - - typedef std::list PlayerCardsList; - - struct Data - { - PlayerCardsList playerCards; - }; - - NetPacketAllInShowCards(); - virtual ~NetPacketAllInShowCards(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketAllInShowCards *ToNetPacketAllInShowCards() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketEndOfHandShowCards : public NetPacket -{ -public: - struct PlayerResult - { - u_int32_t playerId; - u_int16_t cards[2]; - u_int16_t bestHandPos[5]; - u_int32_t valueOfCards; - u_int32_t moneyWon; - u_int32_t playerMoney; - }; - - typedef std::list PlayerResultList; - - struct Data - { - PlayerResultList playerResults; - }; - - NetPacketEndOfHandShowCards(); - virtual ~NetPacketEndOfHandShowCards(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketEndOfHandShowCards *ToNetPacketEndOfHandShowCards() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketEndOfHandHideCards : public NetPacket -{ -public: - struct Data - { - u_int32_t playerId; - u_int32_t moneyWon; - u_int32_t playerMoney; - }; - - NetPacketEndOfHandHideCards(); - virtual ~NetPacketEndOfHandHideCards(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketEndOfHandHideCards *ToNetPacketEndOfHandHideCards() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketEndOfGame : public NetPacket -{ -public: - struct Data - { - u_int32_t winnerPlayerId; - }; - - NetPacketEndOfGame(); - virtual ~NetPacketEndOfGame(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketEndOfGame *ToNetPacketEndOfGame() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketAskKickPlayer : public NetPacket -{ -public: - struct Data - { - u_int32_t playerId; - }; - - NetPacketAskKickPlayer(); - virtual ~NetPacketAskKickPlayer(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketAskKickPlayer *ToNetPacketAskKickPlayer() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketAskKickPlayerDenied : public NetPacket -{ -public: - struct Data - { - u_int32_t playerId; - DenyKickPlayerReason denyReason; - }; - - NetPacketAskKickPlayerDenied(); - virtual ~NetPacketAskKickPlayerDenied(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketAskKickPlayerDenied *ToNetPacketAskKickPlayerDenied() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketStartKickPlayerPetition : public NetPacket -{ -public: - struct Data - { - u_int32_t petitionId; - u_int32_t proposingPlayerId; - u_int32_t kickPlayerId; - u_int16_t kickTimeoutSec; - u_int16_t numVotesNeededToKick; - }; - - NetPacketStartKickPlayerPetition(); - virtual ~NetPacketStartKickPlayerPetition(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketStartKickPlayerPetition *ToNetPacketStartKickPlayerPetition() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketVoteKickPlayer : public NetPacket -{ -public: - struct Data - { - u_int32_t petitionId; - KickVote vote; - }; - - NetPacketVoteKickPlayer(); - virtual ~NetPacketVoteKickPlayer(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketVoteKickPlayer *ToNetPacketVoteKickPlayer() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketVoteKickPlayerAck : public NetPacket -{ -public: - struct Data - { - u_int32_t petitionId; - }; - - NetPacketVoteKickPlayerAck(); - virtual ~NetPacketVoteKickPlayerAck(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketVoteKickPlayerAck *ToNetPacketVoteKickPlayerAck() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketVoteKickPlayerDenied : public NetPacket -{ -public: - struct Data - { - u_int32_t petitionId; - DenyVoteReason denyReason; - }; - - NetPacketVoteKickPlayerDenied(); - virtual ~NetPacketVoteKickPlayerDenied(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketVoteKickPlayerDenied *ToNetPacketVoteKickPlayerDenied() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketKickPlayerPetitionUpdate : public NetPacket -{ -public: - struct Data - { - u_int32_t petitionId; - u_int16_t numVotesAgainstKicking; - u_int16_t numVotesInFavourOfKicking; - u_int16_t numVotesNeededToKick; - }; - - NetPacketKickPlayerPetitionUpdate(); - virtual ~NetPacketKickPlayerPetitionUpdate(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketKickPlayerPetitionUpdate *ToNetPacketKickPlayerPetitionUpdate() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketEndKickPlayerPetition : public NetPacket -{ -public: - struct Data - { - u_int32_t petitionId; - u_int16_t numVotesAgainstKicking; - u_int16_t numVotesInFavourOfKicking; - bool playerKicked; - EndPetitionReason endReason; - }; - - NetPacketEndKickPlayerPetition(); - virtual ~NetPacketEndKickPlayerPetition(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketEndKickPlayerPetition *ToNetPacketEndKickPlayerPetition() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketStatisticsChanged : public NetPacket -{ -public: - struct Data - { - ServerStats stats; - }; - - NetPacketStatisticsChanged(); - virtual ~NetPacketStatisticsChanged(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketStatisticsChanged *ToNetPacketStatisticsChanged() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketRemovedFromGame : public NetPacket -{ -public: - struct Data - { - u_int16_t removeReason; - }; - - NetPacketRemovedFromGame(); - virtual ~NetPacketRemovedFromGame(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketRemovedFromGame *ToNetPacketRemovedFromGame() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketTimeoutWarning : public NetPacket -{ -public: - struct Data - { - NetTimeoutReason timeoutReason; - u_int16_t remainingSeconds; - }; - - NetPacketTimeoutWarning(); - virtual ~NetPacketTimeoutWarning(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketTimeoutWarning *ToNetPacketTimeoutWarning() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketResetTimeout : public NetPacket -{ -public: - - NetPacketResetTimeout(); - virtual ~NetPacketResetTimeout(); - - virtual boost::shared_ptr Clone() const; - - virtual const NetPacketResetTimeout *ToNetPacketResetTimeout() const; - virtual bool IsClientActivity() const {return true;} - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketSendChatText : public NetPacket -{ -public: - struct Data - { - std::string text; - }; - - NetPacketSendChatText(); - virtual ~NetPacketSendChatText(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketSendChatText *ToNetPacketSendChatText() const; - virtual bool IsClientActivity() const {return true;} - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketChatText : public NetPacket -{ -public: - struct Data - { - u_int32_t playerId; - std::string text; - }; - - NetPacketChatText(); - virtual ~NetPacketChatText(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketChatText *ToNetPacketChatText() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketMsgBoxText : public NetPacket -{ -public: - struct Data - { - std::string text; - }; - - NetPacketMsgBoxText(); - virtual ~NetPacketMsgBoxText(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketMsgBoxText *ToNetPacketMsgBoxText() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - -class NetPacketError : public NetPacket -{ -public: - struct Data - { - int errorCode; - }; - - NetPacketError(); - virtual ~NetPacketError(); - - virtual boost::shared_ptr Clone() const; - - void SetData(const Data &inData); - void GetData(Data &outData) const; - - virtual const NetPacketError *ToNetPacketError() const; - -protected: - - virtual void InternalCheck(const NetPacketHeader* data) const; -}; - #endif diff --git a/src/net/senderhelper.h b/src/net/senderhelper.h index 6b6be0a7..156d34a7 100644 --- a/src/net/senderhelper.h +++ b/src/net/senderhelper.h @@ -39,6 +39,8 @@ public: void SignalSessionTerminated(unsigned sessionId); protected: + void InternalStorePacket(SendDataManager &tmpManager, boost::shared_ptr packet); + typedef std::map > SendQueueMap; private: diff --git a/src/net/servergamestate.h b/src/net/servergamestate.h index e8ecea7b..4dc57ef0 100644 --- a/src/net/servergamestate.h +++ b/src/net/servergamestate.h @@ -92,7 +92,7 @@ protected: virtual void InternalProcessPacket(boost::shared_ptr server, SessionWrapper session, boost::shared_ptr packet); - static boost::shared_ptr CreateNetPacketPlayerJoined(const PlayerData &playerData); + static boost::shared_ptr CreateNetPacketPlayerJoined(unsigned gameId, const PlayerData &playerData); private: static ServerGameStateInit s_state; diff --git a/src/net/serverlobbythread.h b/src/net/serverlobbythread.h index 200268ed..7c637d54 100644 --- a/src/net/serverlobbythread.h +++ b/src/net/serverlobbythread.h @@ -67,8 +67,8 @@ public: void NotifyStartingGame(unsigned gameId); void NotifyReopeningGame(unsigned gameId); - void HandleGameRetrievePlayerInfo(SessionWrapper session, const NetPacketRetrievePlayerInfo &tmpPacket); - void HandleGameRetrieveAvatar(SessionWrapper session, const NetPacketRetrieveAvatar &tmpPacket); + void HandleGameRetrievePlayerInfo(SessionWrapper session, const PlayerInfoRequestMessage_t &playerInfoRequest); + void HandleGameRetrieveAvatar(SessionWrapper session, const AvatarRequestMessage_t &retrieveAvatar); bool KickPlayerByName(const std::string &playerName); std::string GetPlayerIPAddress(const std::string &playerName) const; @@ -111,15 +111,15 @@ protected: void HandleRead(const boost::system::error_code &ec, SessionId sessionId, size_t bytesRead); void HandlePacket(SessionWrapper session, boost::shared_ptr packet); - void HandleNetPacketInit(SessionWrapper session, const NetPacketInit &tmpPacket); - void HandleNetPacketAvatarHeader(SessionWrapper session, const NetPacketAvatarHeader &tmpPacket); - void HandleNetPacketUnknownAvatar(SessionWrapper session, const NetPacketUnknownAvatar &tmpPacket); - void HandleNetPacketAvatarFile(SessionWrapper session, const NetPacketAvatarFile &tmpPacket); - void HandleNetPacketAvatarEnd(SessionWrapper session, const NetPacketAvatarEnd &tmpPacket); - void HandleNetPacketRetrievePlayerInfo(SessionWrapper session, const NetPacketRetrievePlayerInfo &tmpPacket); - void HandleNetPacketRetrieveAvatar(SessionWrapper session, const NetPacketRetrieveAvatar &tmpPacket); - void HandleNetPacketCreateGame(SessionWrapper session, const NetPacketCreateGame &tmpPacket); - void HandleNetPacketJoinGame(SessionWrapper session, const NetPacketJoinGame &tmpPacket); + void HandleNetPacketInit(SessionWrapper session, const InitMessage_t &initMessage); + void HandleNetPacketAvatarHeader(SessionWrapper session, unsigned requestId, const AvatarHeader_t &avatarHeader); + void HandleNetPacketUnknownAvatar(SessionWrapper session, unsigned requestId, const UnknownAvatar_t &unknownAvatar); + void HandleNetPacketAvatarFile(SessionWrapper session, unsigned requestId, const AvatarData_t &avatarData); + void HandleNetPacketAvatarEnd(SessionWrapper session, unsigned requestId, const AvatarEnd_t &avatarEnd); + void HandleNetPacketRetrievePlayerInfo(SessionWrapper session, const PlayerInfoRequestMessage_t &playerInfoRequest); + void HandleNetPacketRetrieveAvatar(SessionWrapper session, const AvatarRequestMessage_t &retrieveAvatar); + void HandleNetPacketCreateGame(SessionWrapper session, const std::string &password, const JoinNewGame_t &newGame); + void HandleNetPacketJoinGame(SessionWrapper session, const std::string &password, const JoinExistingGame_t &joinGame); void EstablishSession(SessionWrapper session); void RequestPlayerAvatar(SessionWrapper session); void TimerRemoveGame(const boost::system::error_code &ec); diff --git a/src/net/socket_msg.h b/src/net/socket_msg.h index 6a570758..50a2866f 100644 --- a/src/net/socket_msg.h +++ b/src/net/socket_msg.h @@ -104,9 +104,10 @@ #define NTF_NET_REMOVED_START_FAILED 207 // Notifications - join failed -#define NTF_NET_JOIN_GAME_FULL 210 -#define NTF_NET_JOIN_ALREADY_RUNNING 211 -#define NTF_NET_JOIN_INVALID_PASSWORD 212 +#define NTF_NET_JOIN_GAME_INVALID 210 +#define NTF_NET_JOIN_GAME_FULL 211 +#define NTF_NET_JOIN_ALREADY_RUNNING 212 +#define NTF_NET_JOIN_INVALID_PASSWORD 213 // Notifications - version #define NTF_NET_NEW_RELEASE_AVAILABLE 220 diff --git a/src/playerdata.cpp b/src/playerdata.cpp index 7e865977..8ab1dda8 100644 --- a/src/playerdata.cpp +++ b/src/playerdata.cpp @@ -92,16 +92,16 @@ PlayerData::SetNetSessionData(boost::shared_ptr session) m_netSessionData = session; } -boost::shared_ptr -PlayerData::GetNetAvatarData() const +boost::shared_ptr +PlayerData::GetNetAvatarFile() const { - return m_netAvatarData; + return m_netAvatarFile; } void -PlayerData::SetNetAvatarData(boost::shared_ptr avatarData) +PlayerData::SetNetAvatarFile(boost::shared_ptr AvatarFile) { - m_netAvatarData = avatarData; + m_netAvatarFile = AvatarFile; } PlayerType diff --git a/src/playerdata.h b/src/playerdata.h index 11a86502..43b67733 100644 --- a/src/playerdata.h +++ b/src/playerdata.h @@ -51,9 +51,9 @@ enum AvatarFileType AVATAR_FILE_TYPE_GIF }; -struct AvatarData +struct AvatarFile { - AvatarData() : fileType(AVATAR_FILE_TYPE_UNKNOWN), reportedSize(0) {} + AvatarFile() : fileType(AVATAR_FILE_TYPE_UNKNOWN), reportedSize(0) {} std::vector fileData; AvatarFileType fileType; unsigned reportedSize; @@ -84,8 +84,8 @@ public: void SetAvatarMD5(const MD5Buf &avatarMD5); boost::shared_ptr GetNetSessionData() const; void SetNetSessionData(boost::shared_ptr session); - boost::shared_ptr GetNetAvatarData() const; - void SetNetAvatarData(boost::shared_ptr avatarData); + boost::shared_ptr GetNetAvatarFile() const; + void SetNetAvatarFile(boost::shared_ptr AvatarFile); PlayerType GetType() const; void SetType(PlayerType type); PlayerRights GetRights() const; @@ -105,7 +105,7 @@ private: PlayerType m_type; PlayerRights m_rights; boost::shared_ptr m_netSessionData; - boost::shared_ptr m_netAvatarData; + boost::shared_ptr m_netAvatarFile; mutable boost::mutex m_dataMutex; }; diff --git a/src/pokerth.cpp b/src/pokerth.cpp index 39dd1887..9b5fb91d 100755 --- a/src/pokerth.cpp +++ b/src/pokerth.cpp @@ -18,12 +18,20 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include +#include +#include +#include #include +#include +#include #ifdef __APPLE__ #include #endif +#include + #include "session.h" #include "startwindowimpl.h" #include "configfile.h" @@ -31,14 +39,6 @@ #include "game_defs.h" #include -#include -#include -#include - -#include -#include -#include - #ifdef _MSC_VER #ifdef _DEBUG #define _CRTDBG_MAP_ALLOC diff --git a/src/session.cpp b/src/session.cpp index 114f7b52..29e9a89f 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -17,6 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include #include #include "session.h" #include "game.h"