qa: Replace deprecated qVariantFromValue() by QVariant::fromValue()
[quassel.git] / src / common / remotepeer.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include "common-export.h"
24
25 #include <QDateTime>
26
27 #include "compressor.h"
28 #include "peer.h"
29 #include "protocol.h"
30 #include "signalproxy.h"
31
32 class QTimer;
33
34 class AuthHandler;
35
36 class COMMON_EXPORT RemotePeer : public Peer
37 {
38     Q_OBJECT
39
40 public:
41     // import the virtuals from the baseclass
42     using Peer::dispatch;
43     using Peer::handle;
44
45     RemotePeer(AuthHandler* authHandler, QTcpSocket* socket, Compressor::CompressionLevel level, QObject* parent = nullptr);
46
47     void setSignalProxy(SignalProxy* proxy) override;
48
49     virtual QString protocolName() const = 0;
50     QString description() const override;
51     virtual quint16 enabledFeatures() const { return 0; }
52
53     QString address() const override;
54     quint16 port() const override;
55
56     bool isOpen() const override;
57     bool isSecure() const override;
58     bool isLocal() const override;
59
60     int lag() const override;
61
62     bool compressionEnabled() const;
63     void setCompressionEnabled(bool enabled);
64
65     QTcpSocket* socket() const;
66
67 public slots:
68     void close(const QString& reason = QString()) override;
69
70 signals:
71     void transferProgress(int current, int max);
72     void socketError(QAbstractSocket::SocketError error, const QString& errorString);
73     void statusMessage(const QString& msg);
74
75     // Only used by LegacyPeer
76     void protocolVersionMismatch(int actual, int expected);
77
78 protected:
79     SignalProxy* signalProxy() const override;
80
81     void writeMessage(const QByteArray& msg);
82     virtual void processMessage(const QByteArray& msg) = 0;
83
84     // These protocol messages get handled internally and won't reach SignalProxy
85     void handle(const Protocol::HeartBeat& heartBeat);
86     void handle(const Protocol::HeartBeatReply& heartBeatReply);
87     virtual void dispatch(const Protocol::HeartBeat& msg) = 0;
88     virtual void dispatch(const Protocol::HeartBeatReply& msg) = 0;
89
90 protected slots:
91     virtual void onSocketStateChanged(QAbstractSocket::SocketState state);
92     virtual void onSocketError(QAbstractSocket::SocketError error);
93
94 private slots:
95     void onReadyRead();
96     void onCompressionError(Compressor::Error error);
97
98     void sendHeartBeat();
99     void changeHeartBeatInterval(int secs);
100
101 private:
102     bool readMessage(QByteArray& msg);
103
104 private:
105     QTcpSocket* _socket;
106     Compressor* _compressor;
107     SignalProxy* _signalProxy;
108     QTimer* _heartBeatTimer;
109     int _heartBeatCount;
110     int _lag;
111     quint32 _msgSize;
112 };