9 #include "../serverapp/serverapp.h"
10 #include "../../shared/settings.h"
11 #include "../../shared/util.h"
15 #include <QNetworkInterface>
20 #define CHALLENGE_LEN 20
26 assert(socket !=
nullptr);
31 qDebug(
"*** Client %s created.", qPrintable(
_socket->peerAddress().toString()));
33 connect(
_socket, &QTcpSocket::disconnected,
37 connect(
_socket, QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::errorOccurred),
38 [
this](QAbstractSocket::SocketError) {
41 auto *ssl = qobject_cast<QSslSocket*>(
_socket);
43 connect(ssl, QOverload<
const QList<QSslError> &>::of(&QSslSocket::sslErrors),
44 [
this](
const QList<QSslError> &) {
48 connect(
_socket, &QTcpSocket::readyRead,
56 msgChallenge.
setField(_ID, _CHALLENGE);
67 qDebug() <<
"*** Client" <<
_host <<
" destroyed.";
70 QTimer::singleShot(10, [sck]() {
78 if (
_pingTimeout < QDateTime::currentMSecsSinceEpoch()) {
80 this->
disconnect(
"Disconnecting client because of ping timeout");
86 this->
disconnect(
"Did not authenticate withing three seconds");
88 killTimer(event->timerId());
93 if (
_socket->state() != QAbstractSocket::ConnectedState)
97 qCritical() <<
"SendMessage to client " <<
_name <<
"@" <<
_socket->peerAddress().toString() <<
" failed!";
113 if (
_socket->state() != QAbstractSocket::ConnectedState) {
114 qDebug(
"requestThumb called in bad state");
119 msgTmb.
setField(_X, QString::number(size.width()));
120 msgTmb.
setField(_Y, QString::number(size.height()));
127 if (
_socket->state() != QAbstractSocket::ConnectedState) {
128 qDebug(
"dataArrival called in bad state");
132 while (
_socket->bytesAvailable() > 0) {
135 this->
disconnect(
"Malformed message received from client.");
152 qDebug(
"Received message with empty ID field. ignored.");
164 qDebug(
"Could not decode thumbnail image from client.");
169 }
else if (
id == _VNCSERVER) {
174 qDebug() <<
"Starting VNC server on client" <<
_name <<
" (" <<
_socket->peerAddress().toString() <<
":" << QString::number(
_vncPort) <<
") failed.";
176 qDebug() <<
"Client " <<
_name <<
" stopped its VNC server";
181 qDebug() <<
"Client " <<
_name <<
" started its VNC server";
186 }
else if (
id == _VNCCLIENT) {
190 qDebug() <<
"Client " <<
_name <<
" started its VNC client (watching " << projectionSource <<
")";
195 qDebug() <<
"Client " <<
_name <<
" stopped its VNC client (watched " << projectionSource <<
")";
200 }
else if (
id == _ATTENTION) {
217 request.
ip =
_socket->peerAddress().toString();
219 qDebug() <<
"Login request by " << request.
name << (request.
examMode ?
"(in exam mode)" :
"");
228 qDebug(
"valid, step <- 2");
242 if (
id == _CHALLENGE) {
246 && !(
serverApp->getCurrentRoom()->clientPositions.contains(
_socket->peerAddress().toString()))) {
257 qDebug(
"client's challenge reply was valid, step <- 1");
289 msg.
setField(
"CLIENTID", QString::number(to->
_id));
313 foreach (
const QHostAddress & address, QNetworkInterface::allAddresses())
314 if (address != QHostAddress(QHostAddress::LocalHost)
315 && this->
ip() == address.toString())
337 qDebug() <<
"*** Client" <<
_socket->peerAddress().toString() <<
"disconnected:" << errmsg;
338 if (
_socket->state() == QAbstractSocket::ConnectedState) {
351 return _socket->peerAddress().toString();
void authenticating(Client *client, ClientLogin *request)
void authenticated(Client *client)
static const QByteArray __TRUE("1")
static const QByteArray __FALSE("0")
#define NM_READ_INCOMPLETE
bool writeMessage(QAbstractSocket *socket)
NetworkMessage _fromClient
void timerEvent(QTimerEvent *event) override
QString getFieldString(const QByteArray &key) const
static quint16 hash(const QHostAddress &host)
hash
bool readComplete() const
QByteArray genSha1(const QByteArray *a, const QByteArray *b, const QByteArray *c, const QByteArray *d, const QByteArray *e)
QTcpSocket *const _socket
void disconnect(const char *errmsg)
void startVncClient(const Client *to)
void sendMessage(NetworkMessage &message)
int readMessage(QAbstractSocket *socket)
void thumbUpdated(Client *client, const QImage &thumb)
void removeAttentionInternal()
int projectionSource() const
bool writeComplete() const
void requestThumb(const QSize &size)
Client(QTcpSocket *socket)
bool isManagerMachine() const
Checks if client and manager runs on same machine.
void vncClientStateChange(Client *client)
void vncServerStateChange(Client *client)
static const QByteArray & _BOOL(bool val)
static int _clientIdCounter
QByteArray _rawRemoteScreen
QByteArray getFieldBytes(const QByteArray &key) const
void setField(const QByteArray &key, const QByteArray &value)
void buildErrorMessage(const QString &error)