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