2323#undef write
2424#undef close
2525
26+ #if !CONFIG_DISABLE_HAL_LOCKS
27+ #define WIFISERVER_MUTEX_LOCK () do {} while (xSemaphoreTake(_lock, portMAX_DELAY) != pdPASS)
28+ #define WIFISERVER_MUTEX_UNLOCK () xSemaphoreGive(_lock)
29+ #else
30+ #define WIFISERVER_MUTEX_LOCK ()
31+ #define WIFISERVER_MUTEX_UNLOCK ()
32+ #endif
33+
34+ WiFiServer::WiFiServer (uint16_t port, uint8_t max_clients) :
35+ sockfd(-1 ), _accepted_sockfd(-1 ), _addr(), _port(port), _max_clients(max_clients), _listening(false ), _noDelay(false )
36+ #if !CONFIG_DISABLE_HAL_LOCKS
37+ , _lock(NULL )
38+ #endif
39+ {
40+ log_v (" WiFiServer::WiFiServer(port=%d, ...)" , port);
41+ #if !CONFIG_DISABLE_HAL_LOCKS
42+ if (_lock == NULL ) {
43+ _lock = xSemaphoreCreateMutex ();
44+ if (_lock == NULL ) {
45+ log_e (" xSemaphoreCreateMutex failed" );
46+ return ;
47+ }
48+ }
49+ #endif
50+ }
51+
52+ WiFiServer::WiFiServer (const IPAddress &addr, uint16_t port, uint8_t max_clients) :
53+ sockfd(-1 ), _accepted_sockfd(-1 ), _addr(addr), _port(port), _max_clients(max_clients), _listening(false ), _noDelay(false )
54+ #if !CONFIG_DISABLE_HAL_LOCKS
55+ , _lock(NULL )
56+ #endif
57+ {
58+ log_v (" WiFiServer::WiFiServer(addr=%s, port=%d, ...)" , addr.toString ().c_str (), port);
59+ #if !CONFIG_DISABLE_HAL_LOCKS
60+ if (_lock == NULL ) {
61+ _lock = xSemaphoreCreateMutex ();
62+ if (_lock == NULL ) {
63+ log_e (" xSemaphoreCreateMutex failed" );
64+ return ;
65+ }
66+ }
67+ #endif
68+ }
69+
70+ WiFiServer::~WiFiServer () {
71+ end ();
72+ #if !CONFIG_DISABLE_HAL_LOCKS
73+ if (_lock != NULL ) {
74+ vSemaphoreDelete (_lock);
75+ }
76+ #endif
77+ }
78+
2679int WiFiServer::setTimeout (uint32_t seconds){
2780 struct timeval tv;
2881 tv.tv_sec = seconds;
@@ -33,13 +86,49 @@ int WiFiServer::setTimeout(uint32_t seconds){
3386}
3487
3588size_t WiFiServer::write (const uint8_t *data, size_t len){
36- return 0 ;
89+ WIFISERVER_MUTEX_LOCK ();
90+ static uint32_t lastCheck;
91+ uint32_t m = millis ();
92+ if (m - lastCheck > 100 ) {
93+ lastCheck = m;
94+ acceptClients ();
95+ }
96+ size_t ret = 0 ;
97+ if (len > 0 ) {
98+ for (uint8_t i = 0 ; i < SERVER_MAX_MONITORED_CLIENTS; i++) {
99+ if (connectedClients[i].connected ()) {
100+ ret += connectedClients[i].write (data, len);
101+ }
102+ }
103+ }
104+ WIFISERVER_MUTEX_UNLOCK ();
105+ return ret;
37106}
38107
39108void WiFiServer::stopAll (){}
40109
41- WiFiClient WiFiServer::available (){
42- return accept ();
110+ // https://www.arduino.cc/en/Reference/WiFiServerAvailable
111+ WiFiClient WiFiServer::available () {
112+ WIFISERVER_MUTEX_LOCK ();
113+
114+ acceptClients ();
115+
116+ WiFiClient ret;
117+
118+ // find next client with data available
119+ for (uint8_t i = 0 ; i < SERVER_MAX_MONITORED_CLIENTS; i++) {
120+ if (index == SERVER_MAX_MONITORED_CLIENTS) {
121+ index = 0 ;
122+ }
123+ WiFiClient& client = connectedClients[index];
124+ index++;
125+ if (client.available ()) {
126+ ret = client;
127+ break ;
128+ }
129+ }
130+ WIFISERVER_MUTEX_UNLOCK ();
131+ return ret;
43132}
44133
45134WiFiClient WiFiServer::accept (){
@@ -138,6 +227,14 @@ void WiFiServer::end(){
138227#endif
139228 sockfd = -1 ;
140229 _listening = false ;
230+
231+ WIFISERVER_MUTEX_LOCK ();
232+ for (uint8_t i = 0 ; i < SERVER_MAX_MONITORED_CLIENTS; i++) {
233+ if (connectedClients[i]) {
234+ connectedClients[i].stop ();
235+ }
236+ }
237+ WIFISERVER_MUTEX_UNLOCK ();
141238}
142239
143240void WiFiServer::close (){
@@ -147,3 +244,13 @@ void WiFiServer::close(){
147244void WiFiServer::stop (){
148245 end ();
149246}
247+
248+ void WiFiServer::acceptClients () {
249+ for (uint8_t i = 0 ; i < SERVER_MAX_MONITORED_CLIENTS; i++) {
250+ WiFiClient& client = connectedClients[i];
251+ if (!client.connected () && !client.available ()) {
252+ client = accept ();
253+ }
254+ }
255+ }
256+
0 commit comments