qmlfrontend/net_session.h
author unc0rr
Sat, 11 May 2019 22:39:55 +0200
changeset 14915 a3ad06ac390e
child 15039 a4a058dcbbd6
permissions -rw-r--r--
Proof of concept for new net game client
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14915
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     1
#ifndef NET_SESSION_H
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     2
#define NET_SESSION_H
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     3
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     4
#include <QSharedPointer>
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     5
#include <QSslSocket>
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     6
#include <QStringList>
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     7
#include <QUrl>
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     8
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     9
class NetSession : public QObject {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    10
  Q_OBJECT
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    11
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    12
  // clang-format off
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    13
  Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged)
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    14
  Q_PROPERTY(QAbstractSocket::SocketState state READ state NOTIFY stateChanged)
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    15
  Q_PROPERTY(QString nickname READ nickname WRITE setNickname NOTIFY nicknameChanged)
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    16
  Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged)
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    17
  Q_PROPERTY(SessionState sessionState READ sessionState NOTIFY sessionStateChanged)
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    18
  // clang-format on
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    19
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    20
 public:
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    21
  enum SessionState { NotConnected, Login, Lobby, Room, Game };
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    22
  Q_ENUMS(SessionState)
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    23
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    24
  explicit NetSession(QObject *parent = nullptr);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    25
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    26
  QUrl url() const;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    27
  QAbstractSocket::SocketState state() const;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    28
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    29
  Q_INVOKABLE void open();
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    30
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    31
  QString nickname() const;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    32
  QString password() const;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    33
  SessionState sessionState() const;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    34
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    35
 public slots:
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    36
  void setUrl(const QUrl &url);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    37
  void setNickname(const QString &nickname);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    38
  void setPassword(const QString &password);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    39
  void close();
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    40
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    41
 signals:
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    42
  void urlChanged(const QUrl url);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    43
  void stateChanged(QAbstractSocket::SocketState state);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    44
  void nicknameChanged(const QString &nickname);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    45
  void passwordChanged(const QString &password);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    46
  void sessionStateChanged(SessionState sessionState);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    47
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    48
 private slots:
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    49
  void onReadyRead();
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    50
  void parseNetMessage(const QStringList &message);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    51
  void handleConnected(const QStringList &parameters);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    52
  void handlePing(const QStringList &parameters);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    53
  void handleBye(const QStringList &parameters);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    54
  void handleUnknownCommand(const QStringList &parameters);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    55
  void send(const QString &message);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    56
  void send(const QString &message, const QString &param);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    57
  void send(const QString &message, const QStringList &parameters);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    58
  void send(const QStringList &message);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    59
  void setSessionState(SessionState sessionState);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    60
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    61
 private:
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    62
  QUrl m_url;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    63
  QSharedPointer<QTcpSocket> m_socket;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    64
  QString m_nickname;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    65
  QString m_password;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    66
  QStringList m_buffer;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    67
  SessionState m_sessionState;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    68
};
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    69
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    70
#endif  // NET_SESSION_H