common: Make InternalPeer (more) thread-safe
[quassel.git] / src / common / internalpeer.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 <QString>
24
25 #include "peer.h"
26 #include "protocol.h"
27 #include "signalproxy.h"
28
29 class InternalPeer : public Peer
30 {
31     Q_OBJECT
32
33 public:
34     InternalPeer(QObject *parent = nullptr);
35     ~InternalPeer() override;
36
37     Protocol::Type protocol() const override { return Protocol::InternalProtocol; }
38     QString description() const override;
39
40     QString address() const override;
41     quint16 port() const override;
42
43     SignalProxy *signalProxy() const override;
44     void setSignalProxy(SignalProxy *proxy) override;
45
46     InternalPeer *peer() const;
47     void setPeer(InternalPeer *peer);
48
49     bool isOpen() const override;
50     bool isSecure() const override;
51     bool isLocal() const override;
52
53     int lag() const override;
54
55     void dispatch(const Protocol::SyncMessage &msg) override;
56     void dispatch(const Protocol::RpcCall &msg) override;
57     void dispatch(const Protocol::InitRequest &msg) override;
58     void dispatch(const Protocol::InitData &msg) override;
59
60     /* These are not needed for InternalPeer */
61     void dispatch(const Protocol::RegisterClient &) override {}
62     void dispatch(const Protocol::ClientDenied &) override {}
63     void dispatch(const Protocol::ClientRegistered &) override {}
64     void dispatch(const Protocol::SetupData &) override {}
65     void dispatch(const Protocol::SetupFailed &) override {}
66     void dispatch(const Protocol::SetupDone &) override {}
67     void dispatch(const Protocol::Login &) override {}
68     void dispatch(const Protocol::LoginFailed &) override {}
69     void dispatch(const Protocol::LoginSuccess &) override {}
70     void dispatch(const Protocol::SessionState &) override {}
71
72 public slots:
73     void close(const QString &reason = QString()) override;
74
75 signals:
76     void dispatchMessage(const Protocol::SyncMessage &msg);
77     void dispatchMessage(const Protocol::RpcCall &msg);
78     void dispatchMessage(const Protocol::InitRequest &msg);
79     void dispatchMessage(const Protocol::InitData &msg);
80
81 private slots:
82     void peerDisconnected();
83
84     void handleMessage(const Protocol::SyncMessage &msg);
85     void handleMessage(const Protocol::RpcCall &msg);
86     void handleMessage(const Protocol::InitRequest &msg);
87     void handleMessage(const Protocol::InitData &msg);
88
89 private:
90     template<typename T>
91     void handle(const T &msg);
92
93 private:
94     SignalProxy *_proxy{nullptr};
95     bool _isOpen{false};
96 };