From e02826ca10f372fc4dc840f1bdfe6f02e56066c3 Mon Sep 17 00:00:00 2001 From: XieHan Date: Tue, 12 Apr 2022 21:13:51 +0800 Subject: [PATCH] add PackageWrapper for MySQL Binlog --- BUILD | 1 + CMakeLists_Headers.txt | 1 + src/include/workflow/PackageWrapper.h | 1 + src/protocol/CMakeLists.txt | 3 +- src/protocol/PackageWrapper.cc | 66 +++++++++++++++++++++++++++ src/protocol/PackageWrapper.h | 48 +++++++++++++++++++ 6 files changed, 119 insertions(+), 1 deletion(-) create mode 120000 src/include/workflow/PackageWrapper.h create mode 100644 src/protocol/PackageWrapper.cc create mode 100644 src/protocol/PackageWrapper.h diff --git a/BUILD b/BUILD index 7f0c96c6..3761536d 100644 --- a/BUILD +++ b/BUILD @@ -55,6 +55,7 @@ cc_library( 'src/protocol/DnsMessage.cc', 'src/protocol/DnsUtil.cc', 'src/protocol/SSLWrapper.cc', + 'src/protocol/PackageWrapper.cc', 'src/protocol/dns_parser.c', 'src/server/WFServer.cc', 'src/kernel/CommRequest.cc', diff --git a/CMakeLists_Headers.txt b/CMakeLists_Headers.txt index 8cd7f564..fa7493f5 100644 --- a/CMakeLists_Headers.txt +++ b/CMakeLists_Headers.txt @@ -50,6 +50,7 @@ set(INCLUDE_HEADERS src/protocol/mysql_parser.h src/protocol/mysql_types.h src/protocol/mysql_byteorder.h + src/protocol/PackageWrapper.h src/protocol/SSLWrapper.h src/protocol/dns_parser.h src/protocol/DnsMessage.h diff --git a/src/include/workflow/PackageWrapper.h b/src/include/workflow/PackageWrapper.h new file mode 120000 index 00000000..f4031f30 --- /dev/null +++ b/src/include/workflow/PackageWrapper.h @@ -0,0 +1 @@ +../../protocol/PackageWrapper.h \ No newline at end of file diff --git a/src/protocol/CMakeLists.txt b/src/protocol/CMakeLists.txt index afe8ed3c..4ba6b7f9 100644 --- a/src/protocol/CMakeLists.txt +++ b/src/protocol/CMakeLists.txt @@ -2,10 +2,11 @@ cmake_minimum_required(VERSION 3.6) project(protocol) set(SRC + PackageWrapper.cc + SSLWrapper.cc dns_parser.c DnsMessage.cc DnsUtil.cc - SSLWrapper.cc http_parser.c HttpMessage.cc HttpUtil.cc diff --git a/src/protocol/PackageWrapper.cc b/src/protocol/PackageWrapper.cc new file mode 100644 index 00000000..4f4f1805 --- /dev/null +++ b/src/protocol/PackageWrapper.cc @@ -0,0 +1,66 @@ +/* + Copyright (c) 2022 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 "PackageWrapper.h" + +namespace protocol +{ + +int PackageWrapper::encode(struct iovec vectors[], int max) +{ + int cnt = 0; + int ret; + + while (max >= 8) + { + ret = this->ProtocolWrapper::encode(vectors, max); + if ((unsigned int)ret > (unsigned int)max) + { + if (ret < 0) + return ret; + + break; + } + + cnt += ret; + this->msg = this->next(this->msg); + if (!this->msg) + return cnt; + + vectors += ret; + max -= ret; + } + + errno = EOVERFLOW; + return -1; +} + +int PackageWrapper::append(const void *buf, size_t *size) +{ + int ret = this->ProtocolWrapper::append(buf, size); + + if (ret <= 0) + return ret; + + this->msg = this->next(this->msg); + return !this->msg; +} + +} + diff --git a/src/protocol/PackageWrapper.h b/src/protocol/PackageWrapper.h new file mode 100644 index 00000000..861eb4d0 --- /dev/null +++ b/src/protocol/PackageWrapper.h @@ -0,0 +1,48 @@ +/* + Copyright (c) 2022 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 _PACKAGEWRAPPER_H_ +#define _PACKAGEWRAPPER_H_ + +#include "ProtocolMessage.h" + +namespace protocol +{ + +class PackageWrapper : public ProtocolWrapper +{ +private: + virtual ProtocolMessage *next(ProtocolMessage *msg) + { + return NULL; + } + +protected: + virtual int encode(struct iovec vectors[], int max); + virtual int append(const void *buf, size_t *size); + +public: + PackageWrapper(ProtocolMessage *msg) : ProtocolWrapper(msg) + { + } +}; + +} + +#endif +