forked from halfgaar/FlashMQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublishcopyfactory.h
59 lines (50 loc) · 2.23 KB
/
publishcopyfactory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021-2023 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of The Open Software License 3.0 (OSL-3.0).
See LICENSE for license details.
*/
#ifndef PUBLISHCOPYFACTORY_H
#define PUBLISHCOPYFACTORY_H
#include <vector>
#include <unordered_map>
#include "forward_declarations.h"
#include "types.h"
/**
* @brief The PublishCopyFactory class is for managing copies of an incoming publish, including sometimes not making copies at all.
*
* The idea is that certain incoming packets can just be written to the receiving client as-is, without constructing a new one. We do have to change the bytes
* where the QoS is stored, so we keep track of the original QoS.
*
* Ownership info: object of this type are never copied or transferred, so the internal pointers come from (near) the same scope these objects
* are created from.
*/
class PublishCopyFactory
{
MqttPacket *packet = nullptr;
Publish *publish = nullptr;
std::unique_ptr<MqttPacket> oneShotPacket;
const uint8_t orgQos;
const bool orgRetain = false;
std::unordered_map<uint8_t, std::unique_ptr<MqttPacket>> constructedPacketCache;
size_t sharedSubscriptionHashKey;
public:
PublishCopyFactory(MqttPacket *packet);
PublishCopyFactory(Publish *publish);
PublishCopyFactory(const PublishCopyFactory &other) = delete;
PublishCopyFactory(PublishCopyFactory &&other) = delete;
MqttPacket *getOptimumPacket(const uint8_t max_qos, const ProtocolVersion protocolVersion, uint16_t topic_alias, bool skip_topic);
uint8_t getEffectiveQos(uint8_t max_qos) const;
bool getEffectiveRetain(bool retainAsPublished) const;
const std::string &getTopic() const;
const std::vector<std::string> &getSubtopics();
std::string_view getPayload() const;
bool getRetain() const;
Publish getNewPublish(uint8_t new_max_qos, bool retainAsPublished) const;
std::shared_ptr<Client> getSender();
const std::vector<std::pair<std::string, std::string>> *getUserProperties() const;
void setSharedSubscriptionHashKey(size_t hash);
size_t getSharedSubscriptionHashKey() const { return sharedSubscriptionHashKey; }
};
#endif // PUBLISHCOPYFACTORY_H