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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
#ifndef COMMON_H
#define COMMON_H 1
#include <ksocket/helper.hpp>
#include "examples/example.pb-c.h"
/* Could be also be done via protobuf */
enum PduTypes {
PDU_SOMETHING_WITH_UINTS = 1,
PDU_SOMETHING_MORE = 2,
PDU_EVEN_MORE = 3
};
class SomethingWithUINTsSerializer : virtual public BaseSerializer {
public:
SomethingWithUINTsSerializer(void) {}
~SomethingWithUINTsSerializer(void) {}
void SetId(uint32_t id) {
swu.has_id = TRUE;
swu.id = id;
}
void SetIpAddress(uint32_t ip_address) {
swu.has_ip_address = TRUE;
swu.ip_address = ip_address;
}
void SetPortNum(uint32_t port_num) {
swu.has_port_num = TRUE;
swu.port_num = port_num;
}
uint16_t GetPduType(void) override { return PDU_SOMETHING_WITH_UINTS; }
size_t GetSerializedSize(void) override {
return something_with_uints__get_packed_size(&swu);
}
size_t Serialize(uint8_t *buf) override {
return something_with_uints__pack(&swu, buf);
}
SomethingWithUINTs swu = SOMETHING_WITH_UINTS__INIT;
};
class SomethingWithUINTsDeserializer : virtual public BaseDeserializer {
public:
SomethingWithUINTsDeserializer(void) {}
~SomethingWithUINTsDeserializer(void) { DeserializeFree(); }
bool Deserialize(size_t pdu_len, uint8_t *buf) override {
swu = something_with_uints__unpack(NULL, pdu_len, buf);
return swu != NULL;
}
void DeserializeFree(void) override {
if (swu != NULL)
something_with_uints__free_unpacked(swu, NULL);
}
SomethingWithUINTs *swu = NULL;
};
class SomethingMoreSerializer : virtual public BaseSerializer,
public SomethingWithUINTsSerializer {
public:
SomethingMoreSerializer(void) { sm.uints = &swu; }
~SomethingMoreSerializer(void) {}
void SetErrorCode(SomethingMore__Errors error_code) {
sm.has_error_code = TRUE;
sm.error_code = error_code;
}
uint16_t GetPduType(void) override { return PDU_SOMETHING_MORE; }
size_t GetSerializedSize(void) override {
return something_more__get_packed_size(&sm);
}
size_t Serialize(uint8_t *buf) override {
return something_more__pack(&sm, buf);
}
SomethingMore sm = SOMETHING_MORE__INIT;
};
class SomethingMoreDeserializer : virtual public BaseDeserializer {
public:
SomethingMoreDeserializer(void) {}
~SomethingMoreDeserializer(void) { DeserializeFree(); }
bool Deserialize(size_t pdu_len, uint8_t *buf) override {
sm = something_more__unpack(NULL, pdu_len, buf);
return sm != NULL;
}
void DeserializeFree(void) override {
if (sm != NULL)
something_more__free_unpacked(sm, NULL);
}
SomethingMore *sm = NULL;
};
class EvenMoreSerializer : virtual public BaseSerializer {
public:
explicit EvenMoreSerializer(void) = default;
explicit EvenMoreSerializer(EvenMore__SomeEnum enum_value,
ProtobufCBinaryDataClass name,
ProtobufCBinaryDataClass value)
: uints() {
em.enum_value = enum_value;
em.name = name;
em.value = value;
}
~EvenMoreSerializer(void) { free(s); }
void AddUints(SomethingWithUINTsSerializer *uints) {
this->uints.push_back(uints);
}
void SetS(eastl::string s) {
size_t l = s.size();
this->s = (char *)malloc(l + 1);
memcpy(this->s, s.c_str(), l);
this->s[l] = '\0';
}
uint16_t GetPduType(void) override { return PDU_EVEN_MORE; }
size_t GetSerializedSize(void) override {
em.s = s;
em.n_uints = this->uints.size();
if (em.n_uints > 0) {
SomethingWithUINTs *out[em.n_uints];
ConvertUintsVectorToCArray(out);
return even_more__get_packed_size(&em);
}
return even_more__get_packed_size(&em);
}
size_t Serialize(uint8_t *buf) override {
em.s = s;
em.n_uints = this->uints.size();
if (em.n_uints > 0) {
SomethingWithUINTs *out[em.n_uints];
ConvertUintsVectorToCArray(out);
return even_more__pack(&em, buf);
}
return even_more__pack(&em, buf);
}
EvenMore em = EVEN_MORE__INIT;
eastl::vector<SomethingWithUINTsSerializer *> uints;
char *s = NULL;
void ConvertUintsVectorToCArray(SomethingWithUINTs **out) {
for (size_t i = 0; i < em.n_uints; ++i) {
out[i] = &uints[i]->swu;
}
em.uints = out;
}
};
class EvenMoreDeserializer : virtual public BaseDeserializer {
public:
EvenMoreDeserializer(void) {}
~EvenMoreDeserializer(void) { DeserializeFree(); }
bool Deserialize(size_t pdu_len, uint8_t *buf) override {
em = even_more__unpack(NULL, pdu_len, buf);
return em != NULL;
}
void DeserializeFree(void) override {
if (em != NULL)
even_more__free_unpacked(em, NULL);
}
EvenMore *em = NULL;
};
#endif
|