author | unc0rr |
Wed, 27 Sep 2006 20:52:22 +0000 | |
changeset 175 | d226d976d836 |
parent 172 | 5294ada3910b |
child 177 | c67c15e6fae3 |
permissions | -rw-r--r-- |
172 | 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 |
||
164 | 34 |
#include "hwmap.h" |
35 |
#include "hwconsts.h" |
|
36 |
||
37 |
#include <QMessageBox> |
|
168 | 38 |
#include <QMutex> |
39 |
#include <QList> |
|
40 |
||
41 |
QList<HWMap*> srvsList; |
|
42 |
||
164 | 43 |
HWMap::HWMap() : |
44 |
m_isStarted(false) |
|
45 |
{ |
|
169
a78d4a552500
new more working, but still not completely working version :)
displacer
parents:
168
diff
changeset
|
46 |
IPCServer = new QTcpServer(this); |
a78d4a552500
new more working, but still not completely working version :)
displacer
parents:
168
diff
changeset
|
47 |
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
a78d4a552500
new more working, but still not completely working version :)
displacer
parents:
168
diff
changeset
|
48 |
IPCServer->setMaxPendingConnections(1); |
164 | 49 |
} |
50 |
||
168 | 51 |
HWMap::~HWMap() |
52 |
{ |
|
53 |
} |
|
54 |
||
164 | 55 |
void HWMap::getImage(std::string seed) |
56 |
{ |
|
57 |
m_seed=seed; |
|
58 |
Start(); |
|
59 |
} |
|
60 |
||
61 |
void HWMap::ClientDisconnect() |
|
62 |
{ |
|
63 |
QImage im((uchar*)(const char*)readbuffer, 256, 128, QImage::Format_Mono); |
|
64 |
im.setNumColors(2); |
|
65 |
||
66 |
IPCSocket->close(); |
|
67 |
IPCServer->close(); |
|
168 | 68 |
|
164 | 69 |
emit ImageReceived(im); |
168 | 70 |
readbuffer.clear(); |
170 | 71 |
if(srvsList.size()==1) srvsList.pop_front(); |
168 | 72 |
emit isReadyNow(); |
164 | 73 |
} |
74 |
||
75 |
void HWMap::ClientRead() |
|
76 |
{ |
|
77 |
readbuffer.append(IPCSocket->readAll()); |
|
78 |
} |
|
79 |
||
80 |
void HWMap::SendToClientFirst() |
|
81 |
{ |
|
82 |
std::string toSend=std::string("eseed ")+m_seed; |
|
83 |
char ln=(char)toSend.length(); |
|
84 |
IPCSocket->write(&ln, 1); |
|
85 |
IPCSocket->write(toSend.c_str(), ln); |
|
86 |
||
87 |
IPCSocket->write("\x01!", 2); |
|
88 |
} |
|
89 |
||
90 |
void HWMap::NewConnection() |
|
91 |
{ |
|
92 |
QTcpSocket * client = IPCServer->nextPendingConnection(); |
|
93 |
if(!IPCSocket) { |
|
94 |
IPCServer->close(); |
|
95 |
IPCSocket = client; |
|
96 |
connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect())); |
|
97 |
connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
|
98 |
SendToClientFirst(); |
|
99 |
} else { |
|
100 |
qWarning("2nd IPC client?!"); |
|
101 |
client->disconnectFromHost(); |
|
102 |
} |
|
103 |
} |
|
104 |
||
105 |
void HWMap::StartProcessError(QProcess::ProcessError error) |
|
106 |
{ |
|
107 |
QMessageBox::critical(0, tr("Error"), |
|
108 |
tr("Unable to run engine: %1 (") |
|
109 |
.arg(error) + bindir->absolutePath() + "/hwengine)"); |
|
110 |
} |
|
111 |
||
168 | 112 |
void HWMap::tcpServerReady() |
113 |
{ |
|
114 |
disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady())); |
|
115 |
srvsList.pop_front(); |
|
116 |
||
117 |
RealStart(); |
|
118 |
} |
|
119 |
||
164 | 120 |
void HWMap::Start() |
121 |
{ |
|
170 | 122 |
if(srvsList.isEmpty()) { |
168 | 123 |
srvsList.push_back(this); |
124 |
} else { |
|
125 |
connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady())); |
|
126 |
srvsList.push_back(this); |
|
127 |
return; |
|
128 |
} |
|
129 |
||
130 |
RealStart(); |
|
131 |
} |
|
132 |
||
133 |
void HWMap::RealStart() |
|
134 |
{ |
|
164 | 135 |
IPCSocket = 0; |
136 |
if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) { |
|
137 |
QMessageBox::critical(0, tr("Error"), |
|
138 |
tr("Unable to start the server: %1.") |
|
139 |
.arg(IPCServer->errorString())); |
|
140 |
} |
|
141 |
||
142 |
QProcess * process; |
|
143 |
QStringList arguments; |
|
144 |
process = new QProcess; |
|
145 |
connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError))); |
|
146 |
arguments << "46631"; |
|
147 |
arguments << "landpreview"; |
|
148 |
process->start(bindir->absolutePath() + "/hwengine", arguments); |
|
149 |
} |