From 16935f4aebffdd1b6580b844391a0aa0f4f3012b Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期一, 22 四月 2024 10:29:12 +0800 Subject: [PATCH] bug fixed --- src/nng_wrap.cpp | 48 ++++++++++++++++++++++++++++++++++++------------ 1 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/nng_wrap.cpp b/src/nng_wrap.cpp index 1a2bc3e..3138e2f 100644 --- a/src/nng_wrap.cpp +++ b/src/nng_wrap.cpp @@ -314,17 +314,29 @@ _ps_sub* sub = (_ps_sub*)arg; if (!sub) sub = singleton<_ps_sub>(); + if (!topic && !msg) { + lock_guard<mutex> l{sub->mtx_msg_}; + for (int i = 0; i < 2; i++) { + if (!sub->msg_.empty()) + return 0; + this_thread::sleep_for(chrono::milliseconds(to_ms)); + } + return -1; + } + TAG; int tm = to_ms > 0 ? to_ms : 30; unique_lock<mutex> l(sub->mtx_msg_); - auto status = sub->cv_msg_.wait_for(l, chrono::milliseconds{tm}, [sub]{ - return !sub->msg_.empty(); - }); - if (!status){ - PRNTVITAG("subscribe_read timeout"); - return -1; + if (sub->msg_.empty()) { + auto status = sub->cv_msg_.wait_for(l, chrono::milliseconds{tm}, [sub]{ + return !sub->msg_.empty(); + }); + if (!status){ + PRNTVITAG("subscribe_read timeout"); + return -1; + } } auto& tmp = sub->msg_.front(); *topic = std::move(tmp.topic_); @@ -580,18 +592,30 @@ if (start_reply(rep->url_, get<1>(get<1>(rep->socks_))) != 0) return -1; + if (!src && !msg) { + lock_guard<mutex> l{rep->mtx_msg_}; + for (int i = 0; i < 2; i++) { + if (!rep->msg_.empty()) + return 0; + this_thread::sleep_for(chrono::milliseconds(to_ms)); + } + return -1; + } + int tm = to_ms > 0 ? to_ms : 30; uint64_t key{}; work* w{}; { unique_lock<mutex> l(rep->mtx_msg_); - auto status = rep->cv_msg_.wait_for(l, chrono::milliseconds{tm}, [rep]{ - return !rep->msg_.empty(); - }); - if (!status){ - PRNTVITAG("read_request timeout"); - return -1; + if (rep->msg_.empty()) { + auto status = rep->cv_msg_.wait_for(l, chrono::milliseconds{tm}, [rep]{ + return !rep->msg_.empty(); + }); + if (!status){ + PRNTVITAG("read_request timeout"); + return -1; + } } auto iter = rep->msg_.begin(); key = iter->first; -- Gitblit v1.8.0