The client network code was rewritten to use boost::asio. Some optimization is still required, and some code files can now be removed.

This commit is contained in:
lotodore
2009-06-14 22:07:41 +00:00
parent 7048c19305
commit 4b5d78af81
16 changed files with 1186 additions and 1193 deletions
-1
View File
@@ -118,7 +118,6 @@ HEADERS += src/game.h \
src/net/clientthread.h \ src/net/clientthread.h \
src/net/genericsocket.h \ src/net/genericsocket.h \
src/net/netpacket.h \ src/net/netpacket.h \
src/net/resolverthread.h \
src/net/senderhelper.h \ src/net/senderhelper.h \
src/net/serveraccepthelper.h \ src/net/serveraccepthelper.h \
src/net/serverlobbythread.h \ src/net/serverlobbythread.h \
-4
View File
@@ -72,10 +72,8 @@ HEADERS += \
src/net/clientthread.h \ src/net/clientthread.h \
src/net/genericsocket.h \ src/net/genericsocket.h \
src/net/netpacket.h \ src/net/netpacket.h \
src/net/resolverthread.h \
src/net/senderhelper.h \ src/net/senderhelper.h \
src/net/sendercallback.h \ src/net/sendercallback.h \
src/net/servercontext.h \
src/net/serverexception.h \ src/net/serverexception.h \
src/net/serveraccepthelper.h \ src/net/serveraccepthelper.h \
src/net/servergame.h \ src/net/servergame.h \
@@ -172,10 +170,8 @@ SOURCES += \
src/net/common/downloadhelper.cpp \ src/net/common/downloadhelper.cpp \
src/net/common/downloaderthread.cpp \ src/net/common/downloaderthread.cpp \
src/net/common/netpacket.cpp \ src/net/common/netpacket.cpp \
src/net/common/resolverthread.cpp \
src/net/common/senderhelper.cpp \ src/net/common/senderhelper.cpp \
src/net/common/sendercallback.cpp \ src/net/common/sendercallback.cpp \
src/net/common/servercontext.cpp \
src/net/common/serverexception.cpp \ src/net/common/serverexception.cpp \
src/net/common/serveraccepthelper.cpp \ src/net/common/serveraccepthelper.cpp \
src/net/common/servergame.cpp \ src/net/common/servergame.cpp \
-1
View File
@@ -70,7 +70,6 @@ HEADERS += \
src/net/clientthread.h \ src/net/clientthread.h \
src/net/genericsocket.h \ src/net/genericsocket.h \
src/net/netpacket.h \ src/net/netpacket.h \
src/net/resolverthread.h \
src/net/senderhelper.h \ src/net/senderhelper.h \
src/net/serveraccepthelper.h \ src/net/serveraccepthelper.h \
src/net/serverlobbythread.h \ src/net/serverlobbythread.h \
+4 -12
View File
@@ -23,21 +23,20 @@
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <net/netcontext.h>
#include <net/receivebuffer.h> #include <net/receivebuffer.h>
#include <net/sessiondata.h> #include <net/sessiondata.h>
class ClientContext : public NetContext class ClientContext
{ {
public: public:
ClientContext(); ClientContext();
virtual ~ClientContext(); virtual ~ClientContext();
virtual SOCKET GetSocket() const;
boost::shared_ptr<SessionData> GetSessionData() const; boost::shared_ptr<SessionData> GetSessionData() const;
void SetSessionData(boost::shared_ptr<SessionData> sessionData); void SetSessionData(boost::shared_ptr<SessionData> sessionData);
boost::shared_ptr<boost::asio::ip::tcp::resolver> GetResolver() const;
void SetResolver(boost::shared_ptr<boost::asio::ip::tcp::resolver> resolver);
int GetProtocol() const int GetProtocol() const
{return m_protocol;} {return m_protocol;}
void SetProtocol(int protocol) void SetProtocol(int protocol)
@@ -70,10 +69,6 @@ public:
{return m_password;} {return m_password;}
void SetPassword(const std::string &password) void SetPassword(const std::string &password)
{m_password = password;} {m_password = password;}
const sockaddr_storage *GetClientSockaddr() const
{return &m_clientSockaddr;}
sockaddr_storage *GetClientSockaddr()
{return &m_clientSockaddr;}
const std::string &GetPlayerName() const const std::string &GetPlayerName() const
{return m_playerName;} {return m_playerName;}
void SetPlayerName(const std::string &playerName) void SetPlayerName(const std::string &playerName)
@@ -91,14 +86,12 @@ public:
void SetSubscribeLobbyMsg(bool setSubscribe) void SetSubscribeLobbyMsg(bool setSubscribe)
{m_hasSubscribedLobbyMsg = setSubscribe;} {m_hasSubscribedLobbyMsg = setSubscribe;}
int GetClientSockaddrSize() const
{return m_addrFamily == AF_INET6 ? sizeof(sockaddr_in6) : sizeof(sockaddr_in);}
ReceiveBuffer &GetReceiveBuffer() ReceiveBuffer &GetReceiveBuffer()
{return m_receiveBuffer;} {return m_receiveBuffer;}
private: private:
boost::shared_ptr<SessionData> m_sessionData; boost::shared_ptr<SessionData> m_sessionData;
boost::shared_ptr<boost::asio::ip::tcp::resolver> m_resolver;
int m_protocol; int m_protocol;
int m_addrFamily; int m_addrFamily;
std::string m_serverAddr; std::string m_serverAddr;
@@ -107,7 +100,6 @@ private:
unsigned m_serverPort; unsigned m_serverPort;
std::string m_avatarServerAddr; std::string m_avatarServerAddr;
std::string m_password; std::string m_password;
sockaddr_storage m_clientSockaddr;
std::string m_playerName; std::string m_playerName;
std::string m_avatarFile; std::string m_avatarFile;
std::string m_cacheDir; std::string m_cacheDir;
+95 -117
View File
@@ -21,11 +21,9 @@
#ifndef _CLIENTSTATE_H_ #ifndef _CLIENTSTATE_H_
#define _CLIENTSTATE_H_ #define _CLIENTSTATE_H_
#include <third_party/boost/timers.hpp> #include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#include <string> #include <string>
#include <memory>
#include <net/socket_helper.h> // needed for correct order of header files.
#define CLIENT_INITIAL_STATE ClientStateInit #define CLIENT_INITIAL_STATE ClientStateInit
@@ -41,8 +39,10 @@ class ClientState
public: public:
virtual ~ClientState(); virtual ~ClientState();
// Main processing function of the current state. virtual void Enter(boost::shared_ptr<ClientThread> client) = 0;
virtual int Process(ClientThread &client) = 0; virtual void Exit(boost::shared_ptr<ClientThread> client) = 0;
virtual void HandleRead(const boost::system::error_code& ec, boost::shared_ptr<ClientThread> client, size_t bytesRead) = 0;
}; };
// State: Initialization. // State: Initialization.
@@ -51,14 +51,15 @@ class ClientStateInit : public ClientState
public: public:
// Access the state singleton. // Access the state singleton.
static ClientStateInit &Instance(); static ClientStateInit &Instance();
virtual ~ClientStateInit(); virtual ~ClientStateInit();
// Some basic initialization (socket creation, basic checks). // Some basic initialization (socket creation, basic checks).
virtual int Process(ClientThread &client); virtual void Enter(boost::shared_ptr<ClientThread> client);
virtual void Exit(boost::shared_ptr<ClientThread> client);
virtual void HandleRead(const boost::system::error_code& /*ec*/, boost::shared_ptr<ClientThread> /*client*/, size_t /*bytesRead*/) {}
protected: protected:
// Protected constructor - this is a singleton. // Protected constructor - this is a singleton.
ClientStateInit(); ClientStateInit();
}; };
@@ -69,55 +70,36 @@ class ClientStateStartResolve : public ClientState
public: public:
// Access the state singleton. // Access the state singleton.
static ClientStateStartResolve &Instance(); static ClientStateStartResolve &Instance();
virtual ~ClientStateStartResolve(); virtual ~ClientStateStartResolve();
// Initiate the name resolution. // Initiate the name resolution.
virtual int Process(ClientThread &client); virtual void Enter(boost::shared_ptr<ClientThread> client);
virtual void Exit(boost::shared_ptr<ClientThread> client);
virtual void HandleRead(const boost::system::error_code& /*ec*/, boost::shared_ptr<ClientThread> /*client*/, size_t /*bytesRead*/) {}
protected: protected:
void HandleResolve(
const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::iterator endpoint_iterator,
boost::shared_ptr<ClientThread> client);
// Protected constructor - this is a singleton. // Protected constructor - this is a singleton.
ClientStateStartResolve(); ClientStateStartResolve();
}; };
// State: Name resolution.
class ClientStateResolving : public ClientState
{
public:
// Access the state singleton.
static ClientStateResolving &Instance();
virtual ~ClientStateResolving();
void SetResolver(ResolverThread *resolver);
// Poll for the completion of the name resolution.
virtual int Process(ClientThread &client);
protected:
// Protected constructor - this is a singleton.
ClientStateResolving();
void Cleanup();
private:
ResolverThread *m_resolver;
};
// State: Start download of the server list. // State: Start download of the server list.
class ClientStateStartServerListDownload : public ClientState class ClientStateStartServerListDownload : public ClientState
{ {
public: public:
// Access the state singleton. // Access the state singleton.
static ClientStateStartServerListDownload &Instance(); static ClientStateStartServerListDownload &Instance();
virtual ~ClientStateStartServerListDownload(); virtual ~ClientStateStartServerListDownload();
// Initiate the name resolution. // Initiate the name resolution.
virtual int Process(ClientThread &client); virtual void Enter(boost::shared_ptr<ClientThread> client);
virtual void Exit(boost::shared_ptr<ClientThread> client);
virtual void HandleRead(const boost::system::error_code& /*ec*/, boost::shared_ptr<ClientThread> /*client*/, size_t /*bytesRead*/) {}
protected: protected:
@@ -131,22 +113,26 @@ class ClientStateSynchronizingServerList : public ClientState
public: public:
// Access the state singleton. // Access the state singleton.
static ClientStateSynchronizingServerList &Instance(); static ClientStateSynchronizingServerList &Instance();
virtual ~ClientStateSynchronizingServerList(); virtual ~ClientStateSynchronizingServerList();
void SetDownloadHelper(DownloadHelper *helper); virtual void Enter(boost::shared_ptr<ClientThread> client);
virtual void Exit(boost::shared_ptr<ClientThread> client);
// Poll for the completion of the download. virtual void HandleRead(const boost::system::error_code& /*ec*/, boost::shared_ptr<ClientThread> /*client*/, size_t /*bytesRead*/) {}
virtual int Process(ClientThread &client);
void SetDownloadHelper(boost::shared_ptr<DownloadHelper> helper);
protected: protected:
// Protected constructor - this is a singleton. // Protected constructor - this is a singleton.
ClientStateSynchronizingServerList(); ClientStateSynchronizingServerList();
// Poll for the completion of the download.
void TimerLoop(const boost::system::error_code& ec, boost::shared_ptr<ClientThread> client);
private: private:
std::auto_ptr<DownloadHelper> m_downloadHelper; boost::shared_ptr<DownloadHelper> m_downloadHelper;
}; };
// State: Downloading the server list. // State: Downloading the server list.
@@ -155,22 +141,26 @@ class ClientStateDownloadingServerList : public ClientState
public: public:
// Access the state singleton. // Access the state singleton.
static ClientStateDownloadingServerList &Instance(); static ClientStateDownloadingServerList &Instance();
virtual ~ClientStateDownloadingServerList(); virtual ~ClientStateDownloadingServerList();
void SetDownloadHelper(DownloadHelper *helper); virtual void Enter(boost::shared_ptr<ClientThread> client);
virtual void Exit(boost::shared_ptr<ClientThread> client);
// Poll for the completion of the download. virtual void HandleRead(const boost::system::error_code& /*ec*/, boost::shared_ptr<ClientThread> /*client*/, size_t /*bytesRead*/) {}
virtual int Process(ClientThread &client);
void SetDownloadHelper(boost::shared_ptr<DownloadHelper> helper);
protected: protected:
// Protected constructor - this is a singleton. // Protected constructor - this is a singleton.
ClientStateDownloadingServerList(); ClientStateDownloadingServerList();
// Poll for the completion of the download.
void TimerLoop(const boost::system::error_code& ec, boost::shared_ptr<ClientThread> client);
private: private:
std::auto_ptr<DownloadHelper> m_downloadHelper; boost::shared_ptr<DownloadHelper> m_downloadHelper;
}; };
// State: Reading the server list. // State: Reading the server list.
@@ -178,10 +168,12 @@ class ClientStateReadingServerList : public ClientState
{ {
public: public:
static ClientStateReadingServerList &Instance(); static ClientStateReadingServerList &Instance();
virtual ~ClientStateReadingServerList(); virtual ~ClientStateReadingServerList();
virtual int Process(ClientThread &client); virtual void Enter(boost::shared_ptr<ClientThread> client);
virtual void Exit(boost::shared_ptr<ClientThread> client);
virtual void HandleRead(const boost::system::error_code& /*ec*/, boost::shared_ptr<ClientThread> /*client*/, size_t /*bytesRead*/) {}
protected: protected:
@@ -194,15 +186,19 @@ class ClientStateWaitChooseServer : public ClientState
{ {
public: public:
static ClientStateWaitChooseServer &Instance(); static ClientStateWaitChooseServer &Instance();
virtual ~ClientStateWaitChooseServer(); virtual ~ClientStateWaitChooseServer();
virtual int Process(ClientThread &client); virtual void Enter(boost::shared_ptr<ClientThread> client);
virtual void Exit(boost::shared_ptr<ClientThread> client);
virtual void HandleRead(const boost::system::error_code& /*ec*/, boost::shared_ptr<ClientThread> /*client*/, size_t /*bytesRead*/) {}
protected: protected:
// Protected constructor - this is a singleton. // Protected constructor - this is a singleton.
ClientStateWaitChooseServer(); ClientStateWaitChooseServer();
void TimerLoop(const boost::system::error_code& ec, boost::shared_ptr<ClientThread> client);
}; };
// State: Initiate server connection. // State: Initiate server connection.
@@ -211,40 +207,29 @@ class ClientStateStartConnect : public ClientState
public: public:
// Access the state singleton. // Access the state singleton.
static ClientStateStartConnect &Instance(); static ClientStateStartConnect &Instance();
virtual ~ClientStateStartConnect(); virtual ~ClientStateStartConnect();
// Call connect. // Call connect.
virtual int Process(ClientThread &client); virtual void Enter(boost::shared_ptr<ClientThread> client);
virtual void Exit(boost::shared_ptr<ClientThread> client);
virtual void HandleRead(const boost::system::error_code& /*ec*/, boost::shared_ptr<ClientThread> /*client*/, size_t /*bytesRead*/) {}
void SetRemoteEndpoint(boost::asio::ip::tcp::resolver::iterator endpointIterator);
protected: protected:
// Protected constructor - this is a singleton. // Protected constructor - this is a singleton.
ClientStateStartConnect(); ClientStateStartConnect();
};
// State: Connecting to server. void HandleConnect(const boost::system::error_code& ec,
class ClientStateConnecting : public ClientState boost::asio::ip::tcp::resolver::iterator endpoint_iterator,
{ boost::shared_ptr<ClientThread> client);
public:
// Access the state singleton.
static ClientStateConnecting &Instance();
virtual ~ClientStateConnecting(); void TimerTimeout(const boost::system::error_code& ec, boost::shared_ptr<ClientThread> client);
void SetTimer(const boost::timers::portable::microsec_timer &timer);
// "Poll" for the completion of the TCP/IP connect call.
virtual int Process(ClientThread &client);
protected:
// Protected constructor - this is a singleton.
ClientStateConnecting();
private: private:
boost::asio::ip::tcp::resolver::iterator m_remoteEndpointIterator;
boost::timers::portable::microsec_timer m_connectTimer;
}; };
// State: Session init. // State: Session init.
@@ -253,11 +238,12 @@ class ClientStateStartSession : public ClientState
public: public:
// Access the state singleton. // Access the state singleton.
static ClientStateStartSession &Instance(); static ClientStateStartSession &Instance();
virtual ~ClientStateStartSession(); virtual ~ClientStateStartSession();
// sleep. virtual void Enter(boost::shared_ptr<ClientThread> client);
virtual int Process(ClientThread &client); virtual void Exit(boost::shared_ptr<ClientThread> client);
virtual void HandleRead(const boost::system::error_code& /*ec*/, boost::shared_ptr<ClientThread> /*client*/, size_t /*bytesRead*/) {}
protected: protected:
@@ -271,14 +257,13 @@ class AbstractClientStateReceiving : public ClientState
public: public:
virtual ~AbstractClientStateReceiving(); virtual ~AbstractClientStateReceiving();
// select on socket. virtual void HandleRead(const boost::system::error_code& ec, boost::shared_ptr<ClientThread> client, size_t bytesRead);
virtual int Process(ClientThread &client);
protected: protected:
virtual int InternalProcess(ClientThread &client, boost::shared_ptr<NetPacket> packet) = 0;
AbstractClientStateReceiving(); AbstractClientStateReceiving();
void HandlePacket(boost::shared_ptr<ClientThread> client, boost::shared_ptr<NetPacket> tmpPacket);
virtual void InternalHandlePacket(boost::shared_ptr<ClientThread> client, boost::shared_ptr<NetPacket> tmpPacket) = 0;
}; };
// State: Wait for Session ACK. // State: Wait for Session ACK.
@@ -287,16 +272,16 @@ class ClientStateWaitSession : public AbstractClientStateReceiving
public: public:
// Access the state singleton. // Access the state singleton.
static ClientStateWaitSession &Instance(); static ClientStateWaitSession &Instance();
virtual ~ClientStateWaitSession(); virtual ~ClientStateWaitSession();
virtual void Enter(boost::shared_ptr<ClientThread> client);
virtual void Exit(boost::shared_ptr<ClientThread> client);
protected: protected:
// Protected constructor - this is a singleton. // Protected constructor - this is a singleton.
ClientStateWaitSession(); ClientStateWaitSession();
virtual int InternalProcess(ClientThread &client, boost::shared_ptr<NetPacket> packet); virtual void InternalHandlePacket(boost::shared_ptr<ClientThread> client, boost::shared_ptr<NetPacket> tmpPacket);
}; };
// State: Wait for Join. // State: Wait for Join.
@@ -305,16 +290,17 @@ class ClientStateWaitJoin : public AbstractClientStateReceiving
public: public:
// Access the state singleton. // Access the state singleton.
static ClientStateWaitJoin &Instance(); static ClientStateWaitJoin &Instance();
virtual ~ClientStateWaitJoin(); virtual ~ClientStateWaitJoin();
virtual void Enter(boost::shared_ptr<ClientThread> client);
virtual void Exit(boost::shared_ptr<ClientThread> client);
protected: protected:
// Protected constructor - this is a singleton. // Protected constructor - this is a singleton.
ClientStateWaitJoin(); ClientStateWaitJoin();
virtual int InternalProcess(ClientThread &client, boost::shared_ptr<NetPacket> packet); virtual void InternalHandlePacket(boost::shared_ptr<ClientThread> client, boost::shared_ptr<NetPacket> tmpPacket);
}; };
// State: Wait for start of the game or start info. // State: Wait for start of the game or start info.
@@ -323,15 +309,17 @@ class ClientStateWaitGame : public AbstractClientStateReceiving
public: public:
// Access the state singleton. // Access the state singleton.
static ClientStateWaitGame &Instance(); static ClientStateWaitGame &Instance();
virtual ~ClientStateWaitGame(); virtual ~ClientStateWaitGame();
virtual void Enter(boost::shared_ptr<ClientThread> client);
virtual void Exit(boost::shared_ptr<ClientThread> client);
protected: protected:
// Protected constructor - this is a singleton. // Protected constructor - this is a singleton.
ClientStateWaitGame(); ClientStateWaitGame();
virtual int InternalProcess(ClientThread &client, boost::shared_ptr<NetPacket> packet); virtual void InternalHandlePacket(boost::shared_ptr<ClientThread> client, boost::shared_ptr<NetPacket> tmpPacket);
}; };
// State: Synchronize on game start. // State: Synchronize on game start.
@@ -340,16 +328,18 @@ class ClientStateSynchronizeStart : public AbstractClientStateReceiving
public: public:
// Access the state singleton. // Access the state singleton.
static ClientStateSynchronizeStart &Instance(); static ClientStateSynchronizeStart &Instance();
virtual ~ClientStateSynchronizeStart(); virtual ~ClientStateSynchronizeStart();
virtual int Process(ClientThread &client); virtual void Enter(boost::shared_ptr<ClientThread> client);
virtual void Exit(boost::shared_ptr<ClientThread> client);
protected: protected:
// Protected constructor - this is a singleton. // Protected constructor - this is a singleton.
ClientStateSynchronizeStart(); ClientStateSynchronizeStart();
virtual int InternalProcess(ClientThread &client, boost::shared_ptr<NetPacket> packet); void TimerLoop(const boost::system::error_code& ec, boost::shared_ptr<ClientThread> client);
virtual void InternalHandlePacket(boost::shared_ptr<ClientThread> client, boost::shared_ptr<NetPacket> tmpPacket);
}; };
// State: Wait for game start. // State: Wait for game start.
@@ -358,15 +348,17 @@ class ClientStateWaitStart : public AbstractClientStateReceiving
public: public:
// Access the state singleton. // Access the state singleton.
static ClientStateWaitStart &Instance(); static ClientStateWaitStart &Instance();
virtual ~ClientStateWaitStart(); virtual ~ClientStateWaitStart();
virtual void Enter(boost::shared_ptr<ClientThread> client);
virtual void Exit(boost::shared_ptr<ClientThread> client);
protected: protected:
// Protected constructor - this is a singleton. // Protected constructor - this is a singleton.
ClientStateWaitStart(); ClientStateWaitStart();
virtual int InternalProcess(ClientThread &client, boost::shared_ptr<NetPacket> packet); virtual void InternalHandlePacket(boost::shared_ptr<ClientThread> client, boost::shared_ptr<NetPacket> tmpPacket);
}; };
// State: Wait for start of the next hand. // State: Wait for start of the next hand.
@@ -375,15 +367,17 @@ class ClientStateWaitHand : public AbstractClientStateReceiving
public: public:
// Access the state singleton. // Access the state singleton.
static ClientStateWaitHand &Instance(); static ClientStateWaitHand &Instance();
virtual ~ClientStateWaitHand(); virtual ~ClientStateWaitHand();
virtual void Enter(boost::shared_ptr<ClientThread> client);
virtual void Exit(boost::shared_ptr<ClientThread> client);
protected: protected:
// Protected constructor - this is a singleton. // Protected constructor - this is a singleton.
ClientStateWaitHand(); ClientStateWaitHand();
virtual int InternalProcess(ClientThread &client, boost::shared_ptr<NetPacket> packet); virtual void InternalHandlePacket(boost::shared_ptr<ClientThread> client, boost::shared_ptr<NetPacket> tmpPacket);
}; };
// State: Hand Loop. // State: Hand Loop.
@@ -392,36 +386,20 @@ class ClientStateRunHand : public AbstractClientStateReceiving
public: public:
// Access the state singleton. // Access the state singleton.
static ClientStateRunHand &Instance(); static ClientStateRunHand &Instance();
virtual ~ClientStateRunHand(); virtual ~ClientStateRunHand();
virtual void Enter(boost::shared_ptr<ClientThread> client);
virtual void Exit(boost::shared_ptr<ClientThread> client);
protected: protected:
// Protected constructor - this is a singleton. // Protected constructor - this is a singleton.
ClientStateRunHand(); ClientStateRunHand();
virtual int InternalProcess(ClientThread &client, boost::shared_ptr<NetPacket> packet); virtual void InternalHandlePacket(boost::shared_ptr<ClientThread> client, boost::shared_ptr<NetPacket> tmpPacket);
static void ResetPlayerActions(Game &curGame); static void ResetPlayerActions(Game &curGame);
static void ResetPlayerSets(Game &curGame); static void ResetPlayerSets(Game &curGame);
}; };
// State: Final (just for testing, should not be used).
class ClientStateFinal : public ClientState
{
public:
// Access the state singleton.
static ClientStateFinal &Instance();
virtual ~ClientStateFinal();
// sleep.
virtual int Process(ClientThread &client);
protected:
// Protected constructor - this is a singleton.
ClientStateFinal();
};
#endif #endif
+16 -3
View File
@@ -23,6 +23,7 @@
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <string> #include <string>
#include <core/thread.h> #include <core/thread.h>
@@ -42,7 +43,7 @@ class NetPacket;
class AvatarManager; class AvatarManager;
class QtToolsInterface; class QtToolsInterface;
class ClientThread : public Thread class ClientThread : public Thread, public boost::enable_shared_from_this<ClientThread>
{ {
public: public:
ClientThread(GuiInterface &gui, AvatarManager &avatarManager); ClientThread(GuiInterface &gui, AvatarManager &avatarManager);
@@ -63,6 +64,7 @@ public:
const std::string &playerName, const std::string &playerName,
const std::string &avatarFile, const std::string &avatarFile,
const std::string &cacheDir); const std::string &cacheDir);
virtual void SignalTermination();
void SendKickPlayer(unsigned playerId); void SendKickPlayer(unsigned playerId);
void SendLeaveCurrentGame(); void SendLeaveCurrentGame();
@@ -76,6 +78,9 @@ public:
void SendAskKickPlayer(unsigned playerId); void SendAskKickPlayer(unsigned playerId);
void SendVoteKick(bool doKick); void SendVoteKick(bool doKick);
void StartAsyncRead();
void HandleRead(const boost::system::error_code& ec, size_t bytesRead);
void SelectServer(unsigned serverId); void SelectServer(unsigned serverId);
ServerInfo GetServerInfo(unsigned serverId) const; ServerInfo GetServerInfo(unsigned serverId) const;
@@ -98,9 +103,12 @@ protected:
// Main function of the thread. // Main function of the thread.
virtual void Main(); virtual void Main();
void RegisterTimers();
void CancelTimers();
void InitGame();
void AddPacket(boost::shared_ptr<NetPacket> packet); void AddPacket(boost::shared_ptr<NetPacket> packet);
void SendPacketLoop(); void TimerSendPacketLoop(const boost::system::error_code &ec);
bool GetCachedPlayerInfo(unsigned id, PlayerInfo &info) const; bool GetCachedPlayerInfo(unsigned id, PlayerInfo &info) const;
void RequestPlayerInfo(unsigned id, bool requestAvatar = false); void RequestPlayerInfo(unsigned id, bool requestAvatar = false);
@@ -115,7 +123,7 @@ protected:
void PassAvatarDataToManager(unsigned playerId, boost::shared_ptr<AvatarData> avatarData); void PassAvatarDataToManager(unsigned playerId, boost::shared_ptr<AvatarData> avatarData);
void SetUnknownAvatar(unsigned playerId); void SetUnknownAvatar(unsigned playerId);
void CheckAvatarDownloads(); void TimerCheckAvatarDownloads(const boost::system::error_code& ec);
void UnsubscribeLobbyMsg(); void UnsubscribeLobbyMsg();
void ResubscribeLobbyMsg(); void ResubscribeLobbyMsg();
@@ -126,6 +134,7 @@ protected:
ClientState &GetState(); ClientState &GetState();
void SetState(ClientState &newState); void SetState(ClientState &newState);
boost::asio::deadline_timer &GetStateTimer();
SenderHelper &GetSender(); SenderHelper &GetSender();
ReceiverHelper &GetReceiver(); ReceiverHelper &GetReceiver();
@@ -235,6 +244,10 @@ private:
mutable boost::mutex m_curStatsMutex; mutable boost::mutex m_curStatsMutex;
ServerStats m_curStats; ServerStats m_curStats;
boost::asio::deadline_timer m_stateTimer;
boost::asio::deadline_timer m_avatarTimer;
boost::asio::deadline_timer m_sendTimer;
friend class AbstractClientStateReceiving; friend class AbstractClientStateReceiving;
friend class ClientStateInit; friend class ClientStateInit;
friend class ClientStateStartResolve; friend class ClientStateStartResolve;
+12 -8
View File
@@ -24,7 +24,6 @@ ClientContext::ClientContext()
: m_protocol(0), m_addrFamily(AF_INET), m_useServerList(false), m_serverPort(0), : m_protocol(0), m_addrFamily(AF_INET), m_useServerList(false), m_serverPort(0),
m_hasSubscribedLobbyMsg(true) m_hasSubscribedLobbyMsg(true)
{ {
bzero(&m_clientSockaddr, sizeof(m_clientSockaddr));
} }
ClientContext::~ClientContext() ClientContext::~ClientContext()
@@ -32,13 +31,6 @@ ClientContext::~ClientContext()
m_sessionData.reset(); m_sessionData.reset();
} }
SOCKET
ClientContext::GetSocket() const
{
assert(m_sessionData.get());
return m_sessionData->GetSocket();
}
boost::shared_ptr<SessionData> boost::shared_ptr<SessionData>
ClientContext::GetSessionData() const ClientContext::GetSessionData() const
{ {
@@ -51,3 +43,15 @@ ClientContext::SetSessionData(boost::shared_ptr<SessionData> sessionData)
m_sessionData = sessionData; m_sessionData = sessionData;
} }
boost::shared_ptr<boost::asio::ip::tcp::resolver>
ClientContext::GetResolver() const
{
return m_resolver;
}
void
ClientContext::SetResolver(boost::shared_ptr<boost::asio::ip::tcp::resolver> resolver)
{
m_resolver = resolver;
}
File diff suppressed because it is too large Load Diff
+110 -30
View File
@@ -39,6 +39,8 @@
#include <cassert> #include <cassert>
#define TEMP_AVATAR_FILENAME "avatar.tmp" #define TEMP_AVATAR_FILENAME "avatar.tmp"
#define CLIENT_AVATAR_LOOP_MSEC 100
#define CLIENT_SEND_LOOP_MSEC 50
using namespace std; using namespace std;
using boost::asio::ip::tcp; using boost::asio::ip::tcp;
@@ -62,10 +64,11 @@ private:
}; };
ClientThread::ClientThread(GuiInterface &gui, AvatarManager &avatarManager) ClientThread::ClientThread(GuiInterface &gui, AvatarManager &avatarManager)
: m_curState(NULL), m_gui(gui), m_avatarManager(avatarManager), m_isServerSelected(false), : m_ioService(new boost::asio::io_service), m_curState(NULL), m_gui(gui),
m_curGameId(0), m_curGameNum(1), m_guiPlayerId(0), m_sessionEstablished(false) m_avatarManager(avatarManager), m_isServerSelected(false),
m_curGameId(0), m_curGameNum(1), m_guiPlayerId(0), m_sessionEstablished(false),
m_stateTimer(*m_ioService), m_avatarTimer(*m_ioService), m_sendTimer(*m_ioService)
{ {
m_ioService.reset(new boost::asio::io_service());
m_context.reset(new ClientContext); m_context.reset(new ClientContext);
m_receiver.reset(new ReceiverHelper); m_receiver.reset(new ReceiverHelper);
myQtToolsInterface.reset(CreateQtToolsWrapper()); myQtToolsInterface.reset(CreateQtToolsWrapper());
@@ -106,6 +109,13 @@ ClientThread::Init(
context.SetCacheDir(cacheDir); context.SetCacheDir(cacheDir);
} }
void
ClientThread::SignalTermination()
{
Thread::SignalTermination();
m_ioService->stop();
}
void void
ClientThread::SendKickPlayer(unsigned playerId) ClientThread::SendKickPlayer(unsigned playerId)
{ {
@@ -286,6 +296,27 @@ ClientThread::SendVoteKick(bool doKick)
m_outPacketList.push_back(vote); m_outPacketList.push_back(vote);
} }
void
ClientThread::StartAsyncRead()
{
ReceiveBuffer &buf = GetContext().GetSessionData()->GetReceiveBuffer();
GetContext().GetSessionData()->GetAsioSocket()->async_read_some(
boost::asio::buffer(buf.recvBuf + buf.recvBufUsed, RECV_BUF_SIZE - buf.recvBufUsed),
boost::bind(
&ClientThread::HandleRead,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void
ClientThread::HandleRead(const boost::system::error_code& ec, size_t bytesRead)
{
GetState().HandleRead(ec, shared_from_this(), bytesRead);
if (!ec)
StartAsyncRead();
}
void void
ClientThread::SelectServer(unsigned serverId) ClientThread::SelectServer(unsigned serverId)
{ {
@@ -381,23 +412,53 @@ ClientThread::Main()
m_avatarDownloader.reset(new DownloaderThread); m_avatarDownloader.reset(new DownloaderThread);
m_avatarDownloader->Run(); m_avatarDownloader->Run();
SetState(CLIENT_INITIAL_STATE::Instance()); SetState(CLIENT_INITIAL_STATE::Instance());
RegisterTimers();
// Main loop. // Main loop.
boost::asio::io_service::work ioWork(*m_ioService); boost::asio::io_service::work ioWork(*m_ioService);
try try
{ {
while (!ShouldTerminate())
{ {
int msg = GetState().Process(*this); boost::asio::io_service::work ioWork(*m_ioService);
if (msg != MSG_SOCK_INTERNAL_PENDING) m_ioService->run(); // Will only be aborted asynchronously.
}
} catch (const PokerTHException &e)
{ {
if (msg <= MSG_SOCK_LIMIT_CONNECT) GetCallback().SignalNetClientError(e.GetErrorId(), e.GetOsErrorCode());
GetCallback().SignalNetClientConnect(msg); }
else // Cancel timers.
GetCallback().SignalNetClientGameInfo(msg); GetStateTimer().cancel();
CancelTimers();
// Terminate sub-threads.
m_avatarDownloader->SignalTermination();
m_avatarDownloader->Join(DOWNLOADER_THREAD_TERMINATE_TIMEOUT);
}
// Additionally signal the start of the game. void
if (msg == MSG_NET_GAME_CLIENT_START) ClientThread::RegisterTimers()
{
m_avatarTimer.expires_from_now(
boost::posix_time::milliseconds(CLIENT_AVATAR_LOOP_MSEC));
m_avatarTimer.async_wait(
boost::bind(
&ClientThread::TimerCheckAvatarDownloads, shared_from_this(), boost::asio::placeholders::error));
m_sendTimer.expires_from_now(
boost::posix_time::milliseconds(CLIENT_SEND_LOOP_MSEC));
m_sendTimer.async_wait(
boost::bind(
&ClientThread::TimerSendPacketLoop, shared_from_this(), boost::asio::placeholders::error));
}
void
ClientThread::CancelTimers()
{
m_avatarTimer.cancel();
m_sendTimer.cancel();
}
void
ClientThread::InitGame()
{ {
// EngineFactory erstellen // EngineFactory erstellen
boost::shared_ptr<EngineFactory> factory(new ClientEngineFactory); // LocalEngine erstellen boost::shared_ptr<EngineFactory> factory(new ClientEngineFactory); // LocalEngine erstellen
@@ -411,20 +472,6 @@ ClientThread::Main()
// Signal start of game to GUI. // Signal start of game to GUI.
GetCallback().SignalNetClientGameStart(m_game); GetCallback().SignalNetClientGameStart(m_game);
} }
}
if (IsSessionEstablished())
SendPacketLoop();
m_ioService->poll();
Thread::Msleep(10);
}
} catch (const PokerTHException &e)
{
GetCallback().SignalNetClientError(e.GetErrorId(), e.GetOsErrorCode());
}
// Terminate sub-threads.
m_avatarDownloader->SignalTermination();
m_avatarDownloader->Join(DOWNLOADER_THREAD_TERMINATE_TIMEOUT);
}
void void
ClientThread::AddPacket(boost::shared_ptr<NetPacket> packet) ClientThread::AddPacket(boost::shared_ptr<NetPacket> packet)
@@ -434,7 +481,11 @@ ClientThread::AddPacket(boost::shared_ptr<NetPacket> packet)
} }
void void
ClientThread::SendPacketLoop() ClientThread::TimerSendPacketLoop(const boost::system::error_code &ec)
{
if (!ec)
{
if (IsSessionEstablished())
{ {
boost::mutex::scoped_lock lock(m_outPacketListMutex); boost::mutex::scoped_lock lock(m_outPacketListMutex);
@@ -451,6 +502,13 @@ ClientThread::SendPacketLoop()
m_outPacketList.clear(); m_outPacketList.clear();
} }
} }
m_sendTimer.expires_from_now(
boost::posix_time::milliseconds(CLIENT_SEND_LOOP_MSEC));
m_sendTimer.async_wait(
boost::bind(
&ClientThread::TimerSendPacketLoop, shared_from_this(), boost::asio::placeholders::error));
}
}
bool bool
ClientThread::GetCachedPlayerInfo(unsigned id, PlayerInfo &info) const ClientThread::GetCachedPlayerInfo(unsigned id, PlayerInfo &info) const
@@ -668,7 +726,9 @@ ClientThread::SetUnknownAvatar(unsigned playerId)
} }
void void
ClientThread::CheckAvatarDownloads() ClientThread::TimerCheckAvatarDownloads(const boost::system::error_code& ec)
{
if (!ec)
{ {
if (m_avatarDownloader && m_avatarDownloader->HasDownloadResult()) if (m_avatarDownloader && m_avatarDownloader->HasDownloadResult())
{ {
@@ -678,6 +738,12 @@ ClientThread::CheckAvatarDownloads()
tmpAvatar->reportedSize = tmpAvatar->fileData.size(); tmpAvatar->reportedSize = tmpAvatar->fileData.size();
PassAvatarDataToManager(playerId, tmpAvatar); PassAvatarDataToManager(playerId, tmpAvatar);
} }
m_avatarTimer.expires_from_now(
boost::posix_time::milliseconds(CLIENT_AVATAR_LOOP_MSEC));
m_avatarTimer.async_wait(
boost::bind(
&ClientThread::TimerCheckAvatarDownloads, shared_from_this(), boost::asio::placeholders::error));
}
} }
void void
@@ -724,10 +790,13 @@ void
ClientThread::CreateContextSession() ClientThread::CreateContextSession()
{ {
bool validSocket = false; bool validSocket = false;
// TODO ipv6
// TODO sctp // TODO sctp
try { try {
boost::shared_ptr<tcp::socket> newSock(new boost::asio::ip::tcp::socket(*m_ioService, tcp::v4())); boost::shared_ptr<tcp::socket> newSock;
if (GetContext().GetAddrFamily() == AF_INET6)
newSock.reset(new boost::asio::ip::tcp::socket(*m_ioService, tcp::v6()));
else
newSock.reset(new boost::asio::ip::tcp::socket(*m_ioService, tcp::v4()));
boost::asio::socket_base::non_blocking_io command(true); boost::asio::socket_base::non_blocking_io command(true);
newSock->io_control(command); newSock->io_control(command);
newSock->set_option(tcp::no_delay(true)); newSock->set_option(tcp::no_delay(true));
@@ -737,6 +806,8 @@ ClientThread::CreateContextSession()
newSock, newSock,
SESSION_ID_GENERIC, SESSION_ID_GENERIC,
*m_senderCallback))); *m_senderCallback)));
GetContext().SetResolver(boost::shared_ptr<boost::asio::ip::tcp::resolver>(
new boost::asio::ip::tcp::resolver(*m_ioService)));
validSocket = true; validSocket = true;
} catch (...) } catch (...)
{ {
@@ -755,7 +826,16 @@ ClientThread::GetState()
void void
ClientThread::SetState(ClientState &newState) ClientThread::SetState(ClientState &newState)
{ {
if (m_curState)
m_curState->Exit(shared_from_this());
m_curState = &newState; m_curState = &newState;
m_curState->Enter(shared_from_this());
}
boost::asio::deadline_timer &
ClientThread::GetStateTimer()
{
return m_stateTimer;
} }
SenderHelper & SenderHelper &
-42
View File
@@ -1,42 +0,0 @@
/***************************************************************************
* Copyright (C) 2007 by Lothar May *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <net/servercontext.h>
ServerContext::ServerContext()
: m_protocol(0), m_addrFamily(AF_INET), m_serverPort(0)
{
}
ServerContext::~ServerContext()
{
}
SOCKET
ServerContext::GetSocket() const
{
return m_sock;
}
void
ServerContext::SetSocket(SOCKET sock)
{
m_sock = sock;
}
+7 -6
View File
@@ -165,8 +165,8 @@ ServerLobbyThread::AddConnection(boost::shared_ptr<tcp::socket> sock)
boost::bind( boost::bind(
&ServerLobbyThread::HandleRead, &ServerLobbyThread::HandleRead,
this, this,
sessionData->GetId(),
boost::asio::placeholders::error, boost::asio::placeholders::error,
sessionData->GetId(),
boost::asio::placeholders::bytes_transferred)); boost::asio::placeholders::bytes_transferred));
} }
} }
@@ -513,9 +513,10 @@ ServerLobbyThread::Main()
RegisterTimers(); RegisterTimers();
try try
{ {
m_work.reset(new boost::asio::io_service::work(*m_ioService)); {
boost::asio::io_service::work ioWork(*m_ioService);
m_ioService->run(); // Will only be aborted asynchronously. m_ioService->run(); // Will only be aborted asynchronously.
m_work.reset(); }
// Clear all sessions. // Clear all sessions.
m_sessionManager.Clear(); m_sessionManager.Clear();
@@ -585,7 +586,7 @@ ServerLobbyThread::CancelTimers()
} }
void void
ServerLobbyThread::HandleRead(SessionId sessionId, const boost::system::error_code &error, size_t bytesRead) ServerLobbyThread::HandleRead(const boost::system::error_code &ec, SessionId sessionId, size_t bytesRead)
{ {
// Find the session. // Find the session.
SessionWrapper session = m_sessionManager.GetSessionById(sessionId); SessionWrapper session = m_sessionManager.GetSessionById(sessionId);
@@ -593,7 +594,7 @@ ServerLobbyThread::HandleRead(SessionId sessionId, const boost::system::error_co
session = m_gameSessionManager.GetSessionById(sessionId); session = m_gameSessionManager.GetSessionById(sessionId);
if (session.sessionData) if (session.sessionData)
{ {
if (!error) if (!ec)
{ {
ReceiveBuffer &buf = session.sessionData->GetReceiveBuffer(); ReceiveBuffer &buf = session.sessionData->GetReceiveBuffer();
buf.recvBufUsed += bytesRead; buf.recvBufUsed += bytesRead;
@@ -625,8 +626,8 @@ ServerLobbyThread::HandleRead(SessionId sessionId, const boost::system::error_co
boost::bind( boost::bind(
&ServerLobbyThread::HandleRead, &ServerLobbyThread::HandleRead,
this, this,
sessionId,
boost::asio::placeholders::error, boost::asio::placeholders::error,
sessionId,
boost::asio::placeholders::bytes_transferred)); boost::asio::placeholders::bytes_transferred));
} }
else else
+1 -2
View File
@@ -19,7 +19,6 @@
#include <net/socket_helper.h> #include <net/socket_helper.h>
#include <net/servermanager.h> #include <net/servermanager.h>
#include <net/servercontext.h>
#include <net/ircthread.h> #include <net/ircthread.h>
#include <net/connectdata.h> #include <net/connectdata.h>
#include <net/serverlobbythread.h> #include <net/serverlobbythread.h>
@@ -39,7 +38,7 @@ using namespace std;
ServerManager::ServerManager(GuiInterface &gui, ConfigFile *config, AvatarManager &avatarManager) ServerManager::ServerManager(GuiInterface &gui, ConfigFile *config, AvatarManager &avatarManager)
: m_gui(gui), m_playerConfig(config), m_avatarManager(avatarManager) : m_gui(gui), m_playerConfig(config), m_avatarManager(avatarManager)
{ {
m_ioService.reset(new boost::asio::io_service()); m_ioService.reset(new boost::asio::io_service);
} }
ServerManager::~ServerManager() ServerManager::~ServerManager()
-57
View File
@@ -1,57 +0,0 @@
/***************************************************************************
* Copyright (C) 2007 by Lothar May *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/* Context of network server. */
#ifndef _SERVERCONTEXT_H_
#define _SERVERCONTEXT_H_
#include <net/netcontext.h>
class ServerContext : public NetContext
{
public:
ServerContext();
virtual ~ServerContext();
virtual SOCKET GetSocket() const;
void SetSocket(SOCKET sock);
int GetProtocol() const
{return m_protocol;}
void SetProtocol(int protocol)
{m_protocol = protocol;}
int GetAddrFamily() const
{return m_addrFamily;}
void SetAddrFamily(int addrFamily)
{m_addrFamily = addrFamily;}
unsigned GetServerPort() const
{return m_serverPort;}
void SetServerPort(unsigned serverPort)
{m_serverPort = serverPort;}
private:
SOCKET m_sock;
int m_protocol;
int m_addrFamily;
unsigned m_serverPort;
};
#endif
+1 -2
View File
@@ -115,7 +115,7 @@ protected:
void RegisterTimers(); void RegisterTimers();
void CancelTimers(); void CancelTimers();
void HandleRead(SessionId sessionId, const boost::system::error_code &error, size_t bytesRead); void HandleRead(const boost::system::error_code &ec, SessionId sessionId, size_t bytesRead);
void HandlePacket(SessionWrapper session, boost::shared_ptr<NetPacket> packet); void HandlePacket(SessionWrapper session, boost::shared_ptr<NetPacket> packet);
void HandleNetPacketInit(SessionWrapper session, const NetPacketInit &tmpPacket); void HandleNetPacketInit(SessionWrapper session, const NetPacketInit &tmpPacket);
void HandleNetPacketAvatarHeader(SessionWrapper session, const NetPacketAvatarHeader &tmpPacket); void HandleNetPacketAvatarHeader(SessionWrapper session, const NetPacketAvatarHeader &tmpPacket);
@@ -173,7 +173,6 @@ protected:
private: private:
boost::shared_ptr<boost::asio::io_service> m_ioService; boost::shared_ptr<boost::asio::io_service> m_ioService;
boost::shared_ptr<boost::asio::io_service::work> m_work;
boost::shared_ptr<ServerSenderCallback> m_senderCallback; boost::shared_ptr<ServerSenderCallback> m_senderCallback;
boost::shared_ptr<SenderHelper> m_sender; boost::shared_ptr<SenderHelper> m_sender;
+12 -16
View File
@@ -43,8 +43,7 @@
using namespace std; using namespace std;
Session::Session(GuiInterface *g, ConfigFile *c) Session::Session(GuiInterface *g, ConfigFile *c)
: currentGameNum(0), myNetClient(NULL), myNetServer(NULL), myClientIrcThread(NULL), : currentGameNum(0), myGui(g), myConfig(c), myGameType(GAME_TYPE_NONE)
myGui(g), myConfig(c), myGameType(GAME_TYPE_NONE)
{ {
myQtToolsInterface = CreateQtToolsWrapper(); myQtToolsInterface = CreateQtToolsWrapper();
} }
@@ -165,7 +164,7 @@ void Session::startInternetClient()
if (myConfig->readConfigInt("UseIRCLobbyChat")) if (myConfig->readConfigInt("UseIRCLobbyChat"))
{ {
myClientIrcThread = new IrcThread(myGui); myClientIrcThread.reset(new IrcThread(myGui));
myClientIrcThread->Init( myClientIrcThread->Init(
myConfig->readConfigString("IRCServerAddress"), myConfig->readConfigString("IRCServerAddress"),
myConfig->readConfigInt("IRCServerPort"), myConfig->readConfigInt("IRCServerPort"),
@@ -180,7 +179,7 @@ void Session::startInternetClient()
myClientIrcThread->Run(); myClientIrcThread->Run();
} }
myNetClient = new ClientThread(*myGui, *myAvatarManager); myNetClient.reset(new ClientThread(*myGui, *myAvatarManager));
bool useAvatarServer = myConfig->readConfigInt("UseAvatarServer") != 0; bool useAvatarServer = myConfig->readConfigInt("UseAvatarServer") != 0;
myNetClient->Init( myNetClient->Init(
@@ -211,7 +210,7 @@ void Session::startNetworkClient(const string &serverAddress, unsigned serverPor
} }
myGameType = GAME_TYPE_NETWORK; myGameType = GAME_TYPE_NETWORK;
myNetClient = new ClientThread(*myGui, *myAvatarManager); myNetClient.reset(new ClientThread(*myGui, *myAvatarManager));
myNetClient->Init( myNetClient->Init(
serverAddress, serverAddress,
"", "",
@@ -237,7 +236,7 @@ void Session::startNetworkClientForLocalServer(const GameData &gameData)
} }
myGameType = GAME_TYPE_NETWORK; myGameType = GAME_TYPE_NETWORK;
myNetClient = new ClientThread(*myGui, *myAvatarManager); myNetClient.reset(new ClientThread(*myGui, *myAvatarManager));
bool useIpv6 = myConfig->readConfigInt("ServerUseIpv6") == 1; bool useIpv6 = myConfig->readConfigInt("ServerUseIpv6") == 1;
const char *loopbackAddr = useIpv6 ? "::1" : "127.0.0.1"; const char *loopbackAddr = useIpv6 ? "::1" : "127.0.0.1";
myNetClient->Init( myNetClient->Init(
@@ -265,13 +264,11 @@ void Session::terminateNetworkClient()
myClientIrcThread->SignalTermination(); myClientIrcThread->SignalTermination();
// Give the threads some time to terminate. // Give the threads some time to terminate.
if (myNetClient->Join(NET_CLIENT_TERMINATE_TIMEOUT_MSEC)) if (myNetClient->Join(NET_CLIENT_TERMINATE_TIMEOUT_MSEC))
delete myNetClient; myNetClient.reset();
if (myClientIrcThread && myClientIrcThread->Join(NET_IRC_TERMINATE_TIMEOUT_MSEC)) if (myClientIrcThread && myClientIrcThread->Join(NET_IRC_TERMINATE_TIMEOUT_MSEC))
delete myClientIrcThread; myClientIrcThread.reset();
// If termination fails, leave a memory leak to prevent a crash. // If termination fails, leave a memory leak to prevent a crash.
myNetClient = 0;
myClientIrcThread = 0;
myGameType = GAME_TYPE_NONE; myGameType = GAME_TYPE_NONE;
} }
@@ -297,12 +294,12 @@ void Session::startNetworkServer()
return; return;
} }
myNetServer = new ServerManager(*myGui, myConfig, *myAvatarManager); myNetServer.reset(new ServerManager(*myGui, myConfig, *myAvatarManager));
boost::shared_ptr<IrcThread> tmpIrcThread; boost::shared_ptr<IrcThread> tmpIrcThread;
if (myConfig->readConfigInt("UseAdminIRC")) if (myConfig->readConfigInt("UseAdminIRC"))
{ {
tmpIrcThread = boost::shared_ptr<IrcThread>(new IrcThread(myNetServer)); tmpIrcThread = boost::shared_ptr<IrcThread>(new IrcThread(myNetServer.get()));
tmpIrcThread->Init( tmpIrcThread->Init(
myConfig->readConfigString("AdminIRCServerAddress"), myConfig->readConfigString("AdminIRCServerAddress"),
@@ -332,9 +329,8 @@ void Session::terminateNetworkServer()
myNetServer->SignalTerminationAll(); myNetServer->SignalTerminationAll();
// Give the thread some time to terminate. // Give the thread some time to terminate.
if (myNetServer->JoinAll(true)) if (myNetServer->JoinAll(true))
delete myNetServer; myNetServer.reset();
// If termination fails, leave a memory leak to prevent a crash. // If termination fails, leave a memory leak to prevent a crash.
myNetServer = 0;
} }
bool Session::pollNetworkServerTerminated() bool Session::pollNetworkServerTerminated()
@@ -433,13 +429,13 @@ void Session::voteKick(bool doKick)
bool Session::isNetworkClientRunning() const bool Session::isNetworkClientRunning() const
{ {
// This, and every place which calls this, is a HACK. // This, and every place which calls this, is a HACK.
return myNetClient != NULL; return myNetClient.get() != NULL;
} }
bool Session::isNetworkServerRunning() const bool Session::isNetworkServerRunning() const
{ {
// This, and every place which calls this, is a HACK. // This, and every place which calls this, is a HACK.
return myNetServer != NULL; return myNetServer.get() != NULL;
} }
ServerInfo Session::getClientServerInfo(unsigned serverId) const ServerInfo Session::getClientServerInfo(unsigned serverId) const
+3 -3
View File
@@ -106,9 +106,9 @@ private:
std::string myIrcNick; std::string myIrcNick;
ClientThread *myNetClient; boost::shared_ptr<ClientThread> myNetClient;
ServerManager *myNetServer; boost::shared_ptr<ServerManager> myNetServer;
IrcThread *myClientIrcThread; boost::shared_ptr<IrcThread> myClientIrcThread;
boost::shared_ptr<AvatarManager> myAvatarManager; boost::shared_ptr<AvatarManager> myAvatarManager;