Skip to content

Commit 55ed0f6

Browse files
committed
connection state management modified
- check member _connected first in connect_client() to prevent querying connected() of disconnected _client - added method status() to check TCP connection state (ESP8266 only)
1 parent 57967d3 commit 55ed0f6

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

utility/WiFiClientStream.h

+7-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
published under the same license.
2222
23-
Last updated April 17th, 2016
23+
Last updated April 23rd, 2016
2424
*/
2525

2626
#ifndef WIFI_CLIENT_STREAM_H
@@ -41,21 +41,20 @@ class WiFiClientStream : public WiFiStream
4141
*/
4242
virtual inline bool connect_client()
4343
{
44-
if( _client && _client.connected() ) return true;
45-
46-
if( _connected )
44+
if ( _connected )
4745
{
46+
if ( _client && _client.connected() ) return true;
4847
stop();
4948
}
5049

5150
// active TCP connect
52-
if( WiFi.status() == WL_CONNECTED )
51+
if ( WiFi.status() == WL_CONNECTED )
5352
{
5453
// if the client is disconnected, try to reconnect every 5 seconds
55-
if( millis() - _time_connect >= MILLIS_RECONNECT )
54+
if ( millis() - _time_connect >= MILLIS_RECONNECT )
5655
{
5756
_connected = _client.connect( _remote_ip, _port );
58-
if( !_connected )
57+
if ( !_connected )
5958
{
6059
_time_connect = millis();
6160
}
@@ -89,7 +88,7 @@ class WiFiClientStream : public WiFiStream
8988
*/
9089
virtual inline void stop()
9190
{
92-
if( _client)
91+
if ( _client)
9392
{
9493
_client.stop();
9594
if ( _currentHostConnectionCallback )

utility/WiFiServerStream.h

+7-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
published under the same license.
2222
23-
Last updated April 17th, 2016
23+
Last updated April 23rd, 2016
2424
*/
2525

2626
#ifndef WIFI_SERVER_STREAM_H
@@ -40,16 +40,15 @@ class WiFiServerStream : public WiFiStream
4040
*/
4141
virtual inline bool connect_client()
4242
{
43-
if( _client && _client.connected() ) return true;
44-
45-
if( _connected )
43+
if ( _connected )
4644
{
45+
if ( _client && _client.connected() ) return true;
4746
stop();
4847
}
4948

5049
// passive TCP connect (accept)
5150
WiFiClient newClient = _server.available();
52-
if( !newClient ) return false;
51+
if ( !newClient ) return false;
5352
_client = newClient;
5453
_connected = true;
5554
if ( _currentHostConnectionCallback )
@@ -72,11 +71,11 @@ class WiFiServerStream : public WiFiStream
7271
*/
7372
virtual inline bool maintain()
7473
{
75-
if( connect_client() ) return true;
74+
if ( connect_client() ) return true;
7675

7776
stop();
7877

79-
if( !_listening && WiFi.status() == WL_CONNECTED )
78+
if ( !_listening && WiFi.status() == WL_CONNECTED )
8079
{
8180
// start TCP server after first WiFi connect
8281
_server = WiFiServer(_port);
@@ -92,7 +91,7 @@ class WiFiServerStream : public WiFiStream
9291
*/
9392
virtual inline void stop()
9493
{
95-
if( _client)
94+
if ( _client)
9695
{
9796
_client.stop();
9897
if ( _currentHostConnectionCallback )

utility/WiFiStream.h

+23-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
See file LICENSE.txt for further informations on licensing terms.
1717
18-
Last updated April 17th, 2016
18+
Last updated April 23rd, 2016
1919
*/
2020

2121
#ifndef WIFI_STREAM_H
@@ -114,6 +114,28 @@ class WiFiStream : public Stream
114114
* @return true if WiFi and TCP connection are established
115115
*/
116116
virtual bool maintain() = 0;
117+
118+
#ifdef ESP8266
119+
/**
120+
* get status of TCP connection
121+
* @return status of TCP connection
122+
* CLOSED = 0 (typical)
123+
* LISTEN = 1 (not used)
124+
* SYN_SENT = 2
125+
* SYN_RCVD = 3
126+
* ESTABLISHED = 4 (typical)
127+
* FIN_WAIT_1 = 5
128+
* FIN_WAIT_2 = 6
129+
* CLOSE_WAIT = 7
130+
* CLOSING = 8
131+
* LAST_ACK = 9
132+
* TIME_WAIT = 10
133+
*/
134+
inline uint8_t status()
135+
{
136+
return _client.status();
137+
}
138+
#endif
117139

118140
/**
119141
* close TCP client connection

0 commit comments

Comments
 (0)