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{
|
|
87 |
{"CONNECTED", &NetSession::handleConnected},
|
|
88 |
{"PING", &NetSession::handlePing},
|
|
89 |
{"BYE", &NetSession::handleBye}};
|
|
90 |
|
|
91 |
auto handler =
|
|
92 |
commandsMap.value(message[0], &NetSession::handleUnknownCommand);
|
|
93 |
|
|
94 |
handler(this, message.mid(1));
|
|
95 |
}
|
|
96 |
|
|
97 |
void NetSession::handleConnected(const QStringList ¶meters) {
|
|
98 |
setSessionState(Login);
|
|
99 |
}
|
|
100 |
|
|
101 |
void NetSession::handlePing(const QStringList ¶meters) {
|
|
102 |
send("PONG", parameters);
|
|
103 |
}
|
|
104 |
|
|
105 |
void NetSession::handleBye(const QStringList ¶meters) { close(); }
|
|
106 |
|
|
107 |
void NetSession::handleUnknownCommand(const QStringList ¶meters) {
|
|
108 |
Q_UNUSED(parameters);
|
|
109 |
|
|
110 |
qWarning() << "Command is not recognized";
|
|
111 |
}
|
|
112 |
|
|
113 |
void NetSession::send(const QString &message) { send(QStringList(message)); }
|
|
114 |
|
|
115 |
void NetSession::send(const QString &message, const QString ¶m) {
|
|
116 |
send(QStringList{message, param});
|
|
117 |
}
|
|
118 |
|
|
119 |
void NetSession::send(const QString &message, const QStringList ¶meters) {
|
|
120 |
send(QStringList(message) + parameters);
|
|
121 |
}
|
|
122 |
|
|
123 |
void NetSession::send(const QStringList &message) {
|
|
124 |
Q_ASSERT(!m_socket.isNull());
|
|
125 |
|
|
126 |
qDebug() << "[CLIENT]" << message;
|
|
127 |
|
|
128 |
m_socket->write(message.join('\n').toUtf8() + "\n\n");
|
|
129 |
}
|
|
130 |
|
|
131 |
void NetSession::setSessionState(NetSession::SessionState sessionState) {
|
|
132 |
if (m_sessionState == sessionState) return;
|
|
133 |
|
|
134 |
m_sessionState = sessionState;
|
|
135 |
|
|
136 |
emit sessionStateChanged(sessionState);
|
|
137 |
}
|