author | unc0rr |
Wed, 17 Nov 2010 20:28:42 +0300 | |
changeset 4361 | 64ea345ab655 |
parent 3697 | d5b30d6373fc |
child 4976 | 088d40d8aba2 |
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> |
184 | 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"), |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
44 |
tr("Unable to start the server: %1.") |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
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())); |
|
62 |
connect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
|
185 | 63 |
SendToClientFirst(); |
184 | 64 |
} |
65 |
||
66 |
void TCPBase::RealStart() |
|
67 |
{ |
|
197 | 68 |
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
184 | 69 |
IPCSocket = 0; |
389 | 70 |
|
184 | 71 |
QProcess * process; |
72 |
process = new QProcess; |
|
73 |
connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError))); |
|
74 |
QStringList arguments=setArguments(); |
|
3697 | 75 |
|
3157 | 76 |
// redirect everything written on stdout/stderr |
77 |
if(isDevBuild) |
|
78 |
process->setProcessChannelMode(QProcess::ForwardedChannels); |
|
184 | 79 |
process->start(bindir->absolutePath() + "/hwengine", arguments); |
80 |
} |
|
81 |
||
82 |
void TCPBase::ClientDisconnect() |
|
83 |
{ |
|
443
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
419
diff
changeset
|
84 |
disconnect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
184 | 85 |
onClientDisconnect(); |
86 |
||
87 |
if(srvsList.size()==1) srvsList.pop_front(); |
|
88 |
emit isReadyNow(); |
|
443
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
419
diff
changeset
|
89 |
IPCSocket->deleteLater(); |
390 | 90 |
deleteLater(); |
184 | 91 |
} |
92 |
||
93 |
void TCPBase::ClientRead() |
|
94 |
{ |
|
443
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
419
diff
changeset
|
95 |
QByteArray readed=IPCSocket->readAll(); |
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
419
diff
changeset
|
96 |
if(readed.isEmpty()) return; |
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
419
diff
changeset
|
97 |
readbuffer.append(readed); |
184 | 98 |
onClientRead(); |
99 |
} |
|
100 |
||
101 |
void TCPBase::StartProcessError(QProcess::ProcessError error) |
|
102 |
{ |
|
103 |
QMessageBox::critical(0, tr("Error"), |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
104 |
tr("Unable to run engine: %1 (") |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
105 |
.arg(error) + bindir->absolutePath() + "/hwengine)"); |
184 | 106 |
} |
107 |
||
108 |
void TCPBase::tcpServerReady() |
|
109 |
{ |
|
390 | 110 |
disconnect(srvsList.takeFirst(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady())); |
184 | 111 |
|
185 | 112 |
RealStart(); |
184 | 113 |
} |
114 |
||
115 |
void TCPBase::Start() |
|
116 |
{ |
|
117 |
if(srvsList.isEmpty()) { |
|
118 |
srvsList.push_back(this); |
|
119 |
} else { |
|
120 |
connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady())); |
|
121 |
srvsList.push_back(this); |
|
122 |
return; |
|
123 |
} |
|
389 | 124 |
|
184 | 125 |
RealStart(); |
126 |
} |
|
127 |
||
128 |
void TCPBase::onClientRead() |
|
129 |
{ |
|
130 |
} |
|
131 |
||
132 |
void TCPBase::onClientDisconnect() |
|
133 |
{ |
|
134 |
} |
|
135 |
||
136 |
void TCPBase::SendToClientFirst() |
|
137 |
{ |
|
138 |
} |
|
139 |
||
140 |
void TCPBase::SendIPC(const QByteArray & buf) |
|
141 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
142 |
if (buf.size() > MAXMSGCHARS) return; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
143 |
quint8 len = buf.size(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
144 |
RawSendIPC(QByteArray::fromRawData((char *)&len, 1) + buf); |
184 | 145 |
} |
146 |
||
147 |
void TCPBase::RawSendIPC(const QByteArray & buf) |
|
148 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
149 |
if (!IPCSocket) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
150 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
151 |
toSendBuf += buf; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
152 |
} else |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
153 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
154 |
if (toSendBuf.size() > 0) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
155 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
156 |
IPCSocket->write(toSendBuf); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
157 |
if(m_isDemoMode) demo.append(toSendBuf); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
158 |
toSendBuf.clear(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
159 |
} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
160 |
if(!buf.isEmpty()) { |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
161 |
IPCSocket->write(buf); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
162 |
if(m_isDemoMode) demo.append(buf); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
163 |
} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
164 |
} |
184 | 165 |
} |