mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
Allow WiFiServer to bind to a specific address (#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).
This commit is contained in:
parent
34045d4908
commit
5b28ae23c2
@ -31,6 +31,17 @@
|
|||||||
#define DEBUG_OUTPUT Serial
|
#define DEBUG_OUTPUT Serial
|
||||||
|
|
||||||
|
|
||||||
|
ESP8266WebServer::ESP8266WebServer(IPAddress addr, int port)
|
||||||
|
: _server(addr, port)
|
||||||
|
, _firstHandler(0)
|
||||||
|
, _lastHandler(0)
|
||||||
|
, _currentArgCount(0)
|
||||||
|
, _currentArgs(0)
|
||||||
|
, _headerKeysCount(0)
|
||||||
|
, _currentHeaders(0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
ESP8266WebServer::ESP8266WebServer(int port)
|
ESP8266WebServer::ESP8266WebServer(int port)
|
||||||
: _server(port)
|
: _server(port)
|
||||||
, _firstHandler(0)
|
, _firstHandler(0)
|
||||||
@ -46,8 +57,6 @@ ESP8266WebServer::~ESP8266WebServer() {
|
|||||||
if (_currentHeaders)
|
if (_currentHeaders)
|
||||||
delete[]_currentHeaders;
|
delete[]_currentHeaders;
|
||||||
_headerKeysCount = 0;
|
_headerKeysCount = 0;
|
||||||
if (!_firstHandler)
|
|
||||||
return;
|
|
||||||
RequestHandler* handler = _firstHandler;
|
RequestHandler* handler = _firstHandler;
|
||||||
while (handler) {
|
while (handler) {
|
||||||
RequestHandler* next = handler->next();
|
RequestHandler* next = handler->next();
|
||||||
|
@ -59,6 +59,7 @@ typedef struct {
|
|||||||
class ESP8266WebServer
|
class ESP8266WebServer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
ESP8266WebServer(IPAddress addr, int port = 80);
|
||||||
ESP8266WebServer(int port = 80);
|
ESP8266WebServer(int port = 80);
|
||||||
~ESP8266WebServer();
|
~ESP8266WebServer();
|
||||||
|
|
||||||
|
@ -38,33 +38,41 @@ extern "C" {
|
|||||||
#include "cbuf.h"
|
#include "cbuf.h"
|
||||||
#include "include/ClientContext.h"
|
#include "include/ClientContext.h"
|
||||||
|
|
||||||
WiFiServer::WiFiServer(uint16_t port)
|
WiFiServer::WiFiServer(IPAddress addr, uint16_t port)
|
||||||
|
: _port(port)
|
||||||
|
, _addr(addr)
|
||||||
|
, _pcb(nullptr)
|
||||||
|
, _unclaimed(nullptr)
|
||||||
|
, _discarded(nullptr)
|
||||||
{
|
{
|
||||||
_port = port;
|
|
||||||
_pcb = 0;
|
|
||||||
_unclaimed = 0;
|
|
||||||
_discarded = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WiFiServer::begin()
|
WiFiServer::WiFiServer(uint16_t port)
|
||||||
|
: _port(port)
|
||||||
|
, _addr((uint32_t) IPADDR_ANY)
|
||||||
|
, _pcb(nullptr)
|
||||||
|
, _unclaimed(nullptr)
|
||||||
|
, _discarded(nullptr)
|
||||||
{
|
{
|
||||||
err_t err;
|
}
|
||||||
|
|
||||||
|
void WiFiServer::begin() {
|
||||||
|
err_t err;
|
||||||
tcp_pcb* pcb = tcp_new();
|
tcp_pcb* pcb = tcp_new();
|
||||||
if (!pcb)
|
if (!pcb)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
err = tcp_bind(pcb, INADDR_ANY, _port);
|
ip_addr_t local_addr;
|
||||||
|
local_addr.addr = (uint32_t) _addr;
|
||||||
|
err = tcp_bind(pcb, &local_addr, _port);
|
||||||
|
|
||||||
if (err != ERR_OK)
|
if (err != ERR_OK) {
|
||||||
{
|
|
||||||
tcp_close(pcb);
|
tcp_close(pcb);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
tcp_pcb* listen_pcb = tcp_listen(pcb);
|
tcp_pcb* listen_pcb = tcp_listen(pcb);
|
||||||
if (!listen_pcb)
|
if (!listen_pcb) {
|
||||||
{
|
|
||||||
tcp_close(pcb);
|
tcp_close(pcb);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -74,25 +82,29 @@ void WiFiServer::begin()
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WiFiServer::setNoDelay(bool nodelay) {
|
void WiFiServer::setNoDelay(bool nodelay) {
|
||||||
if(!_pcb) return;
|
if (!_pcb)
|
||||||
if(nodelay) tcp_nagle_disable(_pcb);
|
return;
|
||||||
else tcp_nagle_enable(_pcb);
|
|
||||||
|
if (nodelay)
|
||||||
|
tcp_nagle_disable(_pcb);
|
||||||
|
else
|
||||||
|
tcp_nagle_enable(_pcb);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WiFiServer::getNoDelay() {
|
bool WiFiServer::getNoDelay() {
|
||||||
if(!_pcb) return false;
|
if (!_pcb)
|
||||||
|
return false;
|
||||||
return tcp_nagle_disabled(_pcb);
|
return tcp_nagle_disabled(_pcb);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WiFiServer::hasClient() {
|
bool WiFiServer::hasClient() {
|
||||||
if (_unclaimed) return true;
|
if (_unclaimed)
|
||||||
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
WiFiClient WiFiServer::available(byte* status)
|
WiFiClient WiFiServer::available(byte* status) {
|
||||||
{
|
if (_unclaimed) {
|
||||||
if (_unclaimed)
|
|
||||||
{
|
|
||||||
WiFiClient result(_unclaimed);
|
WiFiClient result(_unclaimed);
|
||||||
_unclaimed = _unclaimed->next();
|
_unclaimed = _unclaimed->next();
|
||||||
DEBUGV("WS:av\r\n");
|
DEBUGV("WS:av\r\n");
|
||||||
@ -100,7 +112,6 @@ WiFiClient WiFiServer::available(byte* status)
|
|||||||
}
|
}
|
||||||
|
|
||||||
optimistic_yield(1000);
|
optimistic_yield(1000);
|
||||||
|
|
||||||
return WiFiClient();
|
return WiFiClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,21 +122,18 @@ uint8_t WiFiServer::status() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
size_t WiFiServer::write(uint8_t b)
|
size_t WiFiServer::write(uint8_t b) {
|
||||||
{
|
|
||||||
return write(&b, 1);
|
return write(&b, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t WiFiServer::write(const uint8_t *buffer, size_t size)
|
size_t WiFiServer::write(const uint8_t *buffer, size_t size) {
|
||||||
{
|
|
||||||
// write to all clients
|
// write to all clients
|
||||||
// not implemented
|
// not implemented
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T* slist_append_tail(T* head, T* item)
|
T* slist_append_tail(T* head, T* item) {
|
||||||
{
|
|
||||||
if (!head)
|
if (!head)
|
||||||
return item;
|
return item;
|
||||||
T* last = head;
|
T* last = head;
|
||||||
@ -135,29 +143,23 @@ T* slist_append_tail(T* head, T* item)
|
|||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t WiFiServer::_accept(tcp_pcb* apcb, int8_t err)
|
int8_t WiFiServer::_accept(tcp_pcb* apcb, int8_t err) {
|
||||||
{
|
|
||||||
DEBUGV("WS:ac\r\n");
|
DEBUGV("WS:ac\r\n");
|
||||||
ClientContext* client = new ClientContext(apcb, &WiFiServer::_s_discard, this);
|
ClientContext* client = new ClientContext(apcb, &WiFiServer::_s_discard, this);
|
||||||
_unclaimed = slist_append_tail(_unclaimed, client);
|
_unclaimed = slist_append_tail(_unclaimed, client);
|
||||||
tcp_accepted(_pcb);
|
tcp_accepted(_pcb);
|
||||||
// printf("WiFiServer::_accept\r\n");
|
|
||||||
return ERR_OK;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WiFiServer::_discard(ClientContext* client)
|
void WiFiServer::_discard(ClientContext* client) {
|
||||||
{
|
|
||||||
// _discarded = slist_append_tail(_discarded, client);
|
// _discarded = slist_append_tail(_discarded, client);
|
||||||
DEBUGV("WS:dis\r\n");
|
DEBUGV("WS:dis\r\n");
|
||||||
// printf("WiFiServer::_discard\r\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t WiFiServer::_s_accept(void *arg, tcp_pcb* newpcb, int8_t err)
|
int8_t WiFiServer::_s_accept(void *arg, tcp_pcb* newpcb, int8_t err) {
|
||||||
{
|
|
||||||
return reinterpret_cast<WiFiServer*>(arg)->_accept(newpcb, err);
|
return reinterpret_cast<WiFiServer*>(arg)->_accept(newpcb, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WiFiServer::_s_discard(void* server, ClientContext* ctx)
|
void WiFiServer::_s_discard(void* server, ClientContext* ctx) {
|
||||||
{
|
|
||||||
reinterpret_cast<WiFiServer*>(server)->_discard(ctx);
|
reinterpret_cast<WiFiServer*>(server)->_discard(ctx);
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@ extern "C" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#include "Server.h"
|
#include "Server.h"
|
||||||
|
#include "IPAddress.h"
|
||||||
|
|
||||||
class ClientContext;
|
class ClientContext;
|
||||||
class WiFiClient;
|
class WiFiClient;
|
||||||
@ -36,12 +37,14 @@ class WiFiClient;
|
|||||||
class WiFiServer : public Server {
|
class WiFiServer : public Server {
|
||||||
private:
|
private:
|
||||||
uint16_t _port;
|
uint16_t _port;
|
||||||
|
IPAddress _addr;
|
||||||
tcp_pcb* _pcb;
|
tcp_pcb* _pcb;
|
||||||
|
|
||||||
ClientContext* _unclaimed;
|
ClientContext* _unclaimed;
|
||||||
ClientContext* _discarded;
|
ClientContext* _discarded;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
WiFiServer(IPAddress addr, uint16_t port);
|
||||||
WiFiServer(uint16_t port);
|
WiFiServer(uint16_t port);
|
||||||
WiFiClient available(uint8_t* status = NULL);
|
WiFiClient available(uint8_t* status = NULL);
|
||||||
bool hasClient();
|
bool hasClient();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user