Add TLVMessage.

This commit is contained in:
Xie Han
2023-07-26 22:05:56 +08:00
parent 4dd82db8ca
commit 930fe5f0fa
6 changed files with 157 additions and 0 deletions

1
BUILD
View File

@@ -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',

View File

@@ -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

View File

@@ -0,0 +1 @@
../../protocol/TLVMessage.h

View File

@@ -10,6 +10,7 @@ set(SRC
http_parser.c
HttpMessage.cc
HttpUtil.cc
TLVMessage.cc
)
if (NOT MYSQL STREQUAL "n")

View File

@@ -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 <stdint.h>
#include <string.h>
#include <arpa/inet.h>
#include <string>
#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;
}
}

69
src/protocol/TLVMessage.h Normal file
View File

@@ -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 <stdint.h>
#include <utility>
#include <string>
#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