1 /*****************************************************************************
2 * PokerTH - The open source texas holdem engine *
3 * Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May *
4 * *
5 * This program is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU Affero General Public License as *
7 * published by the Free Software Foundation, either version 3 of the *
8 * License, or (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU Affero General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU Affero General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 * *
18 * *
19 * Additional permission under GNU AGPL version 3 section 7 *
20 * *
21 * If you modify this program, or any covered work, by linking or *
22 * combining it with the OpenSSL project's OpenSSL library (or a *
23 * modified version of that library), containing parts covered by the *
24 * terms of the OpenSSL or SSLeay licenses, the authors of PokerTH *
25 * (Felix Hammer, Florian Thauer, Lothar May) grant you additional *
26 * permission to convey the resulting work. *
27 * Corresponding Source for a non-source form of such a combination *
28 * shall include the source code for the parts of OpenSSL used as well *
29 * as that of the covered work. *
30 *****************************************************************************/
31
32 #include <net/netpacket.h>
33 #include <net/socket_msg.h>
34
35 #include <boost/foreach.hpp>
36
37 using namespace std;
38
NetPacket()39 NetPacket::NetPacket()
40 {
41 m_msg = PokerTHMessage::default_instance().New();
42 }
43
NetPacket(PokerTHMessage * msg)44 NetPacket::NetPacket(PokerTHMessage *msg)
45 : m_msg(msg)
46 {
47 }
48
~NetPacket()49 NetPacket::~NetPacket()
50 {
51 delete m_msg;
52 }
53
54 boost::shared_ptr<NetPacket>
Create(const char * data,size_t dataSize)55 NetPacket::Create(const char *data, size_t dataSize)
56 {
57 boost::shared_ptr<NetPacket> tmpPacket;
58
59 // Check minimum requirements.
60 if (data && dataSize > 0) {
61 PokerTHMessage *msg = PokerTHMessage::default_instance().New();
62 if (msg->ParseFromArray(data, static_cast<int>(dataSize))) {
63 tmpPacket.reset(new NetPacket(msg));
64 } else {
65 delete msg;
66 }
67 }
68 return tmpPacket;
69 }
70
71 bool
IsClientActivity() const72 NetPacket::IsClientActivity() const
73 {
74 bool retVal = false;
75 if (m_msg &&
76 (m_msg->messagetype() == PokerTHMessage::Type_InitMessage
77 || m_msg->messagetype() == PokerTHMessage::Type_JoinNewGameMessage
78 || m_msg->messagetype() == PokerTHMessage::Type_JoinExistingGameMessage
79 || m_msg->messagetype() == PokerTHMessage::Type_RejoinExistingGameMessage
80 || m_msg->messagetype() == PokerTHMessage::Type_KickPlayerRequestMessage
81 || m_msg->messagetype() == PokerTHMessage::Type_LeaveGameRequestMessage
82 || m_msg->messagetype() == PokerTHMessage::Type_StartEventMessage
83 || m_msg->messagetype() == PokerTHMessage::Type_MyActionRequestMessage
84 || m_msg->messagetype() == PokerTHMessage::Type_ResetTimeoutMessage
85 || m_msg->messagetype() == PokerTHMessage::Type_ChatRequestMessage)) {
86 retVal = true;
87 }
88 return retVal;
89 }
90
91 string
ToString() const92 NetPacket::ToString() const
93 {
94 return m_msg ? m_msg->SerializeAsString() : "NULL";
95 }
96
97 void
SetGameData(const GameData & inData,NetGameInfo & outData)98 NetPacket::SetGameData(const GameData &inData, NetGameInfo &outData)
99 {
100 outData.set_netgametype(static_cast<NetGameInfo::NetGameType>(inData.gameType));
101 outData.set_allowspectators(inData.allowSpectators);
102 outData.set_maxnumplayers(inData.maxNumberOfPlayers);
103 outData.set_raiseintervalmode(static_cast<NetGameInfo::RaiseIntervalMode>(inData.raiseIntervalMode));
104 if (inData.raiseIntervalMode == RAISE_ON_HANDNUMBER) {
105 outData.set_raiseeveryhands(inData.raiseSmallBlindEveryHandsValue);
106 } else {
107 outData.set_raiseeveryminutes(inData.raiseSmallBlindEveryMinutesValue);
108 }
109 outData.set_endraisemode(static_cast<NetGameInfo::EndRaiseMode>(inData.afterManualBlindsMode));
110 outData.set_proposedguispeed(inData.guiSpeed);
111 outData.set_delaybetweenhands(inData.delayBetweenHandsSec);
112 outData.set_playeractiontimeout(inData.playerActionTimeoutSec);
113 outData.set_endraisesmallblindvalue(inData.afterMBAlwaysRaiseValue);
114 outData.set_firstsmallblind(inData.firstSmallBlind);
115 outData.set_startmoney(inData.startMoney);
116
117 BOOST_FOREACH(int manualBlind, inData.manualBlindsList) {
118 outData.add_manualblinds(manualBlind);
119 }
120 }
121
122 void
GetGameData(const NetGameInfo & inData,GameData & outData)123 NetPacket::GetGameData(const NetGameInfo &inData, GameData &outData)
124 {
125 int numManualBlinds = inData.manualblinds_size();
126
127 outData.gameType = static_cast<GameType>(inData.netgametype());
128 outData.allowSpectators = inData.allowspectators();
129 outData.maxNumberOfPlayers = inData.maxnumplayers();
130 outData.raiseIntervalMode = static_cast<RaiseIntervalMode>(inData.raiseintervalmode());
131 outData.raiseSmallBlindEveryHandsValue = outData.raiseSmallBlindEveryMinutesValue = 0;
132 if (outData.raiseIntervalMode == RAISE_ON_HANDNUMBER)
133 outData.raiseSmallBlindEveryHandsValue = inData.raiseeveryhands();
134 else
135 outData.raiseSmallBlindEveryMinutesValue = inData.raiseeveryminutes();
136 outData.raiseMode = numManualBlinds > 0 ? MANUAL_BLINDS_ORDER : DOUBLE_BLINDS;
137 outData.afterManualBlindsMode = static_cast<AfterManualBlindsMode>(inData.endraisemode());
138 outData.guiSpeed = inData.proposedguispeed();
139 outData.delayBetweenHandsSec = inData.delaybetweenhands();
140 outData.playerActionTimeoutSec = inData.playeractiontimeout();
141 outData.firstSmallBlind = inData.firstsmallblind();
142 outData.afterMBAlwaysRaiseValue = inData.endraisesmallblindvalue();
143 outData.startMoney = inData.startmoney();
144
145 for (int i = 0; i < numManualBlinds; i++) {
146 outData.manualBlindsList.push_back(static_cast<int>(inData.manualblinds(i)));
147 }
148 }
149
150 int
NetErrorToGameError(ErrorMessage::ErrorReason netErrorReason)151 NetPacket::NetErrorToGameError(ErrorMessage::ErrorReason netErrorReason)
152 {
153 int retVal;
154 switch(netErrorReason) {
155 case ErrorMessage::initVersionNotSupported :
156 retVal = ERR_NET_VERSION_NOT_SUPPORTED;
157 break;
158 case ErrorMessage::initServerFull :
159 retVal = ERR_NET_SERVER_FULL;
160 break;
161 case ErrorMessage::initAuthFailure :
162 retVal = ERR_NET_INVALID_PASSWORD;
163 break;
164 case ErrorMessage::initPlayerNameInUse :
165 retVal = ERR_NET_PLAYER_NAME_IN_USE;
166 break;
167 case ErrorMessage::initInvalidPlayerName :
168 retVal = ERR_NET_INVALID_PLAYER_NAME;
169 break;
170 case ErrorMessage::initServerMaintenance :
171 retVal = ERR_NET_SERVER_MAINTENANCE;
172 break;
173 case ErrorMessage::initBlocked :
174 retVal = ERR_NET_INIT_BLOCKED;
175 break;
176 case ErrorMessage::avatarTooLarge :
177 retVal = ERR_NET_AVATAR_TOO_LARGE;
178 break;
179 case ErrorMessage::invalidPacket :
180 retVal = ERR_SOCK_INVALID_PACKET;
181 break;
182 case ErrorMessage::invalidState :
183 retVal = ERR_SOCK_INVALID_STATE;
184 break;
185 case ErrorMessage::kickedFromServer :
186 retVal = ERR_NET_PLAYER_KICKED;
187 break;
188 case ErrorMessage::bannedFromServer :
189 retVal = ERR_NET_PLAYER_BANNED;
190 break;
191 case ErrorMessage::blockedByServer :
192 retVal = ERR_NET_PLAYER_BLOCKED;
193 break;
194 case ErrorMessage::sessionTimeout :
195 retVal = ERR_NET_SESSION_TIMED_OUT;
196 break;
197 default :
198 retVal = ERR_SOCK_INTERNAL;
199 break;
200 }
201 return retVal;
202 }
203
204 ErrorMessage::ErrorReason
GameErrorToNetError(int gameErrorReason)205 NetPacket::GameErrorToNetError(int gameErrorReason)
206 {
207 ErrorMessage::ErrorReason retVal;
208 switch(gameErrorReason) {
209 case ERR_NET_VERSION_NOT_SUPPORTED :
210 retVal = ErrorMessage::initVersionNotSupported;
211 break;
212 case ERR_NET_SERVER_FULL :
213 retVal = ErrorMessage::initServerFull;
214 break;
215 case ERR_NET_INVALID_PASSWORD :
216 retVal = ErrorMessage::initAuthFailure;
217 break;
218 case ERR_NET_PLAYER_NAME_IN_USE :
219 retVal = ErrorMessage::initPlayerNameInUse;
220 break;
221 case ERR_NET_INVALID_PLAYER_NAME :
222 retVal = ErrorMessage::initInvalidPlayerName;
223 break;
224 case ERR_NET_SERVER_MAINTENANCE :
225 retVal = ErrorMessage::initServerMaintenance;
226 break;
227 case ERR_NET_INIT_BLOCKED :
228 retVal = ErrorMessage::initBlocked;
229 break;
230 case ERR_NET_AVATAR_TOO_LARGE :
231 retVal = ErrorMessage::avatarTooLarge;
232 break;
233 case ERR_SOCK_INVALID_PACKET :
234 retVal = ErrorMessage::invalidPacket;
235 break;
236 case ERR_SOCK_INVALID_STATE :
237 retVal = ErrorMessage::invalidState;
238 break;
239 case ERR_NET_PLAYER_KICKED :
240 retVal = ErrorMessage::kickedFromServer;
241 break;
242 case ERR_NET_PLAYER_BLOCKED :
243 retVal = ErrorMessage::blockedByServer;
244 break;
245 case ERR_NET_PLAYER_BANNED :
246 retVal = ErrorMessage::bannedFromServer;
247 break;
248 case ERR_NET_SESSION_TIMED_OUT :
249 retVal = ErrorMessage::sessionTimeout;
250 break;
251 default :
252 retVal = ErrorMessage::pthreserved;
253 break;
254 }
255 return retVal;
256 }
257
258