Fixed local network game. Now using new config options. Client sends packets deferred to allow initialization.

This commit is contained in:
lotodore
2007-08-26 12:15:10 +00:00
parent ba811cee6f
commit 87941c632a
7 changed files with 129 additions and 28 deletions
+4 -4
View File
@@ -761,8 +761,8 @@ void mainWindowImpl::callCreateNetworkGameDialog() {
myStartNetworkGameDialog->setSession(&getSession()); myStartNetworkGameDialog->setSession(&getSession());
myStartNetworkGameDialog->treeWidget->clear(); myStartNetworkGameDialog->treeWidget->clear();
myServerGuiInterface->getSession().startNetworkServer(gameData); myServerGuiInterface->getSession().startNetworkServer();
mySession->startNetworkClientForLocalServer(); mySession->startNetworkClientForLocalServer(gameData);
myStartNetworkGameDialog->setMaxPlayerNumber(gameData.maxNumberOfPlayers); myStartNetworkGameDialog->setMaxPlayerNumber(gameData.maxNumberOfPlayers);
@@ -821,8 +821,8 @@ void mainWindowImpl::callGameLobbyDialog() {
myStartNetworkGameDialog->treeWidget->clear(); myStartNetworkGameDialog->treeWidget->clear();
myStartNetworkGameDialog->setSession(&getSession()); myStartNetworkGameDialog->setSession(&getSession());
// Just for testing // Start client for dedicated server.
mySession->startNetworkClientForLocalServer(); mySession->startInternetClient();
myGameLobbyDialog->exec(); myGameLobbyDialog->exec();
+14
View File
@@ -27,6 +27,7 @@
#include <gamedata.h> #include <gamedata.h>
#include <string> #include <string>
#include <memory> #include <memory>
#include <boost/shared_ptr.hpp>
class ClientContext; class ClientContext;
class ClientState; class ClientState;
@@ -34,6 +35,7 @@ class SenderThread;
class ReceiverHelper; class ReceiverHelper;
class ClientSenderCallback; class ClientSenderCallback;
class Game; class Game;
class NetPacket;
class ClientThread : public Thread class ClientThread : public Thread
{ {
@@ -56,6 +58,7 @@ public:
void SendStartEvent(); void SendStartEvent();
void SendPlayerAction(); void SendPlayerAction();
void SendChatMessage(const std::string &msg); void SendChatMessage(const std::string &msg);
void SendJoinFirstGame(const std::string &password);
void SendJoinGame(const std::string &name, const std::string &password); void SendJoinGame(const std::string &name, const std::string &password);
void SendCreateGame(const GameData &gameData, const std::string &name, const std::string &password); void SendCreateGame(const GameData &gameData, const std::string &name, const std::string &password);
@@ -64,10 +67,14 @@ public:
protected: protected:
typedef std::map<unsigned, std::string> GameMap; typedef std::map<unsigned, std::string> GameMap;
typedef std::list<boost::shared_ptr<NetPacket> > NetPacketList;
// Main function of the thread. // Main function of the thread.
virtual void Main(); virtual void Main();
void AddPacket(boost::shared_ptr<NetPacket> packet);
void SendPacketLoop();
const ClientContext &GetContext() const; const ClientContext &GetContext() const;
ClientContext &GetContext(); ClientContext &GetContext();
@@ -101,8 +108,14 @@ protected:
void AddGameInformation(unsigned id, const std::string &name); void AddGameInformation(unsigned id, const std::string &name);
void RemoveGameInformation(unsigned id); void RemoveGameInformation(unsigned id);
bool IsSessionEstablished() const;
void SetSessionEstablished(bool flag);
private: private:
NetPacketList m_outPacketList;
mutable boost::mutex m_outPacketListMutex;
std::auto_ptr<ClientContext> m_context; std::auto_ptr<ClientContext> m_context;
std::auto_ptr<ClientSenderCallback> m_senderCallback; std::auto_ptr<ClientSenderCallback> m_senderCallback;
ClientState *m_curState; ClientState *m_curState;
@@ -122,6 +135,7 @@ private:
unsigned m_curGameId; unsigned m_curGameId;
unsigned m_guiPlayerId; unsigned m_guiPlayerId;
bool m_sessionEstablished;
friend class AbstractClientStateReceiving; friend class AbstractClientStateReceiving;
friend class ClientStateInit; friend class ClientStateInit;
+1
View File
@@ -442,6 +442,7 @@ ClientStateWaitSession::InternalProcess(ClientThread &client, boost::shared_ptr<
client.SetGuiPlayerId(initAckData.playerId); client.SetGuiPlayerId(initAckData.playerId);
client.SetState(ClientStateWaitJoin::Instance()); client.SetState(ClientStateWaitJoin::Instance());
client.SetSessionEstablished(true);
retVal = MSG_SOCK_SESSION_DONE; retVal = MSG_SOCK_SESSION_DONE;
} }
+81 -10
View File
@@ -53,7 +53,7 @@ private:
ClientThread::ClientThread(GuiInterface &gui) ClientThread::ClientThread(GuiInterface &gui)
: m_curState(NULL), m_gui(gui), m_curGameId(1), m_guiPlayerId(0) : m_curState(NULL), m_gui(gui), m_curGameId(1), m_guiPlayerId(0), m_sessionEstablished(false)
{ {
m_context.reset(new ClientContext); m_context.reset(new ClientContext);
m_senderCallback.reset(new ClientSenderCallback(*this)); m_senderCallback.reset(new ClientSenderCallback(*this));
@@ -95,7 +95,8 @@ ClientThread::SendKickPlayer(const string &playerName)
NetPacketKickPlayer::Data requestData; NetPacketKickPlayer::Data requestData;
requestData.playerId = tmpPlayer->GetUniqueId(); requestData.playerId = tmpPlayer->GetUniqueId();
static_cast<NetPacketKickPlayer *>(request.get())->SetData(requestData); static_cast<NetPacketKickPlayer *>(request.get())->SetData(requestData);
GetSender().Send(GetContext().GetSocket(), request); boost::mutex::scoped_lock lock(m_outPacketListMutex);
m_outPacketList.push_back(request);
} }
} }
@@ -105,8 +106,9 @@ ClientThread::SendStartEvent()
// Warning: This function is called in the context of the GUI thread. // Warning: This function is called in the context of the GUI thread.
// Create a network packet for the server start event. // Create a network packet for the server start event.
boost::shared_ptr<NetPacket> startEvent(new NetPacketStartEvent); boost::shared_ptr<NetPacket> startEvent(new NetPacketStartEvent);
// The sender is thread-safe, so just dump the packet. // Just dump the packet.
GetSender().Send(GetContext().GetSocket(), startEvent); boost::mutex::scoped_lock lock(m_outPacketListMutex);
m_outPacketList.push_back(startEvent);
} }
void void
@@ -123,9 +125,15 @@ ClientThread::SendPlayerAction()
actionData.playerBet = GetGame()->getPlayerArray()[0]->getMyLastRelativeSet(); actionData.playerBet = GetGame()->getPlayerArray()[0]->getMyLastRelativeSet();
else else
actionData.playerBet = 0; actionData.playerBet = 0;
try
{
static_cast<NetPacketPlayersAction *>(action.get())->SetData(actionData); static_cast<NetPacketPlayersAction *>(action.get())->SetData(actionData);
// The sender is thread-safe, so just dump the packet. // Just dump the packet.
GetSender().Send(GetContext().GetSocket(), action); boost::mutex::scoped_lock lock(m_outPacketListMutex);
m_outPacketList.push_back(action);
} catch (const NetException &)
{
}
} }
void void
@@ -139,13 +147,34 @@ ClientThread::SendChatMessage(const std::string &msg)
try try
{ {
static_cast<NetPacketSendChatText *>(chat.get())->SetData(chatData); static_cast<NetPacketSendChatText *>(chat.get())->SetData(chatData);
// The sender is thread-safe, so just dump the packet. // Just dump the packet.
GetSender().Send(GetContext().GetSocket(), chat); boost::mutex::scoped_lock lock(m_outPacketListMutex);
m_outPacketList.push_back(chat);
} catch (const NetException &) } catch (const NetException &)
{ {
} }
} }
void
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<NetPacket> join(new NetPacketJoinGame);
NetPacketJoinGame::Data joinData;
joinData.gameId = 0;
joinData.password = password;
try
{
static_cast<NetPacketJoinGame *>(join.get())->SetData(joinData);
boost::mutex::scoped_lock lock(m_outPacketListMutex);
m_outPacketList.push_back(join);
} catch (const NetException &)
{
// TODO
}
}
void void
ClientThread::SendJoinGame(const std::string &name, const std::string &password) ClientThread::SendJoinGame(const std::string &name, const std::string &password)
{ {
@@ -158,7 +187,8 @@ ClientThread::SendJoinGame(const std::string &name, const std::string &password)
{ {
joinData.gameId = GetGameIdByName(name); joinData.gameId = GetGameIdByName(name);
static_cast<NetPacketJoinGame *>(join.get())->SetData(joinData); static_cast<NetPacketJoinGame *>(join.get())->SetData(joinData);
GetSender().Send(GetContext().GetSocket(), join); boost::mutex::scoped_lock lock(m_outPacketListMutex);
m_outPacketList.push_back(join);
} catch (const NetException &) } catch (const NetException &)
{ {
// TODO // TODO
@@ -178,7 +208,8 @@ ClientThread::SendCreateGame(const GameData &gameData, const std::string &name,
try try
{ {
static_cast<NetPacketCreateGame *>(create.get())->SetData(createData); static_cast<NetPacketCreateGame *>(create.get())->SetData(createData);
GetSender().Send(GetContext().GetSocket(), create); boost::mutex::scoped_lock lock(m_outPacketListMutex);
m_outPacketList.push_back(create);
} catch (const NetException &) } catch (const NetException &)
{ {
// TODO // TODO
@@ -232,6 +263,8 @@ ClientThread::Main()
GetCallback().SignalNetClientGameStart(m_game); GetCallback().SignalNetClientGameStart(m_game);
} }
} }
if (IsSessionEstablished())
SendPacketLoop();
} }
} catch (const NetException &e) } catch (const NetException &e)
{ {
@@ -241,6 +274,32 @@ ClientThread::Main()
GetSender().Join(SENDER_THREAD_TERMINATE_TIMEOUT); GetSender().Join(SENDER_THREAD_TERMINATE_TIMEOUT);
} }
void
ClientThread::AddPacket(boost::shared_ptr<NetPacket> packet)
{
boost::mutex::scoped_lock lock(m_outPacketListMutex);
m_outPacketList.push_back(packet);
}
void
ClientThread::SendPacketLoop()
{
boost::mutex::scoped_lock lock(m_outPacketListMutex);
if (!m_outPacketList.empty())
{
NetPacketList::iterator i = m_outPacketList.begin();
NetPacketList::iterator end = m_outPacketList.end();
while (i != end)
{
GetSender().Send(GetContext().GetSocket(), *i);
++i;
}
m_outPacketList.clear();
}
}
const ClientContext & const ClientContext &
ClientThread::GetContext() const ClientThread::GetContext() const
{ {
@@ -520,3 +579,15 @@ ClientThread::RemoveGameInformation(unsigned id)
GetCallback().SignalNetClientGameListRemove(name); GetCallback().SignalNetClientGameListRemove(name);
} }
bool
ClientThread::IsSessionEstablished() const
{
return m_sessionEstablished;
}
void
ClientThread::SetSessionEstablished(bool flag)
{
m_sessionEstablished = flag;
}
+1 -9
View File
@@ -78,15 +78,7 @@ main(int argc, char *argv[])
myServerGuiInterface->setSession(session); myServerGuiInterface->setSession(session);
} }
GameData gameData; myServerGuiInterface->getSession().startNetworkServer();
gameData.maxNumberOfPlayers = myConfig->readConfigInt("NetNumberOfPlayers");
gameData.startMoney = myConfig->readConfigInt("NetStartCash");
gameData.smallBlind = myConfig->readConfigInt("NetSmallBlind");
gameData.handsBeforeRaise = myConfig->readConfigInt("NetHandsBeforeRaiseSmallBlind");
gameData.guiSpeed = 4;
gameData.playerActionTimeoutSec = myConfig->readConfigInt("NetTimeOutPlayerAction");
myServerGuiInterface->getSession().startNetworkServer(gameData);
while (!g_pokerthTerminate) while (!g_pokerthTerminate)
{ {
myServerGuiInterface->getSession().waitForNetworkServer(100); myServerGuiInterface->getSession().waitForNetworkServer(100);
+24 -2
View File
@@ -31,6 +31,8 @@
#define NET_CLIENT_TERMINATE_TIMEOUT_MSEC 1000 #define NET_CLIENT_TERMINATE_TIMEOUT_MSEC 1000
#define NET_SERVER_TERMINATE_TIMEOUT_MSEC 1000 #define NET_SERVER_TERMINATE_TIMEOUT_MSEC 1000
#define NET_DEFAULT_GAME "default"
using namespace std; using namespace std;
Session::Session(GuiInterface *g, ConfigFile *c) Session::Session(GuiInterface *g, ConfigFile *c)
@@ -105,6 +107,24 @@ GuiInterface *Session::getGui()
return myGui; return myGui;
} }
void Session::startInternetClient()
{
if (myNetClient || !myGui)
{
assert(false);
return;
}
myNetClient = new ClientThread(*myGui);
myNetClient->Init(
myConfig->readConfigString("InternetServerAddress"),
myConfig->readConfigInt("InternetServerPort"),
myConfig->readConfigInt("InternetServerUseIpv6") == 1,
myConfig->readConfigInt("InternetServerUseSctp") == 1,
myConfig->readConfigString("InternetServerPassword"),
myConfig->readConfigString("MyName"));
myNetClient->Run();
}
void Session::startNetworkClient(const string &serverAddress, unsigned serverPort, bool ipv6, bool sctp, const string &pwd) void Session::startNetworkClient(const string &serverAddress, unsigned serverPort, bool ipv6, bool sctp, const string &pwd)
{ {
if (myNetClient || !myGui) if (myNetClient || !myGui)
@@ -121,9 +141,10 @@ void Session::startNetworkClient(const string &serverAddress, unsigned serverPor
pwd, pwd,
myConfig->readConfigString("MyName")); myConfig->readConfigString("MyName"));
myNetClient->Run(); myNetClient->Run();
myNetClient->SendJoinFirstGame("");
} }
void Session::startNetworkClientForLocalServer() void Session::startNetworkClientForLocalServer(const GameData &gameData)
{ {
if (myNetClient || !myGui) if (myNetClient || !myGui)
{ {
@@ -141,6 +162,7 @@ void Session::startNetworkClientForLocalServer()
myConfig->readConfigString("ServerPassword"), myConfig->readConfigString("ServerPassword"),
myConfig->readConfigString("MyName")); myConfig->readConfigString("MyName"));
myNetClient->Run(); myNetClient->Run();
myNetClient->SendCreateGame(gameData, NET_DEFAULT_GAME, "");
} }
void Session::terminateNetworkClient() void Session::terminateNetworkClient()
@@ -171,7 +193,7 @@ void Session::clientJoinGame(const std::string &name, const std::string &passwor
myNetClient->SendJoinGame(name, password); myNetClient->SendJoinGame(name, password);
} }
void Session::startNetworkServer(const GameData &gameData) void Session::startNetworkServer()
{ {
if (myNetServer) if (myNetServer)
{ {
+3 -2
View File
@@ -45,13 +45,14 @@ public:
GuiInterface *getGui(); GuiInterface *getGui();
void startInternetClient();
void startNetworkClient(const std::string &serverAddress, unsigned serverPort, bool ipv6, bool sctp, const std::string &pwd); void startNetworkClient(const std::string &serverAddress, unsigned serverPort, bool ipv6, bool sctp, const std::string &pwd);
void startNetworkClientForLocalServer(); void startNetworkClientForLocalServer(const GameData &gameData);
void terminateNetworkClient(); void terminateNetworkClient();
void clientCreateGame(const GameData &gameData, const std::string &name, const std::string &password); void clientCreateGame(const GameData &gameData, const std::string &name, const std::string &password);
void clientJoinGame(const std::string &name, const std::string &password); void clientJoinGame(const std::string &name, const std::string &password);
void startNetworkServer(const GameData &gameData); void startNetworkServer();
void sendStartEvent(); void sendStartEvent();
void terminateNetworkServer(); void terminateNetworkServer();
void waitForNetworkServer(unsigned timeoutMsec); void waitForNetworkServer(unsigned timeoutMsec);