Botcraft 1.21.4
Loading...
Searching...
No Matches
DNSSrvData.hpp
Go to the documentation of this file.
1#pragma once
2
4
5using namespace ProtocolCraft;
6
7namespace Botcraft
8{
9 class DNSSrvData : public NetworkType
10 {
11 public:
12 const unsigned short GetPriority() const
13 {
14 return priority;
15 }
16
17 const unsigned short GetWeight() const
18 {
19 return weight;
20 }
21
22 const unsigned short GetPort() const
23 {
24 return port;
25 }
26
27 const std::vector<std::string>& GetNameLabels() const
28 {
29 return name_labels;
30 }
31
32
33 void SetPriority(const unsigned short priority_)
34 {
35 priority = priority_;
36 }
37
38 void SetWeight(const unsigned short weight_)
39 {
40 weight = weight_;
41 }
42
43 void SetPort(const unsigned short port_)
44 {
45 port = port_;
46 }
47
48 void SetNameLabels(const std::vector<std::string>& name_labels_)
49 {
50 name_labels = name_labels_;
51 }
52
53 protected:
54 virtual void ReadImpl(ReadIterator& iter, size_t& length) override
55 {
56 priority = ReadData<unsigned short>(iter, length);
57 weight = ReadData<unsigned short>(iter, length);
58 port = ReadData<unsigned short>(iter, length);
59
60 // There is no compression for srv answer data
61 std::string label;
62 unsigned char label_len = ReadData<unsigned char>(iter, length);
63 while (label_len != 0)
64 {
65 label = ReadRawString(iter, length, label_len);
66 label_len = ReadData<unsigned char>(iter, length);
67 name_labels.push_back(label);
68 }
69 }
70
71 virtual void WriteImpl(WriteContainer& container) const override
72 {
73
74 WriteData<unsigned short>(priority, container);
75 WriteData<unsigned short>(weight, container);
76 WriteData<unsigned short>(port, container);
77
78 for (int i = 0; i < name_labels.size(); ++i)
79 {
80 WriteData<unsigned char>(static_cast<unsigned char>(name_labels[i].size()), container);
81 WriteRawString(name_labels[i], container);
82 }
83 WriteData<unsigned char>(0, container);
84 }
85
86 virtual Json::Value SerializeImpl() const override
87 {
88 Json::Value output;
89
90 output["priority"] = priority;
91 output["weight"] = weight;
92 output["port"] = port;
93
94 std::string name = "";
95 for (int i = 0; i < name_labels.size(); ++i)
96 {
97 name += name_labels[i] + ".";
98 }
99 output["target"] = name;
100
101 return output;
102 }
103
104 private:
105 unsigned short priority;
106 unsigned short weight;
107 unsigned short port;
108 std::vector<std::string> name_labels;
109 };
110}
unsigned short port
void SetNameLabels(const std::vector< std::string > &name_labels_)
unsigned short priority
void SetPriority(const unsigned short priority_)
virtual void WriteImpl(WriteContainer &container) const override
virtual Json::Value SerializeImpl() const override
const unsigned short GetPort() const
void SetPort(const unsigned short port_)
std::vector< std::string > name_labels
void SetWeight(const unsigned short weight_)
unsigned short weight
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
const unsigned short GetWeight() const
const unsigned short GetPriority() const
const std::vector< std::string > & GetNameLabels() const
Main class, basically a JsonVariant with extra utility functions it doesn't inherit JsonVariant direc...
Definition Json.hpp:45
std::string ReadRawString(ReadIterator &iter, size_t &length, const size_t size)
void WriteRawString(const std::string &s, WriteContainer &container)
std::vector< unsigned char > WriteContainer
std::vector< unsigned char >::const_iterator ReadIterator