# HG changeset patch # User unc0rr # Date 1224337023 0 # Node ID f33fa81e583db4a8cac8f82e2bc2ffdd71ebdc69 # Parent ca719502b87f8bdc92780dd08e5d05f4d2138ebd Get rid of old net server diff -r ca719502b87f -r f33fa81e583d QTfrontend/CMakeLists.txt --- a/QTfrontend/CMakeLists.txt Sat Oct 18 13:26:17 2008 +0000 +++ b/QTfrontend/CMakeLists.txt Sat Oct 18 13:37:03 2008 +0000 @@ -62,7 +62,6 @@ proto.cpp fpsedit.cpp netserver.cpp - netconnectedclient.cpp newnetclient.cpp netudpserver.cpp netudpwidget.cpp @@ -110,7 +109,6 @@ proto.h fpsedit.h netserver.h - netconnectedclient.h newnetclient.h netudpserver.h netudpwidget.h diff -r ca719502b87f -r f33fa81e583d QTfrontend/hedgewars.pro --- a/QTfrontend/hedgewars.pro Sat Oct 18 13:26:17 2008 +0000 +++ b/QTfrontend/hedgewars.pro Sat Oct 18 13:37:03 2008 +0000 @@ -34,11 +34,10 @@ proto.h \ fpsedit.h \ netserver.h \ - netconnectedclient.h \ newnetclient.h \ netudpserver.h \ netudpwidget.h \ - netserverslist.h \ + netserverslist.h \ chatwidget.h \ SDLs.h \ playrecordpage.h \ @@ -69,11 +68,10 @@ proto.cpp \ fpsedit.cpp \ netserver.cpp \ - netconnectedclient.cpp \ newnetclient.cpp \ netudpserver.cpp \ netudpwidget.cpp \ - netserverslist.cpp \ + netserverslist.cpp \ chatwidget.cpp \ SDLs.cpp \ playrecordpage.cpp \ diff -r ca719502b87f -r f33fa81e583d QTfrontend/netconnectedclient.cpp --- a/QTfrontend/netconnectedclient.cpp Sat Oct 18 13:26:17 2008 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,291 +0,0 @@ -/* - * Hedgewars, a free turn based strategy game - * Copyright (c) 2006-2008 Igor Ulyanov - * - * 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; version 2 of the License - * - * 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 -#include -#include -#include - -#include "netconnectedclient.h" -#include "netserver.h" - -extern char delimeter; - -HWConnectedClient::HWConnectedClient(HWNetServer* hwserver, QTcpSocket* client) : - readyToStart(false), - m_hwserver(hwserver), - m_client(client) -{ - connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect())); - connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead())); -} - -HWConnectedClient::~HWConnectedClient() -{ -} - -void HWConnectedClient::ClientDisconnect() -{ - emit(HWClientDisconnected(this)); -} - -void HWConnectedClient::ClientRead() -{ - try { - while (m_client->canReadLine()) { - QString s = QString::fromUtf8(m_client->readLine().trimmed()); - if (s.size() == 0) { - ParseCmd(cmdbuf); - cmdbuf.clear(); - } else - cmdbuf << s; - } - } catch(ShouldDisconnectException& e) { - m_client->close(); - } -} - -void HWConnectedClient::ParseCmd(const QStringList & lst) -{ -//qDebug() << "Server: Parsing:" << lst; - if(!lst.size()) - { - qWarning("Net server: Bad message"); - return; - } - if (lst[0] == "NICK") { - if(lst.size() < 2) - { - qWarning("Net server: Bad 'NICK' message"); - return; - } - if(m_hwserver->haveNick(lst[1])) { - RawSendNet(QString("ERRONEUSNICKNAME")); - throw ShouldDisconnectException(); - } - - client_nick=lst[1]; - RawSendNet(QString("CONNECTED")); - if(m_hwserver->isChiefClient(this)) { - RawSendNet(QString("CONFIGASKED")); - } - else { - RawSendNet(QString("SLAVE")); - // send teams - QList team_conf=m_hwserver->getTeamsConfig(); - for(QList::iterator tmit=team_conf.begin(); tmit!=team_conf.end(); ++tmit) { - RawSendNet(QString("ADDTEAM:")+delimeter+tmit->join(QString(delimeter))); - } - // send config - QMap conf=m_hwserver->getGameCfg(); - for(QMap::iterator it=conf.begin(); it!=conf.end(); ++it) { - RawSendNet(QString("CONFIG_PARAM")+delimeter+it.key()+delimeter+it.value().join(QString(delimeter))); - } - } - m_hwserver->sendNicks(this); - m_hwserver->sendOthers(this, QString("JOINED")+delimeter+client_nick); - return; - } - - if(client_nick=="") - { - qWarning() << "Net server: Message from unnamed client:" << lst; - return; - } - - if (lst[0]=="START:") { - readyToStart=true; - if(m_hwserver->shouldStart(this)) { - // start - m_hwserver->sendAll("RUNGAME"); - m_hwserver->resetStart(); - } - return; - } - - if(lst[0]=="HHNUM") { - if (lst.size()<4) { - qWarning() << "Net server: Bad 'HHNUM' message:" << lst; - return; - } - if(!m_hwserver->isChiefClient(this)) - { - return; // permission denied - } - const QString confstr=lst[0]+"+"+lst[1]+"+"+lst[2]; - QMap::iterator it=m_hwserver->m_gameCfg.find(confstr); - int oldTeamHHNum = it==m_hwserver->m_gameCfg.end() ? 0 : it.value()[0].toUInt(); - int newTeamHHNum = lst[3].toUInt(); - m_hwserver->hhnum+=newTeamHHNum-oldTeamHHNum; -qDebug() << "HHNUM hhnum = " << m_hwserver->hhnum; - // create CONFIG_PARAM to save HHNUM at server from lst - QStringList tmp = lst; - tmp=QStringList("CONFIG_PARAM") << confstr << lst[3]; - m_hwserver->sendOthers(this, tmp.join(QString(delimeter))); - m_hwserver->m_gameCfg[tmp[1]]=tmp.mid(2); -qDebug() << QString("[%1] = %2").arg(tmp[1]).arg(tmp.mid(2)[0]); - return; - } - - if(lst[0]=="CONFIG_PARAM") { - if (lst.size()<3) { - qWarning() << "Net server: Bad 'CONFIG_PARAM' message:" << lst; - return; - } - - if(!m_hwserver->isChiefClient(this)) - { - return; // permission denied - } - else m_hwserver->m_gameCfg[lst[1]]=lst.mid(2); - } - - if(lst[0]=="ADDTEAM:") { - if(lst.size() < 14) - { - qWarning("Net server: Bad 'ADDTEAM' message"); - return; - } - QStringList tmp = lst; - tmp.pop_front(); - - // add team ID - static unsigned int netTeamID=0; - tmp.insert(1, QString::number(++netTeamID)); - - // hedgehogs num count - int maxAdd = 18 - m_hwserver->hhnum; - if (maxAdd <= 0) - { - qWarning("Net server: 'ADDTEAM' message: rejecting"); - return; // reject command - } - if (netIDbyTeamName(tmp[0]) > 0) - { - qWarning("Net server: 'ADDTEAM' message: rejecting (have team with same name)"); - return; // reject command - - } - int toAdd=maxAdd < 4 ? maxAdd : 4; - m_hwserver->hhnum+=toAdd; -qDebug() << "to add = " << toAdd << "m_hwserver->hhnum = " << m_hwserver->hhnum; - // hedgehogs num config - QString hhnumCfg=QString("CONFIG_PARAM%1HHNUM+%2+%3%1%4").arg(delimeter).arg(tmp[0])\ - .arg(netTeamID)\ - .arg(toAdd); - - // creating color config for new team - QString colorCfg=QString("CONFIG_PARAM%1TEAM_COLOR+%2+%3%1%4").arg(delimeter).arg(tmp[0])\ - .arg(netTeamID)\ - .arg(tmp.takeAt(2)); - - m_hwserver->m_gameCfg[colorCfg.split(delimeter)[1]]=colorCfg.split(delimeter).mid(2); - m_hwserver->m_gameCfg[hhnumCfg.split(delimeter)[1]]=hhnumCfg.split(delimeter).mid(2); - m_teamsCfg.push_back(tmp); -qDebug() << QString("[%1] = %2").arg(hhnumCfg.split(delimeter)[1]).arg(hhnumCfg.split(delimeter).mid(2)[0]); - m_hwserver->sendOthers(this, QString("ADDTEAM:")+delimeter+tmp.join(QString(delimeter))); - RawSendNet(QString("TEAM_ACCEPTED%1%2%1%3").arg(delimeter).arg(tmp[0]).arg(tmp[1])); - m_hwserver->sendAll(colorCfg); - m_hwserver->sendAll(hhnumCfg); - return; - } - - if(lst[0]=="REMOVETEAM:") { - if(lst.size() < 2) - { - qWarning("Net server: Bad 'REMOVETEAM' message"); - return; - } - - for(QMap::iterator it=m_hwserver->m_gameCfg.begin(); it!=m_hwserver->m_gameCfg.end(); ++it) - { - QStringList hhTmpList=it.key().split('+'); - if(hhTmpList[0] == "HHNUM") - { - if(hhTmpList[1]==lst[1]) - { - m_hwserver->hhnum-=it.value()[0].toUInt(); - m_hwserver->m_gameCfg.remove(it.key()); - - for(QList::iterator it=m_teamsCfg.begin(); it!=m_teamsCfg.end(); ++it) - if((*it)[0] == lst[1]) - m_teamsCfg.erase(it); - - qDebug() << "REMOVETEAM hhnum = " << m_hwserver->hhnum; - break; - } - } - } - - unsigned int netID=removeTeam(lst[1]); - m_hwserver->sendOthers(this, QString("REMOVETEAM:")+delimeter+lst[1]+delimeter+QString::number(netID)); - return; - } - - m_hwserver->sendOthers(this, lst.join(QString(delimeter))); -} - -unsigned int HWConnectedClient::netIDbyTeamName(const QString& tname) -{ - for(QList::iterator it=m_teamsCfg.begin(); it!=m_teamsCfg.end(); ++it) - if((*it)[0]==tname) - return (*it)[1].toUInt(); - - return 0; -} - -unsigned int HWConnectedClient::removeTeam(const QString& tname) -{ - unsigned int netID = netIDbyTeamName(tname); - - if (netID == 0) - qDebug() << QString("removeTeam: team '%1' not found").arg(tname); - - return netID; -} - -QList HWConnectedClient::getTeamNames() const -{ - return m_teamsCfg; -} - -void HWConnectedClient::RawSendNet(const QString & str) -{ - RawSendNet(str.toUtf8()); -} - -void HWConnectedClient::RawSendNet(const QByteArray & buf) -{ - m_client->write(buf); - m_client->write("\n\n", 2); -} - -QString HWConnectedClient::getClientNick() const -{ - return client_nick; -} - -bool HWConnectedClient::isReady() const -{ - return readyToStart; -} - -QString HWConnectedClient::getHedgehogsDescription() const -{ - return QString();//pclent_team->TeamGameConfig(65535, 4, 100, true).join((QString)delimeter); -} diff -r ca719502b87f -r f33fa81e583d QTfrontend/netconnectedclient.h --- a/QTfrontend/netconnectedclient.h Sat Oct 18 13:26:17 2008 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,74 +0,0 @@ -/* - * Hedgewars, a free turn based strategy game - * Copyright (c) 2006 Igor Ulyanov - * - * 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; version 2 of the License - * - * 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 - */ - -#ifndef _HET_CONNECTEDCLIENT_INCLUDED -#define _HET_CONNECTEDCLIENT_INCLUDED - -#include -#include -#include -#include - -class HWNetServer; -class QTcpSocket; -class QTcpServer; - -class HWConnectedClient : public QObject -{ - Q_OBJECT - - friend class HWNetServer; - - private: - HWConnectedClient(HWNetServer* hwserver, QTcpSocket* client); - ~HWConnectedClient(); - QString getClientNick() const; - - QList getTeamNames() const; - class NoTeamNameException{}; - bool isReady() const; - - QString getHedgehogsDescription() const; - - bool readyToStart; - QList m_teamsCfg; // TeamName - hhs - class ShouldDisconnectException {}; - - QString client_nick; - void ParseCmd(const QStringList & lst); - unsigned int removeTeam(const QString& tname); // returns netID - - HWNetServer* m_hwserver; - QTcpSocket* m_client; - - void RawSendNet(const QString & buf); - void RawSendNet(const QByteArray & buf); - - QStringList cmdbuf; - - unsigned int netIDbyTeamName(const QString& tname); - - signals: - void HWClientDisconnected(HWConnectedClient* client); - - private slots: - void ClientRead(); - void ClientDisconnect(); -}; - -#endif // _HET_CONNECTEDCLIENT_INCLUDED diff -r ca719502b87f -r f33fa81e583d QTfrontend/netserver.cpp --- a/QTfrontend/netserver.cpp Sat Oct 18 13:26:17 2008 +0000 +++ b/QTfrontend/netserver.cpp Sat Oct 18 13:37:03 2008 +0000 @@ -1,6 +1,7 @@ /* * Hedgewars, a free turn based strategy game * Copyright (c) 2006-2008 Igor Ulyanov + * Copyright (c) 2008 Andrey Korotaev * * 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 @@ -16,159 +17,22 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ +#include #include "netserver.h" -#include "netconnectedclient.h" - -#include -#include -#include - -#include - -extern char delimeter; bool HWNetServer::StartServer(quint16 port) { - ds_port = port; - hhnum=0; - IPCServer = new QTcpServer(this); + ds_port = port; - connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); - - return IPCServer->listen(QHostAddress::Any, ds_port); + return true; } void HWNetServer::StopServer() { - QList::iterator it; - for(it=connclients.begin(); it!=connclients.end(); ++it) { - ClientDisconnect(*it); - } - IPCServer->close(); -} - -void HWNetServer::NewConnection() -{ - QTcpSocket* client = IPCServer->nextPendingConnection(); - if(!client) return; - connclients.push_back(new HWConnectedClient(this, client)); - connect(connclients.back(), SIGNAL(HWClientDisconnected(HWConnectedClient*)), - this, SLOT(ClientDisconnect(HWConnectedClient*))); } -void HWNetServer::ClientDisconnect(HWConnectedClient* client) -{ - QList::iterator it=std::find(connclients.begin(), connclients.end(), client); - if(it==connclients.end()) - { - qWarning("Unknown client disconnected"); - return; - } - for(QList::iterator tmIt=(*it)->m_teamsCfg.begin(); tmIt!=(*it)->m_teamsCfg.end(); ++tmIt) { - sendOthers(*it, QString("REMOVETEAM:")+delimeter+*(tmIt->begin()) + delimeter + *(tmIt->begin()+1)); - } - sendOthers(*it, QString("LEFT")+delimeter+client->client_nick); - connclients.erase(it); - //teamChanged(); -} - -QString HWNetServer::getRunningHostName() const -{ - return IPCServer->serverAddress().toString(); -} quint16 HWNetServer::getRunningPort() const { return ds_port; } - -HWConnectedClient* HWNetServer::getChiefClient() const -{ - for(QList::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { - // watch for first fully connected client (with confirmed nick) - if((*it)->getClientNick()!="") return *it; - } - return 0; -} - -bool HWNetServer::isChiefClient(HWConnectedClient* cl) const -{ - return getChiefClient()==cl; -} - -QMap HWNetServer::getGameCfg() const -{ - return m_gameCfg; -} - -bool HWNetServer::haveNick(const QString& nick) const -{ - for(QList::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { - if((*it)->getClientNick()==nick) { - return true; - } - } - return false; -} - -void HWNetServer::sendNicks(HWConnectedClient* cl) const -{ - for(QList::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { - cl->RawSendNet(QString("JOINED")+delimeter+(*it)->client_nick); - } -} - -QList HWNetServer::getTeamsConfig() const -{ - QList lst; - for(QList::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { - try { - lst+=(*it)->getTeamNames(); - } catch(HWConnectedClient::NoTeamNameException& e) { - } - } - return lst; -} - -bool HWNetServer::shouldStart(HWConnectedClient* client) -{ - QList::iterator it=std::find(connclients.begin(), connclients.end(), client); - if(it==connclients.end() || *it!=client) return false; - for(it=connclients.begin(); it!=connclients.end(); ++it) { - if(!(*it)->isReady()) return false; - } - return true; -} - -void HWNetServer::resetStart() -{ - QList::iterator it; - for(it=connclients.begin(); it!=connclients.end(); ++it) { - (*it)->readyToStart=false; - } -} - -QString HWNetServer::prepareConfig(QStringList lst) -{ - QString msg=lst.join((QString)delimeter)+delimeter; - for(QList::iterator it=connclients.begin(); it!=connclients.end(); ++it) { - if(!(*it)->isReady()) continue; - msg+=(*it)->getHedgehogsDescription()+delimeter; - } - return msg; -} - -void HWNetServer::sendAll(QString gameCfg) -{ - for(QList::iterator it=connclients.begin(); it!=connclients.end(); ++it) { - (*it)->RawSendNet(gameCfg); - } -} - -void HWNetServer::sendOthers(HWConnectedClient* this_cl, QString gameCfg) -{ - for(QList::iterator it=connclients.begin(); it!=connclients.end(); ++it) { - if(*it==this_cl) continue; - (*it)->RawSendNet(gameCfg); - } -} diff -r ca719502b87f -r f33fa81e583d QTfrontend/netserver.h --- a/QTfrontend/netserver.h Sat Oct 18 13:26:17 2008 +0000 +++ b/QTfrontend/netserver.h Sat Oct 18 13:37:03 2008 +0000 @@ -20,49 +20,19 @@ #define _NETSERVER_INCLUDED #include -#include -#include - -#include "team.h" - -class HWNetServer; -class QTcpSocket; -class QTcpServer; -class HWConnectedClient; class HWNetServer : public QObject { - Q_OBJECT + Q_OBJECT - public: - bool StartServer(quint16 port); - void StopServer(); - bool isChiefClient(HWConnectedClient* cl) const; - QMap getGameCfg() const; - void sendAll(QString gameCfg); - void sendOthers(HWConnectedClient* this_cl, QString gameCfg); - void sendNicks(HWConnectedClient* cl) const; - bool haveNick(const QString& nick) const; - QString getRunningHostName() const; - quint16 getRunningPort() const; - QList getTeamsConfig() const; - void teamChanged(); - bool shouldStart(HWConnectedClient* client); - QString prepareConfig(QStringList lst); - void resetStart(); +public: + bool StartServer(quint16 port); + void StopServer(); + QString getRunningHostName() const; + quint16 getRunningPort() const; - QMap m_gameCfg; // config_param - value - int hhnum; - - private: - HWConnectedClient* getChiefClient() const; - quint16 ds_port; - QTcpServer* IPCServer; - QList connclients; - - private slots: - void NewConnection(); - void ClientDisconnect(HWConnectedClient* client); +private: + quint16 ds_port; }; #endif // _NETSERVER_INCLUDED