2011-07-02 14:38:06 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
|
* PokerTH - The open source texas holdem engine *
|
|
|
|
|
* Copyright (C) 2006-2011 Felix Hammer, Florian Thauer, Lothar May *
|
|
|
|
|
* *
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as *
|
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
|
* GNU Affero General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License *
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
|
*****************************************************************************/
|
2007-11-16 15:24:25 +00:00
|
|
|
/* Network client thread. */
|
|
|
|
|
|
|
|
|
|
#ifndef _CLIENTTHREAD_H_
|
|
|
|
|
#define _CLIENTTHREAD_H_
|
|
|
|
|
|
2009-05-07 22:36:51 +00:00
|
|
|
#include <boost/shared_ptr.hpp>
|
2009-06-07 08:51:43 +00:00
|
|
|
#include <boost/asio.hpp>
|
2009-06-14 22:07:41 +00:00
|
|
|
#include <boost/enable_shared_from_this.hpp>
|
2009-05-07 22:36:51 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <core/thread.h>
|
2011-02-19 23:47:24 +00:00
|
|
|
#include <net/sessiondatacallback.h>
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <guiinterface.h>
|
2009-04-05 11:19:50 +00:00
|
|
|
#include <serverdata.h>
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <playerdata.h>
|
|
|
|
|
#include <gamedata.h>
|
|
|
|
|
|
|
|
|
|
class ClientContext;
|
|
|
|
|
class ClientState;
|
2009-06-07 08:51:43 +00:00
|
|
|
class SenderHelper;
|
2009-03-21 01:39:00 +00:00
|
|
|
class DownloaderThread;
|
2007-11-16 15:24:25 +00:00
|
|
|
class Game;
|
|
|
|
|
class NetPacket;
|
|
|
|
|
class AvatarManager;
|
2011-01-03 18:41:28 +00:00
|
|
|
class Log;
|
2007-12-11 20:40:39 +00:00
|
|
|
class QtToolsInterface;
|
2009-10-13 21:03:32 +00:00
|
|
|
struct Gsasl;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2011-02-19 23:47:24 +00:00
|
|
|
class ClientThread : public Thread, public boost::enable_shared_from_this<ClientThread>, public SessionDataCallback
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ClientThread(GuiInterface &gui, AvatarManager &avatarManager);
|
|
|
|
|
virtual ~ClientThread();
|
|
|
|
|
|
|
|
|
|
// Set the parameters. Does not do any error checking.
|
|
|
|
|
// Error checking will be done during connect
|
|
|
|
|
// (i.e. after starting the thread).
|
|
|
|
|
void Init(
|
|
|
|
|
const std::string &serverAddress,
|
2008-05-10 15:47:48 +00:00
|
|
|
const std::string &serverListUrl,
|
|
|
|
|
bool useServerList,
|
2007-11-16 15:24:25 +00:00
|
|
|
unsigned serverPort,
|
|
|
|
|
bool ipv6,
|
|
|
|
|
bool sctp,
|
2009-03-18 15:01:43 +00:00
|
|
|
const std::string &avatarServerAddress,
|
2007-11-16 15:24:25 +00:00
|
|
|
const std::string &playerName,
|
2008-04-19 11:48:09 +00:00
|
|
|
const std::string &avatarFile,
|
|
|
|
|
const std::string &cacheDir);
|
2009-06-14 22:07:41 +00:00
|
|
|
virtual void SignalTermination();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
void SendKickPlayer(unsigned playerId);
|
|
|
|
|
void SendLeaveCurrentGame();
|
|
|
|
|
void SendStartEvent(bool fillUpWithCpuPlayers);
|
|
|
|
|
void SendPlayerAction();
|
2009-08-15 10:25:13 +00:00
|
|
|
void SendGameChatMessage(const std::string &msg);
|
|
|
|
|
void SendLobbyChatMessage(const std::string &msg);
|
2010-11-07 08:24:31 +00:00
|
|
|
void SendPrivateChatMessage(unsigned targetPlayerId, const std::string &msg);
|
2011-02-06 13:17:07 +00:00
|
|
|
void SendJoinFirstGame(const std::string &password, bool autoLeave);
|
|
|
|
|
void SendJoinGame(unsigned gameId, const std::string &password, bool autoLeave);
|
|
|
|
|
void SendCreateGame(const GameData &gameData, const std::string &name, const std::string &password, bool autoLeave);
|
2008-03-04 23:47:45 +00:00
|
|
|
void SendResetTimeout();
|
2008-11-16 22:57:11 +00:00
|
|
|
void SendAskKickPlayer(unsigned playerId);
|
2008-11-25 22:18:17 +00:00
|
|
|
void SendVoteKick(bool doKick);
|
2010-07-15 21:55:52 +00:00
|
|
|
void SendShowMyCards();
|
2010-03-29 12:33:12 +00:00
|
|
|
void SendInvitePlayerToCurrentGame(unsigned playerId);
|
|
|
|
|
void SendRejectGameInvitation(unsigned gameId, DenyGameInvitationReason reason);
|
2010-12-24 12:27:46 +00:00
|
|
|
void SendReportAvatar(unsigned reportedPlayerId, const std::string &avatarHash);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-06-14 22:07:41 +00:00
|
|
|
void StartAsyncRead();
|
2011-02-23 09:53:01 +00:00
|
|
|
virtual void CloseSession(boost::shared_ptr<SessionData> session);
|
2011-02-19 23:47:24 +00:00
|
|
|
virtual void HandlePacket(boost::shared_ptr<SessionData> session, boost::shared_ptr<NetPacket> packet);
|
2009-06-14 22:07:41 +00:00
|
|
|
|
2009-04-05 12:31:12 +00:00
|
|
|
void SelectServer(unsigned serverId);
|
2009-11-14 15:04:31 +00:00
|
|
|
void SetLogin(const std::string &userName, const std::string &password, bool isGuest);
|
2009-04-05 11:19:50 +00:00
|
|
|
ServerInfo GetServerInfo(unsigned serverId) const;
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
GameInfo GetGameInfo(unsigned gameId) const;
|
|
|
|
|
PlayerInfo GetPlayerInfo(unsigned playerId) const;
|
|
|
|
|
bool GetPlayerIdFromName(const std::string &playerName, unsigned &playerId) const;
|
2010-07-15 10:34:24 +00:00
|
|
|
unsigned GetGameIdOfPlayer(unsigned playerId) const;
|
2007-11-16 15:24:25 +00:00
|
|
|
ServerStats GetStatData() const;
|
|
|
|
|
unsigned GetGameId() const;
|
2010-03-24 20:10:36 +00:00
|
|
|
unsigned GetGuiPlayerId() const;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-10-13 21:03:32 +00:00
|
|
|
Gsasl *GetAuthContext();
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
ClientCallback &GetCallback();
|
|
|
|
|
GuiInterface &GetGui();
|
|
|
|
|
AvatarManager &GetAvatarManager();
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
typedef std::map<unsigned, GameInfo> GameInfoMap;
|
|
|
|
|
typedef std::list<boost::shared_ptr<NetPacket> > NetPacketList;
|
|
|
|
|
typedef std::map<unsigned, PlayerInfo> PlayerInfoMap;
|
2009-08-02 15:11:40 +00:00
|
|
|
typedef std::map<unsigned, boost::shared_ptr<AvatarFile> > AvatarFileMap;
|
2009-04-05 11:19:50 +00:00
|
|
|
typedef std::map<unsigned, ServerInfo> ServerInfoMap;
|
2011-02-11 20:02:08 +00:00
|
|
|
struct LoginData {
|
2009-11-14 15:04:31 +00:00
|
|
|
LoginData() : isGuest(false) {}
|
|
|
|
|
std::string userName;
|
|
|
|
|
std::string password;
|
|
|
|
|
bool isGuest;
|
|
|
|
|
};
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
// Main function of the thread.
|
|
|
|
|
virtual void Main();
|
2009-06-14 22:07:41 +00:00
|
|
|
void RegisterTimers();
|
|
|
|
|
void CancelTimers();
|
2009-10-13 21:03:32 +00:00
|
|
|
|
|
|
|
|
void InitAuthContext();
|
|
|
|
|
void ClearAuthContext();
|
2009-06-14 22:07:41 +00:00
|
|
|
void InitGame();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-06-17 21:22:01 +00:00
|
|
|
void SendSessionPacket(boost::shared_ptr<NetPacket> packet);
|
2009-06-20 18:41:03 +00:00
|
|
|
void SendQueuedPackets();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
bool GetCachedPlayerInfo(unsigned id, PlayerInfo &info) const;
|
|
|
|
|
void RequestPlayerInfo(unsigned id, bool requestAvatar = false);
|
|
|
|
|
void SetPlayerInfo(unsigned id, const PlayerInfo &info);
|
|
|
|
|
void SetUnknownPlayer(unsigned id);
|
|
|
|
|
void SetNewGameAdmin(unsigned id);
|
|
|
|
|
void RetrieveAvatarIfNeeded(unsigned id, const PlayerInfo &info);
|
|
|
|
|
|
2009-08-02 15:11:40 +00:00
|
|
|
void AddTempAvatarFile(unsigned playerId, unsigned avatarSize, AvatarFileType type);
|
|
|
|
|
void StoreInTempAvatarFile(unsigned playerId, const std::vector<unsigned char> &data);
|
|
|
|
|
void CompleteTempAvatarFile(unsigned playerId);
|
|
|
|
|
void PassAvatarFileToManager(unsigned playerId, boost::shared_ptr<AvatarFile> AvatarFile);
|
2007-11-16 15:24:25 +00:00
|
|
|
void SetUnknownAvatar(unsigned playerId);
|
|
|
|
|
|
2009-06-14 22:07:41 +00:00
|
|
|
void TimerCheckAvatarDownloads(const boost::system::error_code& ec);
|
2009-03-21 11:36:10 +00:00
|
|
|
|
2008-05-16 16:46:45 +00:00
|
|
|
void UnsubscribeLobbyMsg();
|
|
|
|
|
void ResubscribeLobbyMsg();
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
const ClientContext &GetContext() const;
|
|
|
|
|
ClientContext &GetContext();
|
2009-05-04 21:11:29 +00:00
|
|
|
void CreateContextSession();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
ClientState &GetState();
|
|
|
|
|
void SetState(ClientState &newState);
|
2009-06-14 22:07:41 +00:00
|
|
|
boost::asio::deadline_timer &GetStateTimer();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-06-07 08:51:43 +00:00
|
|
|
SenderHelper &GetSender();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
void SetGameId(unsigned id);
|
|
|
|
|
const GameData &GetGameData() const;
|
|
|
|
|
void SetGameData(const GameData &gameData);
|
|
|
|
|
const StartData &GetStartData() const;
|
|
|
|
|
void SetStartData(const StartData &startData);
|
|
|
|
|
void SetGuiPlayerId(unsigned guiPlayerId);
|
|
|
|
|
|
|
|
|
|
boost::shared_ptr<Game> GetGame();
|
|
|
|
|
|
2007-12-12 13:37:45 +00:00
|
|
|
QtToolsInterface &GetQtToolsInterface();
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
void AddPlayerData(boost::shared_ptr<PlayerData> playerData);
|
2008-11-25 23:19:34 +00:00
|
|
|
void RemovePlayerData(unsigned playerId, int removeReason);
|
2007-11-16 15:24:25 +00:00
|
|
|
void ClearPlayerDataList();
|
|
|
|
|
void MapPlayerDataList();
|
|
|
|
|
const PlayerDataList &GetPlayerDataList() const;
|
|
|
|
|
boost::shared_ptr<PlayerData> GetPlayerDataByUniqueId(unsigned id);
|
|
|
|
|
boost::shared_ptr<PlayerData> GetPlayerDataByName(const std::string &name);
|
|
|
|
|
|
|
|
|
|
void RemoveDisconnectedPlayers();
|
|
|
|
|
|
2009-04-05 11:19:50 +00:00
|
|
|
void AddServerInfo(unsigned serverId, const ServerInfo &info);
|
|
|
|
|
void ClearServerInfoMap();
|
2009-04-05 12:31:12 +00:00
|
|
|
bool GetSelectedServer(unsigned &serverId) const;
|
2009-04-05 11:19:50 +00:00
|
|
|
void UseServer(unsigned serverId);
|
|
|
|
|
|
2009-11-14 15:04:31 +00:00
|
|
|
bool GetLoginData(LoginData &loginData) const;
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
unsigned GetGameIdByName(const std::string &name) const;
|
|
|
|
|
void AddGameInfo(unsigned gameId, const GameInfo &info);
|
|
|
|
|
void UpdateGameInfoMode(unsigned gameId, GameMode mode);
|
|
|
|
|
void UpdateGameInfoAdmin(unsigned gameId, unsigned adminPlayerId);
|
|
|
|
|
void RemoveGameInfo(unsigned gameId);
|
|
|
|
|
void ModifyGameInfoAddPlayer(unsigned gameId, unsigned playerId);
|
|
|
|
|
void ModifyGameInfoRemovePlayer(unsigned gameId, unsigned playerId);
|
|
|
|
|
void ClearGameInfoMap();
|
|
|
|
|
|
2008-11-25 22:18:17 +00:00
|
|
|
void StartPetition(unsigned petitionId, unsigned proposingPlayerId, unsigned kickPlayerId, int timeoutSec, int numVotesToKick);
|
2008-12-07 12:22:35 +00:00
|
|
|
void UpdatePetition(unsigned petitionId, int numVotesAgainstKicking, int numVotesInFavourOfKicking, int numVotesToKick);
|
2008-12-07 11:48:01 +00:00
|
|
|
void EndPetition(unsigned petitionId);
|
2008-11-25 22:18:17 +00:00
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
void UpdateStatData(const ServerStats &stats);
|
|
|
|
|
|
|
|
|
|
bool IsSessionEstablished() const;
|
|
|
|
|
void SetSessionEstablished(bool flag);
|
|
|
|
|
|
|
|
|
|
bool IsSynchronized() const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
2009-03-21 10:28:27 +00:00
|
|
|
boost::shared_ptr<boost::asio::io_service> m_ioService;
|
2011-01-03 18:41:28 +00:00
|
|
|
boost::shared_ptr<Log> m_clientLog;
|
2009-03-21 10:28:27 +00:00
|
|
|
|
2009-10-13 21:03:32 +00:00
|
|
|
Gsasl *m_authContext;
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
NetPacketList m_outPacketList;
|
|
|
|
|
|
|
|
|
|
boost::shared_ptr<ClientContext> m_context;
|
|
|
|
|
ClientState *m_curState;
|
|
|
|
|
GuiInterface &m_gui;
|
|
|
|
|
AvatarManager &m_avatarManager;
|
|
|
|
|
|
2009-06-07 08:51:43 +00:00
|
|
|
boost::shared_ptr<SenderHelper> m_senderHelper;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-03-21 01:39:00 +00:00
|
|
|
boost::shared_ptr<DownloaderThread> m_avatarDownloader;
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
GameData m_gameData;
|
|
|
|
|
StartData m_startData;
|
|
|
|
|
PlayerDataList m_playerDataList;
|
|
|
|
|
|
2009-04-05 11:19:50 +00:00
|
|
|
ServerInfoMap m_serverInfoMap;
|
|
|
|
|
mutable boost::mutex m_serverInfoMapMutex;
|
|
|
|
|
|
2009-04-05 12:31:12 +00:00
|
|
|
bool m_isServerSelected;
|
|
|
|
|
unsigned m_selectedServerId;
|
|
|
|
|
mutable boost::mutex m_selectServerMutex;
|
|
|
|
|
|
2009-11-14 15:04:31 +00:00
|
|
|
LoginData m_loginData;
|
|
|
|
|
mutable boost::mutex m_loginDataMutex;
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
GameInfoMap m_gameInfoMap;
|
|
|
|
|
mutable boost::mutex m_gameInfoMapMutex;
|
|
|
|
|
|
|
|
|
|
boost::shared_ptr<Game> m_game;
|
2007-12-11 20:40:39 +00:00
|
|
|
boost::shared_ptr<QtToolsInterface> myQtToolsInterface;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
PlayerInfoMap m_playerInfoMap;
|
|
|
|
|
mutable boost::mutex m_playerInfoMapMutex;
|
|
|
|
|
PlayerIdList m_playerInfoRequestList;
|
|
|
|
|
PlayerIdList m_avatarShouldRequestList;
|
|
|
|
|
PlayerIdList m_avatarHasRequestedList;
|
|
|
|
|
|
|
|
|
|
unsigned m_curGameId;
|
|
|
|
|
mutable boost::mutex m_curGameIdMutex;
|
|
|
|
|
|
2008-11-25 22:18:17 +00:00
|
|
|
unsigned m_curPetitionId;
|
|
|
|
|
mutable boost::mutex m_curPetitionIdMutex;
|
|
|
|
|
|
2009-08-02 15:11:40 +00:00
|
|
|
AvatarFileMap m_tempAvatarMap;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
unsigned m_curGameNum;
|
|
|
|
|
unsigned m_guiPlayerId;
|
2010-03-24 20:10:36 +00:00
|
|
|
mutable boost::mutex m_guiPlayerIdMutex;
|
2007-11-16 15:24:25 +00:00
|
|
|
bool m_sessionEstablished;
|
|
|
|
|
|
|
|
|
|
mutable boost::mutex m_curStatsMutex;
|
|
|
|
|
ServerStats m_curStats;
|
|
|
|
|
|
2009-06-14 22:07:41 +00:00
|
|
|
boost::asio::deadline_timer m_stateTimer;
|
|
|
|
|
boost::asio::deadline_timer m_avatarTimer;
|
|
|
|
|
|
2011-02-11 20:02:08 +00:00
|
|
|
friend class AbstractClientStateReceiving;
|
|
|
|
|
friend class ClientStateInit;
|
|
|
|
|
friend class ClientStateStartResolve;
|
|
|
|
|
friend class ClientStateResolving;
|
|
|
|
|
friend class ClientStateStartServerListDownload;
|
|
|
|
|
friend class ClientStateSynchronizingServerList;
|
|
|
|
|
friend class ClientStateDownloadingServerList;
|
|
|
|
|
friend class ClientStateReadingServerList;
|
|
|
|
|
friend class ClientStateWaitChooseServer;
|
|
|
|
|
friend class ClientStateStartConnect;
|
|
|
|
|
friend class ClientStateConnecting;
|
|
|
|
|
friend class ClientStateStartSession;
|
|
|
|
|
friend class ClientStateWaitEnterLogin;
|
|
|
|
|
friend class ClientStateWaitAuthChallenge;
|
|
|
|
|
friend class ClientStateWaitAuthVerify;
|
|
|
|
|
friend class ClientStateWaitSession;
|
|
|
|
|
friend class ClientStateWaitJoin;
|
|
|
|
|
friend class ClientStateWaitGame;
|
|
|
|
|
friend class ClientStateSynchronizeStart;
|
|
|
|
|
friend class ClientStateWaitStart;
|
|
|
|
|
friend class ClientStateWaitHand;
|
|
|
|
|
friend class ClientStateRunHand;
|
|
|
|
|
friend class ClientStateFinal;
|
2007-11-16 15:24:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|