diff --git a/hardware/ASyncTCP.cpp b/hardware/ASyncTCP.cpp index 2bc4ac4667..b34a8830fa 100644 --- a/hardware/ASyncTCP.cpp +++ b/hardware/ASyncTCP.cpp @@ -2,6 +2,10 @@ #include "ASyncTCP.h" #include "../main/Logger.h" +#ifndef WIN32 + #include //gethostbyname +#endif + /* #ifdef WIN32 #include @@ -37,7 +41,28 @@ void ASyncTCP::connect(const std::string &ip, unsigned short port) // connect socket try { - boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string(ip), port); + std::string fip = ip; + + unsigned long ipn = inet_addr(fip.c_str()); + // if we have a error in the ip, it means we have entered a string + if (ipn == INADDR_NONE) + { + // change Hostname in Server Address + hostent *he = gethostbyname(fip.c_str()); + if (he != NULL) + { + char szIP[20]; + sprintf(szIP, "%d.%d.%d.%d", (uint8_t)he->h_addr_list[0][0], (uint8_t)he->h_addr_list[0][1], (uint8_t)he->h_addr_list[0][2], (uint8_t)he->h_addr_list[0][3]); + fip = szIP; + } + else + { + //we will fail + _log.Log(LOG_ERROR, "TCP: Unable to resolve '%s'", fip.c_str()); + } + } + + boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string(fip), port); connect(endpoint); } diff --git a/hardware/MochadTCP.cpp b/hardware/MochadTCP.cpp index 44b6220fef..d9883d5677 100644 --- a/hardware/MochadTCP.cpp +++ b/hardware/MochadTCP.cpp @@ -53,8 +53,8 @@ MochadTCP::MochadTCP(const int ID, const std::string &IPAddress, const unsigned m_szIPAddress(IPAddress) { m_HwdID=ID; - m_socket=INVALID_SOCKET; m_stoprequested=false; + m_bDoRestart = false; m_usIPPort=usIPPort; m_linecount=0; m_exclmarkfound=0; @@ -93,32 +93,7 @@ MochadTCP::~MochadTCP(void) bool MochadTCP::StartHardware() { m_stoprequested=false; - - memset(&m_addr,0,sizeof(sockaddr_in)); - m_addr.sin_family = AF_INET; - m_addr.sin_port = htons(m_usIPPort); - - unsigned long ip; - ip=inet_addr(m_szIPAddress.c_str()); - - // if we have a error in the ip, it means we have entered a string - if(ip!=INADDR_NONE) - { - m_addr.sin_addr.s_addr=ip; - } - else - { - // change Hostname in serveraddr - hostent *he=gethostbyname(m_szIPAddress.c_str()); - if(he==NULL) - { - return false; - } - else - { - memcpy(&(m_addr.sin_addr),he->h_addr_list[0],4); - } - } + m_bDoRestart = false; //force connect the next first time // m_retrycntr=RETRY_DELAY; @@ -150,6 +125,7 @@ void MochadTCP::OnConnect() { _log.Log(LOG_STATUS, "Mochad: connected to: %s:%ld", m_szIPAddress.c_str(), m_usIPPort); m_bIsStarted = true; + m_bDoRestart = false; sOnConnected(this); } @@ -157,32 +133,15 @@ void MochadTCP::OnConnect() void MochadTCP::OnDisconnect() { _log.Log(LOG_STATUS, "Mochad: disconnected"); + m_bDoRestart = true; } - - -bool MochadTCP::ConnectInternal() -{ - -// connect to the server - connect((const std::string &)m_addr, sizeof(m_addr)); - _log.Log(LOG_STATUS,"Mochad: connected to: %s:%ld", m_szIPAddress.c_str(), m_usIPPort); - -// Init(); - - sOnConnected(this); - return true; -} - - void MochadTCP::OnData(const unsigned char *pData, size_t length) { boost::lock_guard l(readQueueMutex); ParseData(pData, length); } - - void MochadTCP::Do_Work() { bool bFirstTime = true; @@ -209,6 +168,11 @@ void MochadTCP::Do_Work() } else { + if ((m_bDoRestart) && (ltime.tm_sec % 30 == 0)) + { + _log.Log(LOG_STATUS, "Mochad: trying to connect to %s:%d", m_szIPAddress.c_str(), m_usIPPort); + connect(m_szIPAddress, m_usIPPort); + } sleep_milliseconds(40); update(); } diff --git a/hardware/MochadTCP.h b/hardware/MochadTCP.h index e203bc5753..3252201038 100644 --- a/hardware/MochadTCP.h +++ b/hardware/MochadTCP.h @@ -34,12 +34,9 @@ class MochadTCP: public CDomoticzHardwareBase, ASyncTCP void setSecID(unsigned char *p); void Do_Work(); - bool ConnectInternal(); boost::shared_ptr m_thread; volatile bool m_stoprequested; - sockaddr_in m_addr; - int m_socket; - + bool m_bDoRestart; int selected[17][17]; int currentHouse; int currentUnit; diff --git a/msbuild/domoticz.vcxproj.filters b/msbuild/domoticz.vcxproj.filters index d661751a85..34b3baec47 100644 --- a/msbuild/domoticz.vcxproj.filters +++ b/msbuild/domoticz.vcxproj.filters @@ -33,9 +33,6 @@ {b2fc9567-e487-4cee-b23e-9f6002b503b9} - - {44ff1baf-dce6-4a20-b295-313ef10e0509} - {acc2dd9a-c3ba-4abc-9dd5-a6871fd4d465} @@ -435,6 +432,9 @@ {db1e850c-e0ec-47fc-84cd-6528ce21f0aa} + + {44ff1baf-dce6-4a20-b295-313ef10e0509} + @@ -1328,7 +1328,7 @@ Devices\Domoticz - Devices\P1Smart Meter + Devices\P1 Smart Meter Devices\YouLess @@ -1742,10 +1742,10 @@ Devices\RFXCom - Devices\P1Smart Meter + Devices\P1 Smart Meter - Devices\P1Smart Meter + Devices\P1 Smart Meter Webserver @@ -1939,7 +1939,7 @@ Devices\Domoticz - Devices\P1Smart Meter + Devices\P1 Smart Meter Devices\YouLess @@ -2284,10 +2284,10 @@ Devices\RFXCom - Devices\P1Smart Meter + Devices\P1 Smart Meter - Devices\P1Smart Meter + Devices\P1 Smart Meter Devices\Kodi @@ -3648,4 +3648,4 @@ www\secpanel\media - + \ No newline at end of file diff --git a/push/GooglePubSubPush.cpp b/push/GooglePubSubPush.cpp index e4068fe04e..19418c61e2 100644 --- a/push/GooglePubSubPush.cpp +++ b/push/GooglePubSubPush.cpp @@ -12,10 +12,6 @@ #include "../webserver/Base64.h" #include "../webserver/cWebem.h" -#ifndef WIN32 - #include //gethostbyname -#endif - #ifdef ENABLE_PYTHON extern "C" { #include diff --git a/push/HttpPush.cpp b/push/HttpPush.cpp index d57de939bd..95d0fdb981 100644 --- a/push/HttpPush.cpp +++ b/push/HttpPush.cpp @@ -13,10 +13,6 @@ #include "../main/mainworker.h" #include "../json/json.h" -#ifndef WIN32 - #include //gethostbyname -#endif - CHttpPush::CHttpPush() { m_bLinkActive = false; diff --git a/webserver/cWebem.cpp b/webserver/cWebem.cpp index fc5ffb3797..88f4b6811f 100644 --- a/webserver/cWebem.cpp +++ b/webserver/cWebem.cpp @@ -24,10 +24,6 @@ #include "../main/localtime_r.h" #include "../main/Logger.h" -#ifndef WIN32 - #include //gethostbyname -#endif - //10 minutes #define SESSION_TIMEOUT 600