author | nemo |
Mon, 01 Aug 2011 13:29:01 -0400 | |
changeset 5489 | f7ec6e5ad054 |
parent 5213 | a86768368309 |
permissions | -rw-r--r-- |
184 | 1 |
/* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
486 | 3 |
* Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com> |
4976 | 4 |
* Copyright (c) 2007-2011 Andrey Korotaev <unC0Rr@gmail.com> |
184 | 5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License as published by |
|
8 |
* the Free Software Foundation; version 2 of the License |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program; if not, write to the Free Software |
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
18 |
*/ |
|
19 |
||
20 |
#include "tcpBase.h" |
|
21 |
||
22 |
#include <QMessageBox> |
|
23 |
#include <QList> |
|
24 |
||
25 |
#include <QImage> |
|
26 |
||
27 |
#include "hwconsts.h" |
|
28 |
||
29 |
QList<TCPBase*> srvsList; |
|
389 | 30 |
QPointer<QTcpServer> TCPBase::IPCServer(0); |
184 | 31 |
|
419
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
390
diff
changeset
|
32 |
TCPBase::~TCPBase() |
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 |
} |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
390
diff
changeset
|
35 |
|
184 | 36 |
TCPBase::TCPBase(bool demoMode) : |
185 | 37 |
m_isDemoMode(demoMode), |
38 |
IPCSocket(0) |
|
184 | 39 |
{ |
186 | 40 |
if(!IPCServer) { |
390 | 41 |
IPCServer = new QTcpServer(0); |
185 | 42 |
IPCServer->setMaxPendingConnections(1); |
291 | 43 |
if (!IPCServer->listen(QHostAddress::LocalHost)) { |
185 | 44 |
QMessageBox::critical(0, tr("Error"), |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
45 |
tr("Unable to start the server: %1.") |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
46 |
.arg(IPCServer->errorString())); |
291 | 47 |
exit(0); // FIXME - should be graceful exit here |
185 | 48 |
} |
49 |
} |
|
291 | 50 |
ipc_port=IPCServer->serverPort(); |
184 | 51 |
} |
52 |
||
53 |
void TCPBase::NewConnection() |
|
54 |
{ |
|
185 | 55 |
if(IPCSocket) { |
56 |
// connection should be already finished |
|
57 |
return; |
|
58 |
} |
|
389 | 59 |
disconnect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
380 | 60 |
IPCSocket = IPCServer->nextPendingConnection(); |
61 |
if(!IPCSocket) return; |
|
62 |
connect(IPCSocket, SIGNAL(disconnected()), this, SLOT(ClientDisconnect())); |
|
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))); |
|
5213
a86768368309
make the associate button use the user's settings for loading demos/saves
nemo
parents:
4976
diff
changeset
|
75 |
QStringList arguments=getArguments(); |
3697 | 76 |
|
3157 | 77 |
// redirect everything written on stdout/stderr |
78 |
if(isDevBuild) |
|
79 |
process->setProcessChannelMode(QProcess::ForwardedChannels); |
|
184 | 80 |
process->start(bindir->absolutePath() + "/hwengine", arguments); |
81 |
} |
|
82 |
||
83 |
void TCPBase::ClientDisconnect() |
|
84 |
{ |
|
443
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
419
diff
changeset
|
85 |
disconnect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
184 | 86 |
onClientDisconnect(); |
87 |
||
88 |
if(srvsList.size()==1) srvsList.pop_front(); |
|
89 |
emit isReadyNow(); |
|
443
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
419
diff
changeset
|
90 |
IPCSocket->deleteLater(); |
390 | 91 |
deleteLater(); |
184 | 92 |
} |
93 |
||
94 |
void TCPBase::ClientRead() |
|
95 |
{ |
|
443
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
419
diff
changeset
|
96 |
QByteArray readed=IPCSocket->readAll(); |
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
419
diff
changeset
|
97 |
if(readed.isEmpty()) return; |
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
419
diff
changeset
|
98 |
readbuffer.append(readed); |
184 | 99 |
onClientRead(); |
100 |
} |
|
101 |
||
102 |
void TCPBase::StartProcessError(QProcess::ProcessError error) |
|
103 |
{ |
|
104 |
QMessageBox::critical(0, tr("Error"), |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
105 |
tr("Unable to run engine: %1 (") |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
106 |
.arg(error) + bindir->absolutePath() + "/hwengine)"); |
184 | 107 |
} |
108 |
||
109 |
void TCPBase::tcpServerReady() |
|
110 |
{ |
|
390 | 111 |
disconnect(srvsList.takeFirst(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady())); |
184 | 112 |
|
185 | 113 |
RealStart(); |
184 | 114 |
} |
115 |
||
116 |
void TCPBase::Start() |
|
117 |
{ |
|
118 |
if(srvsList.isEmpty()) { |
|
119 |
srvsList.push_back(this); |
|
120 |
} else { |
|
121 |
connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady())); |
|
122 |
srvsList.push_back(this); |
|
123 |
return; |
|
124 |
} |
|
389 | 125 |
|
184 | 126 |
RealStart(); |
127 |
} |
|
128 |
||
129 |
void TCPBase::onClientRead() |
|
130 |
{ |
|
131 |
} |
|
132 |
||
133 |
void TCPBase::onClientDisconnect() |
|
134 |
{ |
|
135 |
} |
|
136 |
||
137 |
void TCPBase::SendToClientFirst() |
|
138 |
{ |
|
139 |
} |
|
140 |
||
141 |
void TCPBase::SendIPC(const QByteArray & buf) |
|
142 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
143 |
if (buf.size() > MAXMSGCHARS) return; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
144 |
quint8 len = buf.size(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
145 |
RawSendIPC(QByteArray::fromRawData((char *)&len, 1) + buf); |
184 | 146 |
} |
147 |
||
148 |
void TCPBase::RawSendIPC(const QByteArray & buf) |
|
149 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
150 |
if (!IPCSocket) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
151 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
152 |
toSendBuf += buf; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
153 |
} else |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
154 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
155 |
if (toSendBuf.size() > 0) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
156 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
157 |
IPCSocket->write(toSendBuf); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
158 |
if(m_isDemoMode) demo.append(toSendBuf); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
159 |
toSendBuf.clear(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
160 |
} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
161 |
if(!buf.isEmpty()) { |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
162 |
IPCSocket->write(buf); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
163 |
if(m_isDemoMode) demo.append(buf); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
164 |
} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
165 |
} |
184 | 166 |
} |