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