Skip to content

Commit 5114b77

Browse files
committed
Make DomoticzTCP stoppable and restartable
1 parent a668baf commit 5114b77

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

hardware/DomoticzTCP.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,17 @@ bool DomoticzTCP::ConnectInternal()
142142
_log.Log(LOG_ERROR, "Domoticz: TCP could not create a TCP/IP socket!");
143143
return false;
144144
}
145-
/*
146-
//Set socket timeout to 2 minutes
145+
146+
// Set socket timeout to 10 seconds for reasonable shutdown delay
147147
#if !defined WIN32
148148
struct timeval tv;
149-
tv.tv_sec = 120;
149+
tv.tv_sec = 10;
150150
setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO,(struct timeval *)&tv,sizeof(struct timeval));
151151
#else
152-
unsigned long nTimeout = 120*1000;
152+
unsigned long nTimeout = 10*1000;
153153
setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&nTimeout, sizeof(DWORD));
154154
#endif
155-
*/
155+
156156
// connect to the server
157157
int nRet;
158158
nRet = connect(m_socket, info->ai_addr, info->ai_addrlen);
@@ -195,13 +195,14 @@ void DomoticzTCP::Do_Work()
195195
{
196196
if (m_socket == INVALID_SOCKET)
197197
{
198-
if (!IsStopRequested(900)) //+100 = 1 second
198+
if (IsStopRequested(900)) //+100 = 1 second
199199
{
200+
break;
200201
}
201-
sleep_seconds(1);
202202
sec_counter++;
203203

204-
if (sec_counter % 12 == 0) {
204+
if (sec_counter % 12 == 0)
205+
{
205206
mytime(&m_LastHeartbeat);
206207
}
207208

@@ -217,21 +218,24 @@ void DomoticzTCP::Do_Work()
217218
}
218219
else
219220
{
220-
//this could take a long time... maybe there will be no data received at all,
221-
//so it's no good to-do the heartbeat timing here
222221
m_LastHeartbeat = mytime(NULL);
223-
224222
int bread = recv(m_socket, (char*)&buf, sizeof(buf), 0);
225-
if (IsStopRequested(10))
226-
break;
227-
if (bread <= 0) {
223+
if ((bread < 0) && (errno != EAGAIN))
224+
{
228225
disconnectTCP();
229226
_log.Log(LOG_ERROR, "Domoticz: TCP/IP connection closed!, retrying in %d seconds...", RETRY_DELAY);
230227
m_retrycntr = 0;
231228
continue;
232229
}
233-
std::lock_guard<std::mutex> l(readQueueMutex);
234-
onInternalMessage((const unsigned char *)&buf, bread, false); // Do not check validity, this might be non RFX-message
230+
if (IsStopRequested(10))
231+
{
232+
break;
233+
}
234+
if (bread > 0)
235+
{
236+
std::lock_guard<std::mutex> l(readQueueMutex);
237+
onInternalMessage((const unsigned char *)&buf, bread, false); // Do not check validity, this might be non RFX-message
238+
}
235239
}
236240
}
237241
disconnectTCP();

0 commit comments

Comments
 (0)