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 |
}
|
184
|
54 |
QTcpSocket * client = IPCServer->nextPendingConnection();
|
185
|
55 |
if(!client) return;
|
|
56 |
IPCSocket = client;
|
|
57 |
connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
|
|
58 |
connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
|
|
59 |
SendToClientFirst();
|
184
|
60 |
}
|
|
61 |
|
|
62 |
void TCPBase::RealStart()
|
|
63 |
{
|
197
|
64 |
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
|
184
|
65 |
IPCSocket = 0;
|
185
|
66 |
|
184
|
67 |
QProcess * process;
|
|
68 |
process = new QProcess;
|
|
69 |
connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
|
|
70 |
QStringList arguments=setArguments();
|
|
71 |
process->start(bindir->absolutePath() + "/hwengine", arguments);
|
|
72 |
}
|
|
73 |
|
|
74 |
void TCPBase::ClientDisconnect()
|
|
75 |
{
|
|
76 |
IPCSocket->close();
|
|
77 |
|
|
78 |
onClientDisconnect();
|
|
79 |
|
|
80 |
readbuffer.clear();
|
|
81 |
|
|
82 |
if(srvsList.size()==1) srvsList.pop_front();
|
|
83 |
emit isReadyNow();
|
|
84 |
}
|
|
85 |
|
|
86 |
void TCPBase::ClientRead()
|
|
87 |
{
|
|
88 |
readbuffer.append(IPCSocket->readAll());
|
|
89 |
onClientRead();
|
|
90 |
}
|
|
91 |
|
|
92 |
void TCPBase::StartProcessError(QProcess::ProcessError error)
|
|
93 |
{
|
|
94 |
QMessageBox::critical(0, tr("Error"),
|
|
95 |
tr("Unable to run engine: %1 (")
|
|
96 |
.arg(error) + bindir->absolutePath() + "/hwengine)");
|
|
97 |
}
|
|
98 |
|
|
99 |
void TCPBase::tcpServerReady()
|
|
100 |
{
|
|
101 |
disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
|
|
102 |
srvsList.pop_front();
|
|
103 |
|
185
|
104 |
RealStart();
|
184
|
105 |
}
|
|
106 |
|
|
107 |
void TCPBase::Start()
|
|
108 |
{
|
|
109 |
if(srvsList.isEmpty()) {
|
|
110 |
srvsList.push_back(this);
|
|
111 |
} else {
|
|
112 |
connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
|
|
113 |
srvsList.push_back(this);
|
|
114 |
return;
|
|
115 |
}
|
185
|
116 |
|
184
|
117 |
RealStart();
|
|
118 |
}
|
|
119 |
|
|
120 |
void TCPBase::onClientRead()
|
|
121 |
{
|
|
122 |
}
|
|
123 |
|
|
124 |
void TCPBase::onClientDisconnect()
|
|
125 |
{
|
|
126 |
}
|
|
127 |
|
|
128 |
void TCPBase::SendToClientFirst()
|
|
129 |
{
|
|
130 |
}
|
|
131 |
|
|
132 |
void TCPBase::SendIPC(const QByteArray & buf)
|
|
133 |
{
|
|
134 |
if (buf.size() > MAXMSGCHARS) return;
|
|
135 |
quint8 len = buf.size();
|
|
136 |
RawSendIPC(QByteArray::fromRawData((char *)&len, 1) + buf);
|
|
137 |
}
|
|
138 |
|
|
139 |
void TCPBase::RawSendIPC(const QByteArray & buf)
|
|
140 |
{
|
|
141 |
if (!IPCSocket)
|
|
142 |
{
|
|
143 |
toSendBuf += buf;
|
|
144 |
} else
|
|
145 |
{
|
|
146 |
if (toSendBuf.size() > 0)
|
|
147 |
{
|
|
148 |
IPCSocket->write(toSendBuf);
|
|
149 |
if(m_isDemoMode) demo->append(toSendBuf);
|
|
150 |
toSendBuf.clear();
|
|
151 |
}
|
|
152 |
IPCSocket->write(buf);
|
|
153 |
if(m_isDemoMode) demo->append(buf);
|
|
154 |
}
|
|
155 |
}
|