qmlfrontend/net_session.cpp
author Wuzzy <Wuzzy2@mail.ru>
Sat, 18 May 2019 16:54:51 +0200
changeset 15002 3ed1cbd31754
parent 14915 a3ad06ac390e
child 15039 a4a058dcbbd6
permissions -rw-r--r--
Backed out changeset 4596357d002d (onPreciseLocal causes desyncs)
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
#include "net_session.h"
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     2
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     3
NetSession::NetSession(QObject *parent) : QObject(parent) {}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     4
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     5
QUrl NetSession::url() const { return m_url; }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     6
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     7
QAbstractSocket::SocketState NetSession::state() const {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     8
  if (m_socket)
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
     9
    return m_socket->state();
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    10
  else
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    11
    return QAbstractSocket::UnconnectedState;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    12
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    13
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    14
void NetSession::open() {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    15
  m_socket.reset(new QTcpSocket());
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    16
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    17
  connect(m_socket.data(), &QAbstractSocket::stateChanged, this,
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    18
          &NetSession::stateChanged);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    19
  connect(m_socket.data(), &QTcpSocket::readyRead, this,
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    20
          &NetSession::onReadyRead);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    21
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    22
  m_socket->connectToHost(m_url.host(),
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    23
                          static_cast<quint16>(m_url.port(46631)));
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    24
}
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
QString NetSession::nickname() const { return m_nickname; }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    27
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    28
QString NetSession::password() const { return m_password; }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    29
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    30
NetSession::SessionState NetSession::sessionState() const {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    31
  return m_sessionState;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    32
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    33
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    34
void NetSession::setUrl(const QUrl &url) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    35
  if (m_url == url) return;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    36
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    37
  m_url = url;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    38
  emit urlChanged(m_url);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    39
}
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
void NetSession::setNickname(const QString &nickname) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    42
  if (m_nickname == nickname) return;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    43
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    44
  m_nickname = nickname;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    45
  emit nicknameChanged(m_nickname);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    46
}
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
void NetSession::setPassword(const QString &password) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    49
  if (m_password == password) return;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    50
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    51
  m_password = password;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    52
  emit passwordChanged(m_password);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    53
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    54
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    55
void NetSession::close() {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    56
  if (!m_socket.isNull()) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    57
    m_socket->disconnectFromHost();
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    58
    m_socket.clear();
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    59
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    60
    setSessionState(NotConnected);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    61
  }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    62
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    63
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    64
void NetSession::onReadyRead() {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    65
  while (m_socket->canReadLine()) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    66
    auto line = QString::fromUtf8(m_socket->readLine().simplified());
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    67
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    68
    if (line.isEmpty()) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    69
      parseNetMessage(m_buffer);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    70
      m_buffer.clear();
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    71
    } else {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    72
      m_buffer.append(line);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    73
    }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    74
  }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    75
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    76
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    77
void NetSession::parseNetMessage(const QStringList &message) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    78
  if (message.isEmpty()) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    79
    qWarning() << "Empty net message received";
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    80
    return;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    81
  }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    82
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    83
  qDebug() << "[SERVER]" << message;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    84
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    85
  using Handler = std::function<void(NetSession *, const QStringList &)>;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    86
  static QMap<QString, Handler> commandsMap{
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    87
      {"CONNECTED", &NetSession::handleConnected},
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    88
      {"PING", &NetSession::handlePing},
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    89
      {"BYE", &NetSession::handleBye}};
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    90
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    91
  auto handler =
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    92
      commandsMap.value(message[0], &NetSession::handleUnknownCommand);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    93
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    94
  handler(this, message.mid(1));
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    95
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    96
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    97
void NetSession::handleConnected(const QStringList &parameters) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    98
  setSessionState(Login);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
    99
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   100
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   101
void NetSession::handlePing(const QStringList &parameters) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   102
  send("PONG", parameters);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   103
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   104
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   105
void NetSession::handleBye(const QStringList &parameters) { close(); }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   106
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   107
void NetSession::handleUnknownCommand(const QStringList &parameters) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   108
  Q_UNUSED(parameters);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   109
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   110
  qWarning() << "Command is not recognized";
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   111
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   112
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   113
void NetSession::send(const QString &message) { send(QStringList(message)); }
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   114
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   115
void NetSession::send(const QString &message, const QString &param) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   116
  send(QStringList{message, param});
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   117
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   118
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   119
void NetSession::send(const QString &message, const QStringList &parameters) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   120
  send(QStringList(message) + parameters);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   121
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   122
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   123
void NetSession::send(const QStringList &message) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   124
  Q_ASSERT(!m_socket.isNull());
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   125
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   126
  qDebug() << "[CLIENT]" << message;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   127
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   128
  m_socket->write(message.join('\n').toUtf8() + "\n\n");
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   129
}
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   130
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   131
void NetSession::setSessionState(NetSession::SessionState sessionState) {
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   132
  if (m_sessionState == sessionState) return;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   133
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   134
  m_sessionState = sessionState;
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   135
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   136
  emit sessionStateChanged(sessionState);
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents:
diff changeset
   137
}