From 77a6c3512a44dfe6540dde71946e6484fe4f173f Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期一, 10 五月 2021 16:05:28 +0800
Subject: [PATCH] test lock code.

---
 src/socket.h |   23 ++++++++++++++---------
 1 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/src/socket.h b/src/socket.h
index cd6bfee..981677f 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>
@@ -37,22 +37,25 @@
 {
 
 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 bool create_or_else_find, 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); }
 	bool Remove() { return Remove(shm(), 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); }
@@ -112,7 +115,7 @@
 			std::unique_lock<std::mutex> lk(st->mutex);
 			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;
@@ -140,6 +143,7 @@
 	template <class... Rest>
 	bool SendImpl(const MQId remote, Rest &&...rest)
 	{
+		// TODO send alloc request, and pack later, higher bit means alloc?
 		send_buffer_.Append(remote, std::forward<decltype(rest)>(rest)...);
 		return true;
 	}
@@ -149,14 +153,15 @@
 	std::atomic<bool> run_;
 
 	Queue mq_;
-	class AsyncCBs
+	template <class Key>
+	class CallbackRecords
 	{
-		std::unordered_map<std::string, RecvCB> store_;
+		std::unordered_map<Key, RecvCB> store_;
 
 	public:
 		bool empty() const { return store_.empty(); }
-		bool Store(const std::string &id, RecvCB &&cb) { return store_.emplace(id, std::move(cb)).second; }
-		bool Pick(const std::string &id, RecvCB &cb)
+		bool Store(const Key &id, RecvCB &&cb) { return store_.emplace(id, std::move(cb)).second; }
+		bool Pick(const Key &id, RecvCB &cb)
 		{
 			auto pos = store_.find(id);
 			if (pos != store_.end()) {
@@ -169,9 +174,9 @@
 		}
 	};
 
-	Synced<AsyncCBs> per_msg_cbs_;
+	Synced<CallbackRecords<std::string>> per_msg_cbs_;
+
 	SendQ send_buffer_;
-	// Synced<SendQ> send_buffer_;
 };
 
 #endif // end of include guard: SOCKET_GWTJHBPO

--
Gitblit v1.8.0