From cf0a3209b51babf72469d962914db0dac2e5f52c Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期二, 27 十二月 2022 14:13:30 +0800
Subject: [PATCH] add get msg timeout

---
 main.cpp |   38 +++++++++++++++++++++-----------------
 1 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/main.cpp b/main.cpp
index 84f63fb..9f400b8 100644
--- a/main.cpp
+++ b/main.cpp
@@ -9,7 +9,7 @@
 #include "cbhomeclient.h"
 #include "message.h"
 
-// #include "3dparty/bus_nng/bn_api.h"
+// #include "3rdparty/bus_nng/bn_api.h"
 #include "bhome_msg_api.pb.h"
 using namespace bhome_msg;
 
@@ -18,11 +18,11 @@
 static void pub(const vector<string>& topics){
     ignoref(pub);
 
-    vector<const char*> pubtpc;
-    for(auto& t : topics) pubtpc.push_back(t.c_str());
+    vector<const char*> tpc;
+    for(auto& t : topics) tpc.push_back(t.c_str());
 
     creg* reg = make_creg(make_cproc("pub", "pubid"),
-        NULL, 0, &pubtpc[0], pubtpc.size(), NULL, 0, NULL, 0);
+        NULL, 0, &tpc[0], tpc.size(), NULL, 0, NULL, 0);
 
     void* handle = bus_client_init(NULL, 0, reg);
     creg_free(reg);
@@ -41,7 +41,7 @@
 
             int ret = bus_client_publish(handle, i.data(), i.size(), msg.data(), msg.size());
             printf("======>> bus_client_pubmsg [%s] ret %d\n", msg.c_str(), ret);
-            this_thread::sleep_for(chrono::seconds{2});
+            this_thread::sleep_for(chrono::seconds{1});
         }
     }
 }
@@ -49,11 +49,11 @@
 static void sub(const vector<string>& topics){
     ignoref(sub);
 
-    vector<const char*> pubtpc;
-    for(auto& t : topics) pubtpc.push_back(t.c_str());
+    vector<const char*> tpc;
+    for(auto& t : topics) tpc.push_back(t.c_str());
 
     creg* reg = make_creg(make_cproc("sub", "subid"),
-        NULL, 0, NULL, 0, &pubtpc[0], pubtpc.size(), NULL, 0);
+        NULL, 0, NULL, 0, &tpc[0], tpc.size(), NULL, 0);
 
     void* handle = bus_client_init(NULL, 0, reg);
     creg_free(reg);
@@ -61,6 +61,7 @@
     while (true) {
         auto msg = bus_client_get_submsg(handle);
         printf("SUB msg topic [%s] data [%s]\n", msg->topic, msg->msg);
+        free_submsg(msg);
     }
 
     bus_client_free(handle);
@@ -70,17 +71,16 @@
     ignoref(req);
 
     const auto topicl = strlen(topic);
-    creg* reg = make_creg(make_cproc("request", "requestid"),
-        NULL, 0, NULL, 0, NULL, 0, NULL, 0);
+    creg* reg = make_creg_from_cproc(make_cproc("request", "requestid"));
 
     void* handle = bus_client_init(NULL, 0, reg);
     creg_free(reg);
 
     size_t count = 0;
-    string base_msg("test_request==");
+    string base_msg("test_request==request message -> msg-");
     this_thread::sleep_for(chrono::seconds(3));
     while (true) {
-        auto msg = base_msg + "request message -> msg-"+to_string(count++);
+        auto msg = base_msg + to_string(count++);
         auto reqmsg = make_req_msg(topic, topicl, msg.data(), msg.size());
         crepmsg* repmsg = NULL;
         if (bus_client_request(handle, reqmsg, &repmsg)){
@@ -96,10 +96,14 @@
     ignoref(reply);
 
     const auto topicl = strlen(topic);
-    vector<const char*> pubtpc{topic};
+    vector<const char*> tpc{topic};
 
-    creg* reg = make_creg(make_cproc("reply", "replyid"),
-        &pubtpc[0], pubtpc.size(), &pubtpc[0], pubtpc.size(), NULL, 0, NULL, 0);
+    // creg* reg = make_creg(make_cproc("reply", "replyid"),
+    //     &tpc[0], tpc.size(), &tpc[0], tpc.size(), NULL, 0, NULL, 0);
+
+    creg* reg = make_creg_from_cproc(make_cproc("reply", "replyid"));
+    creg_add_topic_reply(reg, tpc.data(), tpc.size());
+    creg_add_topic_pub(reg, tpc.data(), tpc.size());
 
     void* handle = bus_client_init(NULL, 0, reg);
     creg_free(reg);
@@ -115,7 +119,7 @@
         printf("REPREQ msg [%s] \n", msg->msg);
 
         free_reqmsg(msg);
-        this_thread::sleep_for(chrono::seconds{2});
+        // this_thread::sleep_for(chrono::seconds{2});
     }
 }
 
@@ -134,4 +138,4 @@
     reply(rrtopic);
 
     return 0;
-}
\ No newline at end of file
+}

--
Gitblit v1.8.0