@@ -179,9 +179,8 @@ class PubSubClient : public PubNub_BASE_CLIENT {
179
179
}
180
180
/* We are still connected. Read the rest of the stream so that
181
181
* we catch the timetoken. */
182
- while (wait_for_data ()) {
183
- char ch = read ();
184
- this ->_state_input (ch, 0 , 0 );
182
+ while (wait_for_data (10 )) {
183
+ read ();
185
184
}
186
185
json_enabled = false ;
187
186
}
@@ -600,6 +599,17 @@ inline void PubSubClient::_grab_timetoken(uint8_t* nextbuf, size_t nextsize)
600
599
timetoken[new_timetoken_len] = 0 ;
601
600
}
602
601
602
+ inline bool await_disconnect (PubNub_BASE_CLIENT& client, unsigned long timeout) {
603
+ unsigned long t_start = millis ();
604
+ while (client.connected ()) {
605
+ if (millis () - t_start > timeout * 1000UL ) {
606
+ return false ;
607
+ }
608
+ delay (10 );
609
+ }
610
+ return true ;
611
+ }
612
+
603
613
604
614
inline PubNonSubClient* PubNub::publish (const char * channel,
605
615
const char * message,
@@ -669,14 +679,16 @@ inline PubNonSubClient* PubNub::publish(const char* channel,
669
679
case PubNub_BH_ERROR:
670
680
DBGprintln (" publish() BH_ERROR" );
671
681
client.stop ();
672
- while (client.connected ())
673
- delay (10 );
682
+ if (!await_disconnect (client, 10 )) {
683
+ DBGprintln (" publish() BH_ERROR: disconnect timeout" );
684
+ }
674
685
return 0 ;
675
686
case PubNub_BH_TIMEOUT:
676
687
DBGprintln (" publish() BH_TIMEOUT" );
677
688
client.stop ();
678
- while (client.connected ())
679
- delay (10 );
689
+ if (!await_disconnect (client, 10 )) {
690
+ DBGprintln (" publish() BH_TIMEOUT: disconnect timeout" );
691
+ }
680
692
return 0 ;
681
693
}
682
694
}
@@ -726,15 +738,17 @@ inline PubSubClient* PubNub::subscribe(const char* channel, int timeout)
726
738
if (!client.wait_for_data ()) {
727
739
DBGprintln (" No data received!" );
728
740
client.stop ();
729
- while (client.connected ())
730
- delay (10 );
741
+ if (!await_disconnect (client, 10 )) {
742
+ DBGprintln (" subscribe() no data received: disconnect timeout" );
743
+ }
731
744
return 0 ;
732
745
}
733
746
if (client.read () != ' [' ) {
734
747
DBGprintln (" Unexpected body in subscribe response" );
735
748
client.stop ();
736
- while (client.connected ())
737
- delay (10 );
749
+ if (!await_disconnect (client, 10 )) {
750
+ DBGprintln (" subscribe() unexpected body: disconnect timeout" );
751
+ }
738
752
return 0 ;
739
753
}
740
754
/* Now return handle to the client for further perusal.
@@ -747,17 +761,21 @@ inline PubSubClient* PubNub::subscribe(const char* channel, int timeout)
747
761
case PubNub_BH_ERROR:
748
762
DBGprintln (" subscribe() BH_ERROR" );
749
763
client.stop ();
750
- while (client.connected ())
751
- delay (10 );
764
+ if (!await_disconnect (client, 10 )) {
765
+ DBGprintln (" subscribe() BH_ERROR: disconnect timeout" );
766
+ }
752
767
return 0 ;
753
768
754
769
case PubNub_BH_TIMEOUT:
755
770
DBGprintln (" subscribe() BH_TIMEOUT" );
756
771
client.stop ();
757
772
DBGprintln (" subscribe() BH_TIMEOUT stopped" );
758
- while (client.connected ())
759
- delay (10 );
760
- DBGprintln (" subscribe() BH_TIMEOUT disconnected" );
773
+ if (await_disconnect (client, 10 )) {
774
+ DBGprintln (" subscribe() BH_TIMEOUT: disconnected" );
775
+ }
776
+ else {
777
+ DBGprintln (" subscribe() BH_TIMEOUT: disconnect timeout" );
778
+ }
761
779
return 0 ;
762
780
}
763
781
}
@@ -790,14 +808,16 @@ inline PubNonSubClient* PubNub::history(const char* channel, int limit, int time
790
808
case PubNub_BH_ERROR:
791
809
DBGprintln (" history() BH_ERROR" );
792
810
client.stop ();
793
- while (client.connected ())
794
- delay (10 );
811
+ if (!await_disconnect (client, 10 )) {
812
+ DBGprintln (" history() BH_ERROR: disconnect timeout" );
813
+ }
795
814
return 0 ;
796
815
case PubNub_BH_TIMEOUT:
797
816
DBGprintln (" history() BH_TIMEOUT" );
798
817
client.stop ();
799
- while (client.connected ())
800
- delay (10 );
818
+ if (!await_disconnect (client, 10 )) {
819
+ DBGprintln (" history() BH_TIMEOUT: disconnect timeout" );
820
+ }
801
821
return 0 ;
802
822
}
803
823
}
0 commit comments