tcp_client.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * tcp_client.cpp
  3. *
  4. * Created on: 2022年10月5日
  5. * Author: Allen
  6. */
  7. #include "tcp_client.h"
  8. #include "entry/EasyuiContext.h"
  9. #include "utils/Log.h"
  10. #include "base/os.hpp"
  11. #include <system/Thread.h>
  12. #include <string>
  13. #include "base/strings.hpp"
  14. #include "net/NetManager.h"
  15. #include "service/BusinessConfig.h"
  16. #include "core/utilities.h"
  17. #define ETHERNETMANAGER NETMANAGER->getEthernetManager()
  18. void handleMsg(byte* msg);
  19. TcpClient::TcpClient() {
  20. busy_ = false;
  21. }
  22. TcpClient::~TcpClient() {
  23. }
  24. TcpClient* TcpClient::instance() {
  25. static TcpClient singleton;
  26. return &singleton;
  27. }
  28. static net::Conn* conn;
  29. void TcpClient::sendMsg(const char* msg){
  30. instance()->internalSendMsg(msg);
  31. }
  32. void TcpClient::sendMsgWithCb(const char* msg, TcpCallback callback){
  33. instance()->internalSendMsg(msg);
  34. TcpCacheManager::instance()->setFunc(callback.tid, callback);
  35. }
  36. void TcpClient::internalSendMsg(const char* msg){
  37. if (busy_) {
  38. LOGD("tcp client is busy");
  39. return;
  40. }
  41. busy_ = true;
  42. std::string msgStr = msg;
  43. std::string endDelimiter = "#END#";
  44. std::string finalMsg = msg + endDelimiter;
  45. const char* sendMsg = finalMsg.c_str();
  46. if (conn){
  47. conn->Write((byte*)(sendMsg), strlen(sendMsg));
  48. LOGD("tcp sended msg : %s",sendMsg);
  49. } else {
  50. LOGD("tcp disconnect");
  51. }
  52. busy_ = false;
  53. }
  54. class MyThread: public Thread {
  55. public:
  56. /**
  57. * 线程创建成功后会调用该函数,可以在该函数中做一些初始化操作
  58. * return true 继续线程
  59. * false 退出线程
  60. */
  61. virtual bool readyToRun() {
  62. LOGD("Thread 已经创建完成");
  63. return true;
  64. }
  65. /**
  66. * 线程循环函数
  67. *
  68. * return true 继续线程循环
  69. * false 推出线程
  70. */
  71. virtual bool threadLoop() {
  72. LOGD("线程循环函数");
  73. //检查是否有退出线程的请求,如果有,则返回false,立即退出线程
  74. if (exitPending()) {
  75. return false;
  76. }
  77. conn = net::Dial("tcp", getTcpGateway());
  78. if (conn) {
  79. byte buf[2048] = {0};
  80. TcpModel tcpModel;
  81. tcpModel.tid = base::format("t%d", TimeHelper::getCurrentTime());
  82. tcpModel.type = TcpType::DEVICE;
  83. tcpModel.action = DeviceAction::CONNECT;
  84. // tcpModel.data = ETHERNETMANAGER->getMacAddr();
  85. Json::Value json;
  86. json["identification"] = StoragePreferences::getString(STORE_MAC_ADDR, "0.0.0.0");
  87. json["software_version"] = versionCode;
  88. json["model"] = "linux_door";;
  89. json["code"] = "SW10600101C-CM";
  90. tcpModel.json = json;
  91. std::string req = getTcpModelString(tcpModel);
  92. LOGD("待发送 %s",req.c_str());
  93. //发送
  94. // conn->Write((byte*)(req.c_str()), req.length());
  95. TcpClient::instance()->sendMsg(req.c_str());
  96. while (true && !exitPending()) {
  97. //读取,超时1000毫秒
  98. int n = conn->Read(buf, sizeof(buf) - 1, 1000);
  99. if (n > 0) {
  100. buf[n] = 0;
  101. handleMsg(buf);
  102. } else if (n == 0) {
  103. LOGD("连接正常断开");
  104. break;
  105. } else if (n == net::E_TIMEOUT) {
  106. // LOGD("读取超时");
  107. } else {
  108. LOGD("出错");
  109. break;
  110. }
  111. }
  112. //关闭连接
  113. conn->Close();
  114. //释放内存
  115. delete conn;
  116. conn = NULL;
  117. }
  118. //休眠5s
  119. usleep(5000 * 1000);
  120. LOGD("暂停5秒重新尝试连接TCP");
  121. //返回真,继续下次线程循环
  122. return true;
  123. }
  124. };
  125. static MyThread my_thread;
  126. long ts = 0;
  127. std::string deviceMac = "";
  128. void TcpClient::dataParse(std::string data) {
  129. long _ts = time(NULL);
  130. //如果与上次数据相同,1500毫秒内不再重复发
  131. if (deviceMac == data && (_ts - ts) < 15) {
  132. return;
  133. }
  134. ts = _ts;
  135. deviceMac = data;
  136. // ffcc020006d2c711#END#
  137. //ffcc 固定开头,02对应报警
  138. std::string signal = "ffcc02";
  139. int size = data.size();
  140. signal += base::format("%04x", size);
  141. signal += data;
  142. const char* req = signal.c_str();
  143. LOGD("ui %s",req);
  144. TcpClient::instance()->sendMsg(req);
  145. }
  146. void TcpClient::startTcp(){
  147. //调用线程类的run函数启动线程, 参数为线程名,可以任意指定。
  148. my_thread.run("tcp thread");
  149. }
  150. void TcpClient::closeTcp() {
  151. bool result = my_thread.isRunning();
  152. if (result) {
  153. my_thread.requestExitAndWait();
  154. LOGD("my_thread已关闭");
  155. }
  156. // my_thread.requestExit();
  157. }
  158. bool TcpClient::busy() {
  159. return instance()->busy_;
  160. }
  161. bool TcpClient::connected() {
  162. return conn != NULL;
  163. }