@@ -142,17 +142,17 @@ bool DomoticzTCP::ConnectInternal()
142
142
_log.Log (LOG_ERROR, " Domoticz: TCP could not create a TCP/IP socket!" );
143
143
return false ;
144
144
}
145
- /*
146
- // Set socket timeout to 2 minutes
145
+
146
+ // Set socket timeout to 10 seconds for reasonable shutdown delay
147
147
#if !defined WIN32
148
148
struct timeval tv;
149
- tv.tv_sec = 120 ;
149
+ tv.tv_sec = 10 ;
150
150
setsockopt (m_socket, SOL_SOCKET, SO_RCVTIMEO,(struct timeval *)&tv,sizeof (struct timeval ));
151
151
#else
152
- unsigned long nTimeout = 120 *1000;
152
+ unsigned long nTimeout = 10 *1000 ;
153
153
setsockopt (m_socket, SOL_SOCKET, SO_RCVTIMEO, (const char *)&nTimeout, sizeof (DWORD));
154
154
#endif
155
- */
155
+
156
156
// connect to the server
157
157
int nRet;
158
158
nRet = connect (m_socket, info->ai_addr , info->ai_addrlen );
@@ -195,13 +195,14 @@ void DomoticzTCP::Do_Work()
195
195
{
196
196
if (m_socket == INVALID_SOCKET)
197
197
{
198
- if (! IsStopRequested (900 )) // +100 = 1 second
198
+ if (IsStopRequested (900 )) // +100 = 1 second
199
199
{
200
+ break ;
200
201
}
201
- sleep_seconds (1 );
202
202
sec_counter++;
203
203
204
- if (sec_counter % 12 == 0 ) {
204
+ if (sec_counter % 12 == 0 )
205
+ {
205
206
mytime (&m_LastHeartbeat);
206
207
}
207
208
@@ -217,21 +218,24 @@ void DomoticzTCP::Do_Work()
217
218
}
218
219
else
219
220
{
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
222
221
m_LastHeartbeat = mytime (NULL );
223
-
224
222
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
+ {
228
225
disconnectTCP ();
229
226
_log.Log (LOG_ERROR, " Domoticz: TCP/IP connection closed!, retrying in %d seconds..." , RETRY_DELAY);
230
227
m_retrycntr = 0 ;
231
228
continue ;
232
229
}
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
+ }
235
239
}
236
240
}
237
241
disconnectTCP ();
0 commit comments