QTfrontend/netserver.cpp
changeset 1374 f33fa81e583d
parent 1066 1f1b3686a2b0
child 1375 c95c6887b289
equal deleted inserted replaced
1373:ca719502b87f 1374:f33fa81e583d
     1 /*
     1 /*
     2  * Hedgewars, a free turn based strategy game
     2  * Hedgewars, a free turn based strategy game
     3  * Copyright (c) 2006-2008 Igor Ulyanov <iulyanov@gmail.com>
     3  * Copyright (c) 2006-2008 Igor Ulyanov <iulyanov@gmail.com>
       
     4  * Copyright (c) 2008 Andrey Korotaev <unC0Rr@gmail.com>
     4  *
     5  *
     5  * This program is free software; you can redistribute it and/or modify
     6  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; version 2 of the License
     8  * the Free Software Foundation; version 2 of the License
     8  *
     9  *
    14  * You should have received a copy of the GNU General Public License
    15  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    16  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  */
    18  */
    18 
    19 
       
    20 #include <QMessageBox>
    19 #include "netserver.h"
    21 #include "netserver.h"
    20 #include "netconnectedclient.h"
       
    21 
       
    22 #include <QTcpServer>
       
    23 #include <QTcpSocket>
       
    24 #include <QMessageBox>
       
    25 
       
    26 #include <algorithm>
       
    27 
       
    28 extern char delimeter;
       
    29 
    22 
    30 bool HWNetServer::StartServer(quint16 port)
    23 bool HWNetServer::StartServer(quint16 port)
    31 {
    24 {
    32   ds_port = port;
    25 	ds_port = port;
    33   hhnum=0;
       
    34   IPCServer = new QTcpServer(this);
       
    35 
    26 
    36   connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
    27 	return true;
    37 
       
    38   return IPCServer->listen(QHostAddress::Any, ds_port);
       
    39 }
    28 }
    40 
    29 
    41 void HWNetServer::StopServer()
    30 void HWNetServer::StopServer()
    42 {
    31 {
    43   QList<HWConnectedClient*>::iterator it;
       
    44   for(it=connclients.begin(); it!=connclients.end(); ++it) {
       
    45     ClientDisconnect(*it);
       
    46   }
       
    47   IPCServer->close();
       
    48 }
    32 }
    49 
    33 
    50 void HWNetServer::NewConnection()
       
    51 {
       
    52   QTcpSocket* client = IPCServer->nextPendingConnection();
       
    53   if(!client) return;
       
    54   connclients.push_back(new HWConnectedClient(this, client));
       
    55   connect(connclients.back(), SIGNAL(HWClientDisconnected(HWConnectedClient*)),
       
    56 	  this, SLOT(ClientDisconnect(HWConnectedClient*)));
       
    57 }
       
    58 
       
    59 void HWNetServer::ClientDisconnect(HWConnectedClient* client)
       
    60 {
       
    61   QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client);
       
    62   if(it==connclients.end())
       
    63   {
       
    64     qWarning("Unknown client disconnected");
       
    65     return;
       
    66   }
       
    67   for(QList<QStringList>::iterator tmIt=(*it)->m_teamsCfg.begin(); tmIt!=(*it)->m_teamsCfg.end(); ++tmIt) {
       
    68     sendOthers(*it, QString("REMOVETEAM:")+delimeter+*(tmIt->begin()) + delimeter + *(tmIt->begin()+1));
       
    69   }
       
    70   sendOthers(*it, QString("LEFT")+delimeter+client->client_nick);
       
    71   connclients.erase(it);
       
    72   //teamChanged();
       
    73 }
       
    74 
       
    75 QString HWNetServer::getRunningHostName() const
       
    76 {
       
    77   return IPCServer->serverAddress().toString();
       
    78 }
       
    79 
    34 
    80 quint16 HWNetServer::getRunningPort() const
    35 quint16 HWNetServer::getRunningPort() const
    81 {
    36 {
    82   return ds_port;
    37   return ds_port;
    83 }
    38 }
    84 
       
    85 HWConnectedClient* HWNetServer::getChiefClient() const
       
    86 {
       
    87   for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
       
    88     // watch for first fully connected client (with confirmed nick)
       
    89     if((*it)->getClientNick()!="") return *it;
       
    90   }
       
    91   return 0;
       
    92 }
       
    93 
       
    94 bool HWNetServer::isChiefClient(HWConnectedClient* cl) const
       
    95 {
       
    96   return getChiefClient()==cl;
       
    97 }
       
    98 
       
    99 QMap<QString, QStringList> HWNetServer::getGameCfg() const
       
   100 {
       
   101   return m_gameCfg;
       
   102 }
       
   103 
       
   104 bool HWNetServer::haveNick(const QString& nick) const
       
   105 {
       
   106   for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
       
   107     if((*it)->getClientNick()==nick) {
       
   108       return true;
       
   109     }
       
   110   }
       
   111   return false;
       
   112 }
       
   113 
       
   114 void HWNetServer::sendNicks(HWConnectedClient* cl) const
       
   115 {
       
   116   for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
       
   117       cl->RawSendNet(QString("JOINED")+delimeter+(*it)->client_nick);
       
   118   }
       
   119 }
       
   120 
       
   121 QList<QStringList> HWNetServer::getTeamsConfig() const
       
   122 {
       
   123   QList<QStringList> lst;
       
   124   for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
       
   125     try {
       
   126       lst+=(*it)->getTeamNames();
       
   127     } catch(HWConnectedClient::NoTeamNameException& e) {
       
   128     }
       
   129   }
       
   130   return lst;
       
   131 }
       
   132 
       
   133 bool HWNetServer::shouldStart(HWConnectedClient* client)
       
   134 {
       
   135   QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client);
       
   136   if(it==connclients.end() || *it!=client) return false;
       
   137   for(it=connclients.begin(); it!=connclients.end(); ++it) {
       
   138     if(!(*it)->isReady()) return false;
       
   139   }
       
   140   return true;
       
   141 }
       
   142 
       
   143 void HWNetServer::resetStart()
       
   144 {
       
   145   QList<HWConnectedClient*>::iterator it;
       
   146   for(it=connclients.begin(); it!=connclients.end(); ++it) {
       
   147     (*it)->readyToStart=false;
       
   148   }
       
   149 }
       
   150 
       
   151 QString HWNetServer::prepareConfig(QStringList lst)
       
   152 {
       
   153   QString msg=lst.join((QString)delimeter)+delimeter;
       
   154   for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
       
   155     if(!(*it)->isReady()) continue;
       
   156     msg+=(*it)->getHedgehogsDescription()+delimeter;
       
   157   }
       
   158   return msg;
       
   159 }
       
   160 
       
   161 void HWNetServer::sendAll(QString gameCfg)
       
   162 {
       
   163   for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
       
   164     (*it)->RawSendNet(gameCfg);
       
   165   }
       
   166 }
       
   167 
       
   168 void HWNetServer::sendOthers(HWConnectedClient* this_cl, QString gameCfg)
       
   169 {
       
   170   for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
       
   171     if(*it==this_cl) continue;
       
   172     (*it)->RawSendNet(gameCfg);
       
   173   }
       
   174 }