From 52f3b434a7d26c2cf7fb614ca14ada0b1abdb378 Mon Sep 17 00:00:00 2001 From: Manuel Schneider Date: Wed, 21 May 2014 12:07:58 +0200 Subject: Several changes: * [Client] Remove unneccessary members _ip, * [Client] Remove unneccessary class forward of QSslSocket * [Client] Make member initializer list of non-const members part of ctor * [Client] Make _socket pointer const member * [Client] Drop socket-delete indirektion --- src/server/net/client.cpp | 56 +++++++++++++++++-------------------------- src/server/net/client.h | 21 ++++++++-------- src/shared/networkmessage.cpp | 2 +- src/shared/networkmessage.h | 2 +- 4 files changed, 35 insertions(+), 46 deletions(-) diff --git a/src/server/net/client.cpp b/src/server/net/client.cpp index 4cb9f8e..fbf5b59 100644 --- a/src/server/net/client.cpp +++ b/src/server/net/client.cpp @@ -9,8 +9,6 @@ #include "../util/global.h" #include "../../shared/settings.h" #include "../../shared/util.h" -#include -#include #include #include @@ -18,14 +16,20 @@ int Client::_clientIdCounter = 0; /******************************************************************************/ -Client::Client(QSslSocket* socket) : - _socket(socket), _authed(0), _timerDelete(0), _desiredProjectionSource(0), _isProjectionSource(false), - _currentProjectionSource(0), _vncPort(0), _activeVncClient(false), _isTutor(false) +Client::Client(QSslSocket* socket) : _socket(socket) { assert(socket != NULL); + _authed = 0; + _desiredProjectionSource = 0; + _isProjectionSource = false; + _currentProjectionSource = 0; + _vncPort = 0; + _activeVncClient = false; + _isTutor = false; + _id = ++_clientIdCounter; - _ip = _socket->peerAddress().toString(); - qDebug("*** Client %s created.", qPrintable(_ip)); + //_ip = _socket->peerAddress().toString(); + qDebug("*** Client %s created.", qPrintable(_socket->peerAddress().toString())); // Connect important signals connect(_socket, SIGNAL(disconnected()), this, SLOT(disconnect())); @@ -57,7 +61,7 @@ Client::~Client() qCritical("**** SOCKET DELETE IN DESTRUCTOR"); _socket->deleteLater(); } - qDebug("*** Client %s destroyed.", qPrintable(_ip)); + qDebug("*** Client %s destroyed.", qPrintable(_socket->peerAddress().toString())); } /******************************************************************************/ @@ -67,7 +71,7 @@ void Client::timerEvent(QTimerEvent* event) { if (_pingTimeout < QDateTime::currentMSecsSinceEpoch()) { - qDebug() << "Client" << _ip << "has a ping timeout."; + qDebug() << "Client" << _socket->peerAddress().toString() << "has a ping timeout."; killTimer(_timerPingTimeout); this->disconnect(); } @@ -79,20 +83,6 @@ void Client::timerEvent(QTimerEvent* event) _timerIdAuthTimeout = 0; this->disconnect(); } - else if (event->timerId() == _timerDelete) - { - if (_socket == NULL || _socket->state() == QAbstractSocket::UnconnectedState) - { - if (_socket != NULL) - _socket->deleteLater(); - _socket = NULL; - killTimer(_timerDelete); - this->deleteLater(); - return; - } - _socket->abort(); - qDebug("A socket is still pending..."); - } else killTimer(event->timerId()); } @@ -105,7 +95,7 @@ void Client::sendMessage(NetworkMessage& message) message.writeMessage(_socket); if (!message.writeComplete()) { - qCritical() << "SendMessage to client " << _name << "@" << _ip << " failed!"; + qCritical() << "SendMessage to client " << _name << "@" << _socket->peerAddress().toString() << " failed!"; } } @@ -187,7 +177,7 @@ void Client::handleMsg() { if (_vncPort <= 0) { - qDebug() << "Starting VNC server on client" << _name << " (" << _ip+_vncPort << ") failed."; + qDebug() << "Starting VNC server on client" << _name << " (" << _socket->peerAddress().toString()+_vncPort << ") failed."; // TODO: Show message on manager } else @@ -318,7 +308,7 @@ void Client::startVncClient(const Client * const to) { NetworkMessage msg; msg.setField(_ID, _VNCCLIENT); - msg.setField("HOST", to->_ip); + msg.setField("HOST", to->_socket->peerAddress().toString()); msg.setField("PORT", QString::number(to->_vncPort)); msg.setField("ROPASS", to->_vncRoPass); msg.setField("CLIENTID", QString::number(to->_id)); @@ -356,12 +346,10 @@ void Client::setTutor(bool enable) /******************************************************************************/ void Client::disconnect() { - if (_timerDelete == 0) - { - _timerDelete = startTimer(500); - qDebug("*** Client %s disconnected.", qPrintable(_ip)); - _socket->blockSignals(true); - _socket->abort(); - emit disconnected(); - } + qDebug("*** Client %s disconnected.", qPrintable(_socket->peerAddress().toString())); + _socket->blockSignals(true); + _socket->abort(); + _socket->deleteLater(); + this->deleteLater(); + emit disconnected(); } diff --git a/src/server/net/client.h b/src/server/net/client.h index bbd7ed0..244411d 100644 --- a/src/server/net/client.h +++ b/src/server/net/client.h @@ -2,12 +2,13 @@ #define CLIENT_H_ #include -#include +#include #include #include +#include #include "../../shared/networkmessage.h" -class QSslSocket; +//class QSslSocket; struct ClientLogin { @@ -17,20 +18,20 @@ struct ClientLogin QString ip; }; + class Client : public QObject { Q_OBJECT -public: +public: explicit Client(QSslSocket* socket); ~Client(); + void requestThumb(const int width, const int height); - //void acceptData(); const inline bool isAuthed() const { return _authed == 2; } const inline QString& name() const { return _name; } const inline QString& host() const { return _host; } - const inline QString& ip() const { return _ip; } - // The computer ID (used eg. for saving the frame positions) is currently the IP, but this is an extra method for easier modification later on + const inline QString ip() const { return _socket->peerAddress().toString(); } const inline int id() const { return _id; } inline const bool isActiveVncClient() const { return _activeVncClient; } inline const bool isActiveVncServer() const { return _vncPort > 0; } @@ -48,17 +49,17 @@ public: void lockScreen(bool); private: - static int _clientIdCounter; - QSslSocket *_socket; + QSslSocket * const _socket; + + static int _clientIdCounter; int _authed; // 0 = challenge sent, awaiting reply 1 = challenge ok, client challenge replied, awaiting login, 2 = ESTABLISHED QString _name; QString _host; - QString _ip; QByteArray _challenge; qint64 _pingTimeout; NetworkMessage _fromClient; - int _timerIdAuthTimeout, _timerDelete, _timerPingTimeout; + int _timerIdAuthTimeout, _timerPingTimeout; int _id; // this client's unique id // If this client should be projected to from another client, the other // client's id is set here. 0 otherwise. This is not currently used and it is diff --git a/src/shared/networkmessage.cpp b/src/shared/networkmessage.cpp index 20501bf..b0b82cd 100644 --- a/src/shared/networkmessage.cpp +++ b/src/shared/networkmessage.cpp @@ -201,7 +201,7 @@ bool NetworkMessage::parseMessage(char *buffer) return true; } -bool NetworkMessage::writeMessage(QAbstractSocket* socket) +bool NetworkMessage::writeMessage(QAbstractSocket * const socket) { if (_mode != 2) { diff --git a/src/shared/networkmessage.h b/src/shared/networkmessage.h index 5e9b3ee..3742890 100644 --- a/src/shared/networkmessage.h +++ b/src/shared/networkmessage.h @@ -58,7 +58,7 @@ public: virtual ~NetworkMessage(); bool readMessage(QAbstractSocket* socket); bool readMessage(char* data, quint32 len); - bool writeMessage(QAbstractSocket* socket); + bool writeMessage(QAbstractSocket * const socket); bool writeMessage(QUdpSocket* socket, const QHostAddress& address, quint16 port); void reset() { _fields.clear(); _bufferSize = 0; _mode = 0; } const bool readComplete() const { return _mode == 3; } -- cgit v1.2.3-55-g7522