From 4c940d4e0abdb3fe3ee284f41806ddfdea1257be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20L=C3=BCke?= Date: Sat, 7 Dec 2024 18:33:15 +0100 Subject: [PATCH] nng: move code into impl --- .clang-format | 6 +-- include/mesytec-mnode/mnode_nng.h | 89 +++++-------------------------- src/mnode_nng.cc | 54 +++++++++++++++++++ 3 files changed, 71 insertions(+), 78 deletions(-) diff --git a/.clang-format b/.clang-format index 458dedc..166daa3 100644 --- a/.clang-format +++ b/.clang-format @@ -78,10 +78,10 @@ BreakBeforeConceptDeclarations: Always BreakBeforeBraces: Custom BreakBeforeInlineASMColon: OnlyMultiline BreakBeforeTernaryOperators: true -BreakConstructorInitializers: BeforeColon +BreakConstructorInitializers: BeforeComma BreakInheritanceList: BeforeColon BreakStringLiterals: true -ColumnLimit: 80 +ColumnLimit: 100 CommentPragmas: '^ IWYU pragma:' CompactNamespaces: false ConstructorInitializerIndentWidth: 4 @@ -193,7 +193,7 @@ SpaceBeforeParensOptions: AfterRequiresInClause: false AfterRequiresInExpression: false BeforeNonEmptyParentheses: false -SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeRangeBasedForLoopColon: false SpaceBeforeSquareBrackets: false SpaceInEmptyBlock: false SpaceInEmptyParentheses: false diff --git a/include/mesytec-mnode/mnode_nng.h b/include/mesytec-mnode/mnode_nng.h index 38d6772..dd88a0c 100644 --- a/include/mesytec-mnode/mnode_nng.h +++ b/include/mesytec-mnode/mnode_nng.h @@ -161,53 +161,27 @@ void log_pipe_info(nng_pipe p, const char *info_text); // 'nng_res' is the result of the last nng function call. using retry_predicate = std::function; -class RetryNTimes -{ - public: - RetryNTimes(size_t maxTries = 3) - : maxTries_(maxTries) - {} - - bool operator()(int) - { - return attempt_++ < maxTries_; - } - - private: - size_t maxTries_; - size_t attempt_ = 0u; -}; - int send_message_retry(nng_socket socket, nng_msg *msg, retry_predicate rp, const char *debugInfo = ""); +struct RetryNTimes +{ + explicit RetryNTimes(size_t maxTries = 3) : maxTries(maxTries) {} + bool operator()(int) { return attempt++ < maxTries; } + + size_t maxTries; + size_t attempt = 0u; +}; + inline int send_message_retry(nng_socket socket, nng_msg *msg, size_t maxTries = 3, const char *debugInfo = "") { - RetryNTimes retryPredicate(maxTries); - - return send_message_retry(socket, msg, retryPredicate, debugInfo); + return send_message_retry(socket, msg, RetryNTimes(maxTries), debugInfo); } -inline int send_empty_message(nng_socket socket, retry_predicate rp) -{ - nng_msg *msg = nullptr; - - if (int res = allocate_reserve_message(&msg, 0)) - return res; - - if (int res = send_message_retry(socket, msg, rp, "send_empty_message")) - { - nng_msg_free(msg); - return res; - } - - return 0; -} +int send_empty_message(nng_socket socket, retry_predicate rp); inline int send_empty_message(nng_socket socket, size_t maxTries = 3) { - RetryNTimes retryPredicate(maxTries); - - return send_empty_message(socket, retryPredicate); + return send_empty_message(socket, RetryNTimes(maxTries)); } // Read type T from the front of msg and trim the message by sizeof(T). @@ -225,43 +199,8 @@ std::optional msg_trim_read(nng_msg *msg) return result; } -inline const char *nng_stat_unit_to_string(int unit) -{ - switch (unit) - { - case NNG_UNIT_BYTES: - return "B"; - case NNG_UNIT_MESSAGES: - return "msgs"; - case NNG_UNIT_MILLIS: - return "ms"; - case NNG_UNIT_EVENTS: - return "events"; - } - - return ""; -} - -inline const char *nng_stat_type_to_string(int type) -{ - switch (type) - { - case NNG_STAT_SCOPE: - return "scope"; - case NNG_STAT_LEVEL: - return "level"; - case NNG_STAT_COUNTER: - return "counter"; - case NNG_STAT_STRING: - return "string"; - case NNG_STAT_BOOLEAN: - return "bool"; - case NNG_STAT_ID: - return "id"; - } - - return ""; -} +const char *nng_stat_unit_to_string(int nng_unit); +const char *nng_stat_type_to_string(int nng_stat_type); // Important: as of nng-1.8.0 sockets stats are only implemented for pair1 type // sockets! diff --git a/src/mnode_nng.cc b/src/mnode_nng.cc index 9f2eedf..c108c8a 100644 --- a/src/mnode_nng.cc +++ b/src/mnode_nng.cc @@ -38,6 +38,44 @@ void log_pipe_info(nng_pipe p, const char *info_text) spdlog::info("{}: {}={}", info_text, NNG_OPT_REMADDR, remoteAddress); } +const char *nng_stat_unit_to_string(int nng_unit) +{ + switch (nng_unit) + { + case NNG_UNIT_BYTES: + return "B"; + case NNG_UNIT_MESSAGES: + return "msgs"; + case NNG_UNIT_MILLIS: + return "ms"; + case NNG_UNIT_EVENTS: + return "events"; + } + + return nullptr; +} + +const char *nng_stat_type_to_string(int nng_stat_type) +{ + switch (nng_stat_type) + { + case NNG_STAT_SCOPE: + return "scope"; + case NNG_STAT_LEVEL: + return "level"; + case NNG_STAT_COUNTER: + return "counter"; + case NNG_STAT_STRING: + return "string"; + case NNG_STAT_BOOLEAN: + return "bool"; + case NNG_STAT_ID: + return "id"; + } + + return nullptr; +} + template std::string format_stat(int type, const char *name, const char *desc, std::uint64_t ts, Value value, int unit) { @@ -85,6 +123,22 @@ int send_message_retry(nng_socket socket, nng_msg *msg, retry_predicate rp, cons return 0; } +int send_empty_message(nng_socket socket, retry_predicate rp) +{ + nng_msg *msg = nullptr; + + if (int res = allocate_reserve_message(&msg, 0)) + return res; + + if (int res = send_message_retry(socket, msg, rp, "send_empty_message")) + { + nng_msg_free(msg); + return res; + } + + return 0; +} + nng_socket make_pair_socket(nng_duration timeout) { return make_socket(nng_pair0_open, timeout);