nng: move code into impl

This commit is contained in:
Florian Lüke 2024-12-07 18:33:15 +01:00
parent 4e4606c454
commit 4c940d4e0a
3 changed files with 71 additions and 78 deletions

View file

@ -78,10 +78,10 @@ BreakBeforeConceptDeclarations: Always
BreakBeforeBraces: Custom BreakBeforeBraces: Custom
BreakBeforeInlineASMColon: OnlyMultiline BreakBeforeInlineASMColon: OnlyMultiline
BreakBeforeTernaryOperators: true BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeColon BreakInheritanceList: BeforeColon
BreakStringLiterals: true BreakStringLiterals: true
ColumnLimit: 80 ColumnLimit: 100
CommentPragmas: '^ IWYU pragma:' CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false CompactNamespaces: false
ConstructorInitializerIndentWidth: 4 ConstructorInitializerIndentWidth: 4
@ -193,7 +193,7 @@ SpaceBeforeParensOptions:
AfterRequiresInClause: false AfterRequiresInClause: false
AfterRequiresInExpression: false AfterRequiresInExpression: false
BeforeNonEmptyParentheses: false BeforeNonEmptyParentheses: false
SpaceBeforeRangeBasedForLoopColon: true SpaceBeforeRangeBasedForLoopColon: false
SpaceBeforeSquareBrackets: false SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false SpaceInEmptyParentheses: false

View file

@ -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. // 'nng_res' is the result of the last nng function call.
using retry_predicate = std::function<bool (int nng_res)>; using retry_predicate = std::function<bool (int nng_res)>;
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 = ""); 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 = "") 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, RetryNTimes(maxTries), debugInfo);
return send_message_retry(socket, msg, retryPredicate, debugInfo);
} }
inline int send_empty_message(nng_socket socket, retry_predicate rp) 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;
}
inline int send_empty_message(nng_socket socket, size_t maxTries = 3) inline int send_empty_message(nng_socket socket, size_t maxTries = 3)
{ {
RetryNTimes retryPredicate(maxTries); return send_empty_message(socket, RetryNTimes(maxTries));
return send_empty_message(socket, retryPredicate);
} }
// Read type T from the front of msg and trim the message by sizeof(T). // Read type T from the front of msg and trim the message by sizeof(T).
@ -225,43 +199,8 @@ std::optional<T> msg_trim_read(nng_msg *msg)
return result; return result;
} }
inline const char *nng_stat_unit_to_string(int unit) const char *nng_stat_unit_to_string(int nng_unit);
{ const char *nng_stat_type_to_string(int nng_stat_type);
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 "";
}
// Important: as of nng-1.8.0 sockets stats are only implemented for pair1 type // Important: as of nng-1.8.0 sockets stats are only implemented for pair1 type
// sockets! // sockets!

View file

@ -38,6 +38,44 @@ void log_pipe_info(nng_pipe p, const char *info_text)
spdlog::info("{}: {}={}", info_text, NNG_OPT_REMADDR, remoteAddress); 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<typename Value> template<typename Value>
std::string format_stat(int type, const char *name, const char *desc, std::uint64_t ts, Value value, int unit) 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; 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) nng_socket make_pair_socket(nng_duration timeout)
{ {
return make_socket(nng_pair0_open, timeout); return make_socket(nng_pair0_open, timeout);