From 930fe5f0faf59cbef67537da046f3cd1eea5a1a9 Mon Sep 17 00:00:00 2001 From: Xie Han <63350856@qq.com> Date: Wed, 26 Jul 2023 22:05:56 +0800 Subject: [PATCH] Add TLVMessage. --- BUILD | 1 + CMakeLists_Headers.txt | 1 + src/include/workflow/TLVMessage.h | 1 + src/protocol/CMakeLists.txt | 1 + src/protocol/TLVMessage.cc | 84 +++++++++++++++++++++++++++++++ src/protocol/TLVMessage.h | 69 +++++++++++++++++++++++++ 6 files changed, 157 insertions(+) create mode 120000 src/include/workflow/TLVMessage.h create mode 100644 src/protocol/TLVMessage.cc create mode 100644 src/protocol/TLVMessage.h diff --git a/BUILD b/BUILD index 8ffc1bf97f..b66139d0fc 100644 --- a/BUILD +++ b/BUILD @@ -52,6 +52,7 @@ cc_library( 'src/manager/WFGlobal.cc', 'src/nameservice/WFDnsResolver.cc', 'src/nameservice/WFNameService.cc', + 'src/protocol/TLVMessage.cc', 'src/protocol/DnsMessage.cc', 'src/protocol/DnsUtil.cc', 'src/protocol/SSLWrapper.cc', diff --git a/CMakeLists_Headers.txt b/CMakeLists_Headers.txt index a0f6e36e71..95fa6c781a 100644 --- a/CMakeLists_Headers.txt +++ b/CMakeLists_Headers.txt @@ -54,6 +54,7 @@ set(INCLUDE_HEADERS src/protocol/dns_parser.h src/protocol/DnsMessage.h src/protocol/DnsUtil.h + src/protocol/TLVMessage.h src/protocol/ConsulDataTypes.h src/server/WFServer.h src/server/WFDnsServer.h diff --git a/src/include/workflow/TLVMessage.h b/src/include/workflow/TLVMessage.h new file mode 120000 index 0000000000..a9cf834927 --- /dev/null +++ b/src/include/workflow/TLVMessage.h @@ -0,0 +1 @@ +../../protocol/TLVMessage.h \ No newline at end of file diff --git a/src/protocol/CMakeLists.txt b/src/protocol/CMakeLists.txt index 1ff93c5ec8..6e53a11f87 100644 --- a/src/protocol/CMakeLists.txt +++ b/src/protocol/CMakeLists.txt @@ -10,6 +10,7 @@ set(SRC http_parser.c HttpMessage.cc HttpUtil.cc + TLVMessage.cc ) if (NOT MYSQL STREQUAL "n") diff --git a/src/protocol/TLVMessage.cc b/src/protocol/TLVMessage.cc new file mode 100644 index 0000000000..26a3ef3fd0 --- /dev/null +++ b/src/protocol/TLVMessage.cc @@ -0,0 +1,84 @@ +/* + Copyright (c) 2023 Sogou, Inc. + + 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. + + Author: Xie Han (xiehan@sogou-inc.com) +*/ + +#include +#include +#include +#include +#include "TLVMessage.h" + +namespace protocol +{ + +int TLVMessage::encode(struct iovec vectors[], int max) +{ + this->head[0] = htonl((uint32_t)this->type); + this->head[1] = htonl(this->value.size()); + + vectors[0].iov_base = this->head; + vectors[0].iov_len = 8; + vectors[1].iov_base = (char *)this->value.c_str(); + vectors[1].iov_len = this->value.size(); + return 2; +} + +int TLVMessage::append(const void *buf, size_t *size) +{ + size_t n = *size; + size_t head_left; + + head_left = 8 - this->head_received; + if (head_left > 0) + { + void *p = (char *)this->head + this->head_received; + + if (n < head_left) + { + memcpy(p, buf, n); + this->head_received += n; + return 0; + } + + memcpy(p, buf, head_left); + this->head_received = 8; + buf = (const char *)buf + head_left; + n -= head_left; + + this->type = (int)ntohl(this->head[0]); + *this->head = ntohl(this->head[1]); + if (*this->head > this->size_limit) + { + errno = EMSGSIZE; + return -1; + } + + this->value.reserve(*this->head); + } + + if (this->value.size() + n > *this->head) + { + n = *this->head - this->value.size(); + *size = n + head_left; + } + + this->value.append((const char *)buf, (const char *)buf + n); + return this->value.size() == *this->head; +} + +} + diff --git a/src/protocol/TLVMessage.h b/src/protocol/TLVMessage.h new file mode 100644 index 0000000000..f78159a685 --- /dev/null +++ b/src/protocol/TLVMessage.h @@ -0,0 +1,69 @@ +/* + Copyright (c) 2023 Sogou, Inc. + + 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. + + Author: Xie Han (xiehan@sogou-inc.com) +*/ + +#ifndef _TLVMESSAGE_H_ +#define _TLVMESSAGE_H_ + +#include +#include +#include +#include "ProtocolMessage.h" + +namespace protocol +{ + +class TLVMessage : public ProtocolMessage +{ +public: + int get_type() const { return this->type; } + void set_type(int type) { this->type = type; } + + std::string *get_value() { return &this->value; } + void set_value(std::string value) { this->value = std::move(value); } + +protected: + virtual int encode(struct iovec vectors[], int max); + virtual int append(const void *buf, size_t *size); + +protected: + int type; + std::string value; + +private: + uint32_t head[2]; + size_t head_received; + +public: + TLVMessage() + { + this->type = 0; + this->head_received = 0; + } + +public: + TLVMessage(TLVMessage&& msg) = default; + TLVMessage& operator = (TLVMessage&& msg) = default; +}; + +using TLVRequest = TLVMessage; +using TLVResponse = TLVMessage; + +} + +#endif +