From fe39fc1a609d1d1282d3df81cc484d78638538eb Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期四, 30 十二月 2021 17:48:09 +0800
Subject: [PATCH] performance

---
 src/nng_wrap.cpp |   17 ++++++++++-------
 1 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/nng_wrap.cpp b/src/nng_wrap.cpp
index 3ae3f62..d5e1928 100644
--- a/src/nng_wrap.cpp
+++ b/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{};
@@ -209,19 +209,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) == 0){
+                            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));
@@ -407,6 +406,7 @@
     case SEND:
         if ((rv = nng_aio_result(work->aio)) != 0) {
             nng_msg_free(work->msg);
+            work->msg = NULL;
         }
         work->state = RECV;
         nng_ctx_recv(work->ctx, work->aio);
@@ -445,9 +445,11 @@
     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;
@@ -473,6 +475,7 @@
     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);

--
Gitblit v1.8.0