zhangmeng
2022-08-04 d90b3c3779d9c6bd2884540621d107e4ac10e930
src/nng_wrap.cpp
@@ -423,7 +423,7 @@
static void cb_recv_for_aio(work* w){
    nng_msg *om = w->msg;
    if (!om) return;
    if (!om) {nng_sleep_aio(0, w->aio); return;}
    _rr* rep = (_rr*)w->user_data;
@@ -438,7 +438,7 @@
    rep->cv_msg_.notify_all();
}
static struct work *alloc_work(nng_socket sock, _rr* rep)
static struct work *alloc_work(nng_socket sock, _rr* rep, const int mode)
{
    struct work *w;
    int          rv;
@@ -458,10 +458,12 @@
        return NULL;
    }
    w->state = INIT;
    w->mode = mode;
    return (w);
}
static int create_server(nng_socket* sock, const string& url, const int count, _rr* rep){
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;
@@ -474,7 +476,7 @@
    work** works = (work**)malloc(sizeof(void*) * count);
    for (int i = 0; i < count; i++) {
        works[i] = alloc_work(*sock, rep);
        works[i] = alloc_work(*sock, rep, mode);
    }
    remove_exist(url);
@@ -511,12 +513,12 @@
        ipc = url;
    }
    rep->url_ = ipc;
    if(create_server(&get<0>(rep->socks_), ipc, 62, rep) != 0) return -1;
    if(create_server(&get<0>(rep->socks_), ipc, 62, rep, REPLY_IPC) != 0) return -1;
    if (port > 0){
        get<1>(get<1>(rep->socks_)) = port;
        ipc = "tcp://0.0.0.0:" + to_string(port);
        if(create_server(&get<0>(get<1>(rep->socks_)), ipc, 62, rep) != 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 {
        get<0>(get<1>(rep->socks_)).id = numeric_limits<int32_t>::max();
@@ -566,6 +568,7 @@
    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]{
@@ -579,12 +582,18 @@
        key = iter->first;
        *msg = move(iter->second);
        rep->msg_.erase(iter);
        auto witer = rep->works_.find(key);
        if (witer != rep->works_.end()){
            w = witer->second;
    }
    }
    if (!w) return -1;
    *src = malloc(sizeof(uint64_t));
    *(uint64_t*)(*src) = key;
    return 0;
    return w->mode;
}
int send_reply(const void* src, const void* msg, const int msg_len, void* arg/*=NULL*/){