80 lines
2.1 KiB
C
80 lines
2.1 KiB
C
|
|
#pragma once
|
||
|
|
|
||
|
|
#include <nng/nng.h>
|
||
|
|
#include <nng/protocol/pubsub0/pub.h>
|
||
|
|
#include <nng/protocol/pubsub0/sub.h>
|
||
|
|
#include "common.h"
|
||
|
|
|
||
|
|
inline std::string socket_get_string_opt(nng_socket s, const char *opt)
|
||
|
|
{
|
||
|
|
char *dest = nullptr;
|
||
|
|
|
||
|
|
if (nng_socket_get_string(s, opt, &dest))
|
||
|
|
return {};
|
||
|
|
|
||
|
|
std::string result{*dest};
|
||
|
|
nng_strfree(dest);
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
inline std::string pipe_get_string_opt(nng_pipe p, const char *opt)
|
||
|
|
{
|
||
|
|
char *dest = nullptr;
|
||
|
|
|
||
|
|
if (nng_pipe_get_string(p, opt, &dest))
|
||
|
|
return {};
|
||
|
|
|
||
|
|
std::string result{*dest};
|
||
|
|
nng_strfree(dest);
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
inline void log_socket_info(nng_socket s, const char *info)
|
||
|
|
{
|
||
|
|
auto sockName = socket_get_string_opt(s, NNG_OPT_SOCKNAME);
|
||
|
|
auto localAddress = socket_get_string_opt(s, NNG_OPT_LOCADDR);
|
||
|
|
auto remoteAddress = socket_get_string_opt(s, NNG_OPT_REMADDR);
|
||
|
|
|
||
|
|
spdlog::info("{}: {}={}", info, NNG_OPT_SOCKNAME, sockName);
|
||
|
|
spdlog::info("{}: {}={}", info, NNG_OPT_LOCADDR, localAddress);
|
||
|
|
spdlog::info("{}: {}={}", info, NNG_OPT_REMADDR, remoteAddress);
|
||
|
|
}
|
||
|
|
|
||
|
|
inline void log_pipe_info(nng_pipe p, const char *info)
|
||
|
|
{
|
||
|
|
auto sockName = pipe_get_string_opt(p, NNG_OPT_SOCKNAME);
|
||
|
|
auto localAddress = pipe_get_string_opt(p, NNG_OPT_LOCADDR);
|
||
|
|
auto remoteAddress = pipe_get_string_opt(p, NNG_OPT_REMADDR);
|
||
|
|
|
||
|
|
spdlog::info("{}: {}={}", info, NNG_OPT_SOCKNAME, sockName);
|
||
|
|
spdlog::info("{}: {}={}", info, NNG_OPT_LOCADDR, localAddress);
|
||
|
|
spdlog::info("{}: {}={}", info, NNG_OPT_REMADDR, remoteAddress);
|
||
|
|
}
|
||
|
|
|
||
|
|
inline int send_message_retry(nng_socket socket, nng_msg *msg, size_t maxTries = 0, const char *debugInfo = "")
|
||
|
|
{
|
||
|
|
int res = 0;
|
||
|
|
size_t attempt = 0u;
|
||
|
|
|
||
|
|
do
|
||
|
|
{
|
||
|
|
res = nng_sendmsg(socket, msg, 0);
|
||
|
|
|
||
|
|
if (res)
|
||
|
|
{
|
||
|
|
if (res != NNG_ETIMEDOUT)
|
||
|
|
return res;
|
||
|
|
|
||
|
|
if (res == NNG_ETIMEDOUT)
|
||
|
|
spdlog::warn("send_message_retry: {} - send timeout", debugInfo);
|
||
|
|
|
||
|
|
if (maxTries > 0 && attempt >= maxTries)
|
||
|
|
return res;
|
||
|
|
|
||
|
|
++attempt;
|
||
|
|
}
|
||
|
|
} while (res == NNG_ETIMEDOUT);
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|