@@ -51,7 +51,9 @@ GSMClient::GSMClient(int socket, bool synch) :
51
51
_host(NULL ),
52
52
_port(0 ),
53
53
_ssl(false ),
54
- _writeSync(true )
54
+ _writeSync(true ),
55
+ _connectTimeout(30000UL ),
56
+ _socketTimeout(15000UL )
55
57
{
56
58
MODEM.addUrcHandler (this );
57
59
}
@@ -233,15 +235,27 @@ int GSMClient::connect()
233
235
}
234
236
235
237
if (_synch) {
236
- while (ready () == 0 );
238
+ unsigned long start = millis ();
239
+ while (ready () == 0 ) {
240
+ if (_connectTimeout && !((millis () - start) < _connectTimeout)) {
241
+ stop ();
242
+ return 2 ;
243
+ }
244
+ }
237
245
} else if (ready () == 0 ) {
238
246
return 0 ;
239
247
}
240
248
241
249
_state = CLIENT_STATE_CREATE_SOCKET;
242
250
243
251
if (_synch) {
252
+ unsigned long start = millis ();
244
253
while (ready () == 0 ) {
254
+ if (_connectTimeout && !((millis () - start) < _connectTimeout)) {
255
+ stop ();
256
+ return 2 ;
257
+ }
258
+
245
259
delay (100 );
246
260
}
247
261
@@ -271,7 +285,13 @@ size_t GSMClient::write(const uint8_t *buf)
271
285
size_t GSMClient::write (const uint8_t * buf, size_t size)
272
286
{
273
287
if (_writeSync) {
274
- while (ready () == 0 );
288
+ unsigned long start = millis ();
289
+ while (ready () == 0 ) {
290
+ if (_socketTimeout && !((millis () - start) < _socketTimeout)) {
291
+ stop ();
292
+ return 0 ;
293
+ }
294
+ }
275
295
} else if (ready () == 0 ) {
276
296
return 0 ;
277
297
}
@@ -385,7 +405,13 @@ int GSMClient::read()
385
405
int GSMClient::available ()
386
406
{
387
407
if (_synch) {
388
- while (ready () == 0 );
408
+ unsigned long start = millis ();
409
+ while (ready () == 0 ) {
410
+ if (_socketTimeout && !((millis () - start) < _socketTimeout)) {
411
+ stop ();
412
+ return 0 ;
413
+ }
414
+ }
389
415
} else if (ready () == 0 ) {
390
416
return 0 ;
391
417
}
@@ -434,6 +460,16 @@ void GSMClient::stop()
434
460
_connected = false ;
435
461
}
436
462
463
+ void GSMClient::setSocketTimeout (unsigned long timeout)
464
+ {
465
+ _socketTimeout = timeout;
466
+ }
467
+
468
+ void GSMClient::setConnectTimeout (unsigned long timeout)
469
+ {
470
+ _connectTimeout = timeout;
471
+ }
472
+
437
473
void GSMClient::handleUrc (const String& urc)
438
474
{
439
475
if (urc.startsWith (" +UUSORD: " )) {
0 commit comments