184
|
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 |
|
|
26 |
#include "hwconsts.h"
|
|
27 |
|
|
28 |
QList<TCPBase*> srvsList;
|
185
|
29 |
QTcpServer* TCPBase::IPCServer(0);
|
184
|
30 |
|
|
31 |
TCPBase::TCPBase(bool demoMode) :
|
185
|
32 |
m_isDemoMode(demoMode),
|
|
33 |
IPCSocket(0)
|
184
|
34 |
{
|
186
|
35 |
if(!IPCServer) {
|
185
|
36 |
IPCServer = new QTcpServer(this);
|
|
37 |
IPCServer->setMaxPendingConnections(1);
|
291
|
38 |
if (!IPCServer->listen(QHostAddress::LocalHost)) {
|
185
|
39 |
QMessageBox::critical(0, tr("Error"),
|
|
40 |
tr("Unable to start the server: %1.")
|
|
41 |
.arg(IPCServer->errorString()));
|
291
|
42 |
exit(0); // FIXME - should be graceful exit here
|
185
|
43 |
}
|
|
44 |
}
|
291
|
45 |
ipc_port=IPCServer->serverPort();
|
184
|
46 |
}
|
|
47 |
|
|
48 |
void TCPBase::NewConnection()
|
|
49 |
{
|
185
|
50 |
if(IPCSocket) {
|
|
51 |
// connection should be already finished
|
|
52 |
return;
|
|
53 |
}
|
380
|
54 |
IPCSocket = IPCServer->nextPendingConnection();
|
|
55 |
if(!IPCSocket) return;
|
|
56 |
connect(IPCSocket, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
|
|
57 |
connect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
|
185
|
58 |
SendToClientFirst();
|
184
|
59 |
}
|
|
60 |
|
|
61 |
void TCPBase::RealStart()
|
|
62 |
{
|
197
|
63 |
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
|
184
|
64 |
IPCSocket = 0;
|
185
|
65 |
|
184
|
66 |
QProcess * process;
|
|
67 |
process = new QProcess;
|
|
68 |
connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
|
|
69 |
QStringList arguments=setArguments();
|
|
70 |
process->start(bindir->absolutePath() + "/hwengine", arguments);
|
|
71 |
}
|
|
72 |
|
|
73 |
void TCPBase::ClientDisconnect()
|
|
74 |
{
|
|
75 |
IPCSocket->close();
|
380
|
76 |
delete IPCSocket;
|
|
77 |
IPCSocket = 0;
|
184
|
78 |
|
|
79 |
onClientDisconnect();
|
|
80 |
|
|
81 |
readbuffer.clear();
|
|
82 |
|
|
83 |
if(srvsList.size()==1) srvsList.pop_front();
|
|
84 |
emit isReadyNow();
|
|
85 |
}
|
|
86 |
|
|
87 |
void TCPBase::ClientRead()
|
|
88 |
{
|
|
89 |
readbuffer.append(IPCSocket->readAll());
|
|
90 |
onClientRead();
|
|
91 |
}
|
|
92 |
|
|
93 |
void TCPBase::StartProcessError(QProcess::ProcessError error)
|
|
94 |
{
|
|
95 |
QMessageBox::critical(0, tr("Error"),
|
|
96 |
tr("Unable to run engine: %1 (")
|
|
97 |
.arg(error) + bindir->absolutePath() + "/hwengine)");
|
|
98 |
}
|
|
99 |
|
|
100 |
void TCPBase::tcpServerReady()
|
|
101 |
{
|
|
102 |
disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
|
|
103 |
srvsList.pop_front();
|
|
104 |
|
185
|
105 |
RealStart();
|
184
|
106 |
}
|
|
107 |
|
|
108 |
void TCPBase::Start()
|
|
109 |
{
|
|
110 |
if(srvsList.isEmpty()) {
|
|
111 |
srvsList.push_back(this);
|
|
112 |
} else {
|
|
113 |
connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
|
|
114 |
srvsList.push_back(this);
|
|
115 |
return;
|
|
116 |
}
|
185
|
117 |
|
184
|
118 |
RealStart();
|
|
119 |
}
|
|
120 |
|
|
121 |
void TCPBase::onClientRead()
|
|
122 |
{
|
|
123 |
}
|
|
124 |
|
|
125 |
void TCPBase::onClientDisconnect()
|
|
126 |
{
|
|
127 |
}
|
|
128 |
|
|
129 |
void TCPBase::SendToClientFirst()
|
|
130 |
{
|
|
131 |
}
|
|
132 |
|
|
133 |
void TCPBase::SendIPC(const QByteArray & buf)
|
|
134 |
{
|
|
135 |
if (buf.size() > MAXMSGCHARS) return;
|
|
136 |
quint8 len = buf.size();
|
|
137 |
RawSendIPC(QByteArray::fromRawData((char *)&len, 1) + buf);
|
|
138 |
}
|
|
139 |
|
|
140 |
void TCPBase::RawSendIPC(const QByteArray & buf)
|
|
141 |
{
|
|
142 |
if (!IPCSocket)
|
|
143 |
{
|
|
144 |
toSendBuf += buf;
|
|
145 |
} else
|
|
146 |
{
|
|
147 |
if (toSendBuf.size() > 0)
|
|
148 |
{
|
|
149 |
IPCSocket->write(toSendBuf);
|
|
150 |
if(m_isDemoMode) demo->append(toSendBuf);
|
|
151 |
toSendBuf.clear();
|
|
152 |
}
|
379
|
153 |
if(!buf.isEmpty()) {
|
|
154 |
IPCSocket->write(buf);
|
|
155 |
if(m_isDemoMode && demo) demo->append(buf);
|
|
156 |
}
|
184
|
157 |
}
|
|
158 |
}
|