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