You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.1 KiB

​
​
4 months ago
​
Ethernet2UART gateway: fix the reconnect loop, leaked connections and ping check From the la_mojo log of a gateway reached over the Internet (2.54.237.28:8234): deviceETH2UART - In TCP mode, link health is the TCP connection itself (socket, I/O thread, SO_ERROR, TCP keepalive). ICMP isn't used there: gateways on mobile networks often don't answer ping, and the working connection was closed every second ("No Ping to device!"). - Ping (UDP modes) up to 3 echoes x 2000 ms, at most every 10 s. Only an echo reply from the gateway with our id and sequence counts. A signal (EINTR) no longer ends the wait. The old single recvfrom() with SO_RCVTIMEO returned EINTR at the first signal, even with SA_RESTART. - TCP connect timeout 1 s -> 5 s (E2UART_CONNECT_TIMEOUT_MS), and EINTR in select() continues waiting. - create_SOL() closes the previous socket and I/O thread first. It overwrote the socket (the connection leaked) and init() failed while the old thread ran, so each reconnect left an open TCP connection at the gateway and the node stayed invalid although "Connected". If init() fails, the new socket is closed. - create()/validate()/reconnect() are serialised: they run on the node factory thread and on the hardware manager thread. - Registered with the hardware manager once. Re-creation from the hardware manager locked its non-recursive list mutex again (deadlock). - Reconnect back-off 2 s -> 30 s. - recv() == 0 on TCP means the gateway closed the connection (a stale errno was checked before). - Destructor resigns from the hardware manager before closing. deviceNodeFactory - deleteNode() deletes the gateway object. It was only removed from the list and kept running next to the new node created for the same id. - Nodes are deleted after the validation loop (erasing inside it skipped the next node). deviceNode::validate - Compares the configuration only. "online" changes whenever another object of the same node id is created or deleted. hardwareManager::assignResource - Ignores a resource that is already assigned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
3 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Ethernet2UART gateway: fix the reconnect loop, leaked connections and ping check From the la_mojo log of a gateway reached over the Internet (2.54.237.28:8234): deviceETH2UART - In TCP mode, link health is the TCP connection itself (socket, I/O thread, SO_ERROR, TCP keepalive). ICMP isn't used there: gateways on mobile networks often don't answer ping, and the working connection was closed every second ("No Ping to device!"). - Ping (UDP modes) up to 3 echoes x 2000 ms, at most every 10 s. Only an echo reply from the gateway with our id and sequence counts. A signal (EINTR) no longer ends the wait. The old single recvfrom() with SO_RCVTIMEO returned EINTR at the first signal, even with SA_RESTART. - TCP connect timeout 1 s -> 5 s (E2UART_CONNECT_TIMEOUT_MS), and EINTR in select() continues waiting. - create_SOL() closes the previous socket and I/O thread first. It overwrote the socket (the connection leaked) and init() failed while the old thread ran, so each reconnect left an open TCP connection at the gateway and the node stayed invalid although "Connected". If init() fails, the new socket is closed. - create()/validate()/reconnect() are serialised: they run on the node factory thread and on the hardware manager thread. - Registered with the hardware manager once. Re-creation from the hardware manager locked its non-recursive list mutex again (deadlock). - Reconnect back-off 2 s -> 30 s. - recv() == 0 on TCP means the gateway closed the connection (a stale errno was checked before). - Destructor resigns from the hardware manager before closing. deviceNodeFactory - deleteNode() deletes the gateway object. It was only removed from the list and kept running next to the new node created for the same id. - Nodes are deleted after the validation loop (erasing inside it skipped the next node). deviceNode::validate - Compares the configuration only. "online" changes whenever another object of the same node id is created or deleted. hardwareManager::assignResource - Ignores a resource that is already assigned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
3 days ago
​
​
​
​
​
​
​
​
Ethernet2UART gateway: fix the reconnect loop, leaked connections and ping check From the la_mojo log of a gateway reached over the Internet (2.54.237.28:8234): deviceETH2UART - In TCP mode, link health is the TCP connection itself (socket, I/O thread, SO_ERROR, TCP keepalive). ICMP isn't used there: gateways on mobile networks often don't answer ping, and the working connection was closed every second ("No Ping to device!"). - Ping (UDP modes) up to 3 echoes x 2000 ms, at most every 10 s. Only an echo reply from the gateway with our id and sequence counts. A signal (EINTR) no longer ends the wait. The old single recvfrom() with SO_RCVTIMEO returned EINTR at the first signal, even with SA_RESTART. - TCP connect timeout 1 s -> 5 s (E2UART_CONNECT_TIMEOUT_MS), and EINTR in select() continues waiting. - create_SOL() closes the previous socket and I/O thread first. It overwrote the socket (the connection leaked) and init() failed while the old thread ran, so each reconnect left an open TCP connection at the gateway and the node stayed invalid although "Connected". If init() fails, the new socket is closed. - create()/validate()/reconnect() are serialised: they run on the node factory thread and on the hardware manager thread. - Registered with the hardware manager once. Re-creation from the hardware manager locked its non-recursive list mutex again (deadlock). - Reconnect back-off 2 s -> 30 s. - recv() == 0 on TCP means the gateway closed the connection (a stale errno was checked before). - Destructor resigns from the hardware manager before closing. deviceNodeFactory - deleteNode() deletes the gateway object. It was only removed from the list and kept running next to the new node created for the same id. - Nodes are deleted after the validation loop (erasing inside it skipped the next node). deviceNode::validate - Compares the configuration only. "online" changes whenever another object of the same node id is created or deleted. hardwareManager::assignResource - Ignores a resource that is already assigned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
3 days ago
​
​
​
​
​
​
​
4 months ago
​
​
​
​
​
​
​
4 months ago
​
​
​
​
4 months ago
​
​
4 months ago
​
​
​
  1. #ifndef __DEVICE_NODE_ETHERNET2SERIAL_HEADER__
  2. #define __DEVICE_NODE_ETHERNET2SERIAL_HEADER__
  3. #include "../../data/transport/transportSocket.h"
  4. //#include "../components/base/basicTransport.h"
  5. #include "deviceNode.h"
  6. #include "hardwareResource.h"
  7. #include <mutex>
  8. // Timeouts for a gateway reached over the Internet (the 1 s connect timeout was a LAN value)
  9. #ifndef E2UART_CONNECT_TIMEOUT_MS
  10. #define E2UART_CONNECT_TIMEOUT_MS 5000 // TCP connect
  11. #endif
  12. #ifndef E2UART_PING_TIMEOUT_MS
  13. #define E2UART_PING_TIMEOUT_MS 2000 // wait for one ICMP echo reply
  14. #endif
  15. #ifndef E2UART_PING_ATTEMPTS
  16. #define E2UART_PING_ATTEMPTS 3 // echo requests before the device counts as unreachable
  17. #endif
  18. #ifndef E2UART_PING_INTERVAL_MS
  19. #define E2UART_PING_INTERVAL_MS 10000 // UDP modes: ping at most this often (validate() runs every second)
  20. #endif
  21. // Reconnect back-off after a failed connect: 2 s, 4 s, ... up to 30 s
  22. #define E2UART_RECONNECT_MIN_MS 2000
  23. #define E2UART_RECONNECT_MAX_MS 30000
  24. // TCP keepalive: a dead link is detected in about IDLE + INTVL * CNT seconds
  25. #define E2UART_KEEPALIVE_IDLE_S 20
  26. #define E2UART_KEEPALIVE_INTVL_S 5
  27. #define E2UART_KEEPALIVE_CNT 4
  28. #ifdef SERIAL_PING_PONG
  29. class deviceETH2UART : public deviceNode, public hardwareResource, public basicTransport, public basicThread
  30. #else
  31. class deviceETH2UART : public deviceNode, public hardwareResource, public basicTransport, public basicDualThread
  32. #endif
  33. {
  34. private:
  35. // basicLogCollection * m_pLog;
  36. DBC_UART_DATA m_sUartData;
  37. int m_iDeviceFD;
  38. int m_iMasterFD;
  39. #ifdef __linux__
  40. int m_iInFID;
  41. int m_iOutFID;
  42. #else
  43. DCB PortDCB;
  44. HANDLE m_ioFID;
  45. #endif
  46. // create() / validate() / reconnect() / closePort() run on the node factory thread AND on the
  47. // hardware manager thread (the class is a deviceNode and a hardwareResource): one at a time
  48. std::recursive_mutex m_mConnLock;
  49. bool m_bRegistered; // assigned to the hardware manager (once)
  50. bool m_bLastPingOk;
  51. uint64_t m_nNextPingMs;
  52. uint64_t m_nNextConnectMs;
  53. uint32_t m_nConnectBackoffMs;
  54. bool pingDevice();
  55. bool pingOnce(int sockfd, uint16_t id, uint16_t seq, char * reason, size_t reasonSize);
  56. bool linkAlive(void);
  57. bool connectSOL(void); // create_SOL() with the reconnect back-off
  58. bool create_SOL();
  59. bool closePort();
  60. int sendBlock(unsigned char * Data, int DataSize);
  61. int recptBlock(unsigned char * Buffer, int * BufferLength);
  62. protected:
  63. #ifdef SERIAL_PING_PONG
  64. virtual void m_fThreadProc(void);
  65. Q_SIO_TZ m_qIO;
  66. #else
  67. U8 m_pInputBuffer[MAXIMAL_SOCKET_IO_BUFFER_LENGTH];
  68. U8 m_pOutputBuffer[MAXIMAL_SOCKET_IO_BUFFER_LENGTH];
  69. int m_nInputBufferSize;
  70. int m_nOutputBufferSize;
  71. virtual void m_fThreadAProc(void);
  72. virtual void m_fThreadBProc(void);
  73. #endif
  74. public:
  75. explicit deviceETH2UART(uint32_t nodeID);
  76. ~deviceETH2UART();
  77. virtual bool create(void);
  78. virtual bool validate(void);
  79. #ifdef SERIAL_PING_PONG
  80. virtual bool sendData(uint8_t * Data, int DataSize, protocolParser *src = NULL );
  81. #else
  82. virtual bool sendData(uint8_t * Data, int DataSize);
  83. #endif
  84. virtual bool reconnect(void);
  85. };
  86. #endif