|
1 #ifndef NET_SESSION_H |
|
2 #define NET_SESSION_H |
|
3 |
|
4 #include <QSharedPointer> |
|
5 #include <QSslSocket> |
|
6 #include <QStringList> |
|
7 #include <QUrl> |
|
8 |
|
9 class NetSession : public QObject { |
|
10 Q_OBJECT |
|
11 |
|
12 // clang-format off |
|
13 Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged) |
|
14 Q_PROPERTY(QAbstractSocket::SocketState state READ state NOTIFY stateChanged) |
|
15 Q_PROPERTY(QString nickname READ nickname WRITE setNickname NOTIFY nicknameChanged) |
|
16 Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged) |
|
17 Q_PROPERTY(SessionState sessionState READ sessionState NOTIFY sessionStateChanged) |
|
18 // clang-format on |
|
19 |
|
20 public: |
|
21 enum SessionState { NotConnected, Login, Lobby, Room, Game }; |
|
22 Q_ENUMS(SessionState) |
|
23 |
|
24 explicit NetSession(QObject *parent = nullptr); |
|
25 |
|
26 QUrl url() const; |
|
27 QAbstractSocket::SocketState state() const; |
|
28 |
|
29 Q_INVOKABLE void open(); |
|
30 |
|
31 QString nickname() const; |
|
32 QString password() const; |
|
33 SessionState sessionState() const; |
|
34 |
|
35 public slots: |
|
36 void setUrl(const QUrl &url); |
|
37 void setNickname(const QString &nickname); |
|
38 void setPassword(const QString &password); |
|
39 void close(); |
|
40 |
|
41 signals: |
|
42 void urlChanged(const QUrl url); |
|
43 void stateChanged(QAbstractSocket::SocketState state); |
|
44 void nicknameChanged(const QString &nickname); |
|
45 void passwordChanged(const QString &password); |
|
46 void sessionStateChanged(SessionState sessionState); |
|
47 |
|
48 private slots: |
|
49 void onReadyRead(); |
|
50 void parseNetMessage(const QStringList &message); |
|
51 void handleConnected(const QStringList ¶meters); |
|
52 void handlePing(const QStringList ¶meters); |
|
53 void handleBye(const QStringList ¶meters); |
|
54 void handleUnknownCommand(const QStringList ¶meters); |
|
55 void send(const QString &message); |
|
56 void send(const QString &message, const QString ¶m); |
|
57 void send(const QString &message, const QStringList ¶meters); |
|
58 void send(const QStringList &message); |
|
59 void setSessionState(SessionState sessionState); |
|
60 |
|
61 private: |
|
62 QUrl m_url; |
|
63 QSharedPointer<QTcpSocket> m_socket; |
|
64 QString m_nickname; |
|
65 QString m_password; |
|
66 QStringList m_buffer; |
|
67 SessionState m_sessionState; |
|
68 }; |
|
69 |
|
70 #endif // NET_SESSION_H |