author | nemo |
Sat, 27 Mar 2010 17:03:27 +0000 | |
changeset 3114 | 3a3d1ee2ebd0 |
parent 2948 | 3f21a9dc93d0 |
child 3157 | 77ed36827e30 |
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(); |
|
75 |
process->start(bindir->absolutePath() + "/hwengine", arguments); |
|
76 |
} |
|
77 |
||
78 |
void TCPBase::ClientDisconnect() |
|
79 |
{ |
|
443
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
419
diff
changeset
|
80 |
disconnect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
184 | 81 |
onClientDisconnect(); |
82 |
||
83 |
if(srvsList.size()==1) srvsList.pop_front(); |
|
84 |
emit isReadyNow(); |
|
443
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
419
diff
changeset
|
85 |
IPCSocket->deleteLater(); |
390 | 86 |
deleteLater(); |
184 | 87 |
} |
88 |
||
89 |
void TCPBase::ClientRead() |
|
90 |
{ |
|
443
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
419
diff
changeset
|
91 |
QByteArray readed=IPCSocket->readAll(); |
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
419
diff
changeset
|
92 |
if(readed.isEmpty()) return; |
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
419
diff
changeset
|
93 |
readbuffer.append(readed); |
184 | 94 |
onClientRead(); |
95 |
} |
|
96 |
||
97 |
void TCPBase::StartProcessError(QProcess::ProcessError error) |
|
98 |
{ |
|
99 |
QMessageBox::critical(0, tr("Error"), |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
100 |
tr("Unable to run engine: %1 (") |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
101 |
.arg(error) + bindir->absolutePath() + "/hwengine)"); |
184 | 102 |
} |
103 |
||
104 |
void TCPBase::tcpServerReady() |
|
105 |
{ |
|
390 | 106 |
disconnect(srvsList.takeFirst(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady())); |
184 | 107 |
|
185 | 108 |
RealStart(); |
184 | 109 |
} |
110 |
||
111 |
void TCPBase::Start() |
|
112 |
{ |
|
113 |
if(srvsList.isEmpty()) { |
|
114 |
srvsList.push_back(this); |
|
115 |
} else { |
|
116 |
connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady())); |
|
117 |
srvsList.push_back(this); |
|
118 |
return; |
|
119 |
} |
|
389 | 120 |
|
184 | 121 |
RealStart(); |
122 |
} |
|
123 |
||
124 |
void TCPBase::onClientRead() |
|
125 |
{ |
|
126 |
} |
|
127 |
||
128 |
void TCPBase::onClientDisconnect() |
|
129 |
{ |
|
130 |
} |
|
131 |
||
132 |
void TCPBase::SendToClientFirst() |
|
133 |
{ |
|
134 |
} |
|
135 |
||
136 |
void TCPBase::SendIPC(const QByteArray & buf) |
|
137 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
138 |
if (buf.size() > MAXMSGCHARS) return; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
139 |
quint8 len = buf.size(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
140 |
RawSendIPC(QByteArray::fromRawData((char *)&len, 1) + buf); |
184 | 141 |
} |
142 |
||
143 |
void TCPBase::RawSendIPC(const QByteArray & buf) |
|
144 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
145 |
if (!IPCSocket) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
146 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
147 |
toSendBuf += buf; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
148 |
} else |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
149 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
150 |
if (toSendBuf.size() > 0) |
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 |
IPCSocket->write(toSendBuf); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
153 |
if(m_isDemoMode) demo.append(toSendBuf); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
154 |
toSendBuf.clear(); |
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 |
if(!buf.isEmpty()) { |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
157 |
IPCSocket->write(buf); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
1066
diff
changeset
|
158 |
if(m_isDemoMode) demo.append(buf); |
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 |
} |
184 | 161 |
} |