QTfrontend/tcpBase.cpp
author unc0rr
Sun, 10 Apr 2011 20:36:09 +0400
changeset 5132 11d61349efcf
parent 4976 088d40d8aba2
child 5213 a86768368309
permissions -rw-r--r--
Make WriteLnToConsole write to stderr instead of stdout
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     1
/*
1066
1f1b3686a2b0 Update copyright headers a bit
unc0rr
parents: 533
diff changeset
     2
 * Hedgewars, a free turn based strategy game
486
7ea71cd3acd5 - Change proto version to 4
unc0rr
parents: 443
diff changeset
     3
 * Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com>
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 3697
diff changeset
     4
 * Copyright (c) 2007-2011 Andrey Korotaev <unC0Rr@gmail.com>
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     5
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     8
 * the Free Software Foundation; version 2 of the License
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     9
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    13
 * GNU General Public License for more details.
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    14
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    15
 * You should have received a copy of the GNU General Public License
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    16
 * along with this program; if not, write to the Free Software
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    18
 */
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    19
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    20
#include "tcpBase.h"
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    21
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    22
#include <QMessageBox>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    23
#include <QList>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    24
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    25
#include <QImage>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    26
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    27
#include "hwconsts.h"
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    28
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    29
QList<TCPBase*> srvsList;
389
9628e69b609f Fix IPCSocket issues
unc0rr
parents: 381
diff changeset
    30
QPointer<QTcpServer> TCPBase::IPCServer(0);
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    31
419
fdeed9718e6b virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents: 390
diff changeset
    32
TCPBase::~TCPBase()
fdeed9718e6b virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents: 390
diff changeset
    33
{
fdeed9718e6b virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents: 390
diff changeset
    34
}
fdeed9718e6b virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents: 390
diff changeset
    35
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    36
TCPBase::TCPBase(bool demoMode) :
185
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    37
  m_isDemoMode(demoMode),
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    38
  IPCSocket(0)
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    39
{
186
7157b34a24d6 unneded variable removed
displacer
parents: 185
diff changeset
    40
  if(!IPCServer) {
390
dca6bd77d71d memory leak fixed (tanks unC0Rr)
displacer
parents: 389
diff changeset
    41
    IPCServer = new QTcpServer(0);
185
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    42
    IPCServer->setMaxPendingConnections(1);
291
afa378f1e961 automatic assign of game server TCP port
displacer
parents: 197
diff changeset
    43
    if (!IPCServer->listen(QHostAddress::LocalHost)) {
185
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    44
      QMessageBox::critical(0, tr("Error"),
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
    45
                tr("Unable to start the server: %1.")
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
    46
                .arg(IPCServer->errorString()));
291
afa378f1e961 automatic assign of game server TCP port
displacer
parents: 197
diff changeset
    47
      exit(0); // FIXME - should be graceful exit here
185
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    48
    }
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    49
  }
291
afa378f1e961 automatic assign of game server TCP port
displacer
parents: 197
diff changeset
    50
  ipc_port=IPCServer->serverPort();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    51
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    52
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    53
void TCPBase::NewConnection()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    54
{
185
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    55
  if(IPCSocket) {
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    56
    // connection should be already finished
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    57
    return;
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    58
  }
389
9628e69b609f Fix IPCSocket issues
unc0rr
parents: 381
diff changeset
    59
  disconnect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
380
86f3d818da1c small memory leak fixed
displacer
parents: 379
diff changeset
    60
  IPCSocket = IPCServer->nextPendingConnection();
86f3d818da1c small memory leak fixed
displacer
parents: 379
diff changeset
    61
  if(!IPCSocket) return;
86f3d818da1c small memory leak fixed
displacer
parents: 379
diff changeset
    62
  connect(IPCSocket, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
86f3d818da1c small memory leak fixed
displacer
parents: 379
diff changeset
    63
  connect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
185
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
    64
  SendToClientFirst();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    65
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    66
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    67
void TCPBase::RealStart()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    68
{
197
8a7123b84d1b multiple classes accept signal receive bugfix
displacer
parents: 186
diff changeset
    69
  connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    70
  IPCSocket = 0;
389
9628e69b609f Fix IPCSocket issues
unc0rr
parents: 381
diff changeset
    71
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    72
  QProcess * process;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    73
  process = new QProcess;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    74
  connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    75
  QStringList arguments=setArguments();
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3157
diff changeset
    76
3157
77ed36827e30 Frontend:
smxx
parents: 2948
diff changeset
    77
  // redirect everything written on stdout/stderr
77ed36827e30 Frontend:
smxx
parents: 2948
diff changeset
    78
  if(isDevBuild)
77ed36827e30 Frontend:
smxx
parents: 2948
diff changeset
    79
    process->setProcessChannelMode(QProcess::ForwardedChannels);
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    80
  process->start(bindir->absolutePath() + "/hwengine", arguments);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    81
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    82
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    83
void TCPBase::ClientDisconnect()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    84
{
443
eec37eb7f5db fort, grave and difficulty information for net team
displacer
parents: 419
diff changeset
    85
  disconnect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    86
  onClientDisconnect();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    87
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    88
  if(srvsList.size()==1) srvsList.pop_front();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    89
  emit isReadyNow();
443
eec37eb7f5db fort, grave and difficulty information for net team
displacer
parents: 419
diff changeset
    90
  IPCSocket->deleteLater();
390
dca6bd77d71d memory leak fixed (tanks unC0Rr)
displacer
parents: 389
diff changeset
    91
  deleteLater();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    92
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    93
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    94
void TCPBase::ClientRead()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    95
{
443
eec37eb7f5db fort, grave and difficulty information for net team
displacer
parents: 419
diff changeset
    96
  QByteArray readed=IPCSocket->readAll();
eec37eb7f5db fort, grave and difficulty information for net team
displacer
parents: 419
diff changeset
    97
  if(readed.isEmpty()) return;
eec37eb7f5db fort, grave and difficulty information for net team
displacer
parents: 419
diff changeset
    98
  readbuffer.append(readed);
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    99
  onClientRead();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   100
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   101
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   102
void TCPBase::StartProcessError(QProcess::ProcessError error)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   103
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   104
  QMessageBox::critical(0, tr("Error"),
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   105
            tr("Unable to run engine: %1 (")
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   106
            .arg(error) + bindir->absolutePath() + "/hwengine)");
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   107
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   108
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   109
void TCPBase::tcpServerReady()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   110
{
390
dca6bd77d71d memory leak fixed (tanks unC0Rr)
displacer
parents: 389
diff changeset
   111
  disconnect(srvsList.takeFirst(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   112
185
3b777d7008d9 static TCP Server
displacer
parents: 184
diff changeset
   113
  RealStart();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   114
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   115
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   116
void TCPBase::Start()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   117
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   118
  if(srvsList.isEmpty()) {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   119
    srvsList.push_back(this);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   120
  } else {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   121
    connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   122
    srvsList.push_back(this);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   123
    return;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   124
  }
389
9628e69b609f Fix IPCSocket issues
unc0rr
parents: 381
diff changeset
   125
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   126
  RealStart();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   127
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   128
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   129
void TCPBase::onClientRead()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   130
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   131
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   132
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   133
void TCPBase::onClientDisconnect()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   134
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   135
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   136
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   137
void TCPBase::SendToClientFirst()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   138
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   139
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   140
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   141
void TCPBase::SendIPC(const QByteArray & buf)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   142
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   143
    if (buf.size() > MAXMSGCHARS) return;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   144
    quint8 len = buf.size();
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   145
    RawSendIPC(QByteArray::fromRawData((char *)&len, 1) + buf);
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   146
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   147
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   148
void TCPBase::RawSendIPC(const QByteArray & buf)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   149
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   150
    if (!IPCSocket)
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   151
    {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   152
        toSendBuf += buf;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   153
    } else
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   154
    {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   155
        if (toSendBuf.size() > 0)
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   156
        {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   157
            IPCSocket->write(toSendBuf);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   158
            if(m_isDemoMode) demo.append(toSendBuf);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   159
            toSendBuf.clear();
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   160
        }
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   161
        if(!buf.isEmpty()) {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   162
          IPCSocket->write(buf);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   163
          if(m_isDemoMode) demo.append(buf);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   164
        }
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1066
diff changeset
   165
    }
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   166
}