scope.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "mongoose.h"
  2. #include "stdio.h"
  3. #include "gtest/gtest.h"
  4. #include <tuple>
  5. #include <string>
  6. #include <iostream>
  7. #define SCOPE_CHANNEL 4
  8. class UdpScope
  9. {
  10. public:
  11. static UdpScope *scope[SCOPE_CHANNEL];
  12. struct mg_mgr *mgr;
  13. struct mg_connection *con;
  14. std::string sendBuf;
  15. bool ready = false;
  16. static inline void Init(int port)
  17. {
  18. #ifdef NO_SCOPE
  19. return;
  20. #endif
  21. for (int i = 0; i < SCOPE_CHANNEL; i++)
  22. {
  23. scope[i] = new UdpScope();
  24. scope[i]->mgr = (struct mg_mgr *)malloc(sizeof(struct mg_mgr));
  25. mg_mgr_init(scope[i]->mgr);
  26. scope[i]->con = (struct mg_connection *)malloc(sizeof(struct mg_connection));
  27. char con_str[256];
  28. sprintf(con_str, "tcp://127.0.0.1:%d", port + i);
  29. scope[i]->con = mg_connect(
  30. scope[i]->mgr, con_str, [](struct mg_connection *c, int ev, void *ev_data, void *fn_data) {}, 0);
  31. mg_send(scope[i]->con, "\n", 1);
  32. mg_mgr_poll(scope[i]->mgr, 100);
  33. if (scope[i]->con->is_writable)
  34. {
  35. scope[i]->ready = true;
  36. }
  37. scope[i]->sendBuf.reserve(1400);
  38. }
  39. }
  40. static inline void Deinit()
  41. {
  42. #ifdef NO_SCOPE
  43. return;
  44. #endif
  45. for (int i = 0; i < SCOPE_CHANNEL; i++)
  46. {
  47. if (scope[i]->ready)
  48. {
  49. mg_send(scope[i]->con, scope[i]->sendBuf.c_str(), scope[i]->sendBuf.length());
  50. mg_mgr_poll(scope[i]->mgr, 100);
  51. }
  52. scope[i]->sendBuf.clear();
  53. mg_mgr_free(scope[i]->mgr);
  54. delete scope[i];
  55. }
  56. }
  57. static inline void TrySend(int ch, std::string &frame)
  58. {
  59. if (scope[ch]->ready)
  60. {
  61. scope[ch]->sendBuf += frame;
  62. if (scope[ch]->sendBuf.length() > 1300)
  63. {
  64. mg_send(scope[ch]->con, scope[ch]->sendBuf.c_str(), scope[ch]->sendBuf.length());
  65. mg_mgr_poll(scope[ch]->mgr, 100);
  66. scope[ch]->sendBuf.clear();
  67. }
  68. }
  69. // mg_send(scope[ch]->con, frame.c_str(), frame.length());
  70. }
  71. static inline void Send(int ch, double a)
  72. {
  73. #ifdef NO_SCOPE
  74. return;
  75. #endif
  76. char buf[256];
  77. auto len = sprintf(buf, "%f\n", a);
  78. std::string frame(buf);
  79. TrySend(ch, frame);
  80. }
  81. static inline void Send(int ch, double a, double b)
  82. {
  83. #ifdef NO_SCOPE
  84. return;
  85. #endif
  86. char buf[256];
  87. auto len = sprintf(buf, "%f, %f\n", a, b);
  88. std::string frame(buf);
  89. TrySend(ch, frame);
  90. }
  91. static inline void Send(int ch, double a, double b, double c)
  92. {
  93. #ifdef NO_SCOPE
  94. return;
  95. #endif
  96. char buf[256];
  97. auto len = sprintf(buf, "%f, %f, %f\n", a, b, c);
  98. std::string frame(buf);
  99. TrySend(ch, frame);
  100. }
  101. static inline void Send(int ch, double a, double b, double c, double d)
  102. {
  103. #ifdef NO_SCOPE
  104. return;
  105. #endif
  106. char buf[256];
  107. auto len = sprintf(buf, "%f, %f, %f, %f\n", a, b, c, d);
  108. std::string frame(buf);
  109. TrySend(ch, frame);
  110. }
  111. static inline void Send(int ch, double a, double b, double c, double d, double e)
  112. {
  113. #ifdef NO_SCOPE
  114. return;
  115. #endif
  116. char buf[256];
  117. auto len = sprintf(buf, "%f, %f, %f, %f, %f\n", a, b, c, d, e);
  118. std::string frame(buf);
  119. TrySend(ch, frame);
  120. }
  121. static inline void Send(int ch, double a, double b, double c, double d, double e, double f)
  122. {
  123. #ifdef NO_SCOPE
  124. return;
  125. #endif
  126. char buf[256];
  127. auto len = sprintf(buf, "%f, %f, %f, %f, %f, %f\n", a, b, c, d, e, f);
  128. std::string frame(buf);
  129. TrySend(ch, frame);
  130. }
  131. // template<typename T, typename... Args>
  132. // static inline std::string CSend(const T& t)
  133. // {
  134. // double v = t;
  135. // return std::format("{:g}\n", v);
  136. // }
  137. // template<typename T, typename... Args>
  138. // static inline std::string CSend(const T& t, const Args&... rest)
  139. // {
  140. // double v = t;
  141. // return std::format("{:g},", v) + CSend(rest...);
  142. // }
  143. // template<typename... Args>
  144. // static inline void Send(int ch, Args... args)
  145. // {
  146. // std::string str = CSend(args...);
  147. // // std::cout << str << std::endl;
  148. // mg_printf(scope[ch]->con, "%s", str.c_str());
  149. // mg_mgr_poll(scope[ch]->mgr, 1);
  150. // }
  151. };