| 1 | //! @file data.hpp |
| 2 | //! @author ryftchen |
| 3 | //! @brief The declarations (data) in the application module. |
| 4 | //! @version 0.1.0 |
| 5 | //! @copyright Copyright (c) 2022-2026 ryftchen. All rights reserved. |
| 6 | |
| 7 | #pragma once |
| 8 | |
| 9 | #ifndef _PRECOMPILED_HEADER |
| 10 | #include <netinet/in.h> |
| 11 | #include <cstdint> |
| 12 | #include <system_error> |
| 13 | #include <vector> |
| 14 | #else |
| 15 | #include "application/pch/precompiled_header.hpp" |
| 16 | #endif |
| 17 | |
| 18 | //! @brief The application module. |
| 19 | namespace application // NOLINT(modernize-concat-nested-namespaces) |
| 20 | { |
| 21 | //! @brief Data-processing-related functions in the application module. |
| 22 | namespace data |
| 23 | { |
| 24 | //! @brief Data packet. |
| 25 | class Packet |
| 26 | { |
| 27 | public: |
| 28 | //! @brief Construct a new Packet object. |
| 29 | //! @param pktBuf - packet buffer |
| 30 | //! @param pktLen - buffer length |
| 31 | Packet(char* const pktBuf, const std::size_t pktLen); |
| 32 | |
| 33 | //! @brief Write data to the packet buffer. |
| 34 | //! @tparam Data - type of data to be written |
| 35 | //! @param data - original data |
| 36 | //! @return whether it is continuously writable |
| 37 | template <typename Data> |
| 38 | requires std::is_trivially_copyable_v<Data> |
| 39 | bool write(const Data data); |
| 40 | //! @brief Write data to the packet buffer. |
| 41 | //! @param dst - data after conversion |
| 42 | //! @param offset - data offset |
| 43 | //! @return whether it is continuously writable |
| 44 | bool write(const void* const dst, const std::size_t offset); |
| 45 | //! @brief Read data to the packet buffer. |
| 46 | //! @tparam Data - type of data to be read |
| 47 | //! @param data - original data |
| 48 | //! @return whether it is continuously readable |
| 49 | template <typename Data> |
| 50 | requires std::is_trivially_copyable_v<Data> |
| 51 | bool read(Data* const data); |
| 52 | //! @brief Read data to the packet buffer. |
| 53 | //! @param dst - data after conversion |
| 54 | //! @param offset - data offset |
| 55 | //! @return whether it is continuously readable |
| 56 | bool read(void* const dst, const std::size_t offset); |
| 57 | |
| 58 | private: |
| 59 | //! @brief Pointer to the beginning of the buffer. |
| 60 | char* const head{nullptr}; |
| 61 | //! @brief Pointer to the ending of the buffer. |
| 62 | const char* const tail{nullptr}; |
| 63 | //! @brief Pointer to the current writing location. |
| 64 | char* writer{nullptr}; |
| 65 | //! @brief Pointer to the current reading location. |
| 66 | const char* reader{nullptr}; |
| 67 | }; |
| 68 | |
| 69 | template <typename Data> |
| 70 | requires std::is_trivially_copyable_v<Data> |
| 71 | bool Packet::write(const Data data) |
| 72 | { |
| 73 | Data temp{}; |
| 74 | if constexpr (sizeof(Data) == sizeof(int)) |
| 75 | { |
| 76 | temp = ::htonl(hostlong: data); |
| 77 | } |
| 78 | else if constexpr (sizeof(Data) == sizeof(short)) |
| 79 | { |
| 80 | temp = ::htons(hostshort: data); |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | temp = data; |
| 85 | } |
| 86 | return write(&temp, sizeof(Data)); |
| 87 | } |
| 88 | |
| 89 | template <typename Data> |
| 90 | requires std::is_trivially_copyable_v<Data> |
| 91 | bool Packet::read(Data* const data) |
| 92 | { |
| 93 | if (!data) |
| 94 | { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | const bool isEnd = read(data, sizeof(Data)); |
| 99 | if constexpr (sizeof(Data) == sizeof(int)) |
| 100 | { |
| 101 | *data = ::ntohl(netlong: *data); |
| 102 | } |
| 103 | else if constexpr (sizeof(Data) == sizeof(short)) |
| 104 | { |
| 105 | *data = ::ntohs(netshort: *data); |
| 106 | } |
| 107 | return isEnd; |
| 108 | } |
| 109 | |
| 110 | //! @brief Type-length-value scheme. |
| 111 | namespace tlv |
| 112 | { |
| 113 | //! @brief Invalid shared memory id. |
| 114 | inline constexpr int invalidShmId = -1; |
| 115 | //! @brief Default information size. |
| 116 | inline constexpr std::uint16_t defInfoSize = 256; |
| 117 | |
| 118 | //! @brief Value in TLV. |
| 119 | struct TLVValue |
| 120 | { |
| 121 | //! @brief Flag for stopping the connection. |
| 122 | bool stopTag{false}; |
| 123 | //! @brief Information about the runtime library. |
| 124 | char libInfo[defInfoSize]{'\0'}; |
| 125 | //! @brief Shared memory id of the bash outputs. |
| 126 | int bashShmId{invalidShmId}; |
| 127 | //! @brief Shared memory id of the log contents. |
| 128 | int logShmId{invalidShmId}; |
| 129 | //! @brief Shared memory id of the status reports. |
| 130 | int statusShmId{invalidShmId}; |
| 131 | //! @brief Information about the current configuration. |
| 132 | char configInfo[defInfoSize * 2]{'\0'}; |
| 133 | }; |
| 134 | |
| 135 | extern std::error_code encodeTLV(char* const buf, std::size_t& len, const TLVValue& val); |
| 136 | extern std::error_code decodeTLV(char* const buf, const std::size_t len, TLVValue& val); |
| 137 | } // namespace tlv |
| 138 | |
| 139 | extern void encryptMessage(char* const buffer, const std::size_t length); |
| 140 | extern void decryptMessage(char* const buffer, const std::size_t length); |
| 141 | |
| 142 | extern void compressData(std::vector<char>& cache); |
| 143 | extern void decompressData(std::vector<char>& cache); |
| 144 | |
| 145 | extern std::string toHexString(const char* const buffer, const std::size_t length); |
| 146 | } // namespace data |
| 147 | } // namespace application |
| 148 | |