From cab831748a2a9cc18b7f18f3b5e14a4374b7ab68 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期一, 17 五月 2021 18:34:26 +0800
Subject: [PATCH] socket send using abs addr, avoid shm find by id.
---
src/bh_api.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 48 insertions(+), 4 deletions(-)
diff --git a/src/bh_api.cpp b/src/bh_api.cpp
index efa278e..ca7249d 100644
--- a/src/bh_api.cpp
+++ b/src/bh_api.cpp
@@ -1,6 +1,7 @@
#include "bh_api.h"
#include "defs.h"
#include "topic_node.h"
+#include <cstdio>
#include <memory>
using namespace bhome_shm;
@@ -8,11 +9,47 @@
namespace
{
+std::string GetProcExe()
+{
+ auto f = fopen("/proc/self/stat", "rb");
+ if (f) {
+ DEFER1(fclose(f));
+ char buf[100] = {0};
+ int n = fread(buf, 1, sizeof(buf), f);
+ if (n > 0) {
+ std::string s(buf, n);
+ auto start = s.find('(');
+ if (start != std::string::npos) {
+ ++start;
+ auto end = s.find(')', start);
+ return s.substr(start, end - start);
+ }
+ }
+ }
+ return std::to_string(getpid());
+}
+std::unique_ptr<TopicNode> &ProcNodePtr()
+{
+ static std::mutex mtx;
+ std::lock_guard<std::mutex> lk(mtx);
+
+ static std::unique_ptr<TopicNode> ptr;
+ if (!ptr && GlobalInit(BHomeShm())) {
+ auto InitLog = []() {
+ auto id = GetProcExe();
+ char path[200] = {0};
+ sprintf(path, "/tmp/bhshmq_node_%s.log", id.c_str());
+ ns_log::AddLog(path);
+ return true;
+ };
+ static bool init_log = InitLog();
+ ptr.reset(new TopicNode(BHomeShm()));
+ }
+ return ptr;
+}
TopicNode &ProcNode()
{
- static bool init = GlobalInit(BHomeShm());
- static TopicNode node(BHomeShm());
- return node;
+ return *ProcNodePtr();
}
class TmpPtr : private boost::noncopyable
@@ -82,6 +119,12 @@
return false;
}
MsgOut msg_reply;
+ auto &ptr = ProcNodePtr();
+ if (!ptr) {
+ SetLastError(eNotFound, "center not started.");
+ return 0;
+ }
+
return (ProcNode().*mfunc)(input, msg_reply, timeout_ms) &&
PackOutput(msg_reply, reply, reply_len);
}
@@ -322,7 +365,8 @@
int BHCleanup()
{
- ProcNode().Stop();
+ ProcNodePtr().reset();
+ return 0;
}
int BHGetLastError(void **msg, int *msg_len)
--
Gitblit v1.8.0