YouDunIt/modules/you_done_it/ydi_networking.cpp

83 lines
2.4 KiB
C++

#include "ydi_networking.h"
#include "core/core_bind.h"
#include "core/object/class_db.h"
#include "core/string/print_string.h"
void NetworkData::_bind_methods() {
BIND_ENUM_CONSTANT(CLUE_FIRST);
BIND_ENUM_CONSTANT(CLUE_SECOND);
BIND_ENUM_CONSTANT(CLUE_MAX);
BIND_ENUM_CONSTANT(CONNECTION_DISCONNECTED);
BIND_ENUM_CONSTANT(CONNECTION_CONNECTED);
BIND_ENUM_CONSTANT(CONNECTION_AUTHENTICATED);
}
namespace ydi {
int to_int(zmq::message_t const &msg, int failure) {
try {
return std::stoi(msg.to_string());
} catch (...) {
return failure;
}
}
NetworkData::MessageType to_message_type(zmq::message_t const &msg) {
return (NetworkData::MessageType)to_int(msg, (int)NetworkData::MSG_INVALID);
}
NetworkData::NOKReason to_nok_reason(zmq::message_t const &msg) {
return (NetworkData::NOKReason)to_int(msg, (int)NetworkData::NOK_INVALID_REASON);
}
NetworkData::ClueID to_clue_id(zmq::message_t const &msg) {
return (NetworkData::ClueID)to_int(msg, NetworkData::CLUE_MAX);
}
void print_message_contents(zmq::multipart_t const &mpart) {
print_line("multipart message:");
for (zmq::message_t const &msg : mpart) {
print_line(" - ", msg.to_string().c_str());
}
}
void extend_multipart(zmq::multipart_t &mpart, NetworkData::MessageType type) {
mpart.addstr(std::to_string((int)type));
}
void extend_multipart(zmq::multipart_t &mpart, NetworkData::ClueID id) {
mpart.addstr(std::to_string(id));
}
void extend_multipart(zmq::multipart_t &mpart, std::string const &string) {
mpart.addstr(string);
}
void extend_multipart(zmq::multipart_t &mpart, std::string_view const &strv) {
mpart.addmem(strv.data(), strv.size());
}
void extend_multipart(zmq::multipart_t &mpart, char const *cstr) {
mpart.addstr(cstr);
}
void extend_multipart(zmq::multipart_t &mpart, int const &arg) {
std::string as_string{ std::to_string(arg) };
mpart.addstr(as_string);
}
void extend_multipart(zmq::multipart_t &mpart, zmq::multipart_t const &right) {
mpart.append(right.clone());
}
void extend_multipart(zmq::multipart_t &msg, std::pair<zmq::multipart_t::iterator, zmq::multipart_t::iterator> range) {
for (; range.first != range.second; ++range.first) {
msg.addstr(range.first->to_string());
}
}
void extend_multipart(zmq::multipart_t &msg, std::pair<zmq::multipart_t::const_iterator, zmq::multipart_t::const_iterator> range) {
for (; range.first != range.second; ++range.first) {
msg.addstr(range.first->to_string());
}
}
} //namespace ydi