qmlfrontend/net_session.h
branchui-scaling
changeset 15288 c4fd2813b127
parent 15083 fb7a9b0119d3
equal deleted inserted replaced
13395:0135e64c6c66 15288:c4fd2813b127
       
     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 PlayersListModel;
       
    10 class RoomsListModel;
       
    11 class NetSession : public QObject {
       
    12   Q_OBJECT
       
    13 
       
    14   const int cMinServerVersion = 3;
       
    15   const int cProtocolVersion = 60;
       
    16 
       
    17   // clang-format off
       
    18   Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged)
       
    19   Q_PROPERTY(QAbstractSocket::SocketState state READ state NOTIFY stateChanged)
       
    20   Q_PROPERTY(QString nickname READ nickname WRITE setNickname NOTIFY nicknameChanged)
       
    21   Q_PROPERTY(SessionState sessionState READ sessionState NOTIFY sessionStateChanged)
       
    22   Q_PROPERTY(QString room READ room NOTIFY roomChanged)
       
    23   Q_PROPERTY(QString passwordHash READ passwordHash WRITE setPasswordHash NOTIFY passwordHashChanged)
       
    24   // clang-format on
       
    25 
       
    26  public:
       
    27   enum SessionState { NotConnected, Login, Authentication, Lobby, Room, Game };
       
    28   Q_ENUMS(SessionState)
       
    29 
       
    30   explicit NetSession(QObject *parent = nullptr);
       
    31   ~NetSession() override;
       
    32 
       
    33   QUrl url() const;
       
    34   QAbstractSocket::SocketState state() const;
       
    35 
       
    36   QString nickname() const;
       
    37   SessionState sessionState() const;
       
    38   QString room() const;
       
    39   QString passwordHash() const;
       
    40 
       
    41  public slots:
       
    42   void open();
       
    43   void close();
       
    44 
       
    45   void setUrl(const QUrl &url);
       
    46   void setNickname(const QString &nickname);
       
    47   void setPasswordHash(const QString &passwordHash);
       
    48 
       
    49  signals:
       
    50   void urlChanged(const QUrl url);
       
    51   void stateChanged(QAbstractSocket::SocketState state);
       
    52   void nicknameChanged(const QString &nickname);
       
    53   void sessionStateChanged(SessionState sessionState);
       
    54   void warning(const QString &message);
       
    55   void error(const QString &message);
       
    56   void roomChanged(const QString &room);
       
    57   void passwordHashChanged(const QString &passwordHash);
       
    58   void passwordAsked();
       
    59 
       
    60  private slots:
       
    61   void onReadyRead();
       
    62   void parseNetMessage(const QStringList &message);
       
    63   void handleConnected(const QStringList &parameters);
       
    64   void handlePing(const QStringList &parameters);
       
    65   void handleBye(const QStringList &parameters);
       
    66   void handleUnknownCommand(const QStringList &parameters);
       
    67   void handleAddTeam(const QStringList &parameters);
       
    68   void handleAskPassword(const QStringList &parameters);
       
    69   void handleBanList(const QStringList &parameters);
       
    70   void handleCfg(const QStringList &parameters);
       
    71   void handleChat(const QStringList &parameters);
       
    72   void handleClientFlags(const QStringList &parameters);
       
    73   void handleEm(const QStringList &parameters);
       
    74   void handleError(const QStringList &parameters);
       
    75   void handleHhNum(const QStringList &parameters);
       
    76   void handleInfo(const QStringList &parameters);
       
    77   void handleJoined(const QStringList &parameters);
       
    78   void handleJoining(const QStringList &parameters);
       
    79   void handleKicked(const QStringList &parameters);
       
    80   void handleLeft(const QStringList &parameters);
       
    81   void handleLobbyJoined(const QStringList &parameters);
       
    82   void handleLobbyLeft(const QStringList &parameters);
       
    83   void handleNick(const QStringList &parameters);
       
    84   void handleNotice(const QStringList &parameters);
       
    85   void handlePong(const QStringList &parameters);
       
    86   void handleProto(const QStringList &parameters);
       
    87   void handleRedirect(const QStringList &parameters);
       
    88   void handleRemoveTeam(const QStringList &parameters);
       
    89   void handleReplayStart(const QStringList &parameters);
       
    90   void handleRoomAbandoned(const QStringList &parameters);
       
    91   void handleRoom(const QStringList &parameters);
       
    92   void handleRooms(const QStringList &parameters);
       
    93   void handleRoundFinished(const QStringList &parameters);
       
    94   void handleRunGame(const QStringList &parameters);
       
    95   void handleServerAuth(const QStringList &parameters);
       
    96   void handleServerMessage(const QStringList &parameters);
       
    97   void handleServerVars(const QStringList &parameters);
       
    98   void handleTeamAccepted(const QStringList &parameters);
       
    99   void handleTeamColor(const QStringList &parameters);
       
   100   void handleWarning(const QStringList &parameters);
       
   101 
       
   102   void send(const QString &message);
       
   103   void send(const QString &message, const QString &param);
       
   104   void send(const QString &message, const QStringList &parameters);
       
   105   void send(const QStringList &message);
       
   106 
       
   107   void sendPassword();
       
   108 
       
   109   void setSessionState(SessionState sessionState);
       
   110   void setRoom(const QString &room);
       
   111 
       
   112  private:
       
   113   QUrl m_url;
       
   114   QSharedPointer<QTcpSocket> m_socket;
       
   115   QSharedPointer<PlayersListModel> m_playersModel;
       
   116   QSharedPointer<RoomsListModel> m_roomsModel;
       
   117   QString m_nickname;
       
   118   QStringList m_buffer;
       
   119   SessionState m_sessionState;
       
   120   QString m_serverAuthHash;
       
   121   QString m_room;
       
   122   QString m_serverSalt;
       
   123   QString m_serverHash;
       
   124   QString m_clientSalt;
       
   125   QString m_passwordHash;
       
   126 
       
   127   Q_DISABLE_COPY(NetSession)
       
   128 };
       
   129 
       
   130 #endif  // NET_SESSION_H