author | unc0rr |
Sun, 11 Feb 2007 19:57:29 +0000 | |
changeset 435 | 25820c10c849 |
parent 419 | fdeed9718e6b |
child 443 | eec37eb7f5db |
permissions | -rw-r--r-- |
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 |
|
419
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
390
diff
changeset
|
31 |
TCPBase::~TCPBase() |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
390
diff
changeset
|
32 |
{ |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
390
diff
changeset
|
33 |
} |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
390
diff
changeset
|
34 |
|
184 | 35 |
TCPBase::TCPBase(bool demoMode) : |
185 | 36 |
m_isDemoMode(demoMode), |
37 |
IPCSocket(0) |
|
184 | 38 |
{ |
186 | 39 |
if(!IPCServer) { |
390 | 40 |
IPCServer = new QTcpServer(0); |
185 | 41 |
IPCServer->setMaxPendingConnections(1); |
291 | 42 |
if (!IPCServer->listen(QHostAddress::LocalHost)) { |
185 | 43 |
QMessageBox::critical(0, tr("Error"), |
44 |
tr("Unable to start the server: %1.") |
|
45 |
.arg(IPCServer->errorString())); |
|
291 | 46 |
exit(0); // FIXME - should be graceful exit here |
185 | 47 |
} |
48 |
} |
|
291 | 49 |
ipc_port=IPCServer->serverPort(); |
184 | 50 |
} |
51 |
||
52 |
void TCPBase::NewConnection() |
|
53 |
{ |
|
185 | 54 |
if(IPCSocket) { |
55 |
// connection should be already finished |
|
56 |
return; |
|
57 |
} |
|
389 | 58 |
disconnect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
380 | 59 |
IPCSocket = IPCServer->nextPendingConnection(); |
60 |
if(!IPCSocket) return; |
|
61 |
connect(IPCSocket, SIGNAL(disconnected()), this, SLOT(ClientDisconnect())); |
|
389 | 62 |
connect(IPCSocket, SIGNAL(disconnected()), IPCSocket, SLOT(deleteLater())); |
380 | 63 |
connect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
185 | 64 |
SendToClientFirst(); |
184 | 65 |
} |
66 |
||
67 |
void TCPBase::RealStart() |
|
68 |
{ |
|
197 | 69 |
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
184 | 70 |
IPCSocket = 0; |
389 | 71 |
|
184 | 72 |
QProcess * process; |
73 |
process = new QProcess; |
|
74 |
connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError))); |
|
75 |
QStringList arguments=setArguments(); |
|
76 |
process->start(bindir->absolutePath() + "/hwengine", arguments); |
|
77 |
} |
|
78 |
||
79 |
void TCPBase::ClientDisconnect() |
|
80 |
{ |
|
81 |
onClientDisconnect(); |
|
82 |
||
83 |
if(srvsList.size()==1) srvsList.pop_front(); |
|
84 |
emit isReadyNow(); |
|
390 | 85 |
deleteLater(); |
184 | 86 |
} |
87 |
||
88 |
void TCPBase::ClientRead() |
|
89 |
{ |
|
90 |
readbuffer.append(IPCSocket->readAll()); |
|
91 |
onClientRead(); |
|
92 |
} |
|
93 |
||
94 |
void TCPBase::StartProcessError(QProcess::ProcessError error) |
|
95 |
{ |
|
96 |
QMessageBox::critical(0, tr("Error"), |
|
97 |
tr("Unable to run engine: %1 (") |
|
98 |
.arg(error) + bindir->absolutePath() + "/hwengine)"); |
|
99 |
} |
|
100 |
||
101 |
void TCPBase::tcpServerReady() |
|
102 |
{ |
|
390 | 103 |
disconnect(srvsList.takeFirst(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady())); |
184 | 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 |
} |
|
389 | 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 |
} |