#include "mongoose.h" #include "stdio.h" #include "gtest/gtest.h" #include #include #include #define SCOPE_CHANNEL 4 class UdpScope { public: static UdpScope *scope[SCOPE_CHANNEL]; struct mg_mgr *mgr; struct mg_connection *con; std::string sendBuf; bool ready = false; static inline void Init(int port) { #ifdef NO_SCOPE return; #endif for (int i = 0; i < SCOPE_CHANNEL; i++) { scope[i] = new UdpScope(); scope[i]->mgr = (struct mg_mgr *)malloc(sizeof(struct mg_mgr)); mg_mgr_init(scope[i]->mgr); scope[i]->con = (struct mg_connection *)malloc(sizeof(struct mg_connection)); char con_str[256]; sprintf(con_str, "tcp://127.0.0.1:%d", port + i); scope[i]->con = mg_connect( scope[i]->mgr, con_str, [](struct mg_connection *c, int ev, void *ev_data, void *fn_data) {}, 0); mg_send(scope[i]->con, "\n", 1); mg_mgr_poll(scope[i]->mgr, 100); if (scope[i]->con->is_writable) { scope[i]->ready = true; } scope[i]->sendBuf.reserve(1400); } } static inline void Deinit() { #ifdef NO_SCOPE return; #endif for (int i = 0; i < SCOPE_CHANNEL; i++) { if (scope[i]->ready) { mg_send(scope[i]->con, scope[i]->sendBuf.c_str(), scope[i]->sendBuf.length()); mg_mgr_poll(scope[i]->mgr, 100); } scope[i]->sendBuf.clear(); mg_mgr_free(scope[i]->mgr); delete scope[i]; } } static inline void TrySend(int ch, std::string &frame) { if (scope[ch]->ready) { scope[ch]->sendBuf += frame; if (scope[ch]->sendBuf.length() > 1300) { mg_send(scope[ch]->con, scope[ch]->sendBuf.c_str(), scope[ch]->sendBuf.length()); mg_mgr_poll(scope[ch]->mgr, 100); scope[ch]->sendBuf.clear(); } } // mg_send(scope[ch]->con, frame.c_str(), frame.length()); } static inline void Send(int ch, double a) { #ifdef NO_SCOPE return; #endif char buf[256]; auto len = sprintf(buf, "%f\n", a); std::string frame(buf); TrySend(ch, frame); } static inline void Send(int ch, double a, double b) { #ifdef NO_SCOPE return; #endif char buf[256]; auto len = sprintf(buf, "%f, %f\n", a, b); std::string frame(buf); TrySend(ch, frame); } static inline void Send(int ch, double a, double b, double c) { #ifdef NO_SCOPE return; #endif char buf[256]; auto len = sprintf(buf, "%f, %f, %f\n", a, b, c); std::string frame(buf); TrySend(ch, frame); } static inline void Send(int ch, double a, double b, double c, double d) { #ifdef NO_SCOPE return; #endif char buf[256]; auto len = sprintf(buf, "%f, %f, %f, %f\n", a, b, c, d); std::string frame(buf); TrySend(ch, frame); } static inline void Send(int ch, double a, double b, double c, double d, double e) { #ifdef NO_SCOPE return; #endif char buf[256]; auto len = sprintf(buf, "%f, %f, %f, %f, %f\n", a, b, c, d, e); std::string frame(buf); TrySend(ch, frame); } static inline void Send(int ch, double a, double b, double c, double d, double e, double f) { #ifdef NO_SCOPE return; #endif char buf[256]; auto len = sprintf(buf, "%f, %f, %f, %f, %f, %f\n", a, b, c, d, e, f); std::string frame(buf); TrySend(ch, frame); } // template // static inline std::string CSend(const T& t) // { // double v = t; // return std::format("{:g}\n", v); // } // template // static inline std::string CSend(const T& t, const Args&... rest) // { // double v = t; // return std::format("{:g},", v) + CSend(rest...); // } // template // static inline void Send(int ch, Args... args) // { // std::string str = CSend(args...); // // std::cout << str << std::endl; // mg_printf(scope[ch]->con, "%s", str.c_str()); // mg_mgr_poll(scope[ch]->mgr, 1); // } };