123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #include "mongoose.h"
- #include "stdio.h"
- #include "gtest/gtest.h"
- #include <tuple>
- #include <string>
- #include <iostream>
- #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<typename T, typename... Args>
- // static inline std::string CSend(const T& t)
- // {
- // double v = t;
- // return std::format("{:g}\n", v);
- // }
- // template<typename T, typename... Args>
- // static inline std::string CSend(const T& t, const Args&... rest)
- // {
- // double v = t;
- // return std::format("{:g},", v) + CSend(rest...);
- // }
- // template<typename... Args>
- // 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);
- // }
- };
|