author | displacer |
Mon, 02 Oct 2006 18:09:39 +0000 | |
changeset 180 | ea83b9e9057f |
parent 179 | 06e472d3f9f8 |
child 183 | 57c2ef19f719 |
permissions | -rw-r--r-- |
179 | 1 |
/* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2006 Igor Ulyanov <iulyanov@gmail.com> |
|
4 |
* |
|
5 |
* Distributed under the terms of the BSD-modified licence: |
|
6 |
* |
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 |
* of this software and associated documentation files (the "Software"), to deal |
|
9 |
* with the Software without restriction, including without limitation the |
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is |
|
12 |
* furnished to do so, subject to the following conditions: |
|
13 |
* |
|
14 |
* 1. Redistributions of source code must retain the above copyright notice, |
|
15 |
* this list of conditions and the following disclaimer. |
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
17 |
* this list of conditions and the following disclaimer in the documentation |
|
18 |
* and/or other materials provided with the distribution. |
|
19 |
* 3. The name of the author may not be used to endorse or promote products |
|
20 |
* derived from this software without specific prior written permission. |
|
21 |
* |
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 |
*/ |
|
33 |
||
34 |
#include "tcpBase.h" |
|
35 |
||
36 |
#include <QMessageBox> |
|
37 |
#include <QList> |
|
38 |
||
39 |
#include <QImage> |
|
40 |
||
41 |
#include "hwconsts.h" |
|
42 |
||
43 |
QList<TCPBase*> srvsList; |
|
44 |
||
180
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
45 |
TCPBase::TCPBase(bool demoMode) : |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
46 |
m_isDemoMode(demoMode) |
179 | 47 |
{ |
48 |
IPCServer = new QTcpServer(this); |
|
49 |
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
|
50 |
IPCServer->setMaxPendingConnections(1); |
|
51 |
} |
|
52 |
||
53 |
void TCPBase::NewConnection() |
|
54 |
{ |
|
55 |
QTcpSocket * client = IPCServer->nextPendingConnection(); |
|
56 |
if(!IPCSocket) { |
|
57 |
IPCServer->close(); |
|
58 |
IPCSocket = client; |
|
59 |
connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect())); |
|
60 |
connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
|
61 |
SendToClientFirst(); |
|
62 |
} else { |
|
63 |
qWarning("2nd IPC client?!"); |
|
64 |
client->disconnectFromHost(); |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
void TCPBase::RealStart() |
|
69 |
{ |
|
70 |
IPCSocket = 0; |
|
71 |
if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) { |
|
72 |
QMessageBox::critical(0, tr("Error"), |
|
73 |
tr("Unable to start the server: %1.") |
|
74 |
.arg(IPCServer->errorString())); |
|
75 |
} |
|
76 |
||
77 |
QProcess * process; |
|
78 |
process = new QProcess; |
|
79 |
connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError))); |
|
80 |
QStringList arguments=setArguments(); |
|
81 |
process->start(bindir->absolutePath() + "/hwengine", arguments); |
|
82 |
} |
|
83 |
||
84 |
void TCPBase::ClientDisconnect() |
|
85 |
{ |
|
86 |
IPCSocket->close(); |
|
87 |
IPCServer->close(); |
|
88 |
||
89 |
onClientDisconnect(); |
|
90 |
||
91 |
readbuffer.clear(); |
|
92 |
||
93 |
if(srvsList.size()==1) srvsList.pop_front(); |
|
94 |
emit isReadyNow(); |
|
95 |
} |
|
96 |
||
97 |
void TCPBase::ClientRead() |
|
98 |
{ |
|
99 |
readbuffer.append(IPCSocket->readAll()); |
|
100 |
onClientRead(); |
|
101 |
} |
|
102 |
||
103 |
void TCPBase::StartProcessError(QProcess::ProcessError error) |
|
104 |
{ |
|
105 |
QMessageBox::critical(0, tr("Error"), |
|
106 |
tr("Unable to run engine: %1 (") |
|
107 |
.arg(error) + bindir->absolutePath() + "/hwengine)"); |
|
108 |
} |
|
109 |
||
110 |
void TCPBase::tcpServerReady() |
|
111 |
{ |
|
112 |
disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady())); |
|
113 |
srvsList.pop_front(); |
|
114 |
||
115 |
RealStart(); |
|
116 |
} |
|
117 |
||
118 |
void TCPBase::Start() |
|
119 |
{ |
|
120 |
if(srvsList.isEmpty()) { |
|
121 |
srvsList.push_back(this); |
|
122 |
} else { |
|
123 |
connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady())); |
|
124 |
srvsList.push_back(this); |
|
125 |
return; |
|
126 |
} |
|
127 |
||
128 |
RealStart(); |
|
129 |
} |
|
130 |
||
131 |
void TCPBase::onClientRead() |
|
132 |
{ |
|
133 |
} |
|
134 |
||
135 |
void TCPBase::onClientDisconnect() |
|
136 |
{ |
|
137 |
} |
|
138 |
||
139 |
void TCPBase::SendToClientFirst() |
|
140 |
{ |
|
141 |
} |
|
180
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
142 |
|
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
143 |
void TCPBase::SendIPC(const QByteArray & buf) |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
144 |
{ |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
145 |
if (buf.size() > MAXMSGCHARS) return; |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
146 |
quint8 len = buf.size(); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
147 |
RawSendIPC(QByteArray::fromRawData((char *)&len, 1) + buf); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
148 |
} |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
149 |
|
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
150 |
void TCPBase::RawSendIPC(const QByteArray & buf) |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
151 |
{ |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
152 |
if (!IPCSocket) |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
153 |
{ |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
154 |
toSendBuf += buf; |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
155 |
} else |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
156 |
{ |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
157 |
if (toSendBuf.size() > 0) |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
158 |
{ |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
159 |
IPCSocket->write(toSendBuf); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
160 |
if(m_isDemoMode) demo->append(toSendBuf); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
161 |
toSendBuf.clear(); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
162 |
} |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
163 |
IPCSocket->write(buf); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
164 |
if(m_isDemoMode) demo->append(buf); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
165 |
} |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
179
diff
changeset
|
166 |
} |