From 7ecd6323ffedbfef92c87c02b2a8680dd53b772c Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期四, 06 五月 2021 19:37:50 +0800
Subject: [PATCH] rename atomic queue io function.

---
 src/socket.h |   55 +++++++++++++++++++++++++++++--------------------------
 1 files changed, 29 insertions(+), 26 deletions(-)

diff --git a/src/socket.h b/src/socket.h
index db64b36..08a4b0a 100644
--- a/src/socket.h
+++ b/src/socket.h
@@ -22,7 +22,7 @@
 #include "bh_util.h"
 #include "defs.h"
 #include "sendq.h"
-#include "shm_queue.h"
+#include "shm_msg_queue.h"
 #include <atomic>
 #include <boost/noncopyable.hpp>
 #include <condition_variable>
@@ -33,52 +33,47 @@
 #include <vector>
 
 using namespace bhome_msg;
-
 class ShmSocket : private boost::noncopyable
 {
-	template <class... T>
-	bool SendImpl(const void *valid_remote, T &&...rest)
-	{
-		send_buffer_.Append(*static_cast<const MQId *>(valid_remote), std::forward<decltype(rest)>(rest)...);
-		return true;
-	}
 
 protected:
-	typedef bhome_shm::ShmMsgQueue Queue;
+	typedef ShmMsgQueue Queue;
 
 public:
+	typedef ShmMsgQueue::MQId MQId;
 	typedef bhome_shm::SharedMemory Shm;
+	typedef std::function<void(ShmSocket &sock, MsgI &imsg)> RawRecvCB;
 	typedef std::function<void(ShmSocket &sock, MsgI &imsg, BHMsgHead &head)> RecvCB;
 	typedef std::function<bool(ShmSocket &sock, MsgI &imsg, BHMsgHead &head)> PartialRecvCB;
 	typedef std::function<void(ShmSocket &sock)> IdleCB;
 
-	ShmSocket(Shm &shm, const MQId &id, const int len);
+	ShmSocket(Shm &shm, const MQId id, const int len);
 	ShmSocket(Shm &shm, const int len = 12);
 	~ShmSocket();
-	static bool Remove(SharedMemory &shm, const MQId &id) { return Queue::Remove(shm, id); }
+	static bool Remove(SharedMemory &shm, const MQId id) { return Queue::Remove(shm, id); }
 	bool Remove() { return Remove(shm(), id()); }
-	const MQId &id() const { return mq().Id(); }
+	MQId id() const { return mq().Id(); }
 	// start recv.
+	bool Start(const RawRecvCB &onData, const IdleCB &onIdle, int nworker = 1);
 	bool Start(int nworker = 1, const RecvCB &onData = RecvCB(), const IdleCB &onIdle = IdleCB());
 	bool Start(const RecvCB &onData, const IdleCB &onIdle, int nworker = 1) { return Start(nworker, onData, onIdle); }
 	bool Start(const RecvCB &onData, int nworker = 1) { return Start(nworker, onData); }
 	bool Stop();
-	size_t Pending() const { return mq().Pending(); }
 
 	template <class Body>
-	bool Send(const void *valid_remote, const BHMsgHead &head, const Body &body, const RecvCB &cb = RecvCB())
+	bool Send(const MQId remote, BHMsgHead &head, Body &body, RecvCB &&cb = RecvCB())
 	{
 		try {
 			if (!cb) {
-				return SendImpl(valid_remote, MsgI::Serialize(head, body));
+				return SendImpl(remote, MsgI::Serialize(head, body));
 			} else {
 				std::string msg_id(head.msg_id());
-				per_msg_cbs_->Add(msg_id, cb);
+				per_msg_cbs_->Store(msg_id, std::move(cb));
 				auto onExpireRemoveCB = [this, msg_id](SendQ::Data const &msg) {
 					RecvCB cb_no_use;
-					per_msg_cbs_->Find(msg_id, cb_no_use);
+					per_msg_cbs_->Pick(msg_id, cb_no_use);
 				};
-				return SendImpl(valid_remote, MsgI::Serialize(head, body), onExpireRemoveCB);
+				return SendImpl(remote, MsgI::Serialize(head, body), onExpireRemoveCB);
 			}
 		} catch (...) {
 			SetLastError(eError, "Send internal error.");
@@ -86,15 +81,15 @@
 		}
 	}
 
-	bool Send(const void *valid_remote, const MsgI &imsg)
+	bool Send(const MQId remote, const MsgI &imsg)
 	{
-		return SendImpl(valid_remote, imsg);
+		return SendImpl(remote, imsg);
 	}
 
-	bool SyncRecv(MsgI &msg, bhome::msg::BHMsgHead &head, const int timeout_ms);
+	bool SyncRecv(MsgI &msg, bhome_msg::BHMsgHead &head, const int timeout_ms);
 
 	template <class Body>
-	bool SendAndRecv(const void *remote, const BHMsgHead &head, const Body &body, MsgI &reply, BHMsgHead &reply_head, const int timeout_ms)
+	bool SendAndRecv(const MQId remote, BHMsgHead &head, Body &body, MsgI &reply, BHMsgHead &reply_head, const int timeout_ms)
 	{
 		struct State {
 			std::mutex mutex;
@@ -117,14 +112,15 @@
 			};
 
 			std::unique_lock<std::mutex> lk(st->mutex);
-			bool sendok = Send(remote, head, body, OnRecv);
+			bool sendok = Send(remote, head, body, std::move(OnRecv));
 			if (!sendok) {
-				printf("send timeout\n");
+				LOG_DEBUG() << "send timeout";
 			}
 			if (sendok && st->cv.wait_until(lk, endtime) == std::cv_status::no_timeout) {
 				return true;
 			} else {
 				st->canceled = true;
+				SetLastError(ETIMEDOUT, "timeout");
 				return false;
 			}
 		} catch (...) {
@@ -143,6 +139,13 @@
 	bool StopNoLock();
 	bool RunningNoLock() { return !workers_.empty(); }
 
+	template <class... Rest>
+	bool SendImpl(const MQId remote, Rest &&...rest)
+	{
+		send_buffer_.Append(remote, std::forward<decltype(rest)>(rest)...);
+		return true;
+	}
+
 	std::vector<std::thread> workers_;
 	std::mutex mutex_;
 	std::atomic<bool> run_;
@@ -154,8 +157,8 @@
 
 	public:
 		bool empty() const { return store_.empty(); }
-		bool Add(const std::string &id, const RecvCB &cb) { return store_.emplace(id, cb).second; }
-		bool Find(const std::string &id, RecvCB &cb)
+		bool Store(const std::string &id, RecvCB &&cb) { return store_.emplace(id, std::move(cb)).second; }
+		bool Pick(const std::string &id, RecvCB &cb)
 		{
 			auto pos = store_.find(id);
 			if (pos != store_.end()) {

--
Gitblit v1.8.0