zhangmeng
2022-01-17 b4bda4a8e6aa1acf5e161a95642e944e08c72646
src/nng_wrap.cpp
@@ -138,7 +138,7 @@
                msg = &pub->msg_.front();
                if (msg->topic_.empty()) {pub->msg_.pop_front(); continue;}
            }
            string sndmsg(msg->topic_ + msg->data_);
            string sndmsg = (string{msg->topic_}+='\0')+=msg->data_;
            int rc = nn_send(pub->socket_, sndmsg.data(), sndmsg.size(), 0);
            if (rc == (int)sndmsg.size()){
                char* tmp{};
@@ -162,6 +162,7 @@
}
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>();
@@ -209,19 +210,18 @@
            if (m_len > 0){
                string tmp_msg{m, (size_t)m_len};
                nn_freemsg(m);
                string topic{}, msg{};
                const auto topic{tmp_msg.c_str()};
                string msg{};
                {
                    lock_guard<mutex> l{(*sub)()};
                    for(auto && i : sub->topics_){
                        if (tmp_msg.size() < i.size()) continue;
                        topic = move(tmp_msg.substr(0, i.size()));
                        if (topic == i){
                            msg = move(tmp_msg.substr(i.size()));
                        if (!!!i.compare(topic)){
                            msg = move(tmp_msg.substr(i.size()+1));
                            break;
                        }
                    }
                }
                printf("======>> subscribe recv topic %s msg length %lu\n", topic.c_str(), msg.length());
                printf("======>> subscribe recv topic %s msg length %lu\n", topic, msg.length());
                if (!msg.empty()){
                    lock_guard<mutex> l(sub->mtx_msg_);
                    sub->msg_.emplace_back(topic, move(msg));
@@ -513,6 +513,7 @@
        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;
        printf("======>> create server for remote port %d\n", port);
    }else {
        get<0>(get<1>(rep->socks_)).id = numeric_limits<int32_t>::max();
    }
@@ -565,7 +566,7 @@
            return !rep->msg_.empty();
        });
        if (!status){
            PRNTVITAG("subscribe_read timeout");
            PRNTVITAG("read_request timeout");
            return -1;
        }
        auto iter = rep->msg_.begin();