diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2435ce600c9c75ba4f063cb1aa8e0ad00dd7490 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +src/.vscode/ +src/*/.vscode/ +src/*/Makefile +src/*/*.o +src/host/hdc +src/daemon/hdcd \ No newline at end of file diff --git a/README_zh.md b/README_zh.md old mode 100644 new mode 100755 diff --git a/ohos.build b/ohos.build old mode 100644 new mode 100755 diff --git a/prebuilt/README_zh.md b/prebuilt/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..1d076c0a839fbb22fec6201a1c45d4020aa3501a --- /dev/null +++ b/prebuilt/README_zh.md @@ -0,0 +1,11 @@ +# 几点说明: + +[1.通过git clone方式下载](#section662115419449) +建议通过gitclone方式下载该仓 命令为: +**git clone git@gitee.com:openharmony/developtools_hdc_standard.git** + +[2.通过网页形式下载](#section15908143623714) +通过网页形式下载prebuilt,请 使用类似如下URL打开网页 https://gitee.com/openharmony/developtools_hdc_standard/blob/master/prebuilt/windows/hdc-std.exe ,点击中间下载方式进行下载,windows版本文件大小在**5M左右**,linux版本在**2M左右**,不要使用右击另存为方式进行保存下载,下载后检查文件大小。 + +[3.支持环境](#section161941989596) +支持运行环境 linux版本建议ubuntu20 CentOS8 64位,其他版本相近也应该可以,libc++.so引用错误请使用ldd\readelf等命令检查库引用 windows版本建议windows10 64位,windows8也应该可以,Windows7等EOF版本尚未测试,如果低版本windows winusb库缺失,请使用zadia更新库。 \ No newline at end of file diff --git a/prebuilt/linux/hdc-std b/prebuilt/linux/hdc_std similarity index 100% rename from prebuilt/linux/hdc-std rename to prebuilt/linux/hdc_std diff --git a/prebuilt/windows/hdc-std.exe b/prebuilt/windows/hdc-std.exe deleted file mode 100644 index c61be9369edb13c80d653bcbd7bc2fe15a151eee..0000000000000000000000000000000000000000 --- a/prebuilt/windows/hdc-std.exe +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:b668bf2366c668b6d213eeb57bad3105cb2679f96441ef7b71d4334846698f40 -size 4664727 diff --git a/prebuilt/windows/hdc_std.exe b/prebuilt/windows/hdc_std.exe new file mode 100644 index 0000000000000000000000000000000000000000..067e01d6a5c3bcac3fa2f0ef5403c079f9102881 Binary files /dev/null and b/prebuilt/windows/hdc_std.exe differ diff --git a/src/common/async_cmd.cpp b/src/common/async_cmd.cpp old mode 100644 new mode 100755 diff --git a/src/common/async_cmd.h b/src/common/async_cmd.h old mode 100644 new mode 100755 diff --git a/src/common/auth.cpp b/src/common/auth.cpp old mode 100644 new mode 100755 diff --git a/src/common/auth.h b/src/common/auth.h old mode 100644 new mode 100755 diff --git a/src/common/base.cpp b/src/common/base.cpp old mode 100644 new mode 100755 index 457f62185a8bcb431ba698969d0a600e38b28ffe..7298624cc473011b79ce46c973e0f7c70be1afaa --- a/src/common/base.cpp +++ b/src/common/base.cpp @@ -164,13 +164,14 @@ namespace Base { va_end(ap); } + // if can linkwith -lstdc++fs, use std::filesystem::path(path).filename(); string GetFileNameAny(string &path) { - // if can linkwith -lstdc++fs, use std::filesystem::path(path).filename(); string tmpString = path; - size_t tmpNum = 0; - if ((tmpNum = tmpString.rfind('/')) == std::string::npos) { - if ((tmpNum = tmpString.rfind('\\')) == std::string::npos) { + size_t tmpNum = tmpString.rfind('/'); + if (tmpNum == std::string::npos) { + tmpNum = tmpString.rfind('\\'); + if (tmpNum == std::string::npos) { return tmpString; } } @@ -1098,7 +1099,7 @@ namespace Base { uv_os_sock_t dupFd = -1; #ifdef _WIN32 WSAPROTOCOL_INFO info; - memset(&info, 0, sizeof(info)); + ZeroStruct(info); if (WSADuplicateSocketA(tcp->socket, GetCurrentProcessId(), &info) < 0) { return dupFd; } diff --git a/src/common/base.h b/src/common/base.h old mode 100644 new mode 100755 diff --git a/src/common/channel.cpp b/src/common/channel.cpp old mode 100644 new mode 100755 diff --git a/src/common/channel.h b/src/common/channel.h old mode 100644 new mode 100755 diff --git a/src/common/common.h b/src/common/common.h old mode 100644 new mode 100755 diff --git a/src/common/debug.cpp b/src/common/debug.cpp old mode 100644 new mode 100755 diff --git a/src/common/debug.h b/src/common/debug.h old mode 100644 new mode 100755 diff --git a/src/common/define.h b/src/common/define.h old mode 100644 new mode 100755 diff --git a/src/common/define_plus.h b/src/common/define_plus.h old mode 100644 new mode 100755 diff --git a/src/common/file.cpp b/src/common/file.cpp old mode 100644 new mode 100755 diff --git a/src/common/file.h b/src/common/file.h old mode 100644 new mode 100755 diff --git a/src/common/file_descriptor.cpp b/src/common/file_descriptor.cpp old mode 100644 new mode 100755 diff --git a/src/common/file_descriptor.h b/src/common/file_descriptor.h old mode 100644 new mode 100755 diff --git a/src/common/forward.cpp b/src/common/forward.cpp old mode 100644 new mode 100755 diff --git a/src/common/forward.h b/src/common/forward.h old mode 100644 new mode 100755 diff --git a/src/common/serial_struct.h b/src/common/serial_struct.h old mode 100644 new mode 100755 index 2a9e8588ed0e45e699bd8c0ae97890f1cef3cc8e..db2937fa31d4e875896bbdb0a6b2152353198ea3 --- a/src/common/serial_struct.h +++ b/src/common/serial_struct.h @@ -14,1287 +14,11 @@ */ #ifndef HDC_SERIAL_STRUCT_H #define HDC_SERIAL_STRUCT_H - #include "common.h" +#include "serial_struct_define.h" #include "transfer.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -// clang-format off namespace Hdc { -namespace SerialStruct { - namespace SerialDetail { - template struct MemPtr { - }; - template struct MemPtr { - using type = T; - using MemberType = U; - }; - template struct MessageImpl { - public: - MessageImpl(Fields &&... fields) - : _fields(std::move(fields)...) - { - } - - template void Visit(Handler &&handler) const - { - VisitImpl(std::forward(handler), std::make_index_sequence()); - } - - private: - std::tuple _fields; - - template void VisitImpl(Handler &&handler, std::index_sequence) const - { - (handler(std::get(_fields)), ...); - } - }; - - template struct FieldImpl { - using type = typename SerialDetail::MemPtr::type; - using MemberType = typename SerialDetail::MemPtr::MemberType; - constexpr static const uint32_t tag = Tag; - constexpr static const uint32_t flags = Flags; - const std::string field_name; - - static decltype(auto) get(const type &value) - { - return value.*MemPtr; - } - - static decltype(auto) get(type &value) - { - return value.*MemPtr; - } - }; - - template struct OneofFieldImpl { - using type = typename SerialDetail::MemPtr::type; - using MemberType = typename SerialDetail::MemPtr::MemberType; - constexpr static const uint32_t tag = Tag; - constexpr static const size_t index = Index; - constexpr static const uint32_t flags = Flags; - const std::string field_name; - - static decltype(auto) get(const type &value) - { - return value.*MemPtr; - } - - static decltype(auto) get(type &value) - { - return value.*MemPtr; - } - }; - - template - struct MapFieldImpl { - using type = typename SerialDetail::MemPtr::type; - using MemberType = typename SerialDetail::MemPtr::MemberType; - constexpr static const uint32_t tag = Tag; - constexpr static const uint32_t KEY_FLAGS = KeyFlags; - constexpr static const uint32_t VALUE_FLAGS = ValueFlags; - - const std::string field_name; - - static decltype(auto) get(const type &value) - { - return value.*MemPtr; - } - - static decltype(auto) get(type &value) - { - return value.*MemPtr; - } - }; - } - - enum class WireType : uint32_t { - VARINT = 0, - FIXED64 = 1, - LENGTH_DELIMETED = 2, - START_GROUP = 3, - END_GROUP = 4, - FIXED32 = 5, - }; - enum flags { no = 0, s = 1, f = 2 }; - template struct FlagsType { - }; - - template struct Descriptor { - static_assert(sizeof(T) == 0, "You need to implement descriptor for your own types"); - static void type() - { - } - }; - - template constexpr auto Message(Fields &&... fields) - { - return SerialDetail::MessageImpl(std::forward(fields)...); - } - - template constexpr auto Field(const std::string &fieldName) - { - return SerialDetail::FieldImpl { fieldName }; - } - - template - constexpr auto OneofField(const std::string &fieldName) - { - return SerialDetail::OneofFieldImpl { fieldName }; - } - - template - constexpr auto MapField(const std::string &fieldName) - { - return SerialDetail::MapFieldImpl { fieldName }; - } - - template const auto &MessageType() - { - static const auto message = Descriptor::type(); - return message; - } - - template struct Serializer; - - struct Writer { - virtual void Write(const void *bytes, size_t size) = 0; - }; - - struct reader { - virtual size_t Read(void *bytes, size_t size) = 0; - }; - - namespace SerialDetail { - template - struct HasSerializePacked : public std::false_type { - }; - - template - struct HasSerializePacked().SerializePacked( - std::declval(), std::declval(), std::declval()))>> : public std::true_type { - }; - - template - constexpr bool HAS_SERIALIZE_PACKED_V = HasSerializePacked::value; - - template - struct HasParsePacked : public std::false_type { - }; - - template - struct HasParsePacked().ParsePacked( - std::declval(), std::declval(), std::declval()))>> : public std::true_type { - }; - - template - constexpr bool HAS_PARSE_PACKED_V = HasParsePacked::value; - - static uint32_t MakeTagWireType(uint32_t tag, WireType wireType) - { - return (tag << 3) | static_cast(wireType); - } - - static inline void ReadTagWireType(uint32_t tagKey, uint32_t &tag, WireType &wireType) - { - wireType = static_cast(tagKey & 0b0111); - tag = tagKey >> 3; - } - - static uint32_t MakeZigzagValue(int32_t value) - { - return (static_cast(value) << 1) ^ static_cast(value >> 31); - } - - static uint64_t MakeZigzagValue(int64_t value) - { - return (static_cast(value) << 1) ^ static_cast(value >> 63); - } - - static int32_t ReadZigzagValue(uint32_t value) - { - return static_cast((value >> 1) ^ (~(value & 1) + 1)); - } - - static int64_t ReadZigzagValue(uint64_t value) - { - return static_cast((value >> 1) ^ (~(value & 1) + 1)); - } - - template To BitCast(From from) - { - static_assert(sizeof(To) == sizeof(From), ""); - static_assert(std::is_trivially_copyable_v, ""); - static_assert(std::is_trivially_copyable_v, ""); - To to; - memcpy_s(&to, sizeof(To), &from, sizeof(from)); - return to; - } - - struct WriterSizeCollector : public Writer { - void Write(const void *, size_t size) override - { - byte_size += size; - } - size_t byte_size = 0; - }; - - struct LimitedReader : public reader { - LimitedReader(reader &parent, size_t sizeLimit) - : _parent(parent), _size_limit(sizeLimit) - { - } - - size_t Read(void *bytes, size_t size) - { - auto sizeToRead = std::min(size, _size_limit); - auto readSize = _parent.Read(bytes, sizeToRead); - _size_limit -= readSize; - return readSize; - } - - size_t AvailableBytes() const - { - return _size_limit; - } - - private: - reader &_parent; - size_t _size_limit; - }; - - static bool ReadByte(uint8_t &value, reader &in) - { - return in.Read(&value, 1) == 1; - } - - static void WriteVarint(uint32_t value, Writer &out) - { - uint8_t b[5] {}; - for (size_t i = 0; i < 5; ++i) { - b[i] = value & 0b0111'1111; - value >>= 7; - if (value) { - b[i] |= 0b1000'0000; - } else { - out.Write(b, i + 1); - break; - } - } - } - - static void WriteVarint(uint64_t value, Writer &out) - { - uint8_t b[10] {}; - for (size_t i = 0; i < 10; ++i) { - b[i] = value & 0b0111'1111; - value >>= 7; - if (value) { - b[i] |= 0b1000'0000; - } else { - out.Write(b, i + 1); - break; - } - } - } - - static bool ReadVarint(uint32_t &value, reader &in) - { - value = 0; - for (size_t c = 0; c < 5; ++c) { - uint8_t x; - if (!ReadByte(x, in)) { - return false; - } - value |= static_cast(x & 0b0111'1111) << 7 * c; - if (!(x & 0b1000'0000)) { - return true; - } - } - - return false; - } - - static bool ReadVarint(uint64_t &value, reader &in) - { - value &= 0; - for (size_t c = 0; c < 10; ++c) { - uint8_t x; - if (!ReadByte(x, in)) { - return false; - } - value |= static_cast(x & 0b0111'1111) << 7 * c; - if (!(x & 0b1000'0000)) { - return true; - } - } - return false; - } - - static void WriteFixed(uint32_t value, Writer &out) - { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - out.Write(&value, sizeof(value)); -#else - static_assert(false, "Not a little-endian"); -#endif - } - - static void WriteFixed(uint64_t value, Writer &out) - { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - out.Write(&value, sizeof(value)); -#else - static_assert(false, "Not a little-endian"); -#endif - } - - static void WriteFixed(double value, Writer &out) - { - WriteFixed(BitCast(value), out); - } - - static void WriteFixed(float value, Writer &out) - { - WriteFixed(BitCast(value), out); - } - - static void WriteVarint(int32_t value, Writer &out) - { - WriteVarint(BitCast(value), out); - } - - static void WriteVarint(int64_t value, Writer &out) - { - WriteVarint(BitCast(value), out); - } - - static void WriteSignedVarint(int32_t value, Writer &out) - { - WriteVarint(MakeZigzagValue(value), out); - } - - static void WriteSignedVarint(int64_t value, Writer &out) - { - WriteVarint(MakeZigzagValue(value), out); - } - - static void WriteSignedFixed(int32_t value, Writer &out) - { - WriteFixed(static_cast(value), out); - } - - static void WriteSignedFixed(int64_t value, Writer &out) - { - WriteFixed(static_cast(value), out); - } - - static void WriteTagWriteType(uint32_t tag, WireType wireType, Writer &out) - { - WriteVarint(MakeTagWireType(tag, wireType), out); - } - - static bool ReadFixed(uint32_t &value, reader &in) - { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - return in.Read(&value, sizeof(value)) == sizeof(value); -#else - static_assert(false, "Not a little-endian"); -#endif - } - - static bool ReadFixed(uint64_t &value, reader &in) - { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - return in.Read(&value, sizeof(value)) == sizeof(value); -#else - static_assert(false, "Not a little-endian"); -#endif - } - - static bool ReadFixed(double &value, reader &in) - { - uint64_t intermediateValue; - if (ReadFixed(intermediateValue, in)) { - value = BitCast(intermediateValue); - return true; - } - return false; - } - - static bool ReadFixed(float &value, reader &in) - { - uint32_t intermediateValue; - if (ReadFixed(intermediateValue, in)) { - value = BitCast(intermediateValue); - return true; - } - return false; - } - - static bool ReadVarint(int32_t &value, reader &in) - { - uint32_t intermediateValue; - if (ReadVarint(intermediateValue, in)) { - value = BitCast(intermediateValue); - return true; - } - return false; - } - - static bool ReadVarint(int64_t &value, reader &in) - { - uint64_t intermediateValue; - if (ReadVarint(intermediateValue, in)) { - value = BitCast(intermediateValue); - return true; - } - return false; - } - - static bool ReadSignedVarint(int32_t &value, reader &in) - { - uint32_t intermediateValue; - if (ReadVarint(intermediateValue, in)) { - value = ReadZigzagValue(intermediateValue); - return true; - } - return false; - } - - static bool ReadSignedVarint(int64_t &value, reader &in) - { - uint64_t intermediateValue; - if (ReadVarint(intermediateValue, in)) { - value = ReadZigzagValue(intermediateValue); - return true; - } - return false; - } - - static bool ReadSignedFixed(int32_t &value, reader &in) - { - uint32_t intermediateValue; - if (ReadFixed(intermediateValue, in)) { - value = static_cast(intermediateValue); - return true; - } - return false; - } - - static bool ReadSignedFixed(int64_t &value, reader &in) - { - uint64_t intermediateValue; - if (ReadFixed(intermediateValue, in)) { - value = static_cast(intermediateValue); - return true; - } - return false; - } - - template - void WriteField(const T &value, - const SerialDetail::OneofFieldImpl &, Writer &out) - { - using OneOf = SerialDetail::OneofFieldImpl; - Serializer::template SerializeOneof( - OneOf::tag, OneOf::get(value), FlagsType(), out); - } - - template - void WriteField(const T &value, - const SerialDetail::MapFieldImpl &, Writer &out) - { - using Map = SerialDetail::MapFieldImpl; - Serializer::SerializeMap( - Map::tag, Map::get(value), FlagsType(), FlagsType(), out); - } - - template - void WriteField(const T &value, const SerialDetail::FieldImpl &, Writer &out) - { - using Field = SerialDetail::FieldImpl; - Serializer::Serialize( - Field::tag, Field::get(value), FlagsType(), out); - } - - template - void WriteMessage(const T &value, const SerialDetail::MessageImpl &message, Writer &out) - { - message.Visit([&](const auto &field) { WriteField(value, field, out); }); - } - - template - void WriteRepeated(uint32_t tag, It begin, It end, Writer &out) - { - if (begin == end) { - return; - } - if constexpr (SerialDetail::HAS_SERIALIZE_PACKED_V, ValueType, FlagsType, - Writer>) { - WriteVarint(MakeTagWireType(tag, WireType::LENGTH_DELIMETED), out); - WriterSizeCollector sizeCollector; - for (auto it = begin; it != end; ++it) { - Serializer::SerializePacked(*it, FlagsType {}, sizeCollector); - } - WriteVarint(sizeCollector.byte_size, out); - for (auto it = begin; it != end; ++it) { - Serializer::SerializePacked(*it, FlagsType {}, out); - } - } else { - for (auto it = begin; it != end; ++it) { - Serializer::Serialize(tag, *it, FlagsType(), out); - } - } - } - - template - void WriteMapKeyValue(const std::pair &value, Writer &out) - { - Serializer::Serialize(1, value.first, FlagsType {}, out, true); - Serializer::Serialize(2, value.second, FlagsType {}, out, true); - } - - template - void WriteMap(uint32_t tag, const T &value, Writer &out) - { - auto begin = std::begin(value); - auto end = std::end(value); - - for (auto it = begin; it != end; ++it) { - WriteTagWriteType(tag, WireType::LENGTH_DELIMETED, out); - WriterSizeCollector sizeCollector; - WriteMapKeyValue(*it, sizeCollector); - WriteVarint(sizeCollector.byte_size, out); - WriteMapKeyValue(*it, out); - } - } - - template - bool ReadMapKeyValue(std::pair &value, reader &in) - { - static const auto pairAsMessage = Message(Field<1, &std::pair::first, KeyFlags>("key"), - Field<2, &std::pair::second, ValueFlags>("value")); - return ReadMessage(value, pairAsMessage, in); - } - - template - bool ReadMap(WireType wireType, T &value, reader &in) - { - if (wireType != WireType::LENGTH_DELIMETED) { - return false; - } - size_t size; - if (ReadVarint(size, in)) { - LimitedReader limitedIn(in, size); - while (limitedIn.AvailableBytes() > 0) { - std::pair item; - if (!ReadMapKeyValue(item, limitedIn)) { - return false; - } - value.insert(std::move(item)); - } - return true; - } - return false; - } - - template - bool ReadRepeated(WireType wireType, OutputIt output_it, reader &in) - { - if constexpr (SerialDetail::HAS_PARSE_PACKED_V, ValueType, FlagsType, - reader>) { - if (wireType != WireType::LENGTH_DELIMETED) { - return false; - } - - size_t size; - if (ReadVarint(size, in)) { - LimitedReader limitedIn(in, size); - - while (limitedIn.AvailableBytes() > 0) { - ValueType value; - if (!Serializer::ParsePacked(value, FlagsType(), limitedIn)) { - return false; - } - output_it = value; - ++output_it; - } - return true; - } - return false; - } else { - ValueType value; - if (Serializer::Parse(wireType, value, FlagsType(), in)) { - output_it = value; - ++output_it; - return true; - } - return false; - } - } - - template - void ReadField(T &value, uint32_t tag, WireType wireType, - const SerialDetail::OneofFieldImpl &, reader &in) - { - if (Tag != tag) { - return; - } - using OneOf = SerialDetail::OneofFieldImpl; - Serializer::template ParseOneof( - wireType, OneOf::get(value), FlagsType(), in); - } - - template - void ReadField(T &value, uint32_t tag, WireType wireType, - const SerialDetail::MapFieldImpl &, reader &in) - { - if (Tag != tag) { - return; - } - using Map = SerialDetail::MapFieldImpl; - Serializer::ParseMap( - wireType, Map::get(value), FlagsType(), FlagsType(), in); - } - - template - void ReadField(T &value, uint32_t tag, WireType wireType, - const SerialDetail::FieldImpl &, reader &in) - { - if (Tag != tag) { - return; - } - using Field = SerialDetail::FieldImpl; - Serializer::Parse(wireType, Field::get(value), FlagsType(), in); - } - - template bool ReadMessage(T &value, const MessageImpl &message, reader &in) - { - uint32_t tagKey; - while (ReadVarint(tagKey, in)) { - uint32_t tag; - WireType wireType; - ReadTagWireType(tagKey, tag, wireType); - message.Visit([&](const auto &field) { ReadField(value, tag, wireType, field, in); }); - } - return true; - } - } - - template struct Serializer { - // Commion Serializer threat type as Message - static void Serialize(uint32_t tag, const T &value, FlagsType<>, Writer &out, bool force = false) - { - SerialDetail::WriterSizeCollector sizeCollector; - SerialDetail::WriteMessage(value, MessageType(), sizeCollector); - if (!force && sizeCollector.byte_size == 0) { - return; - } - SerialDetail::WriteTagWriteType(tag, WireType::LENGTH_DELIMETED, out); - SerialDetail::WriteVarint(sizeCollector.byte_size, out); - SerialDetail::WriteMessage(value, MessageType(), out); - } - - static bool Parse(WireType wireType, T &value, FlagsType<>, reader &in) - { - if (wireType != WireType::LENGTH_DELIMETED) { - return false; - } - size_t size; - if (SerialDetail::ReadVarint(size, in)) { - SerialDetail::LimitedReader limitedIn(in, size); - return SerialDetail::ReadMessage(value, MessageType(), limitedIn); - } - return false; - } - }; - - template<> struct Serializer { - static void Serialize(uint32_t tag, int32_t value, FlagsType<>, Writer &out, bool force = false) - { - SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out); - SerialDetail::WriteVarint(value, out); - } - - static void Serialize(uint32_t tag, int32_t value, FlagsType, Writer &out, bool force = false) - { - SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out); - SerialDetail::WriteSignedVarint(value, out); - } - - static void Serialize( - uint32_t tag, int32_t value, FlagsType, Writer &out, bool force = false) - { - SerialDetail::WriteTagWriteType(tag, WireType::FIXED32, out); - SerialDetail::WriteSignedFixed(value, out); - } - - static void SerializePacked(int32_t value, FlagsType<>, Writer &out) - { - SerialDetail::WriteVarint(value, out); - } - - static void SerializePacked(int32_t value, FlagsType, Writer &out) - { - SerialDetail::WriteSignedVarint(value, out); - } - - static void SerializePacked(int32_t value, FlagsType, Writer &out) - { - SerialDetail::WriteSignedFixed(value, out); - } - - static bool Parse(WireType wire_type, int32_t &value, FlagsType<>, reader &in) - { - if (wire_type != WireType::VARINT) - return false; - return SerialDetail::ReadVarint(value, in); - } - - static bool Parse(WireType wire_type, int32_t &value, FlagsType, reader &in) - { - if (wire_type != WireType::VARINT) - return false; - return SerialDetail::ReadSignedVarint(value, in); - } - - static bool Parse(WireType wire_type, int32_t &value, FlagsType, reader &in) - { - if (wire_type != WireType::FIXED32) - return false; - return SerialDetail::ReadSignedFixed(value, in); - } - - static bool ParsePacked(int32_t &value, FlagsType<>, reader &in) - { - return SerialDetail::ReadVarint(value, in); - } - - static bool ParsePacked(int32_t &value, FlagsType, reader &in) - { - return SerialDetail::ReadSignedVarint(value, in); - } - - static bool ParsePacked(int32_t &value, FlagsType, reader &in) - { - return SerialDetail::ReadSignedFixed(value, in); - } - }; - - template<> struct Serializer { - static void Serialize(uint32_t tag, uint32_t value, FlagsType<>, Writer &out, bool force = false) - { - SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out); - SerialDetail::WriteVarint(value, out); - } - - static void Serialize(uint32_t tag, uint32_t value, FlagsType, Writer &out, bool force = false) - { - SerialDetail::WriteTagWriteType(tag, WireType::FIXED32, out); - SerialDetail::WriteFixed(value, out); - } - - static void SerializePacked(uint32_t value, FlagsType<>, Writer &out) - { - SerialDetail::WriteVarint(value, out); - } - - static void SerializePacked(uint32_t value, FlagsType, Writer &out) - { - SerialDetail::WriteFixed(value, out); - } - - static bool Parse(WireType wire_type, uint32_t &value, FlagsType<>, reader &in) - { - if (wire_type != WireType::VARINT) - return false; - return SerialDetail::ReadVarint(value, in); - } - - static bool Parse(WireType wire_type, uint32_t &value, FlagsType, reader &in) - { - if (wire_type != WireType::FIXED32) - return false; - return SerialDetail::ReadFixed(value, in); - } - - static bool ParsePacked(uint32_t &value, FlagsType<>, reader &in) - { - return SerialDetail::ReadVarint(value, in); - } - - static bool ParsePacked(uint32_t &value, FlagsType, reader &in) - { - return SerialDetail::ReadFixed(value, in); - } - }; - - template<> struct Serializer { - static void Serialize(uint32_t tag, int64_t value, FlagsType<>, Writer &out, bool force = false) - { - SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out); - SerialDetail::WriteVarint(value, out); - } - - static void Serialize(uint32_t tag, int64_t value, FlagsType, Writer &out, bool force = false) - { - SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out); - SerialDetail::WriteSignedVarint(value, out); - } - - static void Serialize( - uint32_t tag, int64_t value, FlagsType, Writer &out, bool force = false) - { - SerialDetail::WriteTagWriteType(tag, WireType::FIXED64, out); - SerialDetail::WriteSignedFixed(value, out); - } - - static void SerializePacked(int64_t value, FlagsType<>, Writer &out) - { - SerialDetail::WriteVarint(value, out); - } - - static void SerializePacked(int64_t value, FlagsType, Writer &out) - { - SerialDetail::WriteSignedVarint(value, out); - } - - static void SerializePacked(int64_t value, FlagsType, Writer &out) - { - SerialDetail::WriteSignedFixed(value, out); - } - - static bool Parse(WireType wire_type, int64_t &value, FlagsType<>, reader &in) - { - if (wire_type != WireType::VARINT) - return false; - return SerialDetail::ReadVarint(value, in); - } - - static bool Parse(WireType wire_type, int64_t &value, FlagsType, reader &in) - { - if (wire_type != WireType::VARINT) - return false; - return SerialDetail::ReadSignedVarint(value, in); - } - - static bool Parse(WireType wire_type, int64_t &value, FlagsType, reader &in) - { - if (wire_type != WireType::FIXED64) - return false; - return SerialDetail::ReadSignedFixed(value, in); - } - - static bool ParsePacked(int64_t &value, FlagsType<>, reader &in) - { - return SerialDetail::ReadVarint(value, in); - } - - static bool ParsePacked(int64_t &value, FlagsType, reader &in) - { - return SerialDetail::ReadSignedVarint(value, in); - } - - static bool ParsePacked(int64_t &value, FlagsType, reader &in) - { - return SerialDetail::ReadSignedFixed(value, in); - } - }; - - template<> struct Serializer { - static void Serialize(uint32_t tag, uint64_t value, FlagsType<>, Writer &out, bool force = false) - { - SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out); - SerialDetail::WriteVarint(value, out); - } - - static void Serialize(uint32_t tag, uint64_t value, FlagsType, Writer &out, bool force = false) - { - if (!force && value == UINT64_C(0)) - return; - - SerialDetail::WriteTagWriteType(tag, WireType::FIXED64, out); - SerialDetail::WriteFixed(value, out); - } - - static void SerializePacked(uint64_t value, FlagsType<>, Writer &out) - { - SerialDetail::WriteVarint(value, out); - } - - static void SerializePacked(uint64_t value, FlagsType, Writer &out) - { - SerialDetail::WriteFixed(value, out); - } - - static bool Parse(WireType wire_type, uint64_t &value, FlagsType<>, reader &in) - { - if (wire_type != WireType::VARINT) - return false; - return SerialDetail::ReadVarint(value, in); - } - - static bool Parse(WireType wire_type, uint64_t &value, FlagsType, reader &in) - { - if (wire_type != WireType::FIXED64) - return false; - return SerialDetail::ReadFixed(value, in); - } - - static bool ParsePacked(uint64_t &value, FlagsType<>, reader &in) - { - return SerialDetail::ReadVarint(value, in); - } - - static bool ParsePacked(uint64_t &value, FlagsType, reader &in) - { - return SerialDetail::ReadFixed(value, in); - } - }; - - template<> struct Serializer { - static void Serialize(uint32_t tag, double value, FlagsType<>, Writer &out, bool force = false) - { - if (!force && std::fpclassify(value) == FP_ZERO) { - return; - } - SerialDetail::WriteTagWriteType(tag, WireType::FIXED64, out); - SerialDetail::WriteFixed(value, out); - } - - static void SerializePacked(double value, FlagsType<>, Writer &out) - { - SerialDetail::WriteFixed(value, out); - } - - static bool Parse(WireType wire_type, double &value, FlagsType<>, reader &in) - { - if (wire_type != WireType::FIXED64) { - return false; - } - return SerialDetail::ReadFixed(value, in); - } - - static bool ParsePacked(double &value, FlagsType<>, reader &in) - { - return SerialDetail::ReadFixed(value, in); - } - }; - - template<> struct Serializer { - static void Serialize(uint32_t tag, float value, FlagsType<>, Writer &out, bool force = false) - { - if (!force && std::fpclassify(value) == FP_ZERO) { - return; - } - SerialDetail::WriteTagWriteType(tag, WireType::FIXED32, out); - SerialDetail::WriteFixed(value, out); - } - - static void SerializePacked(float value, FlagsType<>, Writer &out) - { - SerialDetail::WriteFixed(value, out); - } - - static bool Parse(WireType wire_type, float &value, FlagsType<>, reader &in) - { - if (wire_type != WireType::FIXED32) { - return false; - } - return SerialDetail::ReadFixed(value, in); - } - - static bool ParsePacked(float &value, FlagsType<>, reader &in) - { - return SerialDetail::ReadFixed(value, in); - } - }; - - template<> struct Serializer { - static void Serialize(uint32_t tag, bool value, FlagsType<>, Writer &out, bool force = false) - { - Serializer::Serialize(tag, value ? 1 : 0, FlagsType(), out, force); - } - - static void SerializePacked(bool value, FlagsType<>, Writer &out) - { - Serializer::SerializePacked(value ? 1 : 0, FlagsType(), out); - } - - static bool Parse(WireType wire_type, bool &value, FlagsType<>, reader &in) - { - uint32_t intermedaite_value; - if (Serializer::Parse(wire_type, intermedaite_value, FlagsType<>(), in)) { - value = static_cast(intermedaite_value); - return true; - } - return false; - } - - static bool ParsePacked(bool &value, FlagsType<>, reader &in) - { - uint32_t intermedaite_value; - if (Serializer::ParsePacked(intermedaite_value, FlagsType<>(), in)) { - value = static_cast(intermedaite_value); - return true; - } - return false; - } - }; - - template struct Serializer>> { - using U = std::underlying_type_t; - - static void Serialize(uint32_t tag, T value, FlagsType<>, Writer &out, bool force = false) - { - Serializer::Serialize(tag, static_cast(value), FlagsType<>(), out, force); - } - - static void SerializePacked(T value, FlagsType<>, Writer &out) - { - Serializer::SerializePacked(static_cast(value), FlagsType<>(), out); - } - - static bool Parse(WireType wire_type, T &value, FlagsType<>, reader &in) - { - U intermedaite_value; - if (Serializer::Parse(wire_type, intermedaite_value, FlagsType<>(), in)) { - value = static_cast(intermedaite_value); - return true; - } - return false; - } - - static bool ParsePacked(T &value, FlagsType<>, reader &in) - { - U intermedaite_value; - if (Serializer::ParsePacked(intermedaite_value, FlagsType<>(), in)) { - value = static_cast(intermedaite_value); - return true; - } - return false; - } - }; - - template<> struct Serializer { - static void Serialize(uint32_t tag, const std::string &value, FlagsType<>, Writer &out, bool force = false) - { - SerialDetail::WriteTagWriteType(tag, WireType::LENGTH_DELIMETED, out); - SerialDetail::WriteVarint(value.size(), out); - out.Write(value.data(), value.size()); - } - - static bool Parse(WireType wire_type, std::string &value, FlagsType<>, reader &in) - { - if (wire_type != WireType::LENGTH_DELIMETED) { - return false; - } - size_t size; - if (SerialDetail::ReadVarint(size, in)) { - value.resize(size); - if (in.Read(value.data(), size) == size) { - return true; - } - } - return false; - } - }; - - template struct Serializer> { - template - static void Serialize(uint32_t tag, const std::vector &value, FlagsType, Writer &out) - { - SerialDetail::WriteRepeated(tag, value.begin(), value.end(), out); - } - - template - static bool Parse(WireType wire_type, std::vector &value, FlagsType, reader &in) - { - return SerialDetail::ReadRepeated(wire_type, std::back_inserter(value), in); - } - }; - - template struct Serializer> { - template - static void Serialize(uint32_t tag, const std::optional &value, FlagsType, Writer &out) - { - if (!value.has_value()) { - return; - } - Serializer::Serialize(tag, *value, FlagsType(), out); - } - - template - static bool Parse(WireType wire_type, std::optional &value, FlagsType, reader &in) - { - return Serializer::Parse(wire_type, value.emplace(), FlagsType(), in); - } - }; - - template struct Serializer> { - template - static void SerializeOneof(uint32_t tag, const std::variant &value, FlagsType, Writer &out) - { - if (value.index() != Index) - return; - - Serializer>>::Serialize( - tag, std::get(value), FlagsType(), out); - } - - template - static bool ParseOneof(WireType wire_type, std::variant &value, FlagsType, reader &in) - { - return Serializer>>::Parse( - wire_type, value.template emplace(), FlagsType(), in); - } - }; - - template struct Serializer> { - template - static void SerializeMap( - uint32_t tag, const std::map &value, FlagsType, FlagsType, Writer &out) - { - SerialDetail::WriteMap(tag, value, out); - } - - template - static bool ParseMap( - WireType wire_type, std::map &value, FlagsType, FlagsType, reader &in) - { - return SerialDetail::ReadMap(wire_type, value, in); - } - }; - - struct StringWriter : public Writer { - StringWriter(std::string &out) - : _out(out) - { - } - - void Write(const void *bytes, size_t size) override - { - _out.append(reinterpret_cast(bytes), size); - } - - private: - std::string &_out; - }; - - struct StringReader : public reader { - StringReader(const std::string &in) - : _in(in), _pos(0) - { - } - - size_t Read(void *bytes, size_t size) override - { - size_t readSize = std::min(size, _in.size() - _pos); - if (memcpy_s(bytes, size, _in.data() + _pos, readSize) != EOK) { - return readSize; - } - _pos += readSize; - return readSize; - } - - private: - const std::string &_in; - size_t _pos; - }; - // mytype - template<> struct Serializer { - static void Serialize(uint32_t tag, uint8_t value, FlagsType<>, Writer &out, bool force = false) - { - Serializer::Serialize(tag, value, FlagsType(), out, force); - } - - static void SerializePacked(uint8_t value, FlagsType<>, Writer &out) - { - Serializer::SerializePacked(value, FlagsType(), out); - } - - static bool Parse(WireType wire_type, uint8_t &value, FlagsType<>, reader &in) - { - uint32_t intermedaite_value; - if (Serializer::Parse(wire_type, intermedaite_value, FlagsType<>(), in)) { - value = static_cast(intermedaite_value); - return true; - } - return false; - } - - static bool ParsePacked(uint8_t &value, FlagsType<>, reader &in) - { - uint32_t intermedaite_value; - if (Serializer::ParsePacked(intermedaite_value, FlagsType<>(), in)) { - value = static_cast(intermedaite_value); - return true; - } - return false; - } - }; - template<> struct Serializer { - static void Serialize(uint32_t tag, uint16_t value, FlagsType<>, Writer &out, bool force = false) - { - Serializer::Serialize(tag, value, FlagsType(), out, force); - } - - static void SerializePacked(uint16_t value, FlagsType<>, Writer &out) - { - Serializer::SerializePacked(value, FlagsType(), out); - } - - static bool Parse(WireType wire_type, uint16_t &value, FlagsType<>, reader &in) - { - uint32_t intermedaite_value; - if (Serializer::Parse(wire_type, intermedaite_value, FlagsType<>(), in)) { - value = static_cast(intermedaite_value); - return true; - } - return false; - } - - static bool ParsePacked(uint16_t &value, FlagsType<>, reader &in) - { - uint32_t intermedaite_value; - if (Serializer::ParsePacked(intermedaite_value, FlagsType<>(), in)) { - value = static_cast(intermedaite_value); - return true; - } - return false; - } - }; - // mytype finish - template std::string SerializeToString(const T &value) - { - std::string out; - StringWriter stringOut(out); - SerialDetail::WriteMessage(value, MessageType(), stringOut); - return out; - } - - template bool ParseFromString(T &value, const std::string &in) - { - StringReader stringIn(in); - return SerialDetail::ReadMessage(value, MessageType(), stringIn); - } -} -// clang-format on -// ------------------------------------------------split line-------------------------------------------- - -// Class template special static definition namespace SerialStruct { template<> struct Descriptor { static auto type() diff --git a/src/common/serial_struct_define.h b/src/common/serial_struct_define.h new file mode 100755 index 0000000000000000000000000000000000000000..7facd07854713d68bc79675163862b40db03e548 --- /dev/null +++ b/src/common/serial_struct_define.h @@ -0,0 +1,1294 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef HDC_SERIAL_STRUCT_DEFINE_H +#define HDC_SERIAL_STRUCT_DEFINE_H +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace Hdc { +// clang-format off +namespace SerialStruct { + namespace SerialDetail { + template struct MemPtr { + }; + template struct MemPtr { + using type = T; + using MemberType = U; + }; + template struct MessageImpl { + public: + MessageImpl(Fields &&... fields) + : _fields(std::move(fields)...) + { + } + + template void Visit(Handler &&handler) const + { + VisitImpl(std::forward(handler), std::make_index_sequence()); + } + + private: + std::tuple _fields; + + template void VisitImpl(Handler &&handler, std::index_sequence) const + { + (handler(std::get(_fields)), ...); + } + }; + + template struct FieldImpl { + using type = typename SerialDetail::MemPtr::type; + using MemberType = typename SerialDetail::MemPtr::MemberType; + constexpr static const uint32_t tag = Tag; + constexpr static const uint32_t flags = Flags; + const std::string field_name; + + static decltype(auto) get(const type &value) + { + return value.*MemPtr; + } + + static decltype(auto) get(type &value) + { + return value.*MemPtr; + } + }; + + template struct OneofFieldImpl { + using type = typename SerialDetail::MemPtr::type; + using MemberType = typename SerialDetail::MemPtr::MemberType; + constexpr static const uint32_t tag = Tag; + constexpr static const size_t index = Index; + constexpr static const uint32_t flags = Flags; + const std::string field_name; + + static decltype(auto) get(const type &value) + { + return value.*MemPtr; + } + + static decltype(auto) get(type &value) + { + return value.*MemPtr; + } + }; + + template + struct MapFieldImpl { + using type = typename SerialDetail::MemPtr::type; + using MemberType = typename SerialDetail::MemPtr::MemberType; + constexpr static const uint32_t tag = Tag; + constexpr static const uint32_t KEY_FLAGS = KeyFlags; + constexpr static const uint32_t VALUE_FLAGS = ValueFlags; + + const std::string field_name; + + static decltype(auto) get(const type &value) + { + return value.*MemPtr; + } + + static decltype(auto) get(type &value) + { + return value.*MemPtr; + } + }; + } + + enum class WireType : uint32_t { + VARINT = 0, + FIXED64 = 1, + LENGTH_DELIMETED = 2, + START_GROUP = 3, + END_GROUP = 4, + FIXED32 = 5, + }; + enum flags { no = 0, s = 1, f = 2 }; + template struct FlagsType { + }; + + template struct Descriptor { + static_assert(sizeof(T) == 0, "You need to implement descriptor for your own types"); + static void type() + { + } + }; + + template constexpr auto Message(Fields &&... fields) + { + return SerialDetail::MessageImpl(std::forward(fields)...); + } + + template constexpr auto Field(const std::string &fieldName) + { + return SerialDetail::FieldImpl { fieldName }; + } + + template + constexpr auto OneofField(const std::string &fieldName) + { + return SerialDetail::OneofFieldImpl { fieldName }; + } + + template + constexpr auto MapField(const std::string &fieldName) + { + return SerialDetail::MapFieldImpl { fieldName }; + } + + template const auto &MessageType() + { + static const auto message = Descriptor::type(); + return message; + } + + template struct Serializer; + + struct Writer { + virtual void Write(const void *bytes, size_t size) = 0; + }; + + struct reader { + virtual size_t Read(void *bytes, size_t size) = 0; + }; + + namespace SerialDetail { + template + struct HasSerializePacked : public std::false_type { + }; + + template + struct HasSerializePacked().SerializePacked( + std::declval(), std::declval(), std::declval()))>> : public std::true_type { + }; + + template + constexpr bool HAS_SERIALIZE_PACKED_V = HasSerializePacked::value; + + template + struct HasParsePacked : public std::false_type { + }; + + template + struct HasParsePacked().ParsePacked( + std::declval(), std::declval(), std::declval()))>> : public std::true_type { + }; + + template + constexpr bool HAS_PARSE_PACKED_V = HasParsePacked::value; + + static uint32_t MakeTagWireType(uint32_t tag, WireType wireType) + { + return (tag << 3) | static_cast(wireType); + } + + static inline void ReadTagWireType(uint32_t tagKey, uint32_t &tag, WireType &wireType) + { + wireType = static_cast(tagKey & 0b0111); + tag = tagKey >> 3; + } + + static uint32_t MakeZigzagValue(int32_t value) + { + return (static_cast(value) << 1) ^ static_cast(value >> 31); + } + + static uint64_t MakeZigzagValue(int64_t value) + { + return (static_cast(value) << 1) ^ static_cast(value >> 63); + } + + static int32_t ReadZigzagValue(uint32_t value) + { + return static_cast((value >> 1) ^ (~(value & 1) + 1)); + } + + static int64_t ReadZigzagValue(uint64_t value) + { + return static_cast((value >> 1) ^ (~(value & 1) + 1)); + } + + template To BitCast(From from) + { + static_assert(sizeof(To) == sizeof(From), ""); + static_assert(std::is_trivially_copyable_v, ""); + static_assert(std::is_trivially_copyable_v, ""); + To to; + memcpy_s(&to, sizeof(To), &from, sizeof(from)); + return to; + } + + struct WriterSizeCollector : public Writer { + void Write(const void *, size_t size) override + { + byte_size += size; + } + size_t byte_size = 0; + }; + + struct LimitedReader : public reader { + LimitedReader(reader &parent, size_t sizeLimit) + : _parent(parent), _size_limit(sizeLimit) + { + } + + size_t Read(void *bytes, size_t size) + { + auto sizeToRead = std::min(size, _size_limit); + auto readSize = _parent.Read(bytes, sizeToRead); + _size_limit -= readSize; + return readSize; + } + + size_t AvailableBytes() const + { + return _size_limit; + } + + private: + reader &_parent; + size_t _size_limit; + }; + + static bool ReadByte(uint8_t &value, reader &in) + { + return in.Read(&value, 1) == 1; + } + + static void WriteVarint(uint32_t value, Writer &out) + { + uint8_t b[5] {}; + for (size_t i = 0; i < 5; ++i) { + b[i] = value & 0b0111'1111; + value >>= 7; + if (value) { + b[i] |= 0b1000'0000; + } else { + out.Write(b, i + 1); + break; + } + } + } + + static void WriteVarint(uint64_t value, Writer &out) + { + uint8_t b[10] {}; + for (size_t i = 0; i < 10; ++i) { + b[i] = value & 0b0111'1111; + value >>= 7; + if (value) { + b[i] |= 0b1000'0000; + } else { + out.Write(b, i + 1); + break; + } + } + } + + static bool ReadVarint(uint32_t &value, reader &in) + { + value = 0; + for (size_t c = 0; c < 5; ++c) { + uint8_t x; + if (!ReadByte(x, in)) { + return false; + } + value |= static_cast(x & 0b0111'1111) << 7 * c; + if (!(x & 0b1000'0000)) { + return true; + } + } + + return false; + } + + static bool ReadVarint(uint64_t &value, reader &in) + { + value &= 0; + for (size_t c = 0; c < 10; ++c) { + uint8_t x; + if (!ReadByte(x, in)) { + return false; + } + value |= static_cast(x & 0b0111'1111) << 7 * c; + if (!(x & 0b1000'0000)) { + return true; + } + } + return false; + } + + static void WriteFixed(uint32_t value, Writer &out) + { +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + out.Write(&value, sizeof(value)); +#else + static_assert(false, "Not a little-endian"); +#endif + } + + static void WriteFixed(uint64_t value, Writer &out) + { +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + out.Write(&value, sizeof(value)); +#else + static_assert(false, "Not a little-endian"); +#endif + } + + static void WriteFixed(double value, Writer &out) + { + WriteFixed(BitCast(value), out); + } + + static void WriteFixed(float value, Writer &out) + { + WriteFixed(BitCast(value), out); + } + + static void WriteVarint(int32_t value, Writer &out) + { + WriteVarint(BitCast(value), out); + } + + static void WriteVarint(int64_t value, Writer &out) + { + WriteVarint(BitCast(value), out); + } + + static void WriteSignedVarint(int32_t value, Writer &out) + { + WriteVarint(MakeZigzagValue(value), out); + } + + static void WriteSignedVarint(int64_t value, Writer &out) + { + WriteVarint(MakeZigzagValue(value), out); + } + + static void WriteSignedFixed(int32_t value, Writer &out) + { + WriteFixed(static_cast(value), out); + } + + static void WriteSignedFixed(int64_t value, Writer &out) + { + WriteFixed(static_cast(value), out); + } + + static void WriteTagWriteType(uint32_t tag, WireType wireType, Writer &out) + { + WriteVarint(MakeTagWireType(tag, wireType), out); + } + + static bool ReadFixed(uint32_t &value, reader &in) + { +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + return in.Read(&value, sizeof(value)) == sizeof(value); +#else + static_assert(false, "Not a little-endian"); +#endif + } + + static bool ReadFixed(uint64_t &value, reader &in) + { +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + return in.Read(&value, sizeof(value)) == sizeof(value); +#else + static_assert(false, "Not a little-endian"); +#endif + } + + static bool ReadFixed(double &value, reader &in) + { + uint64_t intermediateValue; + if (ReadFixed(intermediateValue, in)) { + value = BitCast(intermediateValue); + return true; + } + return false; + } + + static bool ReadFixed(float &value, reader &in) + { + uint32_t intermediateValue; + if (ReadFixed(intermediateValue, in)) { + value = BitCast(intermediateValue); + return true; + } + return false; + } + + static bool ReadVarint(int32_t &value, reader &in) + { + uint32_t intermediateValue; + if (ReadVarint(intermediateValue, in)) { + value = BitCast(intermediateValue); + return true; + } + return false; + } + + static bool ReadVarint(int64_t &value, reader &in) + { + uint64_t intermediateValue; + if (ReadVarint(intermediateValue, in)) { + value = BitCast(intermediateValue); + return true; + } + return false; + } + + static bool ReadSignedVarint(int32_t &value, reader &in) + { + uint32_t intermediateValue; + if (ReadVarint(intermediateValue, in)) { + value = ReadZigzagValue(intermediateValue); + return true; + } + return false; + } + + static bool ReadSignedVarint(int64_t &value, reader &in) + { + uint64_t intermediateValue; + if (ReadVarint(intermediateValue, in)) { + value = ReadZigzagValue(intermediateValue); + return true; + } + return false; + } + + static bool ReadSignedFixed(int32_t &value, reader &in) + { + uint32_t intermediateValue; + if (ReadFixed(intermediateValue, in)) { + value = static_cast(intermediateValue); + return true; + } + return false; + } + + static bool ReadSignedFixed(int64_t &value, reader &in) + { + uint64_t intermediateValue; + if (ReadFixed(intermediateValue, in)) { + value = static_cast(intermediateValue); + return true; + } + return false; + } + + template + void WriteField(const T &value, + const SerialDetail::OneofFieldImpl &, Writer &out) + { + using OneOf = SerialDetail::OneofFieldImpl; + Serializer::template SerializeOneof( + OneOf::tag, OneOf::get(value), FlagsType(), out); + } + + template + void WriteField(const T &value, + const SerialDetail::MapFieldImpl &, Writer &out) + { + using Map = SerialDetail::MapFieldImpl; + Serializer::SerializeMap( + Map::tag, Map::get(value), FlagsType(), FlagsType(), out); + } + + template + void WriteField(const T &value, const SerialDetail::FieldImpl &, Writer &out) + { + using Field = SerialDetail::FieldImpl; + Serializer::Serialize( + Field::tag, Field::get(value), FlagsType(), out); + } + + template + void WriteMessage(const T &value, const SerialDetail::MessageImpl &message, Writer &out) + { + message.Visit([&](const auto &field) { WriteField(value, field, out); }); + } + + template + void WriteRepeated(uint32_t tag, It begin, It end, Writer &out) + { + if (begin == end) { + return; + } + if constexpr (SerialDetail::HAS_SERIALIZE_PACKED_V, ValueType, FlagsType, + Writer>) { + WriteVarint(MakeTagWireType(tag, WireType::LENGTH_DELIMETED), out); + WriterSizeCollector sizeCollector; + for (auto it = begin; it != end; ++it) { + Serializer::SerializePacked(*it, FlagsType {}, sizeCollector); + } + WriteVarint(sizeCollector.byte_size, out); + for (auto it = begin; it != end; ++it) { + Serializer::SerializePacked(*it, FlagsType {}, out); + } + } else { + for (auto it = begin; it != end; ++it) { + Serializer::Serialize(tag, *it, FlagsType(), out); + } + } + } + + template + void WriteMapKeyValue(const std::pair &value, Writer &out) + { + Serializer::Serialize(1, value.first, FlagsType {}, out, true); + Serializer::Serialize(2, value.second, FlagsType {}, out, true); + } + + template + void WriteMap(uint32_t tag, const T &value, Writer &out) + { + auto begin = std::begin(value); + auto end = std::end(value); + + for (auto it = begin; it != end; ++it) { + WriteTagWriteType(tag, WireType::LENGTH_DELIMETED, out); + WriterSizeCollector sizeCollector; + WriteMapKeyValue(*it, sizeCollector); + WriteVarint(sizeCollector.byte_size, out); + WriteMapKeyValue(*it, out); + } + } + + template + bool ReadMapKeyValue(std::pair &value, reader &in) + { + static const auto pairAsMessage = Message(Field<1, &std::pair::first, KeyFlags>("key"), + Field<2, &std::pair::second, ValueFlags>("value")); + return ReadMessage(value, pairAsMessage, in); + } + + template + bool ReadMap(WireType wireType, T &value, reader &in) + { + if (wireType != WireType::LENGTH_DELIMETED) { + return false; + } + size_t size; + if (ReadVarint(size, in)) { + LimitedReader limitedIn(in, size); + while (limitedIn.AvailableBytes() > 0) { + std::pair item; + if (!ReadMapKeyValue(item, limitedIn)) { + return false; + } + value.insert(std::move(item)); + } + return true; + } + return false; + } + + template + bool ReadRepeated(WireType wireType, OutputIt output_it, reader &in) + { + if constexpr (SerialDetail::HAS_PARSE_PACKED_V, ValueType, FlagsType, + reader>) { + if (wireType != WireType::LENGTH_DELIMETED) { + return false; + } + + size_t size; + if (ReadVarint(size, in)) { + LimitedReader limitedIn(in, size); + + while (limitedIn.AvailableBytes() > 0) { + ValueType value; + if (!Serializer::ParsePacked(value, FlagsType(), limitedIn)) { + return false; + } + output_it = value; + ++output_it; + } + return true; + } + return false; + } else { + ValueType value; + if (Serializer::Parse(wireType, value, FlagsType(), in)) { + output_it = value; + ++output_it; + return true; + } + return false; + } + } + + template + void ReadField(T &value, uint32_t tag, WireType wireType, + const SerialDetail::OneofFieldImpl &, reader &in) + { + if (Tag != tag) { + return; + } + using OneOf = SerialDetail::OneofFieldImpl; + Serializer::template ParseOneof( + wireType, OneOf::get(value), FlagsType(), in); + } + + template + void ReadField(T &value, uint32_t tag, WireType wireType, + const SerialDetail::MapFieldImpl &, reader &in) + { + if (Tag != tag) { + return; + } + using Map = SerialDetail::MapFieldImpl; + Serializer::ParseMap( + wireType, Map::get(value), FlagsType(), FlagsType(), in); + } + + template + void ReadField(T &value, uint32_t tag, WireType wireType, + const SerialDetail::FieldImpl &, reader &in) + { + if (Tag != tag) { + return; + } + using Field = SerialDetail::FieldImpl; + Serializer::Parse(wireType, Field::get(value), FlagsType(), in); + } + + template bool ReadMessage(T &value, const MessageImpl &message, reader &in) + { + uint32_t tagKey; + while (ReadVarint(tagKey, in)) { + uint32_t tag; + WireType wireType; + ReadTagWireType(tagKey, tag, wireType); + message.Visit([&](const auto &field) { ReadField(value, tag, wireType, field, in); }); + } + return true; + } + } + + template struct Serializer { + // Commion Serializer threat type as Message + static void Serialize(uint32_t tag, const T &value, FlagsType<>, Writer &out, bool force = false) + { + SerialDetail::WriterSizeCollector sizeCollector; + SerialDetail::WriteMessage(value, MessageType(), sizeCollector); + if (!force && sizeCollector.byte_size == 0) { + return; + } + SerialDetail::WriteTagWriteType(tag, WireType::LENGTH_DELIMETED, out); + SerialDetail::WriteVarint(sizeCollector.byte_size, out); + SerialDetail::WriteMessage(value, MessageType(), out); + } + + static bool Parse(WireType wireType, T &value, FlagsType<>, reader &in) + { + if (wireType != WireType::LENGTH_DELIMETED) { + return false; + } + size_t size; + if (SerialDetail::ReadVarint(size, in)) { + SerialDetail::LimitedReader limitedIn(in, size); + return SerialDetail::ReadMessage(value, MessageType(), limitedIn); + } + return false; + } + }; + + template<> struct Serializer { + static void Serialize(uint32_t tag, int32_t value, FlagsType<>, Writer &out, bool force = false) + { + SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out); + SerialDetail::WriteVarint(value, out); + } + + static void Serialize(uint32_t tag, int32_t value, FlagsType, Writer &out, bool force = false) + { + SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out); + SerialDetail::WriteSignedVarint(value, out); + } + + static void Serialize( + uint32_t tag, int32_t value, FlagsType, Writer &out, bool force = false) + { + SerialDetail::WriteTagWriteType(tag, WireType::FIXED32, out); + SerialDetail::WriteSignedFixed(value, out); + } + + static void SerializePacked(int32_t value, FlagsType<>, Writer &out) + { + SerialDetail::WriteVarint(value, out); + } + + static void SerializePacked(int32_t value, FlagsType, Writer &out) + { + SerialDetail::WriteSignedVarint(value, out); + } + + static void SerializePacked(int32_t value, FlagsType, Writer &out) + { + SerialDetail::WriteSignedFixed(value, out); + } + + static bool Parse(WireType wire_type, int32_t &value, FlagsType<>, reader &in) + { + if (wire_type != WireType::VARINT) + return false; + return SerialDetail::ReadVarint(value, in); + } + + static bool Parse(WireType wire_type, int32_t &value, FlagsType, reader &in) + { + if (wire_type != WireType::VARINT) + return false; + return SerialDetail::ReadSignedVarint(value, in); + } + + static bool Parse(WireType wire_type, int32_t &value, FlagsType, reader &in) + { + if (wire_type != WireType::FIXED32) + return false; + return SerialDetail::ReadSignedFixed(value, in); + } + + static bool ParsePacked(int32_t &value, FlagsType<>, reader &in) + { + return SerialDetail::ReadVarint(value, in); + } + + static bool ParsePacked(int32_t &value, FlagsType, reader &in) + { + return SerialDetail::ReadSignedVarint(value, in); + } + + static bool ParsePacked(int32_t &value, FlagsType, reader &in) + { + return SerialDetail::ReadSignedFixed(value, in); + } + }; + + template<> struct Serializer { + static void Serialize(uint32_t tag, uint32_t value, FlagsType<>, Writer &out, bool force = false) + { + SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out); + SerialDetail::WriteVarint(value, out); + } + + static void Serialize(uint32_t tag, uint32_t value, FlagsType, Writer &out, bool force = false) + { + SerialDetail::WriteTagWriteType(tag, WireType::FIXED32, out); + SerialDetail::WriteFixed(value, out); + } + + static void SerializePacked(uint32_t value, FlagsType<>, Writer &out) + { + SerialDetail::WriteVarint(value, out); + } + + static void SerializePacked(uint32_t value, FlagsType, Writer &out) + { + SerialDetail::WriteFixed(value, out); + } + + static bool Parse(WireType wire_type, uint32_t &value, FlagsType<>, reader &in) + { + if (wire_type != WireType::VARINT) + return false; + return SerialDetail::ReadVarint(value, in); + } + + static bool Parse(WireType wire_type, uint32_t &value, FlagsType, reader &in) + { + if (wire_type != WireType::FIXED32) + return false; + return SerialDetail::ReadFixed(value, in); + } + + static bool ParsePacked(uint32_t &value, FlagsType<>, reader &in) + { + return SerialDetail::ReadVarint(value, in); + } + + static bool ParsePacked(uint32_t &value, FlagsType, reader &in) + { + return SerialDetail::ReadFixed(value, in); + } + }; + + template<> struct Serializer { + static void Serialize(uint32_t tag, int64_t value, FlagsType<>, Writer &out, bool force = false) + { + SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out); + SerialDetail::WriteVarint(value, out); + } + + static void Serialize(uint32_t tag, int64_t value, FlagsType, Writer &out, bool force = false) + { + SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out); + SerialDetail::WriteSignedVarint(value, out); + } + + static void Serialize( + uint32_t tag, int64_t value, FlagsType, Writer &out, bool force = false) + { + SerialDetail::WriteTagWriteType(tag, WireType::FIXED64, out); + SerialDetail::WriteSignedFixed(value, out); + } + + static void SerializePacked(int64_t value, FlagsType<>, Writer &out) + { + SerialDetail::WriteVarint(value, out); + } + + static void SerializePacked(int64_t value, FlagsType, Writer &out) + { + SerialDetail::WriteSignedVarint(value, out); + } + + static void SerializePacked(int64_t value, FlagsType, Writer &out) + { + SerialDetail::WriteSignedFixed(value, out); + } + + static bool Parse(WireType wire_type, int64_t &value, FlagsType<>, reader &in) + { + if (wire_type != WireType::VARINT) + return false; + return SerialDetail::ReadVarint(value, in); + } + + static bool Parse(WireType wire_type, int64_t &value, FlagsType, reader &in) + { + if (wire_type != WireType::VARINT) + return false; + return SerialDetail::ReadSignedVarint(value, in); + } + + static bool Parse(WireType wire_type, int64_t &value, FlagsType, reader &in) + { + if (wire_type != WireType::FIXED64) + return false; + return SerialDetail::ReadSignedFixed(value, in); + } + + static bool ParsePacked(int64_t &value, FlagsType<>, reader &in) + { + return SerialDetail::ReadVarint(value, in); + } + + static bool ParsePacked(int64_t &value, FlagsType, reader &in) + { + return SerialDetail::ReadSignedVarint(value, in); + } + + static bool ParsePacked(int64_t &value, FlagsType, reader &in) + { + return SerialDetail::ReadSignedFixed(value, in); + } + }; + + template<> struct Serializer { + static void Serialize(uint32_t tag, uint64_t value, FlagsType<>, Writer &out, bool force = false) + { + SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out); + SerialDetail::WriteVarint(value, out); + } + + static void Serialize(uint32_t tag, uint64_t value, FlagsType, Writer &out, bool force = false) + { + if (!force && value == UINT64_C(0)) + return; + + SerialDetail::WriteTagWriteType(tag, WireType::FIXED64, out); + SerialDetail::WriteFixed(value, out); + } + + static void SerializePacked(uint64_t value, FlagsType<>, Writer &out) + { + SerialDetail::WriteVarint(value, out); + } + + static void SerializePacked(uint64_t value, FlagsType, Writer &out) + { + SerialDetail::WriteFixed(value, out); + } + + static bool Parse(WireType wire_type, uint64_t &value, FlagsType<>, reader &in) + { + if (wire_type != WireType::VARINT) + return false; + return SerialDetail::ReadVarint(value, in); + } + + static bool Parse(WireType wire_type, uint64_t &value, FlagsType, reader &in) + { + if (wire_type != WireType::FIXED64) + return false; + return SerialDetail::ReadFixed(value, in); + } + + static bool ParsePacked(uint64_t &value, FlagsType<>, reader &in) + { + return SerialDetail::ReadVarint(value, in); + } + + static bool ParsePacked(uint64_t &value, FlagsType, reader &in) + { + return SerialDetail::ReadFixed(value, in); + } + }; + + template<> struct Serializer { + static void Serialize(uint32_t tag, double value, FlagsType<>, Writer &out, bool force = false) + { + if (!force && std::fpclassify(value) == FP_ZERO) { + return; + } + SerialDetail::WriteTagWriteType(tag, WireType::FIXED64, out); + SerialDetail::WriteFixed(value, out); + } + + static void SerializePacked(double value, FlagsType<>, Writer &out) + { + SerialDetail::WriteFixed(value, out); + } + + static bool Parse(WireType wire_type, double &value, FlagsType<>, reader &in) + { + if (wire_type != WireType::FIXED64) { + return false; + } + return SerialDetail::ReadFixed(value, in); + } + + static bool ParsePacked(double &value, FlagsType<>, reader &in) + { + return SerialDetail::ReadFixed(value, in); + } + }; + + template<> struct Serializer { + static void Serialize(uint32_t tag, float value, FlagsType<>, Writer &out, bool force = false) + { + if (!force && std::fpclassify(value) == FP_ZERO) { + return; + } + SerialDetail::WriteTagWriteType(tag, WireType::FIXED32, out); + SerialDetail::WriteFixed(value, out); + } + + static void SerializePacked(float value, FlagsType<>, Writer &out) + { + SerialDetail::WriteFixed(value, out); + } + + static bool Parse(WireType wire_type, float &value, FlagsType<>, reader &in) + { + if (wire_type != WireType::FIXED32) { + return false; + } + return SerialDetail::ReadFixed(value, in); + } + + static bool ParsePacked(float &value, FlagsType<>, reader &in) + { + return SerialDetail::ReadFixed(value, in); + } + }; + + template<> struct Serializer { + static void Serialize(uint32_t tag, bool value, FlagsType<>, Writer &out, bool force = false) + { + Serializer::Serialize(tag, value ? 1 : 0, FlagsType(), out, force); + } + + static void SerializePacked(bool value, FlagsType<>, Writer &out) + { + Serializer::SerializePacked(value ? 1 : 0, FlagsType(), out); + } + + static bool Parse(WireType wire_type, bool &value, FlagsType<>, reader &in) + { + uint32_t intermedaite_value; + if (Serializer::Parse(wire_type, intermedaite_value, FlagsType<>(), in)) { + value = static_cast(intermedaite_value); + return true; + } + return false; + } + + static bool ParsePacked(bool &value, FlagsType<>, reader &in) + { + uint32_t intermedaite_value; + if (Serializer::ParsePacked(intermedaite_value, FlagsType<>(), in)) { + value = static_cast(intermedaite_value); + return true; + } + return false; + } + }; + + template struct Serializer>> { + using U = std::underlying_type_t; + + static void Serialize(uint32_t tag, T value, FlagsType<>, Writer &out, bool force = false) + { + Serializer::Serialize(tag, static_cast(value), FlagsType<>(), out, force); + } + + static void SerializePacked(T value, FlagsType<>, Writer &out) + { + Serializer::SerializePacked(static_cast(value), FlagsType<>(), out); + } + + static bool Parse(WireType wire_type, T &value, FlagsType<>, reader &in) + { + U intermedaite_value; + if (Serializer::Parse(wire_type, intermedaite_value, FlagsType<>(), in)) { + value = static_cast(intermedaite_value); + return true; + } + return false; + } + + static bool ParsePacked(T &value, FlagsType<>, reader &in) + { + U intermedaite_value; + if (Serializer::ParsePacked(intermedaite_value, FlagsType<>(), in)) { + value = static_cast(intermedaite_value); + return true; + } + return false; + } + }; + + template<> struct Serializer { + static void Serialize(uint32_t tag, const std::string &value, FlagsType<>, Writer &out, bool force = false) + { + SerialDetail::WriteTagWriteType(tag, WireType::LENGTH_DELIMETED, out); + SerialDetail::WriteVarint(value.size(), out); + out.Write(value.data(), value.size()); + } + + static bool Parse(WireType wire_type, std::string &value, FlagsType<>, reader &in) + { + if (wire_type != WireType::LENGTH_DELIMETED) { + return false; + } + size_t size; + if (SerialDetail::ReadVarint(size, in)) { + value.resize(size); + if (in.Read(value.data(), size) == size) { + return true; + } + } + return false; + } + }; + + template struct Serializer> { + template + static void Serialize(uint32_t tag, const std::vector &value, FlagsType, Writer &out) + { + SerialDetail::WriteRepeated(tag, value.begin(), value.end(), out); + } + + template + static bool Parse(WireType wire_type, std::vector &value, FlagsType, reader &in) + { + return SerialDetail::ReadRepeated(wire_type, std::back_inserter(value), in); + } + }; + + template struct Serializer> { + template + static void Serialize(uint32_t tag, const std::optional &value, FlagsType, Writer &out) + { + if (!value.has_value()) { + return; + } + Serializer::Serialize(tag, *value, FlagsType(), out); + } + + template + static bool Parse(WireType wire_type, std::optional &value, FlagsType, reader &in) + { + return Serializer::Parse(wire_type, value.emplace(), FlagsType(), in); + } + }; + + template struct Serializer> { + template + static void SerializeOneof(uint32_t tag, const std::variant &value, FlagsType, Writer &out) + { + if (value.index() != Index) + return; + + Serializer>>::Serialize( + tag, std::get(value), FlagsType(), out); + } + + template + static bool ParseOneof(WireType wire_type, std::variant &value, FlagsType, reader &in) + { + return Serializer>>::Parse( + wire_type, value.template emplace(), FlagsType(), in); + } + }; + + template struct Serializer> { + template + static void SerializeMap( + uint32_t tag, const std::map &value, FlagsType, FlagsType, Writer &out) + { + SerialDetail::WriteMap(tag, value, out); + } + + template + static bool ParseMap( + WireType wire_type, std::map &value, FlagsType, FlagsType, reader &in) + { + return SerialDetail::ReadMap(wire_type, value, in); + } + }; + + struct StringWriter : public Writer { + StringWriter(std::string &out) + : _out(out) + { + } + + void Write(const void *bytes, size_t size) override + { + _out.append(reinterpret_cast(bytes), size); + } + + private: + std::string &_out; + }; + + struct StringReader : public reader { + StringReader(const std::string &in) + : _in(in), _pos(0) + { + } + + size_t Read(void *bytes, size_t size) override + { + size_t readSize = std::min(size, _in.size() - _pos); + if (memcpy_s(bytes, size, _in.data() + _pos, readSize) != EOK) { + return readSize; + } + _pos += readSize; + return readSize; + } + + private: + const std::string &_in; + size_t _pos; + }; + // mytype begin, just support base type, but really use protobuf raw type(uint32) + template<> struct Serializer { + static void Serialize(uint32_t tag, uint8_t value, FlagsType<>, Writer &out, bool force = false) + { + Serializer::Serialize(tag, value, FlagsType(), out, force); + } + + static void SerializePacked(uint8_t value, FlagsType<>, Writer &out) + { + Serializer::SerializePacked(value, FlagsType(), out); + } + + static bool Parse(WireType wire_type, uint8_t &value, FlagsType<>, reader &in) + { + uint32_t intermedaite_value; + if (Serializer::Parse(wire_type, intermedaite_value, FlagsType<>(), in)) { + value = static_cast(intermedaite_value); + return true; + } + return false; + } + + static bool ParsePacked(uint8_t &value, FlagsType<>, reader &in) + { + uint32_t intermedaite_value; + if (Serializer::ParsePacked(intermedaite_value, FlagsType<>(), in)) { + value = static_cast(intermedaite_value); + return true; + } + return false; + } + }; + template<> struct Serializer { + static void Serialize(uint32_t tag, uint16_t value, FlagsType<>, Writer &out, bool force = false) + { + Serializer::Serialize(tag, value, FlagsType(), out, force); + } + + static void SerializePacked(uint16_t value, FlagsType<>, Writer &out) + { + Serializer::SerializePacked(value, FlagsType(), out); + } + + static bool Parse(WireType wire_type, uint16_t &value, FlagsType<>, reader &in) + { + uint32_t intermedaite_value; + if (Serializer::Parse(wire_type, intermedaite_value, FlagsType<>(), in)) { + value = static_cast(intermedaite_value); + return true; + } + return false; + } + + static bool ParsePacked(uint16_t &value, FlagsType<>, reader &in) + { + uint32_t intermedaite_value; + if (Serializer::ParsePacked(intermedaite_value, FlagsType<>(), in)) { + value = static_cast(intermedaite_value); + return true; + } + return false; + } + }; + // mytype finish + + template std::string SerializeToString(const T &value) + { + std::string out; + StringWriter stringOut(out); + SerialDetail::WriteMessage(value, MessageType(), stringOut); + return out; + } + + template bool ParseFromString(T &value, const std::string &in) + { + StringReader stringIn(in); + return SerialDetail::ReadMessage(value, MessageType(), stringIn); + } +} +// clang-format on +} // Hdc +#endif // HDC_SERIAL_STRUCT_DEFINE_H diff --git a/src/common/session.cpp b/src/common/session.cpp old mode 100644 new mode 100755 diff --git a/src/common/session.h b/src/common/session.h old mode 100644 new mode 100755 diff --git a/src/common/task.cpp b/src/common/task.cpp old mode 100644 new mode 100755 diff --git a/src/common/task.h b/src/common/task.h old mode 100644 new mode 100755 diff --git a/src/common/tcp.cpp b/src/common/tcp.cpp old mode 100644 new mode 100755 diff --git a/src/common/tcp.h b/src/common/tcp.h old mode 100644 new mode 100755 diff --git a/src/common/transfer.cpp b/src/common/transfer.cpp old mode 100644 new mode 100755 diff --git a/src/common/transfer.h b/src/common/transfer.h old mode 100644 new mode 100755 diff --git a/src/common/usb.cpp b/src/common/usb.cpp old mode 100644 new mode 100755 diff --git a/src/common/usb.h b/src/common/usb.h old mode 100644 new mode 100755 diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp old mode 100644 new mode 100755 diff --git a/src/daemon/daemon.h b/src/daemon/daemon.h old mode 100644 new mode 100755 diff --git a/src/daemon/daemon_app.cpp b/src/daemon/daemon_app.cpp old mode 100644 new mode 100755 diff --git a/src/daemon/daemon_app.h b/src/daemon/daemon_app.h old mode 100644 new mode 100755 diff --git a/src/daemon/daemon_common.h b/src/daemon/daemon_common.h old mode 100644 new mode 100755 diff --git a/src/daemon/daemon_forward.cpp b/src/daemon/daemon_forward.cpp old mode 100644 new mode 100755 diff --git a/src/daemon/daemon_forward.h b/src/daemon/daemon_forward.h old mode 100644 new mode 100755 diff --git a/src/daemon/daemon_tcp.cpp b/src/daemon/daemon_tcp.cpp old mode 100644 new mode 100755 diff --git a/src/daemon/daemon_tcp.h b/src/daemon/daemon_tcp.h old mode 100644 new mode 100755 diff --git a/src/daemon/daemon_unity.cpp b/src/daemon/daemon_unity.cpp old mode 100644 new mode 100755 diff --git a/src/daemon/daemon_unity.h b/src/daemon/daemon_unity.h old mode 100644 new mode 100755 diff --git a/src/daemon/daemon_usb.cpp b/src/daemon/daemon_usb.cpp old mode 100644 new mode 100755 diff --git a/src/daemon/daemon_usb.h b/src/daemon/daemon_usb.h old mode 100644 new mode 100755 diff --git a/src/daemon/jdwp.cpp b/src/daemon/jdwp.cpp old mode 100644 new mode 100755 diff --git a/src/daemon/jdwp.h b/src/daemon/jdwp.h old mode 100644 new mode 100755 diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp old mode 100644 new mode 100755 diff --git a/src/daemon/shell.cpp b/src/daemon/shell.cpp old mode 100644 new mode 100755 diff --git a/src/daemon/shell.h b/src/daemon/shell.h old mode 100644 new mode 100755 diff --git a/src/daemon/usb_ffs.h b/src/daemon/usb_ffs.h old mode 100644 new mode 100755 diff --git a/src/host/client.cpp b/src/host/client.cpp old mode 100644 new mode 100755 diff --git a/src/host/client.h b/src/host/client.h old mode 100644 new mode 100755 diff --git a/src/host/host_app.cpp b/src/host/host_app.cpp old mode 100644 new mode 100755 diff --git a/src/host/host_app.h b/src/host/host_app.h old mode 100644 new mode 100755 diff --git a/src/host/host_common.h b/src/host/host_common.h old mode 100644 new mode 100755 diff --git a/src/host/host_forward.cpp b/src/host/host_forward.cpp old mode 100644 new mode 100755 diff --git a/src/host/host_forward.h b/src/host/host_forward.h old mode 100644 new mode 100755 diff --git a/src/host/host_tcp.cpp b/src/host/host_tcp.cpp old mode 100644 new mode 100755 diff --git a/src/host/host_tcp.h b/src/host/host_tcp.h old mode 100644 new mode 100755 diff --git a/src/host/host_unity.cpp b/src/host/host_unity.cpp old mode 100644 new mode 100755 diff --git a/src/host/host_unity.h b/src/host/host_unity.h old mode 100644 new mode 100755 diff --git a/src/host/host_usb.cpp b/src/host/host_usb.cpp old mode 100644 new mode 100755 diff --git a/src/host/host_usb.h b/src/host/host_usb.h old mode 100644 new mode 100755 diff --git a/src/host/main.cpp b/src/host/main.cpp old mode 100644 new mode 100755 diff --git a/src/host/server.cpp b/src/host/server.cpp old mode 100644 new mode 100755 index a276e0157b7a817ade341f02af8bf96588658865..e9e967a3f9f61e6dc64512389ca0f453fc347843 --- a/src/host/server.cpp +++ b/src/host/server.cpp @@ -211,6 +211,26 @@ string HdcServer::GetDaemonMapList(uint8_t opType) return ret; } +void HdcServer::GetDaemonMapOnlyOne(HDaemonInfo &hDaemonInfoInOut) +{ + uv_rwlock_rdlock(&daemonAdmin); + string key; + for (auto &i : mapDaemon) { + if (i.second->connStatus == STATUS_CONNECTED) { + if (key == STRING_EMPTY) { + key = i.first; + } else { + key = STRING_EMPTY; + break; + } + } + } + if (key.size() > 0) { + hDaemonInfoInOut = mapDaemon[key]; + } + uv_rwlock_rdunlock(&daemonAdmin); +} + string HdcServer::AdminDaemonMap(uint8_t opType, const string &connectKey, HDaemonInfo &hDaemonInfoInOut) { string sRet; @@ -261,22 +281,7 @@ string HdcServer::AdminDaemonMap(uint8_t opType, const string &connectKey, HDaem break; } case OP_GET_ONLY: { - uv_rwlock_rdlock(&daemonAdmin); - string key; - for (auto &i : mapDaemon) { - if (i.second->connStatus == STATUS_CONNECTED) { - if (key == STRING_EMPTY) { - key = i.first; - } else { - key = STRING_EMPTY; - break; - } - } - } - if (key.size() > 0) { - hDaemonInfoInOut = mapDaemon[key]; - } - uv_rwlock_rdunlock(&daemonAdmin); + GetDaemonMapOnlyOne(hDaemonInfoInOut); break; } case OP_UPDATE: { // Cannot update the Object HDi lower key value by direct value diff --git a/src/host/server.h b/src/host/server.h old mode 100644 new mode 100755 index 7e9319acb9279a6766a481094005276ffea844d4..47911530f0ca2811d20359e4c4026013bbcee751 --- a/src/host/server.h +++ b/src/host/server.h @@ -52,6 +52,7 @@ private: bool HandServerAuth(HSession hSession, SessionHandShake &handshake); string GetDaemonMapList(uint8_t opType); bool ServerSessionHandshake(HSession hSession, uint8_t *payload, int payloadSize); + void GetDaemonMapOnlyOne(HDaemonInfo &hDaemonInfoInOut); uv_rwlock_t daemonAdmin; map mapDaemon; diff --git a/src/host/server_for_client.cpp b/src/host/server_for_client.cpp old mode 100644 new mode 100755 diff --git a/src/host/server_for_client.h b/src/host/server_for_client.h old mode 100644 new mode 100755 diff --git a/src/host/translate.cpp b/src/host/translate.cpp old mode 100644 new mode 100755 diff --git a/src/host/translate.h b/src/host/translate.h old mode 100644 new mode 100755 diff --git a/src/test/hdc_runtime_command.cpp b/src/test/hdc_runtime_command.cpp old mode 100644 new mode 100755 index 98f4fa782c0e3301bfb514d1a40a24ecffc3b0dd..f9368299c7d51176e387e32116ef2ff19dc05ad7 --- a/src/test/hdc_runtime_command.cpp +++ b/src/test/hdc_runtime_command.cpp @@ -112,13 +112,14 @@ int TestTaskCommand(int method, const string &debugServerPort, const string &deb "install /d/a.hap /mnt/hgfs/vtmp/b.hap /mnt/hgfs/vtmp -lrtsdpg"); // hap break; case UT_TEST_TMP: + TestRunClient(debugServerPort, debugConnectKey, "shell pwd"); +#ifdef DEF_NULL while (true) { uv_sleep(50); TestRunClient(debugServerPort, debugConnectKey, "list targets"); TestRunClient(debugServerPort, debugConnectKey, "shell id"); TestRunClient(debugServerPort, debugConnectKey, "shell bm dump -a"); } -#ifdef DEF_NULL TestRunClient(debugServerPort, debugConnectKey, "install /d/helloworld.hap"); TestRunClient(debugServerPort, debugConnectKey, "target mount"); TestRunClient(debugServerPort, debugConnectKey, "shell pwd"); diff --git a/src/test/hdc_runtime_command.h b/src/test/hdc_runtime_command.h old mode 100644 new mode 100755 diff --git a/src/test/hdc_runtime_frame.cpp b/src/test/hdc_runtime_frame.cpp old mode 100644 new mode 100755 diff --git a/src/test/hdc_runtime_frame.h b/src/test/hdc_runtime_frame.h old mode 100644 new mode 100755 diff --git a/src/test/main.cpp b/src/test/main.cpp old mode 100644 new mode 100755 diff --git a/src/test/ut_common.h b/src/test/ut_common.h old mode 100644 new mode 100755