2023-02-08 06:39:23 +01:00
|
|
|
#include "dp_common.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <nng/protocol/pubsub0/pub.h>
|
|
|
|
#include <nng/protocol/pubsub0/sub.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
void dp_nng_fatal(const char *const msg, int rv)
|
|
|
|
{
|
2023-02-11 23:52:20 +01:00
|
|
|
log_fatal("%s: %s", msg, nng_strerror(rv));
|
2023-02-08 06:39:23 +01:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void dp_errno_fatal(const char *const msg)
|
|
|
|
{
|
2023-02-11 23:52:20 +01:00
|
|
|
log_fatal("%s: %s", msg, strerror(errno));
|
2023-02-08 06:39:23 +01:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2023-02-11 23:52:20 +01:00
|
|
|
void dp_nng_init_limits(int ncpu_max, int pool_thread_limit_max, int resolv_thread_limit)
|
|
|
|
{
|
|
|
|
nng_set_ncpu_max(ncpu_max);
|
|
|
|
nng_set_pool_thread_limit_max(pool_thread_limit_max);
|
|
|
|
nng_set_resolve_thread_max(resolv_thread_limit);
|
|
|
|
}
|
|
|
|
|
2023-02-08 06:39:23 +01:00
|
|
|
nng_socket make_ctrl_pub(const char *url)
|
|
|
|
{
|
|
|
|
nng_socket sock;
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
if ((res = nng_pub0_open(&sock)))
|
|
|
|
dp_nng_fatal("make_ctrl_pub/nng_pub0_open", res);
|
|
|
|
|
|
|
|
if ((res = nng_listen(sock, url, NULL, 0)))
|
|
|
|
dp_nng_fatal("make_ctrl_pub/nng_listen", res);
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
nng_socket make_ctrl_sub(const char *url)
|
|
|
|
{
|
|
|
|
nng_socket sock;
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
if ((res = nng_sub0_open(&sock)))
|
|
|
|
dp_nng_fatal("make_ctrl_sub/nng_sub0_open", res);
|
|
|
|
|
2023-02-11 23:52:20 +01:00
|
|
|
if ((res = nng_socket_set_string(sock, NNG_OPT_SUB_SUBSCRIBE, "")))
|
2023-02-08 06:39:23 +01:00
|
|
|
dp_nng_fatal("make_ctrl_sub/subscribe", res);
|
|
|
|
|
2023-02-11 23:52:20 +01:00
|
|
|
if ((res = nng_socket_set_ms(sock, NNG_OPT_RECVTIMEO, 100)))
|
|
|
|
dp_nng_fatal("make_ctrl_sub/recvtimeo", res);
|
|
|
|
|
|
|
|
|
2023-02-08 06:39:23 +01:00
|
|
|
if ((res = nng_listen(sock, url, NULL, 0)))
|
|
|
|
dp_nng_fatal("make_ctrl_sub/nng_listen", res);
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
nng_socket make_doom_pub(const char *url)
|
|
|
|
{
|
|
|
|
nng_socket sock;
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
if ((res = nng_pub0_open(&sock)))
|
|
|
|
dp_nng_fatal("make_doom_pub/nng_pub0_open", res);
|
|
|
|
|
|
|
|
if ((res = nng_dial(sock, url, NULL, NNG_FLAG_NONBLOCK)))
|
|
|
|
dp_nng_fatal("make_doom_pub/nng_dial", res);
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
nng_socket make_doom_sub(const char *url)
|
|
|
|
{
|
|
|
|
nng_socket sock;
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
if ((res = nng_sub0_open(&sock)))
|
|
|
|
dp_nng_fatal("make_doom_sub/nng_sub0_open", res);
|
|
|
|
|
2023-02-11 23:52:20 +01:00
|
|
|
if ((res = nng_socket_set_string(sock, NNG_OPT_SUB_SUBSCRIBE, "")))
|
2023-02-08 06:39:23 +01:00
|
|
|
dp_nng_fatal("make_doom_sub/subscribe", res);
|
|
|
|
|
2023-02-11 23:52:20 +01:00
|
|
|
if ((res = nng_socket_set_ms(sock, NNG_OPT_RECVTIMEO, 100)))
|
|
|
|
dp_nng_fatal("make_doom_sub/recvtimeo", res);
|
|
|
|
|
2023-02-08 06:39:23 +01:00
|
|
|
if ((res = nng_dial(sock, url, NULL, NNG_FLAG_NONBLOCK)))
|
|
|
|
dp_nng_fatal("make_doom_sub/nng_dial", res);
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
2023-02-12 15:48:15 +01:00
|
|
|
|
|
|
|
const char *const DP_DoomState_Strings[DP_DS_COUNT] =
|
|
|
|
{
|
|
|
|
"DoomState_Ready",
|
|
|
|
"DoomState_Running",
|
|
|
|
"DoomState_Quit",
|
|
|
|
};
|