mesytec-mnode/src/pub_producer.cc

37 lines
886 B
C++
Raw Normal View History

#include "common.h"
#include <nng/protocol/pubsub0/pub.h>
#include <thread>
#include <iostream>
int main(int argc, char *argv[])
{
spdlog::set_level(spdlog::level::info);
nng_socket pubSocket = NNG_SOCKET_INITIALIZER;
if (int res = nng_pub0_open(&pubSocket))
mesy_nng_fatal("nng_pub0_open", res);
if (int res = nng_listen(pubSocket, "tcp://*:42777", nullptr, 0))
mesy_nng_fatal("nng_listen tcp", res);
while (true)
{
std::cout << "Enter size of pub message:";
size_t size = 0;
std::cin >> size;
fmt::print("Publishing message of size {}\n", size);
nng_msg *msg = nullptr;
if (int res = nng_msg_alloc(&msg, size))
mesy_nng_fatal("nng_msg_alloc", res);
if (auto res = nng_sendmsg(pubSocket, msg, 0))
mesy_nng_fatal("nng_sendmsg", res);
}
return 0;
}