From abeb88b481b483e34993b349e335af146145487c Mon Sep 17 00:00:00 2001 From: Saturneric Date: Thu, 10 Dec 2020 19:04:05 +0800 Subject: [PATCH] Message Added. --- include/communicate/commu_options.h | 8 + include/communicate/message.h | 144 +++++++++++++ include/communicate/tcp_server.h | 26 +++ include/debug_tools/print_tools.h | 11 +- include/project.h | 2 +- include/utils/sha256_generator.h | 13 +- src/communicate/CMakeLists.txt | 2 +- src/communicate/message.cpp | 70 +++++++ src/communicate/message_factory.cpp | 254 +++++++++++++++++++++++ src/communicate/message_parser.cpp | 243 ++++++++++++++++++++++ src/communicate/option.cpp | 40 ++++ src/communicate/tcp_server.cpp | 16 +- src/debug_tools/print_tools.cpp | 105 ++++++---- src/utils/sha256_generator.cpp | 26 +-- test/CMakeLists.txt | 3 + test/test_commu/main.cpp | 34 ++- test/test_commu_modules/CMakeLists.txt | 8 + test/test_commu_modules/main.cpp | 10 + test/test_commu_modules/test_message.cpp | 104 ++++++++++ test/test_util/test_sha.cpp | 5 +- 20 files changed, 1033 insertions(+), 91 deletions(-) create mode 100644 include/communicate/commu_options.h create mode 100644 include/communicate/message.h create mode 100644 src/communicate/message.cpp create mode 100644 src/communicate/message_factory.cpp create mode 100644 src/communicate/message_parser.cpp create mode 100644 src/communicate/option.cpp create mode 100644 test/test_commu_modules/CMakeLists.txt create mode 100644 test/test_commu_modules/main.cpp create mode 100644 test/test_commu_modules/test_message.cpp diff --git a/include/communicate/commu_options.h b/include/communicate/commu_options.h new file mode 100644 index 0000000..9e8ce1d --- /dev/null +++ b/include/communicate/commu_options.h @@ -0,0 +1,8 @@ +#pragma once + +#define MSG_VERSION 0x0 + +namespace Net{ + + +} diff --git a/include/communicate/message.h b/include/communicate/message.h new file mode 100644 index 0000000..76e3eb6 --- /dev/null +++ b/include/communicate/message.h @@ -0,0 +1,144 @@ +#pragma once +#include +#include +#include +#include +#include + +#include "communicate/commu_options.h" +#include "utils/sha256_generator.h" + +using std::string; +using std::vector; +using std::shared_ptr; +using std::queue; + +namespace Net { + +class Option { +public: + Option(const string &key, const string &value); + + Option(const Option &o); + + Option(Option &&o) noexcept; + + Option& operator=(const Option &o); + + Option& operator=(Option &&o) noexcept; + + const string &getKey() const; + + const string &getValue() const; + + void updateValue(const string &value); + +private: + shared_ptr key; + shared_ptr value; +}; + +class MessageFactory; + +class Message { +public: + + Message() noexcept; + + void setHead(int32_t tid, int16_t type); + + void addOption(const string &key, const string &value); + + const string &getValue(const string &key) const; + + void pushData(void *const buf, size_t size); + + int32_t getTID() const; + + const vector &getData() const; + + void clear(); + +private: + friend MessageFactory; + + int32_t tid = -1; + int16_t version = MSG_VERSION; + int16_t type = 0x0; + vector