From 1d6c040dcb9a01648edc66d8c0006c8c9294a705 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期四, 22 四月 2021 18:28:30 +0800
Subject: [PATCH] add mutex timeout limit; use atomic as refcount.

---
 box/center.cpp |   25 ++++++-------------------
 1 files changed, 6 insertions(+), 19 deletions(-)

diff --git a/box/center.cpp b/box/center.cpp
index 8625f7f..3059e90 100644
--- a/box/center.cpp
+++ b/box/center.cpp
@@ -27,7 +27,6 @@
 
 using namespace bhome_shm;
 using namespace bhome_msg;
-using namespace bhome::msg;
 typedef BHCenter::MsgHandler Handler;
 
 namespace
@@ -39,7 +38,7 @@
 public:
 	typedef std::string ProcId;
 	typedef std::string Address;
-	typedef bhome::msg::ProcInfo ProcInfo;
+	typedef bhome_msg::ProcInfo ProcInfo;
 	typedef std::function<void(Address const &)> Cleaner;
 
 private:
@@ -97,12 +96,9 @@
 	NodeCenter(const std::string &id, const Cleaner &cleaner, const steady_clock::duration offline_time, const steady_clock::duration kill_time) :
 	    NodeCenter(id, cleaner, duration_cast<seconds>(offline_time).count(), duration_cast<seconds>(kill_time).count()) {}
 
-	const std::string &id() const
-	{
-		return id_;
-	} // no need to lock.
+	// center name, no relative to shm.
+	const std::string &id() const { return id_; }
 
-	//TODO maybe just return serialized string.
 	MsgCommonReply Register(const BHMsgHead &head, MsgRegister &msg)
 	{
 		if (msg.proc().proc_id() != head.proc_id()) {
@@ -399,7 +395,7 @@
 
 Handler Combine(const Handler &h1, const Handler &h2)
 {
-	return [h1, h2](ShmSocket &socket, bhome_msg::MsgI &msg, bhome::msg::BHMsgHead &head) {
+	return [h1, h2](ShmSocket &socket, bhome_msg::MsgI &msg, bhome_msg::BHMsgHead &head) {
 		return h1(socket, msg, head) || h2(socket, msg, head);
 	};
 }
@@ -417,7 +413,7 @@
 
 bool AddCenter(const std::string &id, const NodeCenter::Cleaner &cleaner)
 {
-	auto center_ptr = std::make_shared<Synced<NodeCenter>>(id, cleaner, 5s, 10s);
+	auto center_ptr = std::make_shared<Synced<NodeCenter>>(id, cleaner, 60s, 60s * 2);
 	auto MakeReplyer = [](ShmSocket &socket, BHMsgHead &head, const std::string &proc_id) {
 		return [&](auto &&rep_body) {
 			auto reply_head(InitMsgHead(GetType(rep_body), proc_id, head.msg_id()));
@@ -494,12 +490,6 @@
 
 } // namespace
 
-SharedMemory &BHomeShm()
-{
-	static SharedMemory shm("bhome_default_shm_v0", 1024 * 1024 * 64);
-	return shm;
-}
-
 BHCenter::CenterRecords &BHCenter::Centers()
 {
 	static CenterRecords rec;
@@ -523,16 +513,13 @@
 		printf("remove mq : %s\n", r ? "ok" : "failed");
 	};
 
-	AddCenter("#center", gc);
+	AddCenter("#bhome_center", gc);
 
 	for (auto &kv : Centers()) {
 		auto &info = kv.second;
 		sockets_[info.name_] = std::make_shared<ShmSocket>(shm, *(MQId *) info.mqid_.data(), info.mq_len_);
 	}
 }
-
-BHCenter::BHCenter() :
-    BHCenter(BHomeShm()) {}
 
 bool BHCenter::Start()
 {

--
Gitblit v1.8.0