Skip to content
Merged
15 changes: 7 additions & 8 deletions utility/WiFiClientStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

published under the same license.

Last updated April 17th, 2016
Last updated April 23rd, 2016
*/

#ifndef WIFI_CLIENT_STREAM_H
Expand All @@ -41,21 +41,20 @@ class WiFiClientStream : public WiFiStream
*/
virtual inline bool connect_client()
{
if( _client && _client.connected() ) return true;

if( _connected )
if ( _connected )
{
if ( _client && _client.connected() ) return true;
stop();
}

// active TCP connect
if( WiFi.status() == WL_CONNECTED )
if ( WiFi.status() == WL_CONNECTED )
{
// if the client is disconnected, try to reconnect every 5 seconds
if( millis() - _time_connect >= MILLIS_RECONNECT )
if ( millis() - _time_connect >= MILLIS_RECONNECT )
{
_connected = _client.connect( _remote_ip, _port );
if( !_connected )
if ( !_connected )
{
_time_connect = millis();
}
Expand Down Expand Up @@ -89,7 +88,7 @@ class WiFiClientStream : public WiFiStream
*/
virtual inline void stop()
{
if( _client)
if ( _client)
{
_client.stop();
if ( _currentHostConnectionCallback )
Expand Down
15 changes: 7 additions & 8 deletions utility/WiFiServerStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

published under the same license.

Last updated April 17th, 2016
Last updated April 23rd, 2016
*/

#ifndef WIFI_SERVER_STREAM_H
Expand All @@ -40,16 +40,15 @@ class WiFiServerStream : public WiFiStream
*/
virtual inline bool connect_client()
{
if( _client && _client.connected() ) return true;

if( _connected )
if ( _connected )
{
if ( _client && _client.connected() ) return true;
stop();
}

// passive TCP connect (accept)
WiFiClient newClient = _server.available();
if( !newClient ) return false;
if ( !newClient ) return false;
_client = newClient;
_connected = true;
if ( _currentHostConnectionCallback )
Expand All @@ -72,11 +71,11 @@ class WiFiServerStream : public WiFiStream
*/
virtual inline bool maintain()
{
if( connect_client() ) return true;
if ( connect_client() ) return true;

stop();

if( !_listening && WiFi.status() == WL_CONNECTED )
if ( !_listening && WiFi.status() == WL_CONNECTED )
{
// start TCP server after first WiFi connect
_server = WiFiServer(_port);
Expand All @@ -92,7 +91,7 @@ class WiFiServerStream : public WiFiStream
*/
virtual inline void stop()
{
if( _client)
if ( _client)
{
_client.stop();
if ( _currentHostConnectionCallback )
Expand Down
24 changes: 23 additions & 1 deletion utility/WiFiStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

See file LICENSE.txt for further informations on licensing terms.

Last updated April 17th, 2016
Last updated April 23rd, 2016
*/

#ifndef WIFI_STREAM_H
Expand Down Expand Up @@ -114,6 +114,28 @@ class WiFiStream : public Stream
* @return true if WiFi and TCP connection are established
*/
virtual bool maintain() = 0;

#ifdef ESP8266
/**
* get status of TCP connection
* @return status of TCP connection
* CLOSED = 0 (typical)
* LISTEN = 1 (not used)
* SYN_SENT = 2
* SYN_RCVD = 3
* ESTABLISHED = 4 (typical)
* FIN_WAIT_1 = 5
* FIN_WAIT_2 = 6
* CLOSE_WAIT = 7
* CLOSING = 8
* LAST_ACK = 9
* TIME_WAIT = 10
*/
inline uint8_t status()
{
return _client.status();
}
#endif

/**
* close TCP client connection
Expand Down