tcp_client.cpp 3.2 KB

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