rm_vision/tools/plotter.cpp
2025-12-15 02:33:20 +08:00

29 lines
722 B
C++

#include "plotter.hpp"
#include <arpa/inet.h> // htons, inet_addr
#include <sys/socket.h> // socket, sendto
#include <unistd.h> // close
namespace tools
{
Plotter::Plotter(std::string host, uint16_t port)
{
socket_ = ::socket(AF_INET, SOCK_DGRAM, 0);
destination_.sin_family = AF_INET;
destination_.sin_port = ::htons(port);
destination_.sin_addr.s_addr = ::inet_addr(host.c_str());
}
Plotter::~Plotter() { ::close(socket_); }
void Plotter::plot(const nlohmann::json & json)
{
std::lock_guard<std::mutex> lock(mutex_);
auto data = json.dump();
::sendto(
socket_, data.c_str(), data.length(), 0, reinterpret_cast<sockaddr *>(&destination_),
sizeof(destination_));
}
} // namespace tools