forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWiFiServer.cpp
203 lines (170 loc) · 4.76 KB
/
WiFiServer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
WiFiServer.cpp - TCP/IP server for esp8266, mostly compatible
with Arduino WiFi shield library
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define LWIP_INTERNAL
extern "C" {
#include "osapi.h"
#include "ets_sys.h"
}
#include "debug.h"
#include "ESP8266WiFi.h"
#include "WiFiClient.h"
#include "WiFiServer.h"
#include "lwip/opt.h"
#include "lwip/tcp.h"
#include "lwip/inet.h"
#include "lwip/init.h" // LWIP_VERSION_
#include <include/ClientContext.h>
WiFiServer::WiFiServer(const IPAddress& addr, uint16_t port)
: _port(port)
, _addr(addr)
, _listen_pcb(nullptr)
, _unclaimed(nullptr)
, _discarded(nullptr)
{
}
WiFiServer::WiFiServer(uint16_t port)
: _port(port)
, _addr(IP_ANY_TYPE)
, _listen_pcb(nullptr)
, _unclaimed(nullptr)
, _discarded(nullptr)
{
}
void WiFiServer::begin() {
begin(_port);
}
void WiFiServer::begin(uint16_t port, int backlog) {
close();
_port = port;
err_t err;
tcp_pcb* pcb = tcp_new();
if (!pcb)
return;
pcb->so_options |= SOF_REUSEADDR;
// (IPAddress _addr) operator-converted to (const ip_addr_t*)
err = tcp_bind(pcb, _addr, _port);
if (err != ERR_OK) {
tcp_close(pcb);
return;
}
#if LWIP_VERSION_MAJOR == 1
(void)backlog;
tcp_pcb* listen_pcb = tcp_listen(pcb);
#else
tcp_pcb* listen_pcb = tcp_listen_with_backlog(pcb, backlog);
#endif
if (!listen_pcb) {
tcp_close(pcb);
return;
}
_listen_pcb = listen_pcb;
_port = _listen_pcb->local_port;
tcp_accept(listen_pcb, &WiFiServer::_s_accept);
tcp_arg(listen_pcb, (void*) this);
}
void WiFiServer::setNoDelay(bool nodelay) {
_noDelay = nodelay? _ndTrue: _ndFalse;
}
bool WiFiServer::getNoDelay() {
switch (_noDelay)
{
case _ndFalse: return false;
case _ndTrue: return true;
default: return WiFiClient::getDefaultNoDelay();
}
}
bool WiFiServer::hasClient() {
if (_unclaimed)
return true;
return false;
}
WiFiClient WiFiServer::available(byte* status) {
(void) status;
if (_unclaimed) {
WiFiClient result(_unclaimed);
_unclaimed = _unclaimed->next();
result.setNoDelay(getNoDelay());
DEBUGV("WS:av\r\n");
return result;
}
optimistic_yield(1000);
return WiFiClient();
}
uint8_t WiFiServer::status() {
if (!_listen_pcb)
return CLOSED;
return _listen_pcb->state;
}
uint16_t WiFiServer::port() const {
return _port;
}
void WiFiServer::close() {
if (!_listen_pcb) {
return;
}
tcp_close(_listen_pcb);
_listen_pcb = nullptr;
}
void WiFiServer::stop() {
close();
}
size_t WiFiServer::write(uint8_t b) {
return write(&b, 1);
}
size_t WiFiServer::write(const uint8_t *buffer, size_t size) {
// write to all clients
// not implemented
(void) buffer;
(void) size;
return 0;
}
template<typename T>
T* slist_append_tail(T* head, T* item) {
if (!head)
return item;
T* last = head;
while(last->next())
last = last->next();
last->next(item);
return head;
}
long WiFiServer::_accept(tcp_pcb* apcb, long err) {
(void) err;
DEBUGV("WS:ac\r\n");
ClientContext* client = new ClientContext(apcb, &WiFiServer::_s_discard, this);
_unclaimed = slist_append_tail(_unclaimed, client);
#if LWIP_VERSION_MAJOR == 1
tcp_accepted(_listen_pcb);
#else
tcp_backlog_accepted(_listen_pcb);
// http://lwip.100.n7.nabble.com/Problem-re-opening-listening-pbc-tt32484.html#a32494
// https://www.nongnu.org/lwip/2_1_x/group__tcp__raw.html#gaeff14f321d1eecd0431611f382fcd338
#endif
return ERR_OK;
}
void WiFiServer::_discard(ClientContext* client) {
(void) client;
// _discarded = slist_append_tail(_discarded, client);
DEBUGV("WS:dis\r\n");
}
long WiFiServer::_s_accept(void *arg, tcp_pcb* newpcb, long err) {
return reinterpret_cast<WiFiServer*>(arg)->_accept(newpcb, err);
}
void WiFiServer::_s_discard(void* server, ClientContext* ctx) {
reinterpret_cast<WiFiServer*>(server)->_discard(ctx);
}