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 |
|
|
45 |
TCPBase::TCPBase()
|
|
46 |
{
|
|
47 |
IPCServer = new QTcpServer(this);
|
|
48 |
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
|
|
49 |
IPCServer->setMaxPendingConnections(1);
|
|
50 |
}
|
|
51 |
|
|
52 |
void TCPBase::NewConnection()
|
|
53 |
{
|
|
54 |
QTcpSocket * client = IPCServer->nextPendingConnection();
|
|
55 |
if(!IPCSocket) {
|
|
56 |
IPCServer->close();
|
|
57 |
IPCSocket = client;
|
|
58 |
connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
|
|
59 |
connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
|
|
60 |
SendToClientFirst();
|
|
61 |
} else {
|
|
62 |
qWarning("2nd IPC client?!");
|
|
63 |
client->disconnectFromHost();
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
|
67 |
void TCPBase::RealStart()
|
|
68 |
{
|
|
69 |
IPCSocket = 0;
|
|
70 |
if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) {
|
|
71 |
QMessageBox::critical(0, tr("Error"),
|
|
72 |
tr("Unable to start the server: %1.")
|
|
73 |
.arg(IPCServer->errorString()));
|
|
74 |
}
|
|
75 |
|
|
76 |
QProcess * process;
|
|
77 |
process = new QProcess;
|
|
78 |
connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
|
|
79 |
QStringList arguments=setArguments();
|
|
80 |
process->start(bindir->absolutePath() + "/hwengine", arguments);
|
|
81 |
}
|
|
82 |
|
|
83 |
void TCPBase::ClientDisconnect()
|
|
84 |
{
|
|
85 |
IPCSocket->close();
|
|
86 |
IPCServer->close();
|
|
87 |
|
|
88 |
onClientDisconnect();
|
|
89 |
|
|
90 |
readbuffer.clear();
|
|
91 |
|
|
92 |
if(srvsList.size()==1) srvsList.pop_front();
|
|
93 |
emit isReadyNow();
|
|
94 |
}
|
|
95 |
|
|
96 |
void TCPBase::ClientRead()
|
|
97 |
{
|
|
98 |
readbuffer.append(IPCSocket->readAll());
|
|
99 |
onClientRead();
|
|
100 |
}
|
|
101 |
|
|
102 |
void TCPBase::StartProcessError(QProcess::ProcessError error)
|
|
103 |
{
|
|
104 |
QMessageBox::critical(0, tr("Error"),
|
|
105 |
tr("Unable to run engine: %1 (")
|
|
106 |
.arg(error) + bindir->absolutePath() + "/hwengine)");
|
|
107 |
}
|
|
108 |
|
|
109 |
void TCPBase::tcpServerReady()
|
|
110 |
{
|
|
111 |
disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
|
|
112 |
srvsList.pop_front();
|
|
113 |
|
|
114 |
RealStart();
|
|
115 |
}
|
|
116 |
|
|
117 |
void TCPBase::Start()
|
|
118 |
{
|
|
119 |
if(srvsList.isEmpty()) {
|
|
120 |
srvsList.push_back(this);
|
|
121 |
} else {
|
|
122 |
connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
|
|
123 |
srvsList.push_back(this);
|
|
124 |
return;
|
|
125 |
}
|
|
126 |
|
|
127 |
RealStart();
|
|
128 |
}
|
|
129 |
|
|
130 |
void TCPBase::onClientRead()
|
|
131 |
{
|
|
132 |
}
|
|
133 |
|
|
134 |
void TCPBase::onClientDisconnect()
|
|
135 |
{
|
|
136 |
}
|
|
137 |
|
|
138 |
void TCPBase::SendToClientFirst()
|
|
139 |
{
|
|
140 |
}
|