QXmpp: XmppSocket.h Source File

QXmpp Version: 1.14.3

Loading...
Searching...
No Matches
XmppSocket.h
1// SPDX-FileCopyrightText: 2024 Linus Jahn <lnj@kaidan.im>
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
5#ifndef XMPPSOCKET_H
6#define XMPPSOCKET_H
7
8#include "QXmppLogger.h"
9
10#include "StreamError.h"
11
12#include <QAbstractSocket>
13#include <QDomDocument>
14#include <QSslError>
15#include <QXmlStreamReader>
16
17class QSslSocket;
18class TestStream;
19class tst_QXmppStream;
20
21namespace QXmpp::Private {
22
23struct StreamOpen;
24
25struct ServerAddress {
26 enum ConnectionType {
27 Tcp,
28 Tls,
29 };
30
31 ConnectionType type;
32 QString host;
33 quint16 port;
34};
35
36class SendDataInterface
37{
38public:
39 virtual bool sendData(const QByteArray &) = 0;
40};
41
42class DomReader
43{
44public:
45 struct Unfinished { };
46
47 enum ErrorType {
48 InvalidState,
49 NotWellFormed,
50 UnsupportedXmlFeature,
51 };
52 struct Error {
53 ErrorType type;
54 QString text;
55 };
56
57 using Result = std::variant<QDomElement, Unfinished, Error>;
58
59 Result process(QXmlStreamReader &);
60
61private:
62 QDomDocument doc;
63 QDomElement currentElement;
64 uint depth = 0;
65};
66
67class QXMPP_EXPORT XmppSocket : public QXmppLoggable, public SendDataInterface
68{
69 Q_OBJECT
70public:
71 explicit XmppSocket(QObject *parent);
72 XmppSocket(QSslSocket *socket, QObject *parent);
73 ~XmppSocket() override = default;
74
75 QSslSocket *internalSocket() const { return m_socket; }
76 void resetInternalSocket();
77
78 bool isConnected() const;
79 void connectToHost(const ServerAddress &);
80 void disconnectFromHost();
81 bool sendData(const QByteArray &) override;
82 void resetStream();
83 bool isStreamReceived() const { return m_streamReceived; }
84
85 Q_SIGNAL void started();
86 Q_SIGNAL void disconnected();
87 Q_SIGNAL void stanzaReceived(const QDomElement &);
88 Q_SIGNAL void streamReceived(const QXmpp::Private::StreamOpen &);
89 Q_SIGNAL void streamClosed();
90 Q_SIGNAL void errorOccurred(const QString &text, std::variant<StreamError, QAbstractSocket::SocketError> condition);
91 Q_SIGNAL void sslErrorsOccurred(const QList<QSslError> &errors);
92 // TODO: replace with own connection state
93 Q_SIGNAL void internalSocketStateChanged();
94
95private:
96 void setSocket(QSslSocket *socket);
97 void throwError(const QString &text, StreamError condition);
98 void processData(const QString &data);
99
100 friend class ::tst_QXmppStream;
101
102 QXmlStreamReader m_reader;
103 std::optional<DomReader> m_domReader;
104 bool m_streamReceived = false;
105 bool m_directTls = false;
106 bool m_acceptInput = true;
107
108 QSslSocket *m_socket = nullptr;
109};
110
111} // namespace QXmpp::Private
112
113#endif // XMPPSOCKET_H