author | Wuzzy <Wuzzy2@mail.ru> |
Wed, 29 May 2019 01:05:20 +0200 | |
changeset 15098 | a2d09a597fbb |
parent 15068 | 773beead236f |
child 15099 | fb7a9b0119d3 |
permissions | -rw-r--r-- |
14936 | 1 |
#include "net_session.h" |
2 |
||
15068
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
3 |
#include "players_model.h" |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
4 |
#include "rooms_model.h" |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
5 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
6 |
NetSession::NetSession(QObject *parent) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
7 |
: QObject(parent), |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
8 |
m_playersModel(new PlayersListModel()), |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
9 |
m_roomsModel(new RoomsListModel()) {} |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
10 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
11 |
NetSession::~NetSession() { close(); } |
14936 | 12 |
|
13 |
QUrl NetSession::url() const { return m_url; } |
|
14 |
||
15 |
QAbstractSocket::SocketState NetSession::state() const { |
|
16 |
if (m_socket) |
|
17 |
return m_socket->state(); |
|
18 |
else |
|
19 |
return QAbstractSocket::UnconnectedState; |
|
20 |
} |
|
21 |
||
22 |
void NetSession::open() { |
|
23 |
m_socket.reset(new QTcpSocket()); |
|
24 |
||
25 |
connect(m_socket.data(), &QAbstractSocket::stateChanged, this, |
|
26 |
&NetSession::stateChanged); |
|
27 |
connect(m_socket.data(), &QTcpSocket::readyRead, this, |
|
28 |
&NetSession::onReadyRead); |
|
29 |
||
30 |
m_socket->connectToHost(m_url.host(), |
|
31 |
static_cast<quint16>(m_url.port(46631))); |
|
32 |
} |
|
33 |
||
34 |
QString NetSession::nickname() const { return m_nickname; } |
|
35 |
||
36 |
QString NetSession::password() const { return m_password; } |
|
37 |
||
38 |
NetSession::SessionState NetSession::sessionState() const { |
|
39 |
return m_sessionState; |
|
40 |
} |
|
41 |
||
15068
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
42 |
QString NetSession::room() const { return m_room; } |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
43 |
|
14936 | 44 |
void NetSession::setUrl(const QUrl &url) { |
45 |
if (m_url == url) return; |
|
46 |
||
47 |
m_url = url; |
|
48 |
emit urlChanged(m_url); |
|
49 |
} |
|
50 |
||
51 |
void NetSession::setNickname(const QString &nickname) { |
|
52 |
if (m_nickname == nickname) return; |
|
53 |
||
54 |
m_nickname = nickname; |
|
55 |
emit nicknameChanged(m_nickname); |
|
56 |
} |
|
57 |
||
58 |
void NetSession::setPassword(const QString &password) { |
|
59 |
if (m_password == password) return; |
|
60 |
||
61 |
m_password = password; |
|
62 |
emit passwordChanged(m_password); |
|
63 |
} |
|
64 |
||
15068
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
65 |
void NetSession::setRoom(const QString &room) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
66 |
if (m_room == room) return; |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
67 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
68 |
m_room = room; |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
69 |
emit roomChanged(m_room); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
70 |
} |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
71 |
|
14936 | 72 |
void NetSession::close() { |
73 |
if (!m_socket.isNull()) { |
|
74 |
m_socket->disconnectFromHost(); |
|
75 |
m_socket.clear(); |
|
76 |
||
77 |
setSessionState(NotConnected); |
|
15068
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
78 |
setRoom({}); |
14936 | 79 |
} |
80 |
} |
|
81 |
||
82 |
void NetSession::onReadyRead() { |
|
83 |
while (m_socket->canReadLine()) { |
|
84 |
auto line = QString::fromUtf8(m_socket->readLine().simplified()); |
|
85 |
||
86 |
if (line.isEmpty()) { |
|
87 |
parseNetMessage(m_buffer); |
|
88 |
m_buffer.clear(); |
|
89 |
} else { |
|
90 |
m_buffer.append(line); |
|
91 |
} |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
void NetSession::parseNetMessage(const QStringList &message) { |
|
96 |
if (message.isEmpty()) { |
|
97 |
qWarning() << "Empty net message received"; |
|
98 |
return; |
|
99 |
} |
|
100 |
||
101 |
qDebug() << "[SERVER]" << message; |
|
102 |
||
103 |
using Handler = std::function<void(NetSession *, const QStringList &)>; |
|
104 |
static QMap<QString, Handler> commandsMap{ |
|
15060 | 105 |
{"ADD_TEAM", &NetSession::handleAddTeam}, |
106 |
{"ASKPASSWORD", &NetSession::handleAskPassword}, |
|
107 |
{"BANLIST", &NetSession::handleBanList}, |
|
108 |
{"BYE", &NetSession::handleBye}, |
|
109 |
{"CFG", &NetSession::handleCfg}, |
|
110 |
{"CHAT", &NetSession::handleChat}, |
|
111 |
{"CLIENT_FLAGS", &NetSession::handleClientFlags}, |
|
14936 | 112 |
{"CONNECTED", &NetSession::handleConnected}, |
15060 | 113 |
{"EM", &NetSession::handleEm}, |
114 |
{"ERROR", &NetSession::handleError}, |
|
115 |
{"HH_NUM", &NetSession::handleHhNum}, |
|
116 |
{"INFO", &NetSession::handleInfo}, |
|
117 |
{"JOINED", &NetSession::handleJoined}, |
|
118 |
{"JOINING", &NetSession::handleJoining}, |
|
119 |
{"KICKED", &NetSession::handleKicked}, |
|
120 |
{"LEFT", &NetSession::handleLeft}, |
|
121 |
{"LOBBY:JOINED", &NetSession::handleLobbyJoined}, |
|
122 |
{"LOBBY:LEFT", &NetSession::handleLobbyLeft}, |
|
123 |
{"NICK", &NetSession::handleNick}, |
|
124 |
{"NOTICE", &NetSession::handleNotice}, |
|
14936 | 125 |
{"PING", &NetSession::handlePing}, |
15060 | 126 |
{"PONG", &NetSession::handlePong}, |
127 |
{"PROTO", &NetSession::handleProto}, |
|
128 |
{"REDIRECT", &NetSession::handleRedirect}, |
|
129 |
{"REMOVE_TEAM", &NetSession::handleRemoveTeam}, |
|
130 |
{"REPLAY_START", &NetSession::handleReplayStart}, |
|
131 |
{"ROOMABANDONED", &NetSession::handleRoomAbandoned}, |
|
132 |
{"ROOM", &NetSession::handleRoom}, |
|
133 |
{"ROOMS", &NetSession::handleRooms}, |
|
134 |
{"ROUND_FINISHED", &NetSession::handleRoundFinished}, |
|
135 |
{"RUN_GAME", &NetSession::handleRunGame}, |
|
136 |
{"SERVER_AUTH", &NetSession::handleServerAuth}, |
|
137 |
{"SERVER_MESSAGE", &NetSession::handleServerMessage}, |
|
138 |
{"SERVER_VARS", &NetSession::handleServerVars}, |
|
139 |
{"TEAM_ACCEPTED", &NetSession::handleTeamAccepted}, |
|
140 |
{"TEAM_COLOR", &NetSession::handleTeamColor}, |
|
141 |
{"WARNING", &NetSession::handleWarning}, |
|
142 |
}; |
|
14936 | 143 |
|
144 |
auto handler = |
|
145 |
commandsMap.value(message[0], &NetSession::handleUnknownCommand); |
|
146 |
||
147 |
handler(this, message.mid(1)); |
|
148 |
} |
|
149 |
||
150 |
void NetSession::handleConnected(const QStringList ¶meters) { |
|
15060 | 151 |
if (parameters.length() < 2 || parameters[1].toInt() < cMinServerVersion) { |
152 |
send("QUIT", "Server too old"); |
|
153 |
emit error(tr("Server too old")); |
|
154 |
close(); |
|
155 |
} else { |
|
156 |
setSessionState(Login); |
|
157 |
||
158 |
send("NICK", m_nickname); |
|
159 |
send("PROTO", QString::number(cProtocolVersion)); |
|
160 |
} |
|
14936 | 161 |
} |
162 |
||
163 |
void NetSession::handlePing(const QStringList ¶meters) { |
|
164 |
send("PONG", parameters); |
|
165 |
} |
|
166 |
||
167 |
void NetSession::handleBye(const QStringList ¶meters) { close(); } |
|
168 |
||
169 |
void NetSession::handleUnknownCommand(const QStringList ¶meters) { |
|
170 |
Q_UNUSED(parameters); |
|
171 |
||
172 |
qWarning() << "Command is not recognized"; |
|
173 |
} |
|
174 |
||
15060 | 175 |
void NetSession::handleAddTeam(const QStringList ¶meters) {} |
176 |
||
177 |
void NetSession::handleAskPassword(const QStringList ¶meters) {} |
|
178 |
||
179 |
void NetSession::handleBanList(const QStringList ¶meters) {} |
|
180 |
||
181 |
void NetSession::handleCfg(const QStringList ¶meters) {} |
|
182 |
||
183 |
void NetSession::handleChat(const QStringList ¶meters) {} |
|
184 |
||
185 |
void NetSession::handleClientFlags(const QStringList ¶meters) {} |
|
186 |
||
187 |
void NetSession::handleEm(const QStringList ¶meters) {} |
|
188 |
||
189 |
void NetSession::handleError(const QStringList ¶meters) {} |
|
190 |
||
191 |
void NetSession::handleHhNum(const QStringList ¶meters) {} |
|
192 |
||
193 |
void NetSession::handleInfo(const QStringList ¶meters) {} |
|
194 |
||
195 |
void NetSession::handleJoined(const QStringList ¶meters) {} |
|
196 |
||
197 |
void NetSession::handleJoining(const QStringList ¶meters) {} |
|
198 |
||
199 |
void NetSession::handleKicked(const QStringList ¶meters) {} |
|
200 |
||
201 |
void NetSession::handleLeft(const QStringList ¶meters) {} |
|
202 |
||
15068
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
203 |
void NetSession::handleLobbyJoined(const QStringList ¶meters) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
204 |
for (auto player : parameters) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
205 |
if (player == m_nickname) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
206 |
// check if server is authenticated or no authentication was performed at |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
207 |
// all |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
208 |
if (!m_serverAuthHash.isEmpty()) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
209 |
emit error(tr("Server authentication error")); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
210 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
211 |
close(); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
212 |
} |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
213 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
214 |
setSessionState(Lobby); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
215 |
} |
15060 | 216 |
|
15068
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
217 |
m_playersModel->addPlayer(player, false); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
218 |
} |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
219 |
} |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
220 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
221 |
void NetSession::handleLobbyLeft(const QStringList ¶meters) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
222 |
if (parameters.length() == 1) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
223 |
m_playersModel->removePlayer(parameters[0]); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
224 |
} else if (parameters.length() == 2) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
225 |
m_playersModel->removePlayer(parameters[0], parameters[1]); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
226 |
} else { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
227 |
qWarning("Malformed LOBBY:LEFT message"); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
228 |
} |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
229 |
} |
15060 | 230 |
|
231 |
void NetSession::handleNick(const QStringList ¶meters) { |
|
232 |
if (parameters.length()) setNickname(parameters[0]); |
|
233 |
} |
|
234 |
||
235 |
void NetSession::handleNotice(const QStringList ¶meters) {} |
|
236 |
||
237 |
void NetSession::handlePong(const QStringList ¶meters) { |
|
238 |
Q_UNUSED(parameters) |
|
239 |
} |
|
240 |
||
241 |
void NetSession::handleProto(const QStringList ¶meters) {} |
|
242 |
||
243 |
void NetSession::handleRedirect(const QStringList ¶meters) {} |
|
244 |
||
245 |
void NetSession::handleRemoveTeam(const QStringList ¶meters) {} |
|
246 |
||
247 |
void NetSession::handleReplayStart(const QStringList ¶meters) {} |
|
248 |
||
249 |
void NetSession::handleRoomAbandoned(const QStringList ¶meters) {} |
|
250 |
||
15068
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
251 |
void NetSession::handleRoom(const QStringList ¶meters) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
252 |
if (parameters.size() == 10 && parameters[0] == "ADD") { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
253 |
m_roomsModel->addRoom(parameters.mid(1)); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
254 |
} else if (parameters.length() == 11 && parameters[0] == "UPD") { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
255 |
m_roomsModel->updateRoom(parameters[1], parameters.mid(2)); |
15060 | 256 |
|
15068
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
257 |
// keep track of room name so correct name is displayed |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
258 |
if (m_room == parameters[1]) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
259 |
setRoom(parameters[2]); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
260 |
} |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
261 |
} else if (parameters.size() == 2 && parameters[0] == "DEL") { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
262 |
m_roomsModel->removeRoom(parameters[1]); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
263 |
} |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
264 |
} |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
265 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
266 |
void NetSession::handleRooms(const QStringList ¶meters) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
267 |
if (parameters.size() % 9) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
268 |
qWarning("Net: Malformed ROOMS message"); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
269 |
return; |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
270 |
} |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
271 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
272 |
m_roomsModel->setRoomsList(parameters); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
15060
diff
changeset
|
273 |
} |
15060 | 274 |
|
275 |
void NetSession::handleRoundFinished(const QStringList ¶meters) {} |
|
276 |
||
277 |
void NetSession::handleRunGame(const QStringList ¶meters) {} |
|
278 |
||
279 |
void NetSession::handleServerAuth(const QStringList ¶meters) {} |
|
280 |
||
281 |
void NetSession::handleServerMessage(const QStringList ¶meters) {} |
|
282 |
||
283 |
void NetSession::handleServerVars(const QStringList ¶meters) {} |
|
284 |
||
285 |
void NetSession::handleTeamAccepted(const QStringList ¶meters) {} |
|
286 |
||
287 |
void NetSession::handleTeamColor(const QStringList ¶meters) {} |
|
288 |
||
289 |
void NetSession::handleWarning(const QStringList ¶meters) {} |
|
290 |
||
14936 | 291 |
void NetSession::send(const QString &message) { send(QStringList(message)); } |
292 |
||
293 |
void NetSession::send(const QString &message, const QString ¶m) { |
|
294 |
send(QStringList{message, param}); |
|
295 |
} |
|
296 |
||
297 |
void NetSession::send(const QString &message, const QStringList ¶meters) { |
|
298 |
send(QStringList(message) + parameters); |
|
299 |
} |
|
300 |
||
301 |
void NetSession::send(const QStringList &message) { |
|
302 |
Q_ASSERT(!m_socket.isNull()); |
|
303 |
||
304 |
qDebug() << "[CLIENT]" << message; |
|
305 |
||
306 |
m_socket->write(message.join('\n').toUtf8() + "\n\n"); |
|
307 |
} |
|
308 |
||
309 |
void NetSession::setSessionState(NetSession::SessionState sessionState) { |
|
310 |
if (m_sessionState == sessionState) return; |
|
311 |
||
312 |
m_sessionState = sessionState; |
|
313 |
||
314 |
emit sessionStateChanged(sessionState); |
|
315 |
} |