QTfrontend/netserver.cpp
author unc0rr
Sun, 05 Oct 2008 16:38:26 +0000
changeset 1303 f90bf2276639
parent 1066 1f1b3686a2b0
child 1374 f33fa81e583d
permissions -rw-r--r--
Don't allow team with the same name from the same client (helps with double add due to lags)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
     1
/*
1066
1f1b3686a2b0 Update copyright headers a bit
unc0rr
parents: 884
diff changeset
     2
 * Hedgewars, a free turn based strategy game
884
99cc72c8a812 - Some debug info
unc0rr
parents: 883
diff changeset
     3
 * Copyright (c) 2006-2008 Igor Ulyanov <iulyanov@gmail.com>
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
     4
 *
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
     8
 *
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    12
 * GNU General Public License for more details.
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    13
 *
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    17
 */
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    18
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    19
#include "netserver.h"
420
6cdfc07dceed netserver and netonnectedclient splited to different files
displacer
parents: 404
diff changeset
    20
#include "netconnectedclient.h"
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    21
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    22
#include <QTcpServer>
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    23
#include <QTcpSocket>
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    24
#include <QMessageBox>
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    25
749
3acf2371d73c gcc 4.3 fixes
displacer
parents: 672
diff changeset
    26
#include <algorithm>
3acf2371d73c gcc 4.3 fixes
displacer
parents: 672
diff changeset
    27
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    28
extern char delimeter;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    29
672
08ed55ea1e2a - Fix a bug when server fails to start, and then connecting to another server on the same host (strange, the fix won't work on my machine)
unc0rr
parents: 658
diff changeset
    30
bool HWNetServer::StartServer(quint16 port)
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    31
{
658
a7e625f5d9d0 Make use of server port option
unc0rr
parents: 569
diff changeset
    32
  ds_port = port;
399
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
    33
  hhnum=0;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    34
  IPCServer = new QTcpServer(this);
326
c6718bfdd642 Small fixes
unc0rr
parents: 319
diff changeset
    35
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    36
  connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
672
08ed55ea1e2a - Fix a bug when server fails to start, and then connecting to another server on the same host (strange, the fix won't work on my machine)
unc0rr
parents: 658
diff changeset
    37
08ed55ea1e2a - Fix a bug when server fails to start, and then connecting to another server on the same host (strange, the fix won't work on my machine)
unc0rr
parents: 658
diff changeset
    38
  return IPCServer->listen(QHostAddress::Any, ds_port);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    39
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    40
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    41
void HWNetServer::StopServer()
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    42
{
383
09a8795105a4 some disconnect and remove teams bugfixes
displacer
parents: 382
diff changeset
    43
  QList<HWConnectedClient*>::iterator it;
09a8795105a4 some disconnect and remove teams bugfixes
displacer
parents: 382
diff changeset
    44
  for(it=connclients.begin(); it!=connclients.end(); ++it) {
09a8795105a4 some disconnect and remove teams bugfixes
displacer
parents: 382
diff changeset
    45
    ClientDisconnect(*it);
09a8795105a4 some disconnect and remove teams bugfixes
displacer
parents: 382
diff changeset
    46
  }
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    47
  IPCServer->close();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    48
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    49
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    50
void HWNetServer::NewConnection()
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    51
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    52
  QTcpSocket* client = IPCServer->nextPendingConnection();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    53
  if(!client) return;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    54
  connclients.push_back(new HWConnectedClient(this, client));
326
c6718bfdd642 Small fixes
unc0rr
parents: 319
diff changeset
    55
  connect(connclients.back(), SIGNAL(HWClientDisconnected(HWConnectedClient*)),
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    56
	  this, SLOT(ClientDisconnect(HWConnectedClient*)));
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    57
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    58
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    59
void HWNetServer::ClientDisconnect(HWConnectedClient* client)
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    60
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    61
  QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client);
569
4e41c9e9e4d1 Highlight some bad behavior of network server/clients
unc0rr
parents: 541
diff changeset
    62
  if(it==connclients.end())
4e41c9e9e4d1 Highlight some bad behavior of network server/clients
unc0rr
parents: 541
diff changeset
    63
  {
4e41c9e9e4d1 Highlight some bad behavior of network server/clients
unc0rr
parents: 541
diff changeset
    64
    qWarning("Unknown client disconnected");
4e41c9e9e4d1 Highlight some bad behavior of network server/clients
unc0rr
parents: 541
diff changeset
    65
    return;
4e41c9e9e4d1 Highlight some bad behavior of network server/clients
unc0rr
parents: 541
diff changeset
    66
  }
350
c3ccec3834e8 more correct net disconnection
displacer
parents: 349
diff changeset
    67
  for(QList<QStringList>::iterator tmIt=(*it)->m_teamsCfg.begin(); tmIt!=(*it)->m_teamsCfg.end(); ++tmIt) {
383
09a8795105a4 some disconnect and remove teams bugfixes
displacer
parents: 382
diff changeset
    68
    sendOthers(*it, QString("REMOVETEAM:")+delimeter+*(tmIt->begin()) + delimeter + *(tmIt->begin()+1));
350
c3ccec3834e8 more correct net disconnection
displacer
parents: 349
diff changeset
    69
  }
461
105af110b0e6 chatWidget with layout
displacer
parents: 443
diff changeset
    70
  sendOthers(*it, QString("LEFT")+delimeter+client->client_nick);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    71
  connclients.erase(it);
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
    72
  //teamChanged();
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    73
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    74
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    75
QString HWNetServer::getRunningHostName() const
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    76
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    77
  return IPCServer->serverAddress().toString();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    78
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    79
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    80
quint16 HWNetServer::getRunningPort() const
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    81
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    82
  return ds_port;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    83
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    84
334
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    85
HWConnectedClient* HWNetServer::getChiefClient() const
317
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
    86
{
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
    87
  for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
334
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    88
    // watch for first fully connected client (with confirmed nick)
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    89
    if((*it)->getClientNick()!="") return *it;
317
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
    90
  }
334
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    91
  return 0;
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    92
}
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    93
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    94
bool HWNetServer::isChiefClient(HWConnectedClient* cl) const
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    95
{
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    96
  return getChiefClient()==cl;
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    97
}
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    98
354
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
    99
QMap<QString, QStringList> HWNetServer::getGameCfg() const
334
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
   100
{
404
64a62b679b04 final bugfixes :)
displacer
parents: 401
diff changeset
   101
  return m_gameCfg;
317
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   102
}
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   103
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   104
bool HWNetServer::haveNick(const QString& nick) const
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   105
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   106
  for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   107
    if((*it)->getClientNick()==nick) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   108
      return true;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   109
    }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   110
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   111
  return false;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   112
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   113
465
07eca0a2546c chatWidget with nicks ready
displacer
parents: 461
diff changeset
   114
void HWNetServer::sendNicks(HWConnectedClient* cl) const
07eca0a2546c chatWidget with nicks ready
displacer
parents: 461
diff changeset
   115
{
07eca0a2546c chatWidget with nicks ready
displacer
parents: 461
diff changeset
   116
  for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
07eca0a2546c chatWidget with nicks ready
displacer
parents: 461
diff changeset
   117
      cl->RawSendNet(QString("JOINED")+delimeter+(*it)->client_nick);
07eca0a2546c chatWidget with nicks ready
displacer
parents: 461
diff changeset
   118
  }
07eca0a2546c chatWidget with nicks ready
displacer
parents: 461
diff changeset
   119
}
07eca0a2546c chatWidget with nicks ready
displacer
parents: 461
diff changeset
   120
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   121
QList<QStringList> HWNetServer::getTeamsConfig() const
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   122
{
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   123
  QList<QStringList> lst;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   124
  for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   125
    try {
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   126
      lst+=(*it)->getTeamNames();
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   127
    } catch(HWConnectedClient::NoTeamNameException& e) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   128
    }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   129
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   130
  return lst;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   131
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   132
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   133
bool HWNetServer::shouldStart(HWConnectedClient* client)
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   134
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   135
  QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client);
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   136
  if(it==connclients.end() || *it!=client) return false;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   137
  for(it=connclients.begin(); it!=connclients.end(); ++it) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   138
    if(!(*it)->isReady()) return false;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   139
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   140
  return true;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   141
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   142
388
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   143
void HWNetServer::resetStart()
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   144
{
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   145
  QList<HWConnectedClient*>::iterator it;
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   146
  for(it=connclients.begin(); it!=connclients.end(); ++it) {
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   147
    (*it)->readyToStart=false;
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   148
  }
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   149
}
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   150
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   151
QString HWNetServer::prepareConfig(QStringList lst)
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   152
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   153
  QString msg=lst.join((QString)delimeter)+delimeter;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   154
  for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   155
    if(!(*it)->isReady()) continue;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   156
    msg+=(*it)->getHedgehogsDescription()+delimeter;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   157
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   158
  return msg;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   159
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   160
319
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   161
void HWNetServer::sendAll(QString gameCfg)
317
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   162
{
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   163
  for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   164
    (*it)->RawSendNet(gameCfg);
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   165
  }
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   166
}
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   167
319
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   168
void HWNetServer::sendOthers(HWConnectedClient* this_cl, QString gameCfg)
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   169
{
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   170
  for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   171
    if(*it==this_cl) continue;
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   172
    (*it)->RawSendNet(gameCfg);
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   173
  }
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   174
}