tcp_client.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #define ETHERNETMANAGER NETMANAGER->getEthernetManager()
  17. void handleMsg(byte* msg);
  18. TcpClient::TcpClient() {
  19. busy_ = false;
  20. }
  21. TcpClient::~TcpClient() {
  22. }
  23. TcpClient* TcpClient::instance() {
  24. static TcpClient singleton;
  25. return &singleton;
  26. }
  27. static net::Conn* conn;
  28. void TcpClient::sendMsg(const char* msg){
  29. instance()->internalSendMsg(msg);
  30. }
  31. void TcpClient::sendMsgWithCb(const char* msg, TcpCallback callback){
  32. instance()->internalSendMsg(msg);
  33. TcpCacheManager::instance()->setFunc(callback.tid, callback);
  34. }
  35. void TcpClient::internalSendMsg(const char* msg){
  36. if (busy_) {
  37. LOGD("tcp client is busy");
  38. return;
  39. }
  40. busy_ = true;
  41. std::string msgStr = msg;
  42. std::string endDelimiter = "#END#";
  43. std::string finalMsg = msg + endDelimiter;
  44. const char* sendMsg = finalMsg.c_str();
  45. if (conn){
  46. conn->Write((byte*)(sendMsg), strlen(sendMsg));
  47. LOGD("tcp sended msg : %s",sendMsg);
  48. } else {
  49. LOGD("tcp disconnect");
  50. }
  51. busy_ = false;
  52. }
  53. class MyThread: public Thread {
  54. public:
  55. /**
  56. * 线程创建成功后会调用该函数,可以在该函数中做一些初始化操作
  57. * return true 继续线程
  58. * false 退出线程
  59. */
  60. virtual bool readyToRun() {
  61. LOGD("Thread 已经创建完成");
  62. return true;
  63. }
  64. /**
  65. * 线程循环函数
  66. *
  67. * return true 继续线程循环
  68. * false 推出线程
  69. */
  70. virtual bool threadLoop() {
  71. LOGD("线程循环函数");
  72. //检查是否有退出线程的请求,如果有,则返回false,立即退出线程
  73. if (exitPending()) {
  74. return false;
  75. }
  76. conn = net::Dial("tcp", getTcpGateway());
  77. if (conn) {
  78. byte buf[2048] = {0};
  79. TcpModel tcpModel;
  80. tcpModel.type = TcpType::DEVICE;
  81. tcpModel.action = DeviceAction::CONNECT;
  82. // tcpModel.data = ETHERNETMANAGER->getMacAddr();
  83. Json::Value json;
  84. json["identification"] = ETHERNETMANAGER->getMacAddr();
  85. json["software_version"] = version;
  86. json["model"] = "linux_door";;
  87. json["code"] = "SW10600101C-CM";
  88. tcpModel.json = json;
  89. std::string req = getTcpModelString(tcpModel);
  90. LOGD("待发送 %s",req.c_str());
  91. //发送
  92. // conn->Write((byte*)(req.c_str()), req.length());
  93. TcpClient::instance()->sendMsg(req.c_str());
  94. while (true && !exitPending()) {
  95. //读取,超时1000毫秒
  96. int n = conn->Read(buf, sizeof(buf) - 1, 1000);
  97. if (n > 0) {
  98. buf[n] = 0;
  99. handleMsg(buf);
  100. } else if (n == 0) {
  101. LOGD("连接正常断开");
  102. break;
  103. } else if (n == net::E_TIMEOUT) {
  104. // LOGD("读取超时");
  105. } else {
  106. LOGD("出错");
  107. break;
  108. }
  109. }
  110. //关闭连接
  111. conn->Close();
  112. //释放内存
  113. delete conn;
  114. conn = NULL;
  115. }
  116. //休眠5s
  117. usleep(5000 * 1000);
  118. LOGD("暂停5秒重新尝试连接TCP");
  119. //返回真,继续下次线程循环
  120. return true;
  121. }
  122. };
  123. static MyThread my_thread;
  124. void TcpClient::startTcp(){
  125. //调用线程类的run函数启动线程, 参数为线程名,可以任意指定。
  126. my_thread.run("tcp thread");
  127. }
  128. void TcpClient::closeTcp() {
  129. my_thread.requestExitAndWait();
  130. // my_thread.requestExit();
  131. }
  132. bool TcpClient::busy() {
  133. return instance()->busy_;
  134. }
  135. bool TcpClient::connected() {
  136. return conn != NULL;
  137. }