4 An Arduino Stream extension for a WiFiClient or WiFiServer to be used
5 with legacy Arduino WiFi shield and other boards and shields that
6 are compatible with the Arduino WiFi library.
8 Copyright (C) 2016 Jens B. All rights reserved.
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2.1 of the License, or (at your option) any later version.
15 See file LICENSE.txt for further informations on licensing terms.
17 Parts of this class are based on
19 - WiFiStream - Copyright (C) 2015-2016 Jesse Frush. All rights reserved.
21 published under the same license.
23 Last updated April 23rd, 2016
26 #ifndef WIFI_SERVER_STREAM_H
27 #define WIFI_SERVER_STREAM_H
29 #include "WiFiStream.h"
31 class WiFiServerStream : public WiFiStream
34 WiFiServer _server = WiFiServer(3030);
35 bool _listening = false;
38 * check if TCP client is connected
39 * @return true if connected
41 virtual inline bool connect_client()
45 if ( _client && _client.connected() ) return true;
49 // passive TCP connect (accept)
50 WiFiClient newClient = _server.available();
51 if ( !newClient ) return false;
54 if ( _currentHostConnectionCallback )
56 (*_currentHostConnectionCallback)(HOST_CONNECTION_CONNECTED);
64 * create a WiFi stream with a TCP server
66 WiFiServerStream(uint16_t server_port) : WiFiStream(server_port) {}
69 * maintain WiFi and TCP connection
70 * @return true if WiFi and TCP connection are established
72 virtual inline bool maintain()
74 if ( connect_client() ) return true;
78 if ( !_listening && WiFi.status() == WL_CONNECTED )
80 // start TCP server after first WiFi connect
81 _server = WiFiServer(_port);
90 * stop client connection
92 virtual inline void stop()
97 if ( _currentHostConnectionCallback )
99 (*_currentHostConnectionCallback)(HOST_CONNECTION_DISCONNECTED);
107 #endif //WIFI_SERVER_STREAM_H