55 lines
1.6 KiB
C++
55 lines
1.6 KiB
C++
#include "NngClient.h"
|
|
#include "Core/Logger.h"
|
|
#include "Nng/SocketAisoWrapper.h"
|
|
#include <boost/json/object.hpp>
|
|
#include <boost/json/serialize.hpp>
|
|
#include <sstream>
|
|
|
|
NngClient::NngClient(boost::asio::io_context &ioContex) {
|
|
m_socket = std::make_shared<Nng::Asio::Socket>(ioContex, Nng::Request);
|
|
}
|
|
|
|
void NngClient::asyncRead() {
|
|
m_socket->asyncReceive([ptr{weak_from_this()}](const boost::system::error_code &error, const Nng::Buffer &buffer) {
|
|
if (error) {
|
|
LOG(error) << error.message();
|
|
return;
|
|
}
|
|
if (ptr.expired()) return;
|
|
auto self = ptr.lock();
|
|
// LOG(info) << "nng received: " << buffer.data<char>();
|
|
self->asyncRead();
|
|
});
|
|
}
|
|
|
|
void NngClient::start(const std::string &server, uint16_t port) {
|
|
std::ostringstream oss;
|
|
oss << "tcp://" << server << ":" << port;
|
|
std::error_code error;
|
|
m_socket->dial(oss.str(), error);
|
|
asyncRead();
|
|
}
|
|
|
|
void NngClient::requestZeroCheck() {
|
|
boost::json::object request;
|
|
request["command"] = "ZeroCheck";
|
|
auto json = boost::json::serialize(request);
|
|
m_socket->send(json.data(), json.size() + 1);
|
|
}
|
|
|
|
void NngClient::requestZoom(bool in) {
|
|
boost::json::object request;
|
|
request["command"] = "Zoom";
|
|
request["direction"] = in ? "In" : "Out";
|
|
auto json = boost::json::serialize(request);
|
|
m_socket->send(json.data(), json.size() + 1);
|
|
}
|
|
|
|
void NngClient::requestFocus(bool far) {
|
|
boost::json::object request;
|
|
request["command"] = "Focus";
|
|
request["direction"] = far ? "Far" : "Near";
|
|
auto json = boost::json::serialize(request);
|
|
m_socket->send(json.data(), json.size() + 1);
|
|
}
|