From 07a0e01620f67ef79c0f8957a9e15d3ba0ed56fb Mon Sep 17 00:00:00 2001 From: lotodore Date: Sun, 8 Apr 2007 20:35:42 +0000 Subject: [PATCH] Added callback to display player names of connected players. --- src/gui/qt/guiwrapper.cpp | 1 + src/gui/qt/guiwrapper.h | 1 + src/gui/qt/mainwindow/mainwindowimpl.cpp | 3 +++ src/gui/qt/mainwindow/mainwindowimpl.h | 1 + .../startnetworkgamedialogimpl.cpp | 4 ++-- .../startnetworkgamedialog/startnetworkgamedialogimpl.h | 2 +- src/net/common/netpacket.cpp | 2 +- src/net/common/serverrecvstate.cpp | 4 +++- src/net/common/serverrecvthread.cpp | 9 ++++++++- src/net/common/serverthread.cpp | 2 +- src/net/servercallback.h | 4 ++++ src/net/serverrecvthread.h | 7 ++++++- 12 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/gui/qt/guiwrapper.cpp b/src/gui/qt/guiwrapper.cpp index 4952c796..6eb5b930 100644 --- a/src/gui/qt/guiwrapper.cpp +++ b/src/gui/qt/guiwrapper.cpp @@ -99,4 +99,5 @@ void GuiWrapper::SignalNetClientError(int errorID, int osErrorID) { myW->SignalN void GuiWrapper::SignalNetServerSuccess(int actionID) { } void GuiWrapper::SignalNetServerError(int errorID, int osErrorID) { } +void GuiWrapper::SignalNetServerPlayerJoined(const string &playerName) { myW->SignalNetServerPlayerJoined(QString::fromUtf8(playerName.c_str())); } diff --git a/src/gui/qt/guiwrapper.h b/src/gui/qt/guiwrapper.h index e7bfbdc9..36c72a43 100644 --- a/src/gui/qt/guiwrapper.h +++ b/src/gui/qt/guiwrapper.h @@ -99,6 +99,7 @@ public: void SignalNetServerSuccess(int actionID); void SignalNetServerError(int errorID, int osErrorID); + void SignalNetServerPlayerJoined(const std::string &playerName); private: diff --git a/src/gui/qt/mainwindow/mainwindowimpl.cpp b/src/gui/qt/mainwindow/mainwindowimpl.cpp index fea960ba..3f80d687 100755 --- a/src/gui/qt/mainwindow/mainwindowimpl.cpp +++ b/src/gui/qt/mainwindow/mainwindowimpl.cpp @@ -544,6 +544,9 @@ mainWindowImpl::mainWindowImpl(QMainWindow *parent) // Errors are handled globally, not within one dialog. connect(this, SIGNAL(SignalNetClientError(int, int)), this, SLOT(networkError(int, int))); + connect(this, SIGNAL(SignalNetServerPlayerJoined(QString)), myStartNetworkGameDialog, SLOT(addConnectedPlayer(QString))); + + // textBrowser_Log->append(QString::number(this->pos().x(),10)+" "+QString::number(this->pos().y(),10)); // textBrowser_Log->append(QString::number(this->x(),10)+" "+QString::number(this->y(),10)); diff --git a/src/gui/qt/mainwindow/mainwindowimpl.h b/src/gui/qt/mainwindow/mainwindowimpl.h index 47cf6725..e52c1c93 100755 --- a/src/gui/qt/mainwindow/mainwindowimpl.h +++ b/src/gui/qt/mainwindow/mainwindowimpl.h @@ -94,6 +94,7 @@ signals: void SignalNetClientConnect(int actionID); void SignalNetClientGameInfo(int actionID); void SignalNetClientError(int errorID, int osErrorID); + void SignalNetServerPlayerJoined(QString playerName); public slots: diff --git a/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.cpp b/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.cpp index dc42e234..becf0ceb 100644 --- a/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.cpp +++ b/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.cpp @@ -41,10 +41,10 @@ void startNetworkGameDialogImpl::cancel() { } -void startNetworkGameDialogImpl::addConnectedPlayer(std::string playerName) { +void startNetworkGameDialogImpl::addConnectedPlayer(QString playerName) { QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget,0); - item->setData(0, 0, QString::fromUtf8(playerName.c_str())); + item->setData(0, 0, playerName); } void startNetworkGameDialogImpl::keyPressEvent ( QKeyEvent * event ) { diff --git a/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.h b/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.h index 7d7f02a0..df1cf2b5 100644 --- a/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.h +++ b/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.h @@ -37,7 +37,7 @@ public slots: void startGame(); void cancel(); - void addConnectedPlayer(std::string); + void addConnectedPlayer(QString playerName); void keyPressEvent ( QKeyEvent*); }; diff --git a/src/net/common/netpacket.cpp b/src/net/common/netpacket.cpp index 04da19e1..f7332432 100644 --- a/src/net/common/netpacket.cpp +++ b/src/net/common/netpacket.cpp @@ -25,7 +25,7 @@ using namespace std; -#define ADD_PADDING(x) ((((x) + 3) >> 2) << 2) +#define ADD_PADDING(x) ((((x) + 3) >> 2) << 2) #define NET_TYPE_JOIN_GAME 1 #define NET_TYPE_JOIN_GAME_ACK 2 diff --git a/src/net/common/serverrecvstate.cpp b/src/net/common/serverrecvstate.cpp index 7091da6c..543288d7 100644 --- a/src/net/common/serverrecvstate.cpp +++ b/src/net/common/serverrecvstate.cpp @@ -77,7 +77,9 @@ ServerRecvStateInit::Process(ServerRecvThread &server) if (tmpPacket) { // TODO: check password - // TODO: display name + NetPacketJoinGame::Data playerData; + tmpPacket->GetData(playerData); + server.GetCallback().SignalNetServerPlayerJoined(playerData.playerName); boost::shared_ptr answer(new NetPacketJoinGameAck); server.GetSender().Send(answer, recvSock); session->SetState(SessionData::Established); diff --git a/src/net/common/serverrecvthread.cpp b/src/net/common/serverrecvthread.cpp index 41c63deb..7c3c58e8 100644 --- a/src/net/common/serverrecvthread.cpp +++ b/src/net/common/serverrecvthread.cpp @@ -41,7 +41,8 @@ private: }; -ServerRecvThread::ServerRecvThread() +ServerRecvThread::ServerRecvThread(ServerCallback &cb) +: m_callback(cb) { m_senderCallback.reset(new ServerSenderCallback(*this)); m_sender.reset(new SenderThread(GetSenderCallback())); @@ -85,6 +86,12 @@ ServerRecvThread::AddNotification(unsigned notification) m_notificationQueue.push_back(notification); } +ServerCallback & +ServerRecvThread::GetCallback() +{ + return m_callback; +} + void ServerRecvThread::Main() { diff --git a/src/net/common/serverthread.cpp b/src/net/common/serverthread.cpp index f8859e11..932eb252 100644 --- a/src/net/common/serverthread.cpp +++ b/src/net/common/serverthread.cpp @@ -33,7 +33,7 @@ ServerThread::ServerThread(ServerCallback &cb) : m_callback(cb) { m_context.reset(new ServerContext); - m_recvThread.reset(new ServerRecvThread); + m_recvThread.reset(new ServerRecvThread(cb)); } ServerThread::~ServerThread() diff --git a/src/net/servercallback.h b/src/net/servercallback.h index 77cdb020..d091c298 100644 --- a/src/net/servercallback.h +++ b/src/net/servercallback.h @@ -21,6 +21,8 @@ #ifndef _SERVERCALLBACK_H_ #define _SERVERCALLBACK_H_ +#include + class ServerCallback { public: @@ -28,6 +30,8 @@ public: virtual void SignalNetServerSuccess(int actionID) = 0; virtual void SignalNetServerError(int errorID, int osErrorID) = 0; + + virtual void SignalNetServerPlayerJoined(const std::string &playerName) = 0; }; #endif diff --git a/src/net/serverrecvthread.h b/src/net/serverrecvthread.h index cb848709..f9a45318 100644 --- a/src/net/serverrecvthread.h +++ b/src/net/serverrecvthread.h @@ -28,6 +28,7 @@ #include #include +#include #define RECEIVER_THREAD_TERMINATE_TIMEOUT 200 @@ -43,13 +44,15 @@ class NetPacket; class ServerRecvThread : public Thread { public: - ServerRecvThread(); + ServerRecvThread(ServerCallback &gui); virtual ~ServerRecvThread(); void SendToAllPlayers(boost::shared_ptr packet); void AddConnection(boost::shared_ptr data); void AddNotification(unsigned notification); + ServerCallback &GetCallback(); + protected: typedef std::deque > ConnectQueue; @@ -94,6 +97,8 @@ private: std::auto_ptr m_senderCallback; + ServerCallback &m_callback; + friend class ServerRecvStateInit; };