From 4deeafbd502dc3c57dab8ad6ca601a38a9e7f074 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期二, 06 四月 2021 19:10:49 +0800
Subject: [PATCH] add uni center.

---
 src/socket.cpp |  144 +++++++++++++++++++++--------------------------
 1 files changed, 64 insertions(+), 80 deletions(-)

diff --git a/src/socket.cpp b/src/socket.cpp
index 21928b8..b9def0c 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -20,115 +20,99 @@
 #include "bh_util.h"
 #include "defs.h"
 #include "msg.h"
-#include <chrono>
 
 using namespace bhome_msg;
 using namespace bhome_shm;
-using namespace std::chrono_literals;
 
 namespace
 {
 
-int GetSocketDefaultLen(ShmSocket::Type type)
-{
-	switch (type) {
-	case ShmSocket::eSockRequest: return 12;
-	case ShmSocket::eSockReply: return 64;
-	case ShmSocket::eSockPublish: return 0;
-	case ShmSocket::eSockSubscribe: return 64;
-	default: return 0;
-	}
-}
-
 } // namespace
 
-ShmSocket::ShmSocket(Type type, bhome_shm::SharedMemory &shm) :
-    shm_(shm), type_(type), run_(false)
+ShmSocket::ShmSocket(Shm &shm, const void *id, const int len) :
+    shm_(shm), run_(false)
 {
-	int len = GetSocketDefaultLen(type);
-	if (len != 0) {
-		mq_.reset(new Queue(shm_, len));
-
-		auto RecvProc = [this]() {
-			while (run_) {
-				try {
-					std::unique_lock<std::mutex> lk(mutex_);
-					if (cv_recv_cb_.wait_for(lk, 100ms, [this]() { return HasRecvCB(); })) {
-						BHMsg msg;
-						if (mq_->Recv(msg, 100)) {
-							this->onRecv_(msg);
-						}
-					}
-				} catch (...) {
-				}
-			}
-		};
-		run_.store(true);
-		workers_.emplace_back(RecvProc);
+	if (id && len > 0) {
+		mq_.reset(new Queue(*static_cast<const MQId *>(id), shm, len));
 	}
 }
-
-ShmSocket::ShmSocket(Type type) :
-    ShmSocket(type, BHomeShm())
+ShmSocket::ShmSocket(bhome_shm::SharedMemory &shm, const int len) :
+    shm_(shm), run_(false)
 {
+	if (len > 0) {
+		mq_.reset(new Queue(shm_, len));
+	}
 }
 
 ShmSocket::~ShmSocket()
 {
-	Stop();
+	Stop(); //TODO should stop in sub class, incase thread access sub class data.
 }
 
-bool ShmSocket::Publish(const std::string &topic, const void *data, const size_t size, const int timeout_ms)
+bool ShmSocket::Start(const RecvCB &onData, const IdleCB &onIdle, int nworker)
 {
-	if (type_ != eSockPublish) {
-		return false;
+	if (!mq_ || !onData) {
+		return false; // TODO error code.
 	}
-	assert(!mq_);
-	try {
-		MsgI imsg;
-		if (!imsg.MakeRC(shm_, MakePub(topic, data, size))) {
-			return false;
-		}
-		DEFER1(imsg.Release(shm_));
-		return Queue::Send(shm_, kBHBusQueueId, imsg, timeout_ms);
 
-	} catch (...) {
-		return false;
-	}
-}
-
-bool ShmSocket::Subscribe(const std::vector<std::string> &topics, const int timeout_ms)
-{
-	if (type_ != eSockSubscribe) {
-		return false;
-	}
-	assert(mq_);
-	try {
-		return mq_->Send(kBHBusQueueId, MakeSub(mq_->Id(), topics), timeout_ms);
-	} catch (...) {
-		return false;
-	}
-}
-
-bool ShmSocket::SetRecvCallback(const RecvCB &onRecv)
-{
 	std::lock_guard<std::mutex> lock(mutex_);
-	onRecv_ = onRecv;
-	cv_recv_cb_.notify_one();
+	StopNoLock();
+	auto RecvProc = [this, onData, onIdle]() {
+		while (run_) {
+			try {
+				MsgI imsg;
+				DEFER1(imsg.Release(shm_));
+				if (mq_->Recv(imsg, 100)) {
+					BHMsg msg;
+					if (imsg.Unpack(msg)) {
+						onData(*this, imsg, msg);
+					}
+				} else if (onIdle) {
+					onIdle(*this);
+				}
+			} catch (...) {
+			}
+		}
+	};
+
+	run_.store(true);
+	for (int i = 0; i < nworker; ++i) {
+		workers_.emplace_back(RecvProc);
+	}
 	return true;
 }
 
-bool ShmSocket::HasRecvCB()
+bool ShmSocket::Stop()
 {
-	return static_cast<bool>(onRecv_);
+	std::lock_guard<std::mutex> lock(mutex_);
+	return StopNoLock();
 }
 
-void ShmSocket::Stop()
+bool ShmSocket::StopNoLock()
 {
-	run_ = false;
-	for (auto &t : workers_) {
-		if (t.joinable()) {
-			t.join();
+	if (run_.exchange(false)) {
+		for (auto &w : workers_) {
+			if (w.joinable()) {
+				w.join();
+			}
 		}
+		workers_.clear();
+		return true;
 	}
-}
\ No newline at end of file
+	return false;
+}
+
+bool ShmSocket::SyncSend(const void *id, const bhome_msg::BHMsg &msg, const int timeout_ms)
+{
+	return mq_->Send(*static_cast<const MQId *>(id), msg, timeout_ms);
+}
+
+bool ShmSocket::SyncRecv(bhome_msg::BHMsg &msg, const int timeout_ms)
+{
+	std::lock_guard<std::mutex> lock(mutex_);
+	if (!mq_ || RunningNoLock()) {
+		return false;
+	} else {
+		return mq_->Recv(msg, timeout_ms);
+	}
+}

--
Gitblit v1.8.0