diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2022-09-09 17:13:15 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2022-09-09 17:13:15 +0200 |
commit | 683db70689b8e09fda983b254325fdc4b409ea14 (patch) | |
tree | bc8f40525e662d5f72ec40b0c8127676150ff8cf | |
parent | 38495d423f23b145a4377f87a536db5d2721f814 (diff) |
Cleaned up includes, splitted common.h in the ksocket and examples part.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | Makefile | 9 | ||||
-rw-r--r-- | examples/common.hpp | 165 | ||||
-rw-r--r-- | examples/driver-protobuf-c-tcp.cpp | 11 | ||||
-rw-r--r-- | examples/driver-protobuf-c.cpp | 11 | ||||
-rw-r--r-- | examples/driver.cpp | 6 | ||||
-rw-r--r-- | examples/userspace_client_protobuf.cpp | 4 | ||||
-rw-r--r-- | ksocket/berkeley.c (renamed from berkeley.c) | 0 | ||||
-rw-r--r-- | ksocket/berkeley.h (renamed from berkeley.h) | 2 | ||||
-rw-r--r-- | ksocket/helper.hpp (renamed from common.hpp) | 165 | ||||
-rw-r--r-- | ksocket/ksocket.c (renamed from ksocket.c) | 0 | ||||
-rw-r--r-- | ksocket/ksocket.h (renamed from ksocket.h) | 0 | ||||
-rw-r--r-- | ksocket/wsk.h (renamed from wsk.h) | 0 |
12 files changed, 198 insertions, 175 deletions
@@ -5,7 +5,7 @@ endif include $(DPP_ROOT)/Makefile.inc DRIVER0_NAME = driver -DRIVER0_OBJECTS = examples/$(DRIVER0_NAME).o ksocket.o berkeley.o +DRIVER0_OBJECTS = examples/$(DRIVER0_NAME).o ksocket/ksocket.o ksocket/berkeley.o DRIVER0_TARGET = $(DRIVER0_NAME).sys DRIVER1_NAME = driver-protobuf-c @@ -13,7 +13,7 @@ DRIVER1_OBJECTS = examples/$(DRIVER1_NAME).o protobuf-c/protobuf-c.o examples/ex DRIVER1_TARGET = $(DRIVER1_NAME).sys DRIVER2_NAME = driver-protobuf-c-tcp -DRIVER2_OBJECTS = examples/$(DRIVER2_NAME).o ksocket.o berkeley.o protobuf-c/protobuf-c.o examples/example.pb-c.o +DRIVER2_OBJECTS = examples/$(DRIVER2_NAME).o ksocket/ksocket.o ksocket/berkeley.o protobuf-c/protobuf-c.o examples/example.pb-c.o DRIVER2_TARGET = $(DRIVER2_NAME).sys USERSPACE0_NAME = userspace_client @@ -54,6 +54,11 @@ $(USERSPACE1_TARGET): $(USERSPACE1_OBJECTS) $(call LINK_CPP_USER_TARGET,$(USERSPACE1_OBJECTS),$@) generate: + @echo '==========================================' + @echo '= You need protobuf-c to make this work! =' + @echo '==========================================' + @echo 'Get it here: https://github.com/protobuf-c/protobuf-c' + @echo protoc-c --c_out=. examples/example.proto install: $(DRIVER0_TARGET) $(DRIVER1_TARGET) $(DRIVER2_TARGET) $(USERSPACE0_TARGET) $(USERSPACE1_TARGET) diff --git a/examples/common.hpp b/examples/common.hpp new file mode 100644 index 0000000..5e2b637 --- /dev/null +++ b/examples/common.hpp @@ -0,0 +1,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 diff --git a/examples/driver-protobuf-c-tcp.cpp b/examples/driver-protobuf-c-tcp.cpp index da2142d..fcf4465 100644 --- a/examples/driver-protobuf-c-tcp.cpp +++ b/examples/driver-protobuf-c-tcp.cpp @@ -1,9 +1,10 @@ -#include "berkeley.h" -#include "ksocket.h" -#include "examples/example.pb-c.h" -#include "wsk.h" +#include <ksocket/berkeley.h> +#include <ksocket/helper.hpp> +#include <ksocket/ksocket.h> +#include <ksocket/wsk.h> -#include "common.hpp" +#include "examples/common.hpp" +#include "examples/example.pb-c.h" extern "C" { DRIVER_INITIALIZE DriverEntry; diff --git a/examples/driver-protobuf-c.cpp b/examples/driver-protobuf-c.cpp index 942782d..c3c8445 100644 --- a/examples/driver-protobuf-c.cpp +++ b/examples/driver-protobuf-c.cpp @@ -1,9 +1,10 @@ -#include "berkeley.h" -#include "ksocket.h" -#include "examples/example.pb-c.h" -#include "wsk.h" +#include <ksocket/berkeley.h> +#include <ksocket/helper.hpp> +#include <ksocket/ksocket.h> +#include <ksocket/wsk.h> -#include "common.hpp" +#include "examples/common.hpp" +#include "examples/example.pb-c.h" extern "C" { DRIVER_INITIALIZE DriverEntry; diff --git a/examples/driver.cpp b/examples/driver.cpp index ceae8ff..86e42e9 100644 --- a/examples/driver.cpp +++ b/examples/driver.cpp @@ -1,8 +1,8 @@ extern "C" { -#include "berkeley.h" -#include "ksocket.h" -#include "wsk.h" +#include <ksocket/berkeley.h> +#include <ksocket/ksocket.h> +#include <ksocket/wsk.h> DRIVER_INITIALIZE DriverEntry; DRIVER_UNLOAD DriverUnload; diff --git a/examples/userspace_client_protobuf.cpp b/examples/userspace_client_protobuf.cpp index b8ad711..a387579 100644 --- a/examples/userspace_client_protobuf.cpp +++ b/examples/userspace_client_protobuf.cpp @@ -3,7 +3,9 @@ #include <winsock2.h> #include <ws2tcpip.h> -#include "common.hpp" +#include <ksocket/helper.hpp> + +#include "examples/common.hpp" #include "examples/example.pb-c.h" int main(int argc, char **argv) { diff --git a/berkeley.c b/ksocket/berkeley.c index e72f92e..e72f92e 100644 --- a/berkeley.c +++ b/ksocket/berkeley.c diff --git a/berkeley.h b/ksocket/berkeley.h index 0d09c77..b879d3c 100644 --- a/berkeley.h +++ b/ksocket/berkeley.h @@ -1,7 +1,7 @@ #pragma once #include <ntddk.h> #include <stdint.h> -#include <wsk.h> +#include <ksocket/wsk.h> #define socket socket_connection diff --git a/common.hpp b/ksocket/helper.hpp index 2632fc0..153e549 100644 --- a/common.hpp +++ b/ksocket/helper.hpp @@ -1,4 +1,9 @@ -#include "examples/example.pb-c.h" +#ifndef HELPER_HPP +#define HELPER_HPP 1 + +#include <protobuf-c/protobuf-c.h> +#include <stdint.h> +#include <stdlib.h> #include <EASTL/algorithm.h> #include <EASTL/array.h> @@ -149,160 +154,4 @@ public: ~ProtobufCBinaryDataClass(void) { delete data; } }; -/* Could be also be done via protobuf, but I decided for speed/memory - * efficiency. */ -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 diff --git a/ksocket.c b/ksocket/ksocket.c index 3b6ba4d..3b6ba4d 100644 --- a/ksocket.c +++ b/ksocket/ksocket.c diff --git a/ksocket.h b/ksocket/ksocket.h index 0a6717e..0a6717e 100644 --- a/ksocket.h +++ b/ksocket/ksocket.h |