tcp_client.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.type = TcpType::DEVICE;
  82. tcpModel.action = DeviceAction::CONNECT;
  83. // tcpModel.data = ETHERNETMANAGER->getMacAddr();
  84. Json::Value json;
  85. json["identification"] = StoragePreferences::getString(STORE_MAC_ADDR, "0.0.0.0");
  86. json["software_version"] = getVersionCode();
  87. json["model"] = "linux_door";
  88. json["code"] = "SW10600101C-CM";
  89. tcpModel.json = json;
  90. std::string req = getTcpModelString(tcpModel);
  91. LOGD("待发送 %s",req.c_str());
  92. //发送
  93. // conn->Write((byte*)(req.c_str()), req.length());
  94. TcpClient::instance()->sendMsg(req.c_str());
  95. while (true && !exitPending()) {
  96. //读取,超时1000毫秒
  97. int n = conn->Read(buf, sizeof(buf) - 1, 1000);
  98. if (n > 0) {
  99. buf[n] = 0;
  100. handleMsg(buf);
  101. } else if (n == 0) {
  102. LOGD("连接正常断开");
  103. break;
  104. } else if (n == net::E_TIMEOUT) {
  105. // LOGD("读取超时");
  106. } else {
  107. LOGD("出错");
  108. break;
  109. }
  110. }
  111. //关闭连接
  112. conn->Close();
  113. //释放内存
  114. delete conn;
  115. conn = NULL;
  116. }
  117. //休眠5s
  118. usleep(5000 * 1000);
  119. LOGD("暂停5秒重新尝试连接TCP");
  120. //返回真,继续下次线程循环
  121. return true;
  122. }
  123. };
  124. static MyThread my_thread;
  125. void TcpClient::startTcp(){
  126. //调用线程类的run函数启动线程, 参数为线程名,可以任意指定。
  127. my_thread.run("tcp thread");
  128. }
  129. void TcpClient::closeTcp() {
  130. bool result = my_thread.isRunning();
  131. if (result) {
  132. my_thread.requestExitAndWait();
  133. LOGD("my_thread已关闭");
  134. }
  135. }
  136. bool TcpClient::busy() {
  137. return instance()->busy_;
  138. }
  139. bool TcpClient::connected() {
  140. return conn != NULL;
  141. }