zhangmeng
2024-04-22 16935f4aebffdd1b6580b844391a0aa0f4f3012b
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;