1 #include "stdafx.h"
2 #include "RFLinkTCP.h"
3 #include "../main/Logger.h"
4 #include "../main/Helper.h"
5 #include <iostream>
6 #include "../main/localtime_r.h"
7 
CRFLinkTCP(const int ID,const std::string & IPAddress,const unsigned short usIPPort)8 CRFLinkTCP::CRFLinkTCP(const int ID, const std::string &IPAddress, const unsigned short usIPPort):
9 	m_szIPAddress(IPAddress)
10 {
11 	m_HwdID = ID;
12 	m_bDoRestart = false;
13 	m_usIPPort = usIPPort;
14 }
15 
~CRFLinkTCP(void)16 CRFLinkTCP::~CRFLinkTCP(void)
17 {
18 }
19 
StartHardware()20 bool CRFLinkTCP::StartHardware()
21 {
22 	RequestStart();
23 
24 	m_bDoRestart=false;
25 
26 	//force connect the next first time
27 	m_retrycntr = RFLINK_RETRY_DELAY;
28 	m_bIsStarted=true;
29 
30 	//Start worker thread
31 	m_thread = std::make_shared<std::thread>(&CRFLinkTCP::Do_Work, this);
32 	SetThreadNameInt(m_thread->native_handle());
33 	return (m_thread != nullptr);
34 }
35 
StopHardware()36 bool CRFLinkTCP::StopHardware()
37 {
38 	if (m_thread)
39 	{
40 		RequestStop();
41 		m_thread->join();
42 		m_thread.reset();
43 	}
44 	m_bIsStarted=false;
45 	return true;
46 }
47 
OnConnect()48 void CRFLinkTCP::OnConnect()
49 {
50 	_log.Log(LOG_STATUS,"RFLink: connected to: %s:%d", m_szIPAddress.c_str(), m_usIPPort);
51 	m_bDoRestart=false;
52 	m_bIsStarted=true;
53 	m_rfbufferpos = 0;
54 	m_LastReceivedTime = mytime(NULL);
55 	sOnConnected(this);
56 	write("10;PING;\n");
57 }
58 
OnDisconnect()59 void CRFLinkTCP::OnDisconnect()
60 {
61 	// Note: No need to set m_bDoRestart = true here, the connection is automatically reinited by ASyncTCP
62 	_log.Log(LOG_STATUS,"RFLink: disconnected");
63 }
64 
Do_Work()65 void CRFLinkTCP::Do_Work()
66 {
67 	bool bFirstTime=true;
68 	int sec_counter = 0;
69 	_log.Log(LOG_STATUS, "RFLink: trying to connect to %s:%d", m_szIPAddress.c_str(), m_usIPPort);
70 	connect(m_szIPAddress,m_usIPPort);
71 	while (!IsStopRequested(1000))
72 	{
73 		sec_counter++;
74 
75 		time_t atime = mytime(NULL);
76 		if (sec_counter % 12 == 0) {
77 			m_LastHeartbeat = mytime(NULL);
78 		}
79 		if ((sec_counter % 20 == 0) && (isConnected()))
80 		{
81 			time_t atime = mytime(NULL);
82 			//Send ping (keep alive)
83 			if (atime - m_LastReceivedTime > 30)
84 			{
85 				//Timeout
86 				_log.Log(LOG_ERROR, "RFLink: Nothing received for more than 30 seconds, restarting...");
87 				m_retrycntr = 0;
88 				m_LastReceivedTime = atime;
89 				//TODO: Add method to ASyncTCP to schedule a reconnect
90 				m_bDoRestart = true;
91 			}
92 			else
93 				write("10;PING;\n");
94 		}
95 
96 		if ((m_bDoRestart) && (sec_counter % 30 == 0))
97 		{
98 			_log.Log(LOG_STATUS, "RFLink: trying to connect to %s:%d", m_szIPAddress.c_str(), m_usIPPort);
99 			disconnect();
100 			connect(m_szIPAddress, m_usIPPort);
101 		}
102 	}
103 	terminate();
104 
105 	_log.Log(LOG_STATUS,"RFLink: TCP/IP Worker stopped...");
106 }
107 
OnData(const unsigned char * pData,size_t length)108 void CRFLinkTCP::OnData(const unsigned char *pData, size_t length)
109 {
110 	ParseData((const char*)pData,length);
111 }
112 
OnError(const boost::system::error_code & error)113 void CRFLinkTCP::OnError(const boost::system::error_code& error)
114 {
115 	if (
116 		(error == boost::asio::error::address_in_use) ||
117 		(error == boost::asio::error::connection_refused) ||
118 		(error == boost::asio::error::access_denied) ||
119 		(error == boost::asio::error::host_unreachable) ||
120 		(error == boost::asio::error::timed_out)
121 		)
122 	{
123 		_log.Log(LOG_ERROR, "RFLink: Can not connect to: %s:%d", m_szIPAddress.c_str(), m_usIPPort);
124 	}
125 	else if (
126 		(error == boost::asio::error::eof) ||
127 		(error == boost::asio::error::connection_reset)
128 		)
129 	{
130 		_log.Log(LOG_STATUS, "RFLink: Connection reset!");
131 	}
132 	else
133 		_log.Log(LOG_ERROR, "RFLink: %s", error.message().c_str());
134 }
135 
WriteInt(const std::string & sendString)136 bool CRFLinkTCP::WriteInt(const std::string &sendString)
137 {
138 	if (!isConnected())
139 	{
140 		return false;
141 	}
142 	write(sendString);
143 	return true;
144 }
145