ssl: Use QSslSocket directly to avoid redundant qobject_casts
[quassel.git] / src / common / remotepeer.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 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 "proxyline.h"
31 #include "signalproxy.h"
32
33 class QTimer;
34
35 class AuthHandler;
36
37 class COMMON_EXPORT RemotePeer : public Peer
38 {
39     Q_OBJECT
40
41 public:
42     // import the virtuals from the baseclass
43     using Peer::dispatch;
44     using Peer::handle;
45
46     RemotePeer(AuthHandler* authHandler, QTcpSocket* socket, Compressor::CompressionLevel level, QObject* parent = nullptr);
47
48     void setSignalProxy(SignalProxy* proxy) override;
49
50     void setProxyLine(ProxyLine proxyLine);
51
52     virtual QString protocolName() const = 0;
53     QString description() const override;
54     virtual quint16 enabledFeatures() const { return 0; }
55
56     QString address() const override;
57     QHostAddress hostAddress() const;
58     quint16 port() const override;
59
60     bool isOpen() const override;
61     bool isSecure() const override;
62     bool isLocal() const override;
63
64     int lag() const override;
65
66     bool compressionEnabled() const;
67     void setCompressionEnabled(bool enabled);
68
69     QTcpSocket* socket() const;
70
71 public slots:
72     void close(const QString& reason = QString()) override;
73
74 signals:
75     void transferProgress(int current, int max);
76     void socketError(QAbstractSocket::SocketError error, const QString& errorString);
77     void statusMessage(const QString& msg);
78
79     // Only used by LegacyPeer
80     void protocolVersionMismatch(int actual, int expected);
81
82 protected:
83     SignalProxy* signalProxy() const override;
84
85     void writeMessage(const QByteArray& msg);
86     virtual void processMessage(const QByteArray& msg) = 0;
87
88     // These protocol messages get handled internally and won't reach SignalProxy
89     void handle(const Protocol::HeartBeat& heartBeat);
90     void handle(const Protocol::HeartBeatReply& heartBeatReply);
91     virtual void dispatch(const Protocol::HeartBeat& msg) = 0;
92     virtual void dispatch(const Protocol::HeartBeatReply& msg) = 0;
93
94 protected slots:
95     virtual void onSocketStateChanged(QAbstractSocket::SocketState state);
96     virtual void onSocketError(QAbstractSocket::SocketError error);
97
98 private slots:
99     void onReadyRead();
100     void onCompressionError(Compressor::Error error);
101
102     void sendHeartBeat();
103     void changeHeartBeatInterval(int secs);
104
105 private:
106     bool readMessage(QByteArray& msg);
107
108 private:
109     QTcpSocket* _socket;
110     Compressor* _compressor;
111     SignalProxy* _signalProxy;
112     ProxyLine _proxyLine;
113     bool _useProxyLine;
114     QTimer* _heartBeatTimer;
115     int _heartBeatCount;
116     int _lag;
117     quint32 _msgSize;
118 };