28
|
1 |
/*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2005 Andrey Korotaev <unC0Rr@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 <QMessageBox>
|
|
35 |
#include "netclient.h"
|
|
36 |
|
|
37 |
HWNet::HWNet()
|
|
38 |
: QObject()
|
|
39 |
{
|
|
40 |
state = nsDisconnected;
|
|
41 |
IRCmsg_cmd_param = new QRegExp("^[A-Z]+ :.+$");
|
|
42 |
IRCmsg_number_param = new QRegExp("^:\\S+ [0-9]{3} .+$");
|
|
43 |
|
|
44 |
connect(&NetSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
|
|
45 |
connect(&NetSocket, SIGNAL(connected()), this, SLOT(OnConnect()));
|
|
46 |
connect(&NetSocket, SIGNAL(disconnected()), this, SLOT(OnDisconnect()));
|
|
47 |
connect(&NetSocket, SIGNAL(error(QAbstractSocket::SocketError)), this,
|
|
48 |
SLOT(displayError(QAbstractSocket::SocketError)));
|
|
49 |
}
|
|
50 |
|
|
51 |
void HWNet::ClientRead()
|
|
52 |
{
|
|
53 |
while (NetSocket.canReadLine())
|
|
54 |
{
|
|
55 |
ParseLine(NetSocket.readLine().trimmed());
|
|
56 |
}
|
|
57 |
}
|
|
58 |
|
|
59 |
void HWNet::displayError(QAbstractSocket::SocketError socketError)
|
|
60 |
{
|
|
61 |
switch (socketError)
|
|
62 |
{
|
|
63 |
case QAbstractSocket::RemoteHostClosedError:
|
|
64 |
break;
|
|
65 |
case QAbstractSocket::HostNotFoundError:
|
|
66 |
QMessageBox::information(0, tr("Error"),
|
|
67 |
tr("The host was not found. Please check the host name and port settings."));
|
|
68 |
break;
|
|
69 |
case QAbstractSocket::ConnectionRefusedError:
|
|
70 |
QMessageBox::information(0, tr("Error"),
|
|
71 |
tr("Connection refused"));
|
|
72 |
break;
|
|
73 |
default:
|
|
74 |
QMessageBox::information(0, tr("Error"),
|
|
75 |
NetSocket.errorString());
|
|
76 |
}
|
|
77 |
}
|
|
78 |
|
|
79 |
void HWNet::Connect(const QString & hostName, quint16 port)
|
|
80 |
{
|
|
81 |
state = nsConnecting;
|
|
82 |
NetSocket.connectToHost(hostName, port);
|
|
83 |
}
|
|
84 |
|
|
85 |
|
|
86 |
void HWNet::OnConnect()
|
|
87 |
{
|
|
88 |
state = nsConnected;
|
|
89 |
SendNet(QString("USER hwgame 1 2 Hedgewars game"));
|
|
90 |
SendNet(QString("NICK Hedgewars"));
|
|
91 |
}
|
|
92 |
|
|
93 |
void HWNet::OnDisconnect()
|
|
94 |
{
|
|
95 |
state = nsDisconnected;
|
|
96 |
}
|
|
97 |
|
|
98 |
void HWNet::Perform()
|
|
99 |
{
|
|
100 |
SendNet(QString("LIST"));
|
|
101 |
SendNet(QString("JOIN #hw"));
|
|
102 |
}
|
|
103 |
|
|
104 |
void HWNet::Disconnect()
|
|
105 |
{
|
|
106 |
switch (state)
|
|
107 |
{
|
|
108 |
case nsDisconnected:
|
|
109 |
{
|
|
110 |
break;
|
|
111 |
}
|
|
112 |
case nsConnecting:
|
|
113 |
case nsQuitting:
|
|
114 |
{
|
|
115 |
NetSocket.disconnect();
|
|
116 |
break;
|
|
117 |
}
|
|
118 |
default:
|
|
119 |
{
|
|
120 |
state = nsQuitting;
|
|
121 |
SendNet(QString("QUIT :oops"));
|
|
122 |
}
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
|
126 |
void HWNet::SendNet(const QString & str)
|
|
127 |
{
|
|
128 |
SendNet(str.toLatin1());
|
|
129 |
}
|
|
130 |
|
|
131 |
void HWNet::SendNet(const QByteArray & buf)
|
|
132 |
{
|
|
133 |
if (buf.size() > 510) return;
|
|
134 |
NetSocket.write(buf);
|
|
135 |
NetSocket.write("\x0d\x0a", 2);
|
|
136 |
}
|
|
137 |
|
|
138 |
void HWNet::ParseLine(const QString & msg)
|
|
139 |
{
|
|
140 |
if (IRCmsg_cmd_param->exactMatch(msg))
|
|
141 |
{
|
|
142 |
msgcmd_paramHandler(msg);
|
|
143 |
} else
|
|
144 |
if (IRCmsg_number_param->exactMatch(msg))
|
|
145 |
{
|
|
146 |
msgnumber_paramHandler(msg);
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
void HWNet::msgcmd_paramHandler(const QString & msg)
|
|
151 |
{
|
|
152 |
QStringList list = msg.split(" :");
|
|
153 |
if (list[0] == "PING")
|
|
154 |
{
|
|
155 |
SendNet(QString("PONG %1").arg(list[1]));
|
|
156 |
}
|
|
157 |
}
|
|
158 |
|
|
159 |
void HWNet::msgnumber_paramHandler(const QString & msg)
|
|
160 |
{
|
|
161 |
QStringList list = msg.split(" ");
|
|
162 |
bool ok;
|
|
163 |
quint16 number = list[1].toInt(&ok);
|
|
164 |
if (!ok)
|
|
165 |
return ;
|
|
166 |
switch (number)
|
|
167 |
{
|
|
168 |
case 001 :
|
|
169 |
{
|
|
170 |
Perform();
|
|
171 |
emit Connected();
|
|
172 |
break;
|
|
173 |
}
|
|
174 |
case 322 :
|
|
175 |
{
|
|
176 |
emit AddGame(list[3]);
|
|
177 |
}
|
|
178 |
}
|
|
179 |
}
|