14936
|
1 |
#include "net_session.h"
|
|
2 |
|
|
3 |
NetSession::NetSession(QObject *parent) : QObject(parent) {}
|
|
4 |
|
|
5 |
QUrl NetSession::url() const { return m_url; }
|
|
6 |
|
|
7 |
QAbstractSocket::SocketState NetSession::state() const {
|
|
8 |
if (m_socket)
|
|
9 |
return m_socket->state();
|
|
10 |
else
|
|
11 |
return QAbstractSocket::UnconnectedState;
|
|
12 |
}
|
|
13 |
|
|
14 |
void NetSession::open() {
|
|
15 |
m_socket.reset(new QTcpSocket());
|
|
16 |
|
|
17 |
connect(m_socket.data(), &QAbstractSocket::stateChanged, this,
|
|
18 |
&NetSession::stateChanged);
|
|
19 |
connect(m_socket.data(), &QTcpSocket::readyRead, this,
|
|
20 |
&NetSession::onReadyRead);
|
|
21 |
|
|
22 |
m_socket->connectToHost(m_url.host(),
|
|
23 |
static_cast<quint16>(m_url.port(46631)));
|
|
24 |
}
|
|
25 |
|
|
26 |
QString NetSession::nickname() const { return m_nickname; }
|
|
27 |
|
|
28 |
QString NetSession::password() const { return m_password; }
|
|
29 |
|
|
30 |
NetSession::SessionState NetSession::sessionState() const {
|
|
31 |
return m_sessionState;
|
|
32 |
}
|
|
33 |
|
|
34 |
void NetSession::setUrl(const QUrl &url) {
|
|
35 |
if (m_url == url) return;
|
|
36 |
|
|
37 |
m_url = url;
|
|
38 |
emit urlChanged(m_url);
|
|
39 |
}
|
|
40 |
|
|
41 |
void NetSession::setNickname(const QString &nickname) {
|
|
42 |
if (m_nickname == nickname) return;
|
|
43 |
|
|
44 |
m_nickname = nickname;
|
|
45 |
emit nicknameChanged(m_nickname);
|
|
46 |
}
|
|
47 |
|
|
48 |
void NetSession::setPassword(const QString &password) {
|
|
49 |
if (m_password == password) return;
|
|
50 |
|
|
51 |
m_password = password;
|
|
52 |
emit passwordChanged(m_password);
|
|
53 |
}
|
|
54 |
|
|
55 |
void NetSession::close() {
|
|
56 |
if (!m_socket.isNull()) {
|
|
57 |
m_socket->disconnectFromHost();
|
|
58 |
m_socket.clear();
|
|
59 |
|
|
60 |
setSessionState(NotConnected);
|
|
61 |
}
|
|
62 |
}
|
|
63 |
|
|
64 |
void NetSession::onReadyRead() {
|
|
65 |
while (m_socket->canReadLine()) {
|
|
66 |
auto line = QString::fromUtf8(m_socket->readLine().simplified());
|
|
67 |
|
|
68 |
if (line.isEmpty()) {
|
|
69 |
parseNetMessage(m_buffer);
|
|
70 |
m_buffer.clear();
|
|
71 |
} else {
|
|
72 |
m_buffer.append(line);
|
|
73 |
}
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
void NetSession::parseNetMessage(const QStringList &message) {
|
|
78 |
if (message.isEmpty()) {
|
|
79 |
qWarning() << "Empty net message received";
|
|
80 |
return;
|
|
81 |
}
|
|
82 |
|
|
83 |
qDebug() << "[SERVER]" << message;
|
|
84 |
|
|
85 |
using Handler = std::function<void(NetSession *, const QStringList &)>;
|
|
86 |
static QMap<QString, Handler> commandsMap{
|
15060
|
87 |
{"ADD_TEAM", &NetSession::handleAddTeam},
|
|
88 |
{"ASKPASSWORD", &NetSession::handleAskPassword},
|
|
89 |
{"BANLIST", &NetSession::handleBanList},
|
|
90 |
{"BYE", &NetSession::handleBye},
|
|
91 |
{"CFG", &NetSession::handleCfg},
|
|
92 |
{"CHAT", &NetSession::handleChat},
|
|
93 |
{"CLIENT_FLAGS", &NetSession::handleClientFlags},
|
14936
|
94 |
{"CONNECTED", &NetSession::handleConnected},
|
15060
|
95 |
{"EM", &NetSession::handleEm},
|
|
96 |
{"ERROR", &NetSession::handleError},
|
|
97 |
{"HH_NUM", &NetSession::handleHhNum},
|
|
98 |
{"INFO", &NetSession::handleInfo},
|
|
99 |
{"JOINED", &NetSession::handleJoined},
|
|
100 |
{"JOINING", &NetSession::handleJoining},
|
|
101 |
{"KICKED", &NetSession::handleKicked},
|
|
102 |
{"LEFT", &NetSession::handleLeft},
|
|
103 |
{"LOBBY:JOINED", &NetSession::handleLobbyJoined},
|
|
104 |
{"LOBBY:LEFT", &NetSession::handleLobbyLeft},
|
|
105 |
{"NICK", &NetSession::handleNick},
|
|
106 |
{"NOTICE", &NetSession::handleNotice},
|
14936
|
107 |
{"PING", &NetSession::handlePing},
|
15060
|
108 |
{"PONG", &NetSession::handlePong},
|
|
109 |
{"PROTO", &NetSession::handleProto},
|
|
110 |
{"REDIRECT", &NetSession::handleRedirect},
|
|
111 |
{"REMOVE_TEAM", &NetSession::handleRemoveTeam},
|
|
112 |
{"REPLAY_START", &NetSession::handleReplayStart},
|
|
113 |
{"ROOMABANDONED", &NetSession::handleRoomAbandoned},
|
|
114 |
{"ROOM", &NetSession::handleRoom},
|
|
115 |
{"ROOMS", &NetSession::handleRooms},
|
|
116 |
{"ROUND_FINISHED", &NetSession::handleRoundFinished},
|
|
117 |
{"RUN_GAME", &NetSession::handleRunGame},
|
|
118 |
{"SERVER_AUTH", &NetSession::handleServerAuth},
|
|
119 |
{"SERVER_MESSAGE", &NetSession::handleServerMessage},
|
|
120 |
{"SERVER_VARS", &NetSession::handleServerVars},
|
|
121 |
{"TEAM_ACCEPTED", &NetSession::handleTeamAccepted},
|
|
122 |
{"TEAM_COLOR", &NetSession::handleTeamColor},
|
|
123 |
{"WARNING", &NetSession::handleWarning},
|
|
124 |
};
|
14936
|
125 |
|
|
126 |
auto handler =
|
|
127 |
commandsMap.value(message[0], &NetSession::handleUnknownCommand);
|
|
128 |
|
|
129 |
handler(this, message.mid(1));
|
|
130 |
}
|
|
131 |
|
|
132 |
void NetSession::handleConnected(const QStringList ¶meters) {
|
15060
|
133 |
if (parameters.length() < 2 || parameters[1].toInt() < cMinServerVersion) {
|
|
134 |
send("QUIT", "Server too old");
|
|
135 |
emit error(tr("Server too old"));
|
|
136 |
close();
|
|
137 |
} else {
|
|
138 |
setSessionState(Login);
|
|
139 |
|
|
140 |
send("NICK", m_nickname);
|
|
141 |
send("PROTO", QString::number(cProtocolVersion));
|
|
142 |
}
|
14936
|
143 |
}
|
|
144 |
|
|
145 |
void NetSession::handlePing(const QStringList ¶meters) {
|
|
146 |
send("PONG", parameters);
|
|
147 |
}
|
|
148 |
|
|
149 |
void NetSession::handleBye(const QStringList ¶meters) { close(); }
|
|
150 |
|
|
151 |
void NetSession::handleUnknownCommand(const QStringList ¶meters) {
|
|
152 |
Q_UNUSED(parameters);
|
|
153 |
|
|
154 |
qWarning() << "Command is not recognized";
|
|
155 |
}
|
|
156 |
|
15060
|
157 |
void NetSession::handleAddTeam(const QStringList ¶meters) {}
|
|
158 |
|
|
159 |
void NetSession::handleAskPassword(const QStringList ¶meters) {}
|
|
160 |
|
|
161 |
void NetSession::handleBanList(const QStringList ¶meters) {}
|
|
162 |
|
|
163 |
void NetSession::handleCfg(const QStringList ¶meters) {}
|
|
164 |
|
|
165 |
void NetSession::handleChat(const QStringList ¶meters) {}
|
|
166 |
|
|
167 |
void NetSession::handleClientFlags(const QStringList ¶meters) {}
|
|
168 |
|
|
169 |
void NetSession::handleEm(const QStringList ¶meters) {}
|
|
170 |
|
|
171 |
void NetSession::handleError(const QStringList ¶meters) {}
|
|
172 |
|
|
173 |
void NetSession::handleHhNum(const QStringList ¶meters) {}
|
|
174 |
|
|
175 |
void NetSession::handleInfo(const QStringList ¶meters) {}
|
|
176 |
|
|
177 |
void NetSession::handleJoined(const QStringList ¶meters) {}
|
|
178 |
|
|
179 |
void NetSession::handleJoining(const QStringList ¶meters) {}
|
|
180 |
|
|
181 |
void NetSession::handleKicked(const QStringList ¶meters) {}
|
|
182 |
|
|
183 |
void NetSession::handleLeft(const QStringList ¶meters) {}
|
|
184 |
|
|
185 |
void NetSession::handleLobbyJoined(const QStringList ¶meters) {}
|
|
186 |
|
|
187 |
void NetSession::handleLobbyLeft(const QStringList ¶meters) {}
|
|
188 |
|
|
189 |
void NetSession::handleNick(const QStringList ¶meters) {
|
|
190 |
if (parameters.length()) setNickname(parameters[0]);
|
|
191 |
}
|
|
192 |
|
|
193 |
void NetSession::handleNotice(const QStringList ¶meters) {}
|
|
194 |
|
|
195 |
void NetSession::handlePong(const QStringList ¶meters) {
|
|
196 |
Q_UNUSED(parameters)
|
|
197 |
}
|
|
198 |
|
|
199 |
void NetSession::handleProto(const QStringList ¶meters) {}
|
|
200 |
|
|
201 |
void NetSession::handleRedirect(const QStringList ¶meters) {}
|
|
202 |
|
|
203 |
void NetSession::handleRemoveTeam(const QStringList ¶meters) {}
|
|
204 |
|
|
205 |
void NetSession::handleReplayStart(const QStringList ¶meters) {}
|
|
206 |
|
|
207 |
void NetSession::handleRoomAbandoned(const QStringList ¶meters) {}
|
|
208 |
|
|
209 |
void NetSession::handleRoom(const QStringList ¶meters) {}
|
|
210 |
|
|
211 |
void NetSession::handleRooms(const QStringList ¶meters) {}
|
|
212 |
|
|
213 |
void NetSession::handleRoundFinished(const QStringList ¶meters) {}
|
|
214 |
|
|
215 |
void NetSession::handleRunGame(const QStringList ¶meters) {}
|
|
216 |
|
|
217 |
void NetSession::handleServerAuth(const QStringList ¶meters) {}
|
|
218 |
|
|
219 |
void NetSession::handleServerMessage(const QStringList ¶meters) {}
|
|
220 |
|
|
221 |
void NetSession::handleServerVars(const QStringList ¶meters) {}
|
|
222 |
|
|
223 |
void NetSession::handleTeamAccepted(const QStringList ¶meters) {}
|
|
224 |
|
|
225 |
void NetSession::handleTeamColor(const QStringList ¶meters) {}
|
|
226 |
|
|
227 |
void NetSession::handleWarning(const QStringList ¶meters) {}
|
|
228 |
|
14936
|
229 |
void NetSession::send(const QString &message) { send(QStringList(message)); }
|
|
230 |
|
|
231 |
void NetSession::send(const QString &message, const QString ¶m) {
|
|
232 |
send(QStringList{message, param});
|
|
233 |
}
|
|
234 |
|
|
235 |
void NetSession::send(const QString &message, const QStringList ¶meters) {
|
|
236 |
send(QStringList(message) + parameters);
|
|
237 |
}
|
|
238 |
|
|
239 |
void NetSession::send(const QStringList &message) {
|
|
240 |
Q_ASSERT(!m_socket.isNull());
|
|
241 |
|
|
242 |
qDebug() << "[CLIENT]" << message;
|
|
243 |
|
|
244 |
m_socket->write(message.join('\n').toUtf8() + "\n\n");
|
|
245 |
}
|
|
246 |
|
|
247 |
void NetSession::setSessionState(NetSession::SessionState sessionState) {
|
|
248 |
if (m_sessionState == sessionState) return;
|
|
249 |
|
|
250 |
m_sessionState = sessionState;
|
|
251 |
|
|
252 |
emit sessionStateChanged(sessionState);
|
|
253 |
}
|