23
23
#undef write
24
24
#undef close
25
25
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
+
26
79
int WiFiServer::setTimeout (uint32_t seconds){
27
80
struct timeval tv;
28
81
tv.tv_sec = seconds;
@@ -33,13 +86,49 @@ int WiFiServer::setTimeout(uint32_t seconds){
33
86
}
34
87
35
88
size_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;
37
106
}
38
107
39
108
void WiFiServer::stopAll (){}
40
109
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;
43
132
}
44
133
45
134
WiFiClient WiFiServer::accept (){
@@ -131,6 +220,14 @@ void WiFiServer::end(){
131
220
#endif
132
221
sockfd = -1 ;
133
222
_listening = false ;
223
+
224
+ WIFISERVER_MUTEX_LOCK ();
225
+ for (uint8_t i = 0 ; i < SERVER_MAX_MONITORED_CLIENTS; i++) {
226
+ if (connectedClients[i]) {
227
+ connectedClients[i].stop ();
228
+ }
229
+ }
230
+ WIFISERVER_MUTEX_UNLOCK ();
134
231
}
135
232
136
233
void WiFiServer::close (){
@@ -141,3 +238,12 @@ void WiFiServer::stop(){
141
238
end ();
142
239
}
143
240
241
+ void WiFiServer::acceptClients () {
242
+ for (uint8_t i = 0 ; i < SERVER_MAX_MONITORED_CLIENTS; i++) {
243
+ WiFiClient& client = connectedClients[i];
244
+ if (!client.connected () && !client.available ()) {
245
+ client = accept ();
246
+ }
247
+ }
248
+ }
249
+
0 commit comments