Skip to content

Commit

Permalink
Merge pull request #1333 from Barenboim/master
Browse files Browse the repository at this point in the history
Add TLVMessage.
  • Loading branch information
Barenboim authored Jul 26, 2023
2 parents 578d60b + 930fe5f commit c618185
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 0 deletions.
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions CMakeLists_Headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/include/workflow/TLVMessage.h
1 change: 1 addition & 0 deletions src/protocol/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(SRC
http_parser.c
HttpMessage.cc
HttpUtil.cc
TLVMessage.cc
)

if (NOT MYSQL STREQUAL "n")
Expand Down
84 changes: 84 additions & 0 deletions src/protocol/TLVMessage.cc
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
*/

#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 changes: 69 additions & 0 deletions src/protocol/TLVMessage.h
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
*/

#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

0 comments on commit c618185

Please sign in to comment.