cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / core / identserver.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2022 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 <list>
24
25 #include <QHash>
26 #include <QObject>
27 #include <QPointer>
28 #include <QString>
29 #include <QTcpServer>
30 #include <QTcpSocket>
31
32 #include "coreidentity.h"
33
34 struct Request
35 {
36     QPointer<QTcpSocket> socket;
37     uint16_t localPort;
38     uint16_t remotePort;
39     QString query;
40     qint64 transactionId;
41     qint64 requestId;
42
43     friend bool operator==(const Request& a, const Request& b);
44
45     void respondSuccess(const QString& user);
46     void respondError(const QString& error);
47
48     const static int DISCONNECTION_TIMEOUT = 500;
49 };
50
51 class IdentServer : public QObject
52 {
53     Q_OBJECT
54
55 public:
56     IdentServer(QObject* parent = nullptr);
57
58     bool startListening();
59     void stopListening(const QString& msg);
60     qint64 addWaitingSocket();
61
62 public slots:
63     void addSocket(const CoreIdentity* identity,
64                    const QHostAddress& localAddress,
65                    quint16 localPort,
66                    const QHostAddress& peerAddress,
67                    quint16 peerPort,
68                    qint64 socketId);
69     void removeSocket(const CoreIdentity* identity,
70                       const QHostAddress& localAddress,
71                       quint16 localPort,
72                       const QHostAddress& peerAddress,
73                       quint16 peerPort,
74                       qint64 socketId);
75
76 private slots:
77     void incomingConnection();
78     void respond();
79
80 private:
81     bool responseAvailable(Request request) const;
82
83     qint64 lowestSocketId() const;
84
85     void processWaiting(qint64 socketId);
86
87     void removeWaitingSocket(qint64 socketId);
88
89     QTcpServer _server, _v6server;
90
91     QHash<uint16_t, QString> _connections;
92     std::list<Request> _requestQueue;
93     std::list<qint64> _waiting;
94     qint64 _socketId{0};
95     qint64 _requestId{0};
96 };