2
* Hedgewars, a free turn based strategy game
3
* Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com>
4
* Copyright (c) 2007-2011 Andrey Korotaev <unC0Rr@gmail.com>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22
#include <QMessageBox>
29
QList<TCPBase*> srvsList;
30
QPointer<QTcpServer> TCPBase::IPCServer(0);
36
TCPBase::TCPBase(bool demoMode) :
37
m_isDemoMode(demoMode),
41
IPCServer = new QTcpServer(0);
42
IPCServer->setMaxPendingConnections(1);
43
if (!IPCServer->listen(QHostAddress::LocalHost)) {
44
QMessageBox::critical(0, tr("Error"),
45
tr("Unable to start the server: %1.")
46
.arg(IPCServer->errorString()));
47
exit(0); // FIXME - should be graceful exit here
50
ipc_port=IPCServer->serverPort();
53
void TCPBase::NewConnection()
56
// connection should be already finished
59
disconnect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
60
IPCSocket = IPCServer->nextPendingConnection();
61
if(!IPCSocket) return;
62
connect(IPCSocket, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
63
connect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
67
void TCPBase::RealStart()
69
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
73
process = new QProcess;
74
connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
75
QStringList arguments=getArguments();
77
// redirect everything written on stdout/stderr
79
process->setProcessChannelMode(QProcess::ForwardedChannels);
80
process->start(bindir->absolutePath() + "/hwengine", arguments);
83
void TCPBase::ClientDisconnect()
85
disconnect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
88
if(srvsList.size()==1) srvsList.pop_front();
90
IPCSocket->deleteLater();
94
void TCPBase::ClientRead()
96
QByteArray readed=IPCSocket->readAll();
97
if(readed.isEmpty()) return;
98
readbuffer.append(readed);
102
void TCPBase::StartProcessError(QProcess::ProcessError error)
104
QMessageBox::critical(0, tr("Error"),
105
tr("Unable to run engine: %1 (")
106
.arg(error) + bindir->absolutePath() + "/hwengine)");
109
void TCPBase::tcpServerReady()
111
disconnect(srvsList.takeFirst(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
116
void TCPBase::Start()
118
if(srvsList.isEmpty()) {
119
srvsList.push_back(this);
121
connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
122
srvsList.push_back(this);
129
void TCPBase::onClientRead()
133
void TCPBase::onClientDisconnect()
137
void TCPBase::SendToClientFirst()
141
void TCPBase::SendIPC(const QByteArray & buf)
143
if (buf.size() > MAXMSGCHARS) return;
144
quint8 len = buf.size();
145
RawSendIPC(QByteArray::fromRawData((char *)&len, 1) + buf);
148
void TCPBase::RawSendIPC(const QByteArray & buf)
155
if (toSendBuf.size() > 0)
157
IPCSocket->write(toSendBuf);
158
if(m_isDemoMode) demo.append(toSendBuf);
162
IPCSocket->write(buf);
163
if(m_isDemoMode) demo.append(buf);