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