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 |  644 ++++++++++++++++++++++++++++------------------------------
 1 files changed, 314 insertions(+), 330 deletions(-)

diff --git a/src/nng_wrap.cpp b/src/nng_wrap.cpp
index 5a54168..3138e2f 100644
--- a/src/nng_wrap.cpp
+++ b/src/nng_wrap.cpp
@@ -2,60 +2,24 @@
 
 #include <string.h>
 
-#include <random>
 #include <vector>
-#include <unordered_map>
-#include <unordered_set>
-#include <thread>
-#include <atomic>
-#include <deque>
-#include <mutex>
-#include <condition_variable>
-#include <functional>
-#include <chrono>
-#include <future>
-using namespace std;
+#include "common.h"
 
-#include <unistd.h>
-
-#include <nng/nng.h>
 #include <nng/protocol/reqrep0/rep.h>
 #include <nng/supplemental/util/platform.h>
 
-#include "nng/compat/nanomsg/nn.h"
 #include "nng/compat/nanomsg/reqrep.h"
 #include "nng/compat/nanomsg/pubsub.h"
 #include "nng/compat/nanomsg/survey.h"
 
+using namespace std;
+
 namespace nng_wrap {
 
-// common function
 static int client_socket(const string& url, const int protocol, int family=AF_SP){
     int sock = nn_socket(family, protocol);
     if (sock < 0) return sock;
     int rc = nn_connect(sock, url.c_str());
-    if (rc < 0) {
-        nn_close(sock);
-        return rc;
-    }
-    return sock;
-}
-
-static void remove_exist(const string& url){
-    if (url.find("ipc://") == 0){
-        string address(url);
-        address = address.substr(6);
-        if (access(address.c_str(), F_OK) == 0){
-            remove(address.c_str());
-        }
-    }
-}
-
-static int server_socket(const string& url, const int protocol, int family=AF_SP){
-    int sock = nn_socket(family, protocol);
-    if (sock < 0) return sock;
-    remove_exist(url);
-    int rc = nn_bind(sock, url.c_str());
     if (rc < 0) {
         nn_close(sock);
         return rc;
@@ -93,9 +57,7 @@
 // simple interface
 
 void free_nng(void* data, const int data_len){
-    if (data){
-        free(data);
-    }
+    free(data);
 }
 
 void copy_memory(void** dest, int *dest_len, const void* src, const int src_len){
@@ -106,34 +68,27 @@
         *dest_len = src_len;
 }
 
-static string verbose_info{};
-#ifndef PRNTVITAG
-// #define TAG do{ \
-//     if (verbose_info.length() > 8096) verbose_info.clear(); \
-//     verbose_info=string("function [")+__FUNCTION__+string("]");}while(0)
-/* #define PRNTVITAG(msg) do{ \
-            if (verbose_info.length() > 8096) verbose_info.clear(); \
-            verbose_info+=string("-> (") + msg + string(")"); \
-        }while(0) */
-#define TAG
-#define PRNTVITAG(args)
-#endif
+void set_last_error(const std::string& emsg){
+    // verbose_info += emsg;
+}
+
 void get_last_error(int* ec, void** emsg, int* emsg_len){
     *emsg = NULL;
     *emsg_len = 0;
 
     *ec = nn_errno();
     const char* msg = nn_strerror(*ec);
-    string strMsg(msg);
-    strMsg = strMsg + "{" + verbose_info + "}";
-    copy_memory(emsg, emsg_len, strMsg.data(), strMsg.size());
-    verbose_info.clear();
+    // string strMsg(msg);
+    // strMsg = strMsg + "{" + verbose_info + "}";
+    // copy_memory(emsg, emsg_len, strMsg.data(), strMsg.size());
+    // verbose_info.clear();
+
+    copy_memory(emsg, emsg_len, msg, strlen(msg));
 }
 
 ///////////////////////////////////////////////////////
 // simple request waiting reply
 
-static constexpr int timeout_req_rep = 5162;
 int simple_request(const std::string& url,
     const void* in, const int in_len,
     void** out, int *out_len, const int to_ms){
@@ -161,51 +116,12 @@
     return true;
 }
 
-///////////////////////////////////////////////////////////
-// base class
-#define DISABLE_COPY_AND_ASSIGN(className) className(const className&)=delete; \
-                                className(className&&)=delete; \
-                                className& operator=(const className&)=delete; \
-                                className& operator=(className&&)=delete
-class _nn{
-public:
-    DISABLE_COPY_AND_ASSIGN(_nn);
-    _nn()=default;
-    virtual ~_nn(){ if (socket_ > 0) nn_close(socket_); }
-    int                         socket_{-1};
-    string                      url_{};
-};
-
-///////////////////////////////////////////////
+/////////////////////////////////////////////////////
 // publish
-struct psmsg{
-    DISABLE_COPY_AND_ASSIGN(psmsg);
-    psmsg()=delete;
-    psmsg(const std::string& t, std::string&& m)
-    :topic_(t),msg_(std::move(m)){}
-    std::string topic_{};
-    std::string msg_{};
-};
-class _ps : public _nn{
-public:
-    DISABLE_COPY_AND_ASSIGN(_ps);
-    _ps()=default;
-    virtual ~_ps(){
-        t_quit_.store(true, memory_order_relaxed);
-        if (t_.joinable()) t_.join();
-    }
 
-    thread              t_;
-    atomic_bool         t_quit_{false};
-    deque<psmsg>        msg_{};
-    mutex               mtx_msg_{};
-    condition_variable  cv_msg_{};
-};
-
-static _ps pub_;
-static int connect_to_center(const string& topic){
-    if (pub_.socket_ > 0) return pub_.socket_;
-    pub_.url_ = topic;
+static int pub_connect_to_center(const string& topic, _ps* pub){
+    if (pub->socket_ > 0) return pub->socket_;
+    pub->url_ = topic;
 
     TAG;
     int sock = client_socket(topic, NN_REQ);
@@ -214,37 +130,36 @@
         return -1;
     }
     set_socket_timeout(sock, timeout_req_rep);
-    pub_.socket_ = sock;
-    pub_.t_ = thread([]{
-        while (!pub_.t_quit_.load()) {
-            psmsg *msg{NULL};
+    pub->socket_ = sock;
+    pub->t_ = get_thread([](const auto pub){
+        string sndmsg{};
+        sndmsg.reserve(1024);
+        while (!pub->t_quit_.load()) {
+            _ps::psmsg *msg{NULL};
             {
-                unique_lock<mutex> l{pub_.mtx_msg_};
-                pub_.cv_msg_.wait(l, []{
-                    return !pub_.msg_.empty() || pub_.t_quit_.load();
+                unique_lock<mutex> l{pub->mtx_msg_};
+                pub->cv_msg_.wait(l, [pub]{
+                    return !pub->msg_.empty() || pub->t_quit_.load();
                 });
-                if(pub_.t_quit_.load()) break;
-                msg = &pub_.msg_.front();
-                if (msg->topic_.empty()) {pub_.msg_.pop_front(); continue;}
+                if(pub->t_quit_.load()) break;
+                msg = &pub->msg_.front();
+                if (msg->topic_.empty()) {pub->msg_.pop_front(); continue;}
             }
-            const auto &topic = msg->topic_;
-            const auto topic_size = topic.size();
-            const auto &data = msg->msg_;
-            const auto data_size = data.size();
-
-            char *sndmsg = (char*)malloc(topic_size + data_size);
-            memcpy(sndmsg, topic.data(), topic_size);
-            memcpy(sndmsg+topic_size, data.data(), data_size);
-            int rc = nn_send(pub_.socket_, sndmsg, data_size+topic_size, 0);
-            free(sndmsg);
-            if (rc == (int)(data_size+topic_size)){
+            sndmsg += msg->topic_;
+            sndmsg += '\0';
+            sndmsg += msg->data_;
+            int sndmsgsize = (int)sndmsg.size();
+            // string sndmsg = (string{msg->topic_}+='\0')+=msg->data_;
+            int rc = nn_send(pub->socket_, sndmsg.data(), sndmsg.size(), 0);
+            sndmsg.clear();
+            if (rc == sndmsgsize){
                 char* tmp{};
-                rc = nn_recv(pub_.socket_, &tmp, NN_MSG, 0);
+                rc = nn_recv(pub->socket_, &tmp, NN_MSG, 0);
                 if (rc > 0){
                     nn_freemsg(tmp);
-                    printf("======>> publish topic %s data length %lu\n", topic.c_str(), data_size);
-                    lock_guard<mutex> l{pub_.mtx_msg_};
-                    pub_.msg_.pop_front();
+                    // printf("======>> publish topic %s data length %lu\n", msg->topic_.c_str(), msg->data_.size());
+                    lock_guard<mutex> l{pub->mtx_msg_};
+                    pub->msg_.pop_front();
                     continue;
                 }else{
                     PRNTVITAG("publish req-rep thread nn_recv faild");
@@ -254,147 +169,179 @@
             }
 
         }
-    });
+    }, pub);
     return sock;
 }
 
-int publish(const std::string& topic, const void* data, const int data_len){
+int publish(const std::string& topic, const void* data, const int data_len, void* arg/*=NULL*/){
+    // printf("======>> publish topic %s\n", topic.c_str());
+    _ps* pub = (_ps*)arg;
+    if (!pub) pub = singleton<_ps>();
+
     if (!data && data_len == 0){
         // printf("======>> publish start url %s\n", topic.c_str());
-        return connect_to_center(topic);
+        return pub_connect_to_center(topic, pub);
     }
-    if (pub_.socket_ < 0){
-        connect_to_center(pub_.url_);
+    if (pub->socket_ < 0){
+        pub_connect_to_center(pub->url_, pub);
     }
-    if(pub_.socket_ < 0) {
+    if(pub->socket_ < 0) {
         PRNTVITAG("publish socket_ < 0");
         return -1;
     }
 
     // printf("======>> publish topic %s\n", topic.c_str());
-    lock_guard<mutex> l{pub_.mtx_msg_};
-    pub_.msg_.emplace_back(topic, string{(const char*)data, (const size_t)data_len});
-    pub_.cv_msg_.notify_one();
-    return pub_.msg_.size();
+    lock_guard<mutex> l{pub->mtx_msg_};
+    pub->msg_.emplace_back(topic, string{(const char*)data, (const size_t)data_len});
+    pub->cv_msg_.notify_one();
+    return (*pub)();
 }
 
 ///////////////////////////////////////////////
 // subscribe
-class _ps_sub : public _ps{
-public:
-    DISABLE_COPY_AND_ASSIGN(_ps_sub);
-    _ps_sub()=default;
-    ~_ps_sub()=default;
 
-    unordered_set<string>   topics_{};
-    mutex                   mtx_topics_{};
-    unordered_set<string>   failed_topics_{};
-    mutex                   mtx_failed_topics_{};
-};
+int subscribe_center(const std::string& url, void* arg/*=NULL*/){
+    _ps_sub* sub = (_ps_sub*)arg;
+    if (!sub) sub = singleton<_ps_sub>();
 
-static _ps_sub sub_;
-int subscribe_center(const std::string& url){
-    if (sub_.socket_ > 0) return 0;
-    sub_.url_ = url;
+    if (sub->socket_ > 0) return 0;
+    sub->url_ = url;
 
+    TAG;
     int sock = client_socket(url, NN_SUB);
     if (sock < 0){
+        PRNTVITAG("client_socket faild\n");
         return -1;
     }
-    // set_socket_timeout(sock, timeout_req_rep);
-    sub_.socket_ = sock;
-    sub_.t_ = thread([]{
-        while (!sub_.t_quit_.load()) {
-            char* m;
-            int m_len = nn_recv(sub_.socket_, &m, NN_MSG, NN_DONTWAIT);
+    set_socket_timeout(sock, 300);
+    sub->socket_ = sock;
+    sub->t_ = get_thread([](const auto sub){
+        while (!sub->t_quit_.load()) {
+            // for(auto&& i : sub->topics_) {
+            //     printf("======>> sub topic %s\n", i.c_str());
+            // }
+            char* m{};
+            // int m_len = nn_recv(sub->socket_, &m, NN_MSG, NN_DONTWAIT);
+            int m_len = nn_recv(sub->socket_, &m, NN_MSG, 0);
             if (m_len > 0){
-                string topic{}, msg{};
+                char* topic = m;
+                char* msg = m + strlen(topic) + 1;
+                size_t msgl = m_len - strlen(topic) - 1;
+                // string tmp_msg{m, (size_t)m_len};
+                // nn_freemsg(m);
+                // string topic{tmp_msg.c_str()};
+                // string msg{};
+                bool found_topic = false;
                 {
-                    lock_guard<mutex> l{sub_.mtx_topics_};
-                    for(auto && i : sub_.topics_){
-                        auto topic_len = i.size();
-                        if (m_len <= (int)topic_len) continue;
-                        topic.assign(m, topic_len);
-                        if (topic == i){
-                            msg.assign(m+topic_len, m_len-topic_len);
+                    lock_guard<mutex> l{sub->operator()()};
+                    for(auto && i : sub->topics_){
+                        if (0 == i.compare(topic)){
+                            found_topic = true;
                             break;
                         }
                     }
                 }
-                nn_freemsg(m);
-                printf("======>> subscribe recv topic %s msg length %lu\n", topic.c_str(), msg.length());
-                if (!msg.empty()){
-                    lock_guard<mutex> l(sub_.mtx_msg_);
-                    sub_.msg_.emplace_back(topic, move(msg));
-                    sub_.cv_msg_.notify_all();
+                // printf("======>> subscribe recv topic %s msg length %lu\n", topic, msgl);
+                if (found_topic){
+                    lock_guard<mutex> l(sub->mtx_msg_);
+                    sub->msg_.emplace_back(string(topic), string(msg, msgl));
+                    sub->cv_msg_.notify_all();
                 }
-
+                nn_freemsg(m);
             }else {
+                if (!sub->failed_topics_.empty())
                 {
-                    lock_guard<mutex> l{sub_.mtx_failed_topics_};
-                    if (!sub_.failed_topics_.empty()){
-                        for(auto iter = sub_.failed_topics_.begin(); iter != sub_.failed_topics_.end();){
-                            if (nn_setsockopt(sub_.socket_, NN_SUB, NN_SUB_UNSUBSCRIBE, iter->c_str(), iter->length()) >= 0){
-                                iter = sub_.failed_topics_.erase(iter);
+                    lock_guard<mutex> l{sub->mtx_failed_topics_};
+                    if (!sub->failed_topics_.empty()){
+                        for(auto iter = sub->failed_topics_.begin(); iter != sub->failed_topics_.end();){
+                            if (nn_setsockopt(sub->socket_, NN_SUB, NN_SUB_SUBSCRIBE, iter->c_str(), iter->length()) >= 0){
+                                iter = sub->failed_topics_.erase(iter);
                             }else{
                                 iter++;
                             }
                         }
                     }
                 }
-                this_thread::sleep_for(chrono::milliseconds{6});
+                // this_thread::sleep_for(chrono::milliseconds{6});
                 // printf("======>> subscribe nn_recv failed %s\n", nn_strerror(nn_errno()));
             }
         }
-    });
+    }, sub);
     return 0;
 }
 
-int subscribe_topic(const std::string& topic){
-    if (sub_.socket_ < 0){
-        subscribe_center(sub_.url_);
-    }
-    if (sub_.socket_ < 0) return -1;
+int subscribe_topic(const std::string& topic, void* arg/*=NULL*/){
+    _ps_sub* sub = (_ps_sub*)arg;
+    if (!sub) sub = singleton<_ps_sub>();
 
-    auto ret = nn_setsockopt(sub_.socket_, NN_SUB, NN_SUB_SUBSCRIBE, topic.c_str(), topic.length());
+    TAG;
+    if (sub->socket_ < 0){
+        subscribe_center(sub->url_, sub);
+    }
+    if (sub->socket_ < 0) {
+        PRNTVITAG("socket_ < 0");
+        return -1;
+    }
+
+    auto ret = nn_setsockopt(sub->socket_, NN_SUB, NN_SUB_SUBSCRIBE, topic.c_str(), topic.length());
     // printf("set NN_SUB_SUBSCRIBE topic %s ret %d\n", topic.c_str(), ret);
     if (ret < 0){
-        lock_guard<mutex> l{sub_.mtx_failed_topics_};
-        sub_.failed_topics_.insert(topic);
+        PRNTVITAG("nn_setsockopt failed");
+        lock_guard<mutex> l{sub->mtx_failed_topics_};
+        sub->failed_topics_.insert(topic);
     }
-    lock_guard<mutex> l{sub_.mtx_topics_};
-    sub_.topics_.insert(topic);
+    lock_guard<mutex> l{(*sub)()};
+    sub->topics_.insert(topic);
 
     return 0;
 }
 
-int unsubscribe_topic(const std::string& topic){
-    lock_guard<mutex> l(sub_.mtx_topics_);
-    auto iter = sub_.topics_.find(topic);
-    if (iter != sub_.topics_.end()){
-        nn_setsockopt(sub_.socket_, NN_SUB, NN_SUB_UNSUBSCRIBE, topic.c_str(), topic.length());
-        sub_.topics_.erase(iter);
+int unsubscribe_topic(const std::string& topic, void* arg/*=NULL*/){
+    _ps_sub* sub = (_ps_sub*)arg;
+    if (!sub) sub = singleton<_ps_sub>();
+
+    lock_guard<mutex> l{(*sub)()};
+    auto iter = sub->topics_.find(topic);
+    if (iter != sub->topics_.end()){
+        nn_setsockopt(sub->socket_, NN_SUB, NN_SUB_UNSUBSCRIBE, topic.c_str(), topic.length());
+        sub->topics_.erase(iter);
     }
 
     return 0;
 }
 
-int subscribe_read(std::string* topic, std::string* msg, const int to_ms){
+int subscribe_read(std::string* topic, std::string* msg, const int to_ms, void* arg/*=NULL*/){
+    _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}, []{
-        return !sub_.msg_.empty();
-    });
-    if (!status){
-        PRNTVITAG("subscribe_read timeout");
-        return -1;
+    unique_lock<mutex> l(sub->mtx_msg_);
+    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;
+        }
     }
-    const auto& tmp = sub_.msg_.front();
-    *topic = tmp.topic_;
-    *msg = tmp.msg_;
-    sub_.msg_.pop_front();
+    auto& tmp = sub->msg_.front();
+    *topic = std::move(tmp.topic_);
+    *msg = std::move(tmp.data_);
+    sub->msg_.pop_front();
 
     return 0;
 }
@@ -402,99 +349,69 @@
 ///////////////////////////////////////////////////////////
 // survey respondent for heartbeat
 
-class _sv : public _nn{
-public:
-    DISABLE_COPY_AND_ASSIGN(_sv);
-    _sv()=default;
-    ~_sv(){
-        t_quit_.store(true, memory_order_relaxed);
-        if (t_.joinable()) t_.join();
-    }
+int respond_survey(const std::string& url, std::string&& fixed_msg, void* arg/*=NULL*/){
+    _sv* sv = (_sv*)arg;
+    if (!sv) sv = singleton<_sv>();
 
-    thread      t_;
-    atomic_bool t_quit_{false};
-    string      fixed_msg_{};
-};
+    sv->url_ = url;
+    sv->fixed_msg_ = move(fixed_msg);
+    sv->t_ = get_thread([](const auto sv){
 
-static _sv survey_;
-int respond_survey(const std::string& url, std::string&& fixed_msg){
-    survey_.url_ = url;
-    survey_.fixed_msg_ = move(fixed_msg);
-    survey_.t_ = thread([]{
-        int& sock = survey_.socket_;
-        const auto& msg = survey_.fixed_msg_;
-        while (!survey_.t_quit_.load()) {
+        TAG;
+
+        int& sock = sv->socket_;
+
+        char tmp[1024] = {0};
+        while (!sv->t_quit_.load()) {
             if (sock < 0){
-                sock = client_socket(survey_.url_, NN_RESPONDENT);
-                if (sock > 0){
-                    set_socket_timeout(sock, 126);
-                }
+                sock = client_socket(sv->url_, NN_RESPONDENT);
+                if (sock > 0) set_socket_timeout(sock, 126);
+                continue;
             }
-            if (sock < 0) continue;
 
-            char* tmp{};
             int rc = nn_recv(sock, &tmp, NN_MSG, 0);
             if (rc > 0){
-                nn_freemsg(tmp);
-                rc = nn_send(sock, msg.data(), msg.size(), 0);
+                rc = nn_send(sock, (*sv)().front().data(), (*sv)().front().size(), 0);
                 if (rc < 0){
                     PRNTVITAG("heartbeat survey failed");
                     PRNTVITAG(nn_strerror(nn_errno()));
                 }
             }
         }
-    });
+    }, sv);
 
     return 0;
 }
 
-//////////////////////////////////////////////
-// reply for request
-
 int request2(const std::string &ipc, const void* r, const int r_len,
-    void** reply, int* reply_len, const int to_ms)
+    void** reply, int* reply_len, const int to_ms, void* arg/*=NULL*/)
 {
-    return simple_request(ipc, r, r_len, reply, reply_len, to_ms);
+    const auto suc = simple_request(ipc, r, r_len, reply, reply_len, to_ms);
+    if (suc){
+        const size_t sl = rr_unblocking_msg_.size();
+        const size_t rl = *reply_len;
+        if (sl != rl) return true;
+
+        const auto& s = rr_unblocking_msg_;
+        auto r = (const char*)(*reply);
+        if (s.compare(0, sl, r, rl) == 0){
+            free(*reply);
+            *reply = NULL;
+            *reply_len = 0;
+            return false;
+        }
+    }
+    return suc;
 }
 
-enum { INIT, RECV, WAIT, SEND };
-struct work {
-    int state{-1};
-    nng_aio *aio{};
-    nng_msg *msg{};
-    nng_ctx  ctx;
-    void(*cb_recv)(work*){};
-};
-
-class _rr : public _nn{
-public:
-    DISABLE_COPY_AND_ASSIGN(_rr);
-    _rr()=default;
-    ~_rr(){
-
-    }
-
-    nng_socket                                      sock_local_{0};
-    nng_socket                                      sock_remote_{0};
-    int                                             port_{-1};
-
-    unordered_map<uint64_t, string>                 msg_{};
-    unordered_map<uint64_t, struct work*>           works_{};
-    uint64_t                                        work_index_{0};
-    mutex                                           mtx_msg_{};
-    condition_variable                              cv_msg_{};
-
-};
-
-static _rr reply_;
-
-static void
-server_cb(void *arg)
+static void server_cb(void *arg)
 {
+    if (!arg) return;
+
     struct work *work = (struct work*)arg;
     nng_msg *    msg;
     int          rv;
-    uint32_t     when{0};
+    // uint32_t     when{0};
 
     switch (work->state) {
     case INIT:
@@ -522,7 +439,7 @@
     case SEND:
         if ((rv = nng_aio_result(work->aio)) != 0) {
             nng_msg_free(work->msg);
-            break;
+            work->msg = NULL;
         }
         work->state = RECV;
         nng_ctx_recv(work->ctx, work->aio);
@@ -534,17 +451,21 @@
 
 static void cb_recv_for_aio(work* w){
     nng_msg *om = w->msg;
-    string msg{(const char*)nng_msg_body(om), nng_msg_len(om)};
-    nng_msg_free(om);
+    if (!om) {nng_sleep_aio(0, w->aio); return;}
 
-    lock_guard<mutex> l{reply_.mtx_msg_};
-    reply_.works_[reply_.work_index_] = w;
-    reply_.msg_[reply_.work_index_] = msg;
-    reply_.work_index_++;
-    reply_.cv_msg_.notify_all();
+    _rr* rep = (_rr*)w->user_data;
+
+    auto t = (*rep)();
+    lock_guard<mutex> l{rep->mtx_msg_};
+    rep->works_.emplace(get<0>(t), w);
+    get<1>(t).emplace(get<0>(t), string{(const char*)nng_msg_body(om), nng_msg_len(om)});
+    get<0>(t)++;
+    rep->cv_msg_.notify_all();
+
+    nng_msg_free(om);
 }
 
-static struct work *alloc_work(nng_socket sock)
+static struct work *alloc_work(nng_socket sock, _rr* rep, const int mode)
 {
     struct work *w;
     int          rv;
@@ -553,21 +474,23 @@
         return NULL;;
     }
     w->cb_recv = cb_recv_for_aio;
+    w->user_data = rep;
 
     if ((rv = nng_aio_alloc(&w->aio, server_cb, w)) != 0) {
+        nng_free(w, sizeof(*w));
         return NULL;
     }
     if ((rv = nng_ctx_open(&w->ctx, sock)) != 0) {
+        nng_free(w, sizeof(*w));
         return NULL;
     }
     w->state = INIT;
+    w->mode = mode;
+
     return (w);
 }
 
-static constexpr int PARALLEL = 62;
-static struct work* works[PARALLEL]{};
-
-static int create_server(nng_socket* sock, const string& url){
+static int create_server(nng_socket* sock, const string& url, const int count, _rr* rep, const int mode){
     TAG;
     if (sock->id > 0) return 0;
 
@@ -577,101 +500,162 @@
         PRNTVITAG(url);
         return rv;
     }
-    for (int i = 0; i < PARALLEL; i++) {
-        works[i] = alloc_work(*sock);
+
+    work** works = (work**)malloc(sizeof(void*) * count);
+    for (int i = 0; i < count; i++) {
+        works[i] = alloc_work(*sock, rep, mode);
     }
 
     remove_exist(url);
     rv = nng_listen(*sock, url.c_str(), NULL, 0);
     if (rv < 0){
+        for(int i = 0; i < count; i++) if(works[i]) nng_free(works[i], sizeof(work));
+        free(works);
         PRNTVITAG("create_server nng_listen failed");
         PRNTVITAG(url);
         return rv;
     }
 
-    for (int i = 0; i < PARALLEL; i++) {
+    for (int i = 0; i < count; i++) {
         server_cb(works[i]); // this starts them going (INIT state)
     }
 
+    free(works);
     return 0;
 }
 
-int start_reply(const std::string& url, const int port){
-    TAG;
+static void aio_unblock(work* w, const void* msg, const int msg_len){
+    nng_msg_alloc(&w->msg, 0);
+    nng_msg_append(w->msg, msg, msg_len);
+
+    nng_sleep_aio(0, w->aio);
+}
+
+int start_reply(const std::string& url, const int port, void* arg/*=NULL*/){
+    _rr* rep = (_rr*)arg;
+    if (!rep) rep = singleton<_rr>();
 
     string ipc = "ipc:///tmp/" + url;
     if (url.find("ipc://") == 0){
         ipc = url;
     }
-    reply_.url_ = ipc;
-    if(create_server(&reply_.sock_local_, ipc) != 0) return -1;
+    rep->url_ = ipc;
+    if(create_server(&get<0>(rep->socks_), ipc, 62, rep, REPLY_IPC) != 0) return -1;
 
     if (port > 0){
-        reply_.port_ = port;
+        get<1>(get<1>(rep->socks_)) = port;
         ipc = "tcp://0.0.0.0:" + to_string(port);
-        if(create_server(&reply_.sock_remote_, ipc) != 0) return -1;
+        if(create_server(&get<0>(get<1>(rep->socks_)), ipc, 62, rep, REPLY_TCP) != 0) return -1;
+        // printf("======>> create server for remote port %d\n", port);
     }else {
-        reply_.sock_remote_.id = numeric_limits<int32_t>::max();
+        get<0>(get<1>(rep->socks_)).id = numeric_limits<int32_t>::max();
+    }
+
+    if (!rep->t_unblock_){
+        rep->t_unblock_.reset(new thread(get_thread([](const auto rep){
+            constexpr int idle = 216;
+            const auto data = rr_unblocking_msg_.data();
+            const auto data_size = rr_unblocking_msg_.size();
+            constexpr int life_span = timeout_req_rep*10;
+
+            auto f = [rep]{
+                vector<struct work*> tmp{};
+                lock_guard<mutex> l{rep->mtx_msg_};
+                for(auto iter = rep->works_.begin(); iter != rep->works_.end();){
+                    if ((iter->second+=idle) > life_span){
+                        tmp.push_back(iter->second.w_);
+                        iter = rep->works_.erase(iter);
+                    }else {
+                        ++iter;
+                    }
+                }
+                return tmp;
+            };
+            while (!rep->t_quit_.load()) {
+                this_thread::sleep_for(chrono::milliseconds{idle});
+                vector<struct work*> tmp = f();
+                for(auto && w : tmp){
+                    aio_unblock(w, data, data_size);
+                }
+            }
+        }, rep)));
     }
 
     return 0;
 }
 
-int read_request(void** src, std::string* msg, const int to_ms){
+int read_request(void** src, std::string* msg, const int to_ms, void* arg/*=NULL*/){
+    _rr* rep = (_rr*)arg;
+    if (!rep) rep = singleton<_rr>();
 
-    if (reply_.sock_local_.id == 0 || reply_.sock_remote_.id == 0) {
-        if (start_reply(reply_.url_, reply_.port_) != 0)
+    if (get<0>(rep->socks_).id == 0 || get<0>(get<1>(rep->socks_)).id == 0)
+        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{};
-    string tmpmsg;
+    work* w{};
     {
-        unique_lock<mutex> l(reply_.mtx_msg_);
-        auto status = reply_.cv_msg_.wait_for(l, chrono::milliseconds{tm}, []{
-            return !reply_.msg_.empty();
-        });
-        if (!status){
-            PRNTVITAG("subscribe_read timeout");
-            return -1;
+        unique_lock<mutex> l(rep->mtx_msg_);
+        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;
+            }
         }
-        const auto& iter = reply_.msg_.begin();
+        auto iter = rep->msg_.begin();
         key = iter->first;
-        tmpmsg = iter->second;
-        reply_.msg_.erase(iter);
+        *msg = move(iter->second);
+        rep->msg_.erase(iter);
+        auto witer = rep->works_.find(key);
+        if (witer != rep->works_.end()){
+            w = witer->second;
+        }
     }
-    *msg = move(tmpmsg);
 
-    auto s = (uint64_t*)malloc(sizeof(uint64_t));
-    *s = key;
-    *src = s;
+    if (!w) return -1;
 
-    return 0;
+    *src = malloc(sizeof(key));
+    memcpy(*src, &key, sizeof(key));
+
+    return w->mode;
 }
 
-int send_reply(const void* src, const void* msg, const int msg_len){
+int send_reply(void* src, const void* msg, const int msg_len, void* arg/*=NULL*/){
+    _rr* rep = (_rr*)arg;
+    if (!rep) rep = singleton<_rr>();
+
     struct work* w{};
     {
-        auto key = *(static_cast<uint64_t*>(const_cast<void*>(src)));
+        uint64_t key;
+        memcpy(&key, src, sizeof(key));
+        // auto key = *(static_cast<uint64_t*>(const_cast<void*>(src)));
+        // free(src);
 
-        lock_guard<mutex> l{reply_.mtx_msg_};
-        auto iter = reply_.works_.find(key);
-        if (iter == reply_.works_.end()) return -1;
+        lock_guard<mutex> l{rep->mtx_msg_};
+        auto iter = rep->works_.find(key);
+        if (iter == rep->works_.end()) return -1;
         w = iter->second;
-        reply_.works_.erase(iter);
+        rep->works_.erase(iter);
     }
 
-    TAG;
-
-    nng_msg_alloc(&w->msg, 0);
-    nng_msg_append(w->msg, msg, msg_len);
-
-    nng_sleep_aio(0, w->aio);
+    aio_unblock(w, msg, msg_len);
 
     return 0;
 }
-
 
 }

--
Gitblit v1.8.0