Skip to content

Commit 5b28ae2

Browse files
committed
Allow WiFiServer to bind to a specific address (esp8266#997)
In addition to the existing constructors (WiFiServer::WiFiServer(port) and WiFiWebServer::WiFiWebServer(port)), new constructors are added: WiFiServer::WiFiServer(IPAddress, port) and WiFiWebServer::WiFiWebServer(IPAddress, port).
1 parent 34045d4 commit 5b28ae2

File tree

4 files changed

+66
-51
lines changed

4 files changed

+66
-51
lines changed

libraries/ESP8266WebServer/src/ESP8266WebServer.cpp

+15-6
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,32 @@
3131
#define DEBUG_OUTPUT Serial
3232

3333

34+
ESP8266WebServer::ESP8266WebServer(IPAddress addr, int port)
35+
: _server(addr, port)
36+
, _firstHandler(0)
37+
, _lastHandler(0)
38+
, _currentArgCount(0)
39+
, _currentArgs(0)
40+
, _headerKeysCount(0)
41+
, _currentHeaders(0)
42+
{
43+
}
44+
3445
ESP8266WebServer::ESP8266WebServer(int port)
3546
: _server(port)
3647
, _firstHandler(0)
3748
, _lastHandler(0)
3849
, _currentArgCount(0)
3950
, _currentArgs(0)
40-
,_headerKeysCount(0)
41-
,_currentHeaders(0)
51+
, _headerKeysCount(0)
52+
, _currentHeaders(0)
4253
{
4354
}
4455

4556
ESP8266WebServer::~ESP8266WebServer() {
46-
if (_currentHeaders)
47-
delete[]_currentHeaders;
57+
if (_currentHeaders)
58+
delete[]_currentHeaders;
4859
_headerKeysCount = 0;
49-
if (!_firstHandler)
50-
return;
5160
RequestHandler* handler = _firstHandler;
5261
while (handler) {
5362
RequestHandler* next = handler->next();

libraries/ESP8266WebServer/src/ESP8266WebServer.h

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ typedef struct {
5959
class ESP8266WebServer
6060
{
6161
public:
62+
ESP8266WebServer(IPAddress addr, int port = 80);
6263
ESP8266WebServer(int port = 80);
6364
~ESP8266WebServer();
6465

libraries/ESP8266WiFi/src/WiFiServer.cpp

+46-44
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,41 @@ extern "C" {
3838
#include "cbuf.h"
3939
#include "include/ClientContext.h"
4040

41-
WiFiServer::WiFiServer(uint16_t port)
41+
WiFiServer::WiFiServer(IPAddress addr, uint16_t port)
42+
: _port(port)
43+
, _addr(addr)
44+
, _pcb(nullptr)
45+
, _unclaimed(nullptr)
46+
, _discarded(nullptr)
4247
{
43-
_port = port;
44-
_pcb = 0;
45-
_unclaimed = 0;
46-
_discarded = 0;
4748
}
4849

49-
void WiFiServer::begin()
50+
WiFiServer::WiFiServer(uint16_t port)
51+
: _port(port)
52+
, _addr((uint32_t) IPADDR_ANY)
53+
, _pcb(nullptr)
54+
, _unclaimed(nullptr)
55+
, _discarded(nullptr)
5056
{
51-
err_t err;
57+
}
5258

59+
void WiFiServer::begin() {
60+
err_t err;
5361
tcp_pcb* pcb = tcp_new();
5462
if (!pcb)
5563
return;
5664

57-
err = tcp_bind(pcb, INADDR_ANY, _port);
65+
ip_addr_t local_addr;
66+
local_addr.addr = (uint32_t) _addr;
67+
err = tcp_bind(pcb, &local_addr, _port);
5868

59-
if (err != ERR_OK)
60-
{
69+
if (err != ERR_OK) {
6170
tcp_close(pcb);
6271
return;
6372
}
6473

6574
tcp_pcb* listen_pcb = tcp_listen(pcb);
66-
if (!listen_pcb)
67-
{
75+
if (!listen_pcb) {
6876
tcp_close(pcb);
6977
return;
7078
}
@@ -73,59 +81,59 @@ void WiFiServer::begin()
7381
tcp_arg(listen_pcb, (void*) this);
7482
}
7583

76-
void WiFiServer::setNoDelay(bool nodelay){
77-
if(!_pcb) return;
78-
if(nodelay) tcp_nagle_disable(_pcb);
79-
else tcp_nagle_enable(_pcb);
80-
}
84+
void WiFiServer::setNoDelay(bool nodelay) {
85+
if (!_pcb)
86+
return;
8187

82-
bool WiFiServer::getNoDelay(){
83-
if(!_pcb) return false;
84-
return tcp_nagle_disabled(_pcb);
88+
if (nodelay)
89+
tcp_nagle_disable(_pcb);
90+
else
91+
tcp_nagle_enable(_pcb);
8592
}
8693

87-
bool WiFiServer::hasClient(){
88-
if (_unclaimed) return true;
89-
return false;
94+
bool WiFiServer::getNoDelay() {
95+
if (!_pcb)
96+
return false;
97+
return tcp_nagle_disabled(_pcb);
9098
}
9199

92-
WiFiClient WiFiServer::available(byte* status)
93-
{
100+
bool WiFiServer::hasClient() {
94101
if (_unclaimed)
95-
{
102+
return true;
103+
return false;
104+
}
105+
106+
WiFiClient WiFiServer::available(byte* status) {
107+
if (_unclaimed) {
96108
WiFiClient result(_unclaimed);
97109
_unclaimed = _unclaimed->next();
98110
DEBUGV("WS:av\r\n");
99111
return result;
100112
}
101113

102114
optimistic_yield(1000);
103-
104115
return WiFiClient();
105116
}
106117

107-
uint8_t WiFiServer::status() {
118+
uint8_t WiFiServer::status() {
108119
if (!_pcb)
109120
return CLOSED;
110121
return _pcb->state;
111122
}
112123

113124

114-
size_t WiFiServer::write(uint8_t b)
115-
{
125+
size_t WiFiServer::write(uint8_t b) {
116126
return write(&b, 1);
117127
}
118128

119-
size_t WiFiServer::write(const uint8_t *buffer, size_t size)
120-
{
129+
size_t WiFiServer::write(const uint8_t *buffer, size_t size) {
121130
// write to all clients
122131
// not implemented
123132
return 0;
124133
}
125134

126135
template<typename T>
127-
T* slist_append_tail(T* head, T* item)
128-
{
136+
T* slist_append_tail(T* head, T* item) {
129137
if (!head)
130138
return item;
131139
T* last = head;
@@ -135,29 +143,23 @@ T* slist_append_tail(T* head, T* item)
135143
return head;
136144
}
137145

138-
int8_t WiFiServer::_accept(tcp_pcb* apcb, int8_t err)
139-
{
146+
int8_t WiFiServer::_accept(tcp_pcb* apcb, int8_t err) {
140147
DEBUGV("WS:ac\r\n");
141148
ClientContext* client = new ClientContext(apcb, &WiFiServer::_s_discard, this);
142149
_unclaimed = slist_append_tail(_unclaimed, client);
143150
tcp_accepted(_pcb);
144-
// printf("WiFiServer::_accept\r\n");
145151
return ERR_OK;
146152
}
147153

148-
void WiFiServer::_discard(ClientContext* client)
149-
{
154+
void WiFiServer::_discard(ClientContext* client) {
150155
// _discarded = slist_append_tail(_discarded, client);
151156
DEBUGV("WS:dis\r\n");
152-
// printf("WiFiServer::_discard\r\n");
153157
}
154158

155-
int8_t WiFiServer::_s_accept(void *arg, tcp_pcb* newpcb, int8_t err)
156-
{
159+
int8_t WiFiServer::_s_accept(void *arg, tcp_pcb* newpcb, int8_t err) {
157160
return reinterpret_cast<WiFiServer*>(arg)->_accept(newpcb, err);
158161
}
159162

160-
void WiFiServer::_s_discard(void* server, ClientContext* ctx)
161-
{
163+
void WiFiServer::_s_discard(void* server, ClientContext* ctx) {
162164
reinterpret_cast<WiFiServer*>(server)->_discard(ctx);
163165
}

libraries/ESP8266WiFi/src/WiFiServer.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,22 @@ extern "C" {
2929
}
3030

3131
#include "Server.h"
32+
#include "IPAddress.h"
3233

3334
class ClientContext;
3435
class WiFiClient;
3536

3637
class WiFiServer : public Server {
3738
private:
3839
uint16_t _port;
40+
IPAddress _addr;
3941
tcp_pcb* _pcb;
4042

4143
ClientContext* _unclaimed;
4244
ClientContext* _discarded;
4345

4446
public:
47+
WiFiServer(IPAddress addr, uint16_t port);
4548
WiFiServer(uint16_t port);
4649
WiFiClient available(uint8_t* status = NULL);
4750
bool hasClient();
@@ -56,7 +59,7 @@ class WiFiServer : public Server {
5659

5760
protected:
5861
int8_t _accept(tcp_pcb* newpcb, int8_t err);
59-
void _discard(ClientContext* client);
62+
void _discard(ClientContext* client);
6063

6164
static int8_t _s_accept(void *arg, tcp_pcb* newpcb, int8_t err);
6265
static void _s_discard(void* server, ClientContext* ctx);

0 commit comments

Comments
 (0)