代码拉取完成,页面将自动刷新
From ce029db530429609847b38ae7bae2428064e3a27 Mon Sep 17 00:00:00 2001
From: hurricane618 <hurricane618@hotmail.com>
Date: Mon, 4 Dec 2023 10:06:55 +0800
Subject: [PATCH 5/6] add nullptr check in Subscribe
fix leak of nullptr problem
Signed-off-by: hurricane618 <hurricane618@hotmail.com>
---
observer_agent/grpc_comm/client.cpp | 10 ++++++++--
observer_agent/grpc_comm/server.cpp | 15 ++++++++-------
2 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/observer_agent/grpc_comm/client.cpp b/observer_agent/grpc_comm/client.cpp
index ecb54ae..84b5c96 100644
--- a/observer_agent/grpc_comm/client.cpp
+++ b/observer_agent/grpc_comm/client.cpp
@@ -43,16 +43,22 @@ std::unique_ptr<ClientReader<Message>> PubSubClient::Subscribe(const int topic)
SubscribeRequest request;
request.set_topic(topic);
request.set_sub_name(uuid_str);
+ std::string ret_info;
Message msg;
SubFlag = true;
std::unique_ptr<ClientReader<Message>> reader = stub_->Subscribe(&context, request);
- if (reader == nullptr)
+ ret_info = ReadFrom(reader);
+
+ if (ret_info.substr(0, 6) == "topic:")
+ {
+ std::cout << "Success subscribe." << std::endl;
+ return reader;
+ } else
{
std::cerr << "Failed to subscribe." << std::endl;
return nullptr;
}
- return reader;
}
void PubSubClient::Publish(const int topic, const std::string &content)
diff --git a/observer_agent/grpc_comm/server.cpp b/observer_agent/grpc_comm/server.cpp
index 54ae66f..d53866f 100644
--- a/observer_agent/grpc_comm/server.cpp
+++ b/observer_agent/grpc_comm/server.cpp
@@ -86,6 +86,14 @@ class PubSubServiceImpl final : public SubManager::Service
return grpc::Status(grpc::StatusCode::INTERNAL, "multi-process max connection number, Failed to Subscribe the topic");
}
+ msg.set_text("topic: " + std::to_string(cli_topic) + " Subscribe success!");
+ if (!writer->Write(msg))
+ {
+ std::cerr << "Failed to write the initial message" << std::endl;
+ sub_mutex.unlock();
+ return grpc::Status(grpc::StatusCode::INTERNAL, "Failed to write the message");
+ }
+
suber_topic_[cli_name].push_back(cli_topic);
suber_writer_[cli_name].push_back(writer);
suber_connection_[cli_name].push_back(tmp_index);
@@ -94,13 +102,6 @@ class PubSubServiceImpl final : public SubManager::Service
sub_mutex.unlock();
- msg.set_text("topic: " + std::to_string(cli_topic) + " Subscribe success!");
- if (!writer->Write(msg))
- {
- std::cerr << "Failed to write the initial message" << std::endl;
- return grpc::Status(grpc::StatusCode::INTERNAL, "Failed to write the message");
- }
-
keepalive_msg.set_text("keepalive");
while (connect_status[tmp_index])
{
--
2.33.0
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。