Skip to content

Commit

Permalink
Protobuf 3.0 compatibility
Browse files Browse the repository at this point in the history
Protobuf 3.0 does not support `optional` keyword so emulate it with `oneof`
statement.

Also it have a bug with unused function argument what requires
`-Wno-unused-parameter` to be enabled.

Relates #178
  • Loading branch information
GeorgyKirichenko committed May 30, 2024
1 parent df2247e commit 97ddeb9
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 31 deletions.
19 changes: 10 additions & 9 deletions cli/balancer.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,14 @@ inline void setip(common::icp_proto::IPAddr* pAddr, const common::ip_address_t&

inline common::ip_address_t convert_to_ip_address(const common::icp_proto::IPAddr& proto_ipaddr)
{
if (proto_ipaddr.has_ipv4())
switch (proto_ipaddr.addr_case())
{
return common::ipv4_address_t(proto_ipaddr.ipv4());
}
else
{
return common::ipv6_address_t((uint8_t*)proto_ipaddr.ipv6().data());
case common::icp_proto::IPAddr::AddrCase::kIpv4:
return common::ipv4_address_t(proto_ipaddr.ipv4());
case common::icp_proto::IPAddr::AddrCase::kIpv6:
return common::ipv6_address_t((uint8_t*)proto_ipaddr.ipv6().data());
default:
throw std::string("internal error: address type is not set");
}
}

Expand Down Expand Up @@ -250,16 +251,16 @@ void real_find(std::string module_string,
table.insert(balancer.module(),
virtual_ip,
proto_string,
service.key().has_port() ? std::make_optional(service.key().port()) : std::nullopt,
service.key().port_opt_case() == common::icp_proto::BalancerRealFindResponse_ServiceKey::PortOptCase::kPort ? std::make_optional(service.key().port()) : std::nullopt,
service.scheduler(),
real_ip,
real.has_port() ? std::make_optional(real.port()) : std::nullopt,
real.port_opt_case() == common::icp_proto::BalancerRealFindResponse_Real::PortOptCase::kPort ? std::make_optional(real.port()) : std::nullopt,
real.enabled(),
real.weight(),
connections,
real.packets(),
real.bytes(),
service.has_version() ? std::make_optional(service.version()) : std::nullopt);
service.version_opt_case() == common::icp_proto::BalancerRealFindResponse_Service::VersionOptCase::kVersion ? std::make_optional(service.version()) : std::nullopt);
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion common/sendrecv.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ static int send(int clientSocket, const Req& request)
{
if constexpr (std::is_convertible_v<Req&, ::google::protobuf::Message&>)
{
auto size = request.ByteSizeLong();
#if GOOGLE_PROTOBUF_VERSION < 3001000
uint64_t size = request.ByteSize();
#else
uint64_t size = request.ByteSizeLong();
#endif
std::vector<char> buf(size);

if (!request.SerializeToArray(buf.data(), size))
Expand All @@ -87,7 +91,11 @@ static int send(int clientSocket, Req&& request)
{
if constexpr (std::is_convertible_v<Req&, ::google::protobuf::Message&>)
{
#if GOOGLE_PROTOBUF_VERSION < 3001000
uint64_t size = request.ByteSize();
#else
uint64_t size = request.ByteSizeLong();
#endif
std::vector<char> buf(size);

if (!request.SerializeToArray(buf.data(), size))
Expand Down
23 changes: 12 additions & 11 deletions controlplane/balancer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ inline void setip(common::icp_proto::IPAddr* pAddr, const ip_address_t& value)

inline common::ip_address_t convert_to_ip_address(const common::icp_proto::IPAddr& proto_ipaddr)
{
if (proto_ipaddr.has_ipv4())
switch (proto_ipaddr.addr_case())
{
return common::ipv4_address_t(proto_ipaddr.ipv4());
}
else
{
return common::ipv6_address_t((uint8_t*)proto_ipaddr.ipv6().data());
case common::icp_proto::IPAddr::AddrCase::kIpv4:
return common::ipv4_address_t(proto_ipaddr.ipv4());
case common::icp_proto::IPAddr::AddrCase::kIpv6:
return common::ipv6_address_t((uint8_t*)proto_ipaddr.ipv6().data());
default:
throw std::string("internal error: address type is not set");
}
}

Expand All @@ -89,9 +90,9 @@ void balancer_t::RealFind(
auto response = balancer_real_find({!req->module().empty() ? std::optional<std::string>{req->module()} : std::nullopt,
req->has_virtual_ip() ? std::optional<common::ip_address_t>{convert_to_ip_address(req->virtual_ip())} : std::nullopt,
req->proto() != common::icp_proto::NetProto::undefined ? std::optional<uint8_t>{req->proto() == common::icp_proto::NetProto::tcp ? IPPROTO_TCP : IPPROTO_UDP} : std::nullopt,
req->has_virtual_port() ? std::optional<uint16_t>{req->virtual_port()} : std::nullopt,
req->virtual_port_opt_case() == common::icp_proto::BalancerRealFindRequest::VirtualPortOptCase::kVirtualPort ? std::optional<uint16_t>{req->virtual_port()} : std::nullopt,
req->has_real_ip() ? std::optional<common::ip_address_t>{convert_to_ip_address(req->real_ip())} : std::nullopt,
req->has_real_port() ? std::optional<uint16_t>{req->real_port()} : std::nullopt});
req->real_port_opt_case() == common::icp_proto::BalancerRealFindRequest::RealPortOptCase::kRealPort ? std::optional<uint16_t>{req->real_port()} : std::nullopt});

for (const auto& [key, value] : response)
{
Expand Down Expand Up @@ -161,11 +162,11 @@ void balancer_t::Real(
request.push_back({real.module(),
convert_to_ip_address(real.virtual_ip()),
real.proto() == common::icp_proto::NetProto::tcp ? IPPROTO_TCP : IPPROTO_UDP,
real.has_virtual_port() ? std::make_optional(real.virtual_port()) : std::nullopt,
real.virtual_port_opt_case() == common::icp_proto::BalancerRealRequest_Real::VirtualPortOptCase::kVirtualPort ? std::make_optional(real.virtual_port()) : std::nullopt,
convert_to_ip_address(real.real_ip()),
real.has_real_port() ? std::make_optional(real.real_port()) : std::nullopt,
real.real_port_opt_case() == common::icp_proto::BalancerRealRequest_Real::RealPortOptCase::kRealPort ? std::make_optional(real.real_port()) : std::nullopt,
real.enable(),
real.has_weight() ? std::make_optional(real.weight()) : std::nullopt});
real.weight_opt_case() == common::icp_proto::BalancerRealRequest_Real::WeightOptCase::kWeight ? std::make_optional(real.weight()) : std::nullopt});
}

balancer_real(request);
Expand Down
32 changes: 24 additions & 8 deletions libprotobuf/controlplane.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@ message BalancerRealFindRequest {
string module = 1;
IPAddr virtual_ip = 2;
NetProto proto = 3;
optional uint32 virtual_port = 4;
oneof virtual_port_opt {
uint32 virtual_port = 4;
}
IPAddr real_ip = 5;
optional uint32 real_port = 6;
oneof real_port_opt {
uint32 real_port = 6;
}
}

message BalancerRealFindResponse {
message Real {
IPAddr ip = 1;
optional uint32 port = 2;
oneof port_opt {
uint32 port = 2;
}
bool enabled = 3;
uint32 weight = 4;
uint64 connections = 5;
Expand All @@ -46,13 +52,17 @@ message BalancerRealFindResponse {
message ServiceKey{
IPAddr ip = 1;
NetProto proto = 2;
optional uint32 port = 3;
oneof port_opt {
uint32 port = 3;
}
}
message Service {
ServiceKey key = 1;
string scheduler = 2;
repeated Real reals = 3;
optional string version = 4;
oneof version_opt {
string version = 4;
}
}
message BalancerData {
uint32 balancer_id = 1;
Expand All @@ -68,11 +78,17 @@ message BalancerRealRequest {
string module = 1;
IPAddr virtual_ip = 2;
NetProto proto = 3;
optional uint32 virtual_port = 4;
oneof virtual_port_opt {
uint32 virtual_port = 4;
}
IPAddr real_ip = 5;
optional uint32 real_port = 6;
oneof real_port_opt {
uint32 real_port = 6;
}
bool enable = 7;
optional uint32 weight = 8;
oneof weight_opt {
uint32 weight = 8;
}
}
repeated Real reals = 1;
}
Expand Down
8 changes: 6 additions & 2 deletions libprotobuf/meta.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ syntax = "proto3";
package common.proto;

message RpcMeta {
optional string service_name = 1;
optional string method_name = 2;
oneof service_name_ {
string service_name = 1;
}
oneof method_name_ {
string method_name = 2;
}
}
4 changes: 4 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ add_global_arguments('-DYANET_VERSION_REVISION=' + get_option('version_revision'
add_global_arguments('-DYANET_VERSION_HASH=' + get_option('version_hash'), language: 'cpp')
add_global_arguments('-DYANET_VERSION_CUSTOM=' + get_option('version_custom'), language: 'cpp')

add_global_arguments('-DGOOGLE_PROTOBUF_NO_RTTI', language: 'cpp')

add_global_arguments('-Wno-unused-parameter', language: 'cpp')

libdpdk = subproject('dpdk', default_options: [
'platform=generic',
'cpu_instruction_set=corei7',
Expand Down

0 comments on commit 97ddeb9

Please sign in to comment.