QXmpp: QXmppStreamManagement_p.h Source File

QXmpp Version: 1.14.3

Loading...
Searching...
No Matches
QXmppStreamManagement_p.h
1// SPDX-FileCopyrightText: 2017 Niels Ole Salscheider <niels_ole@salscheider-online.de>
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
5#ifndef QXMPPSTREAMMANAGEMENT_P_H
6#define QXMPPSTREAMMANAGEMENT_P_H
7
8#include "QXmppConstants_p.h"
9#include "QXmppGlobal.h"
10#include "QXmppSendResult.h"
11#include "QXmppStanza.h"
12#include "QXmppTask.h"
13
14#include <QDomDocument>
15#include <QXmlStreamWriter>
16
17class QXmppPacket;
18
19namespace QXmpp::Private {
20class XmlWriter;
21class XmppSocket;
22} // namespace QXmpp::Private
23
24//
25// W A R N I N G
26// -------------
27//
28// This file is not part of the QXmpp API. It exists for the convenience
29// of the QXmppIncomingClient and QXmppOutgoingClient classes.
30//
31// This header file may change from version to version without notice,
32// or even be removed.
33//
34// We mean it.
35//
36
37namespace QXmpp::Private {
38
39struct SmFeature {
40 static constexpr std::tuple XmlTag = { u"sm", QXmpp::Private::ns_stream_management };
41};
42
43struct SmEnable {
44 static constexpr std::tuple XmlTag = { u"enable", QXmpp::Private::ns_stream_management };
45 static std::optional<SmEnable> fromDom(const QDomElement &);
46 void toXml(XmlWriter &w) const;
47
48 bool resume = false;
49 quint64 max = 0;
50};
51
52struct SmEnabled {
53 static constexpr std::tuple XmlTag = { u"enabled", QXmpp::Private::ns_stream_management };
54 static std::optional<SmEnabled> fromDom(const QDomElement &);
55 void toXml(XmlWriter &w) const;
56
57 bool resume = false;
58 QString id;
59 quint64 max = 0;
60 QString location;
61};
62
63struct SmResume {
64 static constexpr std::tuple XmlTag = { u"resume", QXmpp::Private::ns_stream_management };
65 static std::optional<SmResume> fromDom(const QDomElement &);
66 void toXml(XmlWriter &w) const;
67
68 quint32 h = 0;
69 QString previd;
70};
71
72struct SmResumed {
73 static constexpr std::tuple XmlTag = { u"resumed", QXmpp::Private::ns_stream_management };
74 static std::optional<SmResumed> fromDom(const QDomElement &);
75 void toXml(XmlWriter &w) const;
76
77 quint32 h = 0;
78 QString previd;
79};
80
81struct SmFailed {
82 static constexpr std::tuple XmlTag = { u"failed", QXmpp::Private::ns_stream_management };
83 static std::optional<SmFailed> fromDom(const QDomElement &);
84 void toXml(XmlWriter &w) const;
85
86 std::optional<QXmppStanza::Error::Condition> error;
87};
88
89struct SmAck {
90 static constexpr std::tuple XmlTag = { u"a", QXmpp::Private::ns_stream_management };
91 static std::optional<SmAck> fromDom(const QDomElement &);
92 void toXml(XmlWriter &w) const;
93
94 quint32 seqNo = 0;
95};
96
97struct SmRequest {
98 static constexpr std::tuple XmlTag = { u"r", QXmpp::Private::ns_stream_management };
99 static std::optional<SmRequest> fromDom(const QDomElement &);
100 void toXml(XmlWriter &w) const;
101};
102
103//
104// This manager handles sending and receiving of stream management acks.
105// Enabling of stream management and stream resumption is done in the C2sStreamManager.
106//
107class StreamAckManager
108{
109public:
110 explicit StreamAckManager(XmppSocket &socket);
111
112 bool enabled() const { return m_enabled; }
113 unsigned int lastIncomingSequenceNumber() const { return m_lastIncomingSequenceNumber; }
114
115 void handlePacketSent(QXmppPacket &packet, bool sentData);
116 bool handleStanza(const QDomElement &stanza);
117 void onSessionClosed();
118
119 void resetCache();
120 void enableStreamManagement(bool resetSequenceNumber);
121 void setAcknowledgedSequenceNumber(unsigned int sequenceNumber);
122
123 QXmppTask<QXmpp::SendResult> send(QXmppPacket &&);
124 bool sendPacketCompat(QXmppPacket &&);
125 std::tuple<bool, QXmppTask<QXmpp::SendResult>> internalSend(QXmppPacket &&);
126
127 void sendAcknowledgementRequest();
128
129private:
130 void handleAcknowledgement(SmAck ack);
131
132 void sendAcknowledgement();
133
134 QXmpp::Private::XmppSocket &socket;
135
136 bool m_enabled = false;
137 QMap<unsigned int, QXmppPacket> m_unacknowledgedStanzas;
138 unsigned int m_lastOutgoingSequenceNumber = 0;
139 unsigned int m_lastIncomingSequenceNumber = 0;
140};
141
142} // namespace QXmpp::Private
143
144#endif