author | koda |
Mon, 22 Jul 2013 21:32:08 +0200 | |
branch | 0.9.9 |
changeset 1739 | 76d47aa70290 |
parent 1066 | 1f1b3686a2b0 |
child 2948 | 3f21a9dc93d0 |
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"), |
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())); |
|
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"), |
|
100 |
tr("Unable to run engine: %1 (") |
|
101 |
.arg(error) + bindir->absolutePath() + "/hwengine)"); |
|
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 |
{ |
|
138 |
if (buf.size() > MAXMSGCHARS) return; |
|
139 |
quint8 len = buf.size(); |
|
140 |
RawSendIPC(QByteArray::fromRawData((char *)&len, 1) + buf); |
|
141 |
} |
|
142 |
||
143 |
void TCPBase::RawSendIPC(const QByteArray & buf) |
|
144 |
{ |
|
145 |
if (!IPCSocket) |
|
146 |
{ |
|
147 |
toSendBuf += buf; |
|
148 |
} else |
|
149 |
{ |
|
150 |
if (toSendBuf.size() > 0) |
|
151 |
{ |
|
152 |
IPCSocket->write(toSendBuf); |
|
533 | 153 |
if(m_isDemoMode) demo.append(toSendBuf); |
184 | 154 |
toSendBuf.clear(); |
155 |
} |
|
379 | 156 |
if(!buf.isEmpty()) { |
157 |
IPCSocket->write(buf); |
|
533 | 158 |
if(m_isDemoMode) demo.append(buf); |
379 | 159 |
} |
184 | 160 |
} |
161 |
} |