Skip to content

Commit b6573c8

Browse files
committed
WiFiServerStream fix & host connection callback
- fixed client accept in WiFiServerStream - added host connection callback hook to notfiy connect and disconnect
1 parent 3d2085c commit b6573c8

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

utility/WiFiClientStream.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
See file LICENSE.txt for further informations on licensing terms.
1616
1717
Parts of this class are based on
18-
18+
1919
- EthernetClientStream - Copyright (C) 2013 Norbert Truchsess. All rights reserved.
20-
20+
2121
published under the same license.
2222
23-
Last updated April 15th, 2016
23+
Last updated April 17th, 2016
2424
*/
2525

2626
#ifndef WIFI_CLIENT_STREAM_H
@@ -55,9 +55,14 @@ class WiFiClientStream : public WiFiStream
5555
if( millis() - _time_connect >= MILLIS_RECONNECT )
5656
{
5757
_connected = _client.connect( _remote_ip, _port );
58-
if( !_connected ) {
58+
if( !_connected )
59+
{
5960
_time_connect = millis();
6061
}
62+
else if ( _currentHostConnectionCallback )
63+
{
64+
(*_currentHostConnectionCallback)(HOST_CONNECTION_CONNECTED);
65+
}
6166
}
6267
}
6368

@@ -87,6 +92,10 @@ class WiFiClientStream : public WiFiStream
8792
if( _client)
8893
{
8994
_client.stop();
95+
if ( _currentHostConnectionCallback )
96+
{
97+
(*_currentHostConnectionCallback)(HOST_CONNECTION_DISCONNECTED);
98+
}
9099
}
91100
_connected = false;
92101
_time_connect = millis();

utility/WiFiServerStream.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
published under the same license.
2222
23-
Last updated April 16th, 2016
23+
Last updated April 17th, 2016
2424
*/
2525

2626
#ifndef WIFI_SERVER_STREAM_H
@@ -49,8 +49,12 @@ class WiFiServerStream : public WiFiStream
4949

5050
// passive TCP connect (accept)
5151
WiFiClient newClient = _server.available();
52-
if( !_client ) return false; // could this work on all platforms? if( !(_client && _client.connected()) ) return false;
52+
if( !newClient ) return false;
5353
_client = newClient;
54+
if ( _currentHostConnectionCallback )
55+
{
56+
(*_currentHostConnectionCallback)(HOST_CONNECTION_CONNECTED);
57+
}
5458

5559
return true;
5660
}
@@ -90,6 +94,10 @@ class WiFiServerStream : public WiFiStream
9094
if( _client)
9195
{
9296
_client.stop();
97+
if ( _currentHostConnectionCallback )
98+
{
99+
(*_currentHostConnectionCallback)(HOST_CONNECTION_DISCONNECTED);
100+
}
93101
}
94102
_connected = false;
95103
}

utility/WiFiStream.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,29 @@
1515
1616
See file LICENSE.txt for further informations on licensing terms.
1717
18-
Last updated April 15th, 2016
18+
Last updated April 17th, 2016
1919
*/
20-
20+
2121
#ifndef WIFI_STREAM_H
2222
#define WIFI_STREAM_H
2323

2424
#include <inttypes.h>
2525
#include <Stream.h>
2626

27+
#define HOST_CONNECTION_DISCONNECTED 0
28+
#define HOST_CONNECTION_CONNECTED 1
29+
30+
extern "C" {
31+
// callback function types
32+
typedef void (*hostConnectionCallbackFunction)(byte);
33+
}
34+
2735
class WiFiStream : public Stream
2836
{
2937
protected:
3038
WiFiClient _client;
3139
bool _connected = false;
40+
hostConnectionCallbackFunction _currentHostConnectionCallback;
3241

3342
//configuration members
3443
IPAddress _local_ip; // DHCP
@@ -54,6 +63,7 @@ class WiFiStream : public Stream
5463
/** constructor for TCP client */
5564
WiFiStream(IPAddress server_ip, uint16_t server_port) : _remote_ip(server_ip), _port(server_port) {}
5665

66+
inline void attach( hostConnectionCallbackFunction newFunction ) { _currentHostConnectionCallback = newFunction; }
5767

5868
/******************************************************************************
5969
* network configuration

0 commit comments

Comments
 (0)