QTfrontend/tcpBase.cpp
author unc0rr
Thu, 05 Oct 2006 16:33:18 +0000
changeset 183 57c2ef19f719
parent 180 ea83b9e9057f
child 184 f97a7a3dc8f6
permissions -rw-r--r--
Relicense to GPL
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
     1
/*
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
     2
 * Hedgewars, a worms-like game
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
     3
 * Copyright (c) 2006 Igor Ulyanov <iulyanov@gmail.com>
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
     4
 *
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
     8
 *
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    12
 * GNU General Public License for more details.
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    13
 *
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    14
 * You should have received a copy of the GNU General Public License
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    15
 * along with this program; if not, write to the Free Software
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    17
 */
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    18
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    19
#include "tcpBase.h"
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    20
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    21
#include <QMessageBox>
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    22
#include <QList>
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    23
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    24
#include <QImage>
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    25
#include <QTimer>
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    26
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    27
#include "hwconsts.h"
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    28
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    29
QList<TCPBase*> srvsList;
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    30
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    31
TCPBase::TCPBase(bool demoMode) :
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    32
  m_isDemoMode(demoMode)
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    33
{
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    34
  IPCServer = new QTcpServer(this);
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    35
  connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    36
  IPCServer->setMaxPendingConnections(1);
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    37
}
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    38
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    39
void TCPBase::NewConnection()
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    40
{
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    41
  QTcpSocket * client = IPCServer->nextPendingConnection();
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    42
  if(!IPCSocket) {
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    43
    IPCServer->close();
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    44
    IPCSocket = client;
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    45
    connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    46
    connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    47
    SendToClientFirst();
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    48
  } else {
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    49
    qWarning("2nd IPC client?!");
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    50
    client->disconnectFromHost();
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    51
  }
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    52
}
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    53
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    54
void TCPBase::RealStart()
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    55
{
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    56
  IPCSocket = 0;
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    57
  if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) {
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    58
    QMessageBox::critical(0, tr("Error"),
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    59
			  tr("Unable to start the server: %1.")
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    60
			  .arg(IPCServer->errorString()));
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    61
  }
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    62
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    63
  QProcess * process;
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    64
  process = new QProcess;
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    65
  connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    66
  QStringList arguments=setArguments();
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    67
  process->start(bindir->absolutePath() + "/hwengine", arguments);
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    68
}
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    69
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    70
void TCPBase::ClientDisconnect()
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    71
{
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    72
  IPCSocket->close();
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    73
  IPCServer->close();
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    74
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    75
  onClientDisconnect();
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    76
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    77
  readbuffer.clear();
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    78
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    79
  if(srvsList.size()==1) srvsList.pop_front();
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    80
  emit isReadyNow();
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    81
}
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    82
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    83
void TCPBase::ClientRead()
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    84
{
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    85
  readbuffer.append(IPCSocket->readAll());
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    86
  onClientRead();
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    87
}
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    88
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    89
void TCPBase::StartProcessError(QProcess::ProcessError error)
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    90
{
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    91
  QMessageBox::critical(0, tr("Error"),
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    92
			tr("Unable to run engine: %1 (")
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    93
			.arg(error) + bindir->absolutePath() + "/hwengine)");
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    94
}
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    95
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    96
void TCPBase::tcpServerReady()
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    97
{
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    98
  disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
    99
  srvsList.pop_front();
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   100
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   101
  QTimer::singleShot(150, this, SLOT(RealStart()));
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   102
}
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   103
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   104
void TCPBase::Start()
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   105
{
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   106
  if(srvsList.isEmpty()) {
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   107
    srvsList.push_back(this);
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   108
  } else {
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   109
    connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   110
    srvsList.push_back(this);
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   111
    return;
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   112
  }
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   113
  RealStart();
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   114
}
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   115
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   116
void TCPBase::onClientRead()
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   117
{
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   118
}
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   119
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   120
void TCPBase::onClientDisconnect()
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   121
{
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   122
}
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   123
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   124
void TCPBase::SendToClientFirst()
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   125
{
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   126
}
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   127
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   128
void TCPBase::SendIPC(const QByteArray & buf)
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   129
{
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   130
	if (buf.size() > MAXMSGCHARS) return;
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   131
	quint8 len = buf.size();
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   132
	RawSendIPC(QByteArray::fromRawData((char *)&len, 1) + buf);
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   133
}
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   134
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   135
void TCPBase::RawSendIPC(const QByteArray & buf)
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   136
{
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   137
	if (!IPCSocket)
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   138
	{
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   139
		toSendBuf += buf;
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   140
	} else
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   141
	{
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   142
		if (toSendBuf.size() > 0)
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   143
		{
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   144
			IPCSocket->write(toSendBuf);
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   145
			if(m_isDemoMode) demo->append(toSendBuf);
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   146
			toSendBuf.clear();
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   147
		}
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   148
		IPCSocket->write(buf);
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   149
		if(m_isDemoMode) demo->append(buf);
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   150
	}
57c2ef19f719 Relicense to GPL
unc0rr
parents: 180
diff changeset
   151
}