-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1333 from Barenboim/master
Add TLVMessage.
- Loading branch information
Showing
6 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../protocol/TLVMessage.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ set(SRC | |
http_parser.c | ||
HttpMessage.cc | ||
HttpUtil.cc | ||
TLVMessage.cc | ||
) | ||
|
||
if (NOT MYSQL STREQUAL "n") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|