Skip to content

Commit a39dcfd

Browse files
committed
Rename WiFi Server and UDP
1 parent 0ec3884 commit a39dcfd

File tree

26 files changed

+208
-202
lines changed

26 files changed

+208
-202
lines changed

CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ set(ARDUINO_LIBRARY_Networking_SRCS
208208
libraries/Networking/src/NetworkEvents.cpp
209209
libraries/Networking/src/NetworkManager.cpp
210210
libraries/Networking/src/WiFiClient.cpp
211-
libraries/Networking/src/WiFiServer.cpp
212-
libraries/Networking/src/WiFiUdp.cpp)
211+
libraries/Networking/src/NetworkServer.cpp
212+
libraries/Networking/src/NetworkUdp.cpp)
213213

214214
set(ARDUINO_LIBRARY_WiFi_SRCS
215215
libraries/WiFi/src/WiFiAP.cpp

libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <WiFi.h>
22
#include <ESPmDNS.h>
3-
#include <WiFiUdp.h>
3+
#include <NetworkUdp.h>
44
#include <ArduinoOTA.h>
55

66
const char* ssid = "..........";

libraries/ArduinoOTA/src/ArduinoOTA.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class ArduinoOTAClass
8686
String _hostname;
8787
String _partition_label;
8888
String _nonce;
89-
WiFiUDP _udp_ota;
89+
NetworkUDP _udp_ota;
9090
bool _initialized;
9191
bool _rebootOnSuccess;
9292
bool _mdnsEnabled;

libraries/ESPmDNS/examples/mDNS_Web_Server/mDNS_Web_Server.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const char* ssid = "............";
2424
const char* password = "..............";
2525

2626
// TCP server at port 80 will respond to HTTP requests
27-
WiFiServer server(80);
27+
NetworkServer server(80);
2828

2929
void setup(void)
3030
{

libraries/HTTPUpdate/src/HTTPUpdate.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
317317

318318
WiFiClient * tcp = http.getStreamPtr();
319319

320-
// To do? WiFiUDP::stopAll();
320+
// To do? NetworkUDP::stopAll();
321321
// To do? WiFiClient::stopAllExcept(tcp);
322322

323323
delay(100);

libraries/Networking/src/WiFiServer.cpp renamed to libraries/Networking/src/NetworkServer.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
License along with this library; if not, write to the Free Software
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
19-
#include "WiFiServer.h"
19+
#include "NetworkServer.h"
2020
#include <lwip/sockets.h>
2121
#include <lwip/netdb.h>
2222

2323
#undef write
2424
#undef close
2525

26-
int WiFiServer::setTimeout(uint32_t seconds){
26+
int NetworkServer::setTimeout(uint32_t seconds){
2727
struct timeval tv;
2828
tv.tv_sec = seconds;
2929
tv.tv_usec = 0;
@@ -32,11 +32,11 @@ int WiFiServer::setTimeout(uint32_t seconds){
3232
return setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
3333
}
3434

35-
WiFiClient WiFiServer::available(){
35+
WiFiClient NetworkServer::available(){
3636
return accept();
3737
}
3838

39-
WiFiClient WiFiServer::accept(){
39+
WiFiClient NetworkServer::accept(){
4040
if(!_listening)
4141
return WiFiClient();
4242
int client_sock;
@@ -64,11 +64,11 @@ WiFiClient WiFiServer::accept(){
6464
return WiFiClient();
6565
}
6666

67-
void WiFiServer::begin(uint16_t port){
67+
void NetworkServer::begin(uint16_t port){
6868
begin(port, 1);
6969
}
7070

71-
void WiFiServer::begin(uint16_t port, int enable){
71+
void NetworkServer::begin(uint16_t port, int enable){
7272
if(_listening)
7373
return;
7474
if(port){
@@ -99,15 +99,15 @@ void WiFiServer::begin(uint16_t port, int enable){
9999
_accepted_sockfd = -1;
100100
}
101101

102-
void WiFiServer::setNoDelay(bool nodelay) {
102+
void NetworkServer::setNoDelay(bool nodelay) {
103103
_noDelay = nodelay;
104104
}
105105

106-
bool WiFiServer::getNoDelay() {
106+
bool NetworkServer::getNoDelay() {
107107
return _noDelay;
108108
}
109109

110-
bool WiFiServer::hasClient() {
110+
bool NetworkServer::hasClient() {
111111
if (_accepted_sockfd >= 0) {
112112
return true;
113113
}
@@ -124,7 +124,7 @@ bool WiFiServer::hasClient() {
124124
return false;
125125
}
126126

127-
void WiFiServer::end(){
127+
void NetworkServer::end(){
128128
#ifdef ESP_IDF_VERSION_MAJOR
129129
lwip_close(sockfd);
130130
#else
@@ -134,10 +134,10 @@ void WiFiServer::end(){
134134
_listening = false;
135135
}
136136

137-
void WiFiServer::close(){
137+
void NetworkServer::close(){
138138
end();
139139
}
140140

141-
void WiFiServer::stop(){
141+
void NetworkServer::stop(){
142142
end();
143143
}
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Server.h - Server class for Raspberry Pi
3+
Copyright (c) 2016 Hristo Gochkov All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
#ifndef _WIFISERVER_H_
20+
#define _WIFISERVER_H_
21+
22+
#include "Arduino.h"
23+
#include "Server.h"
24+
#include "WiFiClient.h"
25+
#include "IPAddress.h"
26+
27+
class NetworkServer {
28+
private:
29+
int sockfd;
30+
int _accepted_sockfd = -1;
31+
IPAddress _addr;
32+
uint16_t _port;
33+
uint8_t _max_clients;
34+
bool _listening;
35+
bool _noDelay = false;
36+
37+
public:
38+
void listenOnLocalhost(){}
39+
40+
NetworkServer(uint16_t port=80, uint8_t max_clients=4):sockfd(-1),_accepted_sockfd(-1),_addr(),_port(port),_max_clients(max_clients),_listening(false),_noDelay(false) {
41+
log_v("NetworkServer::NetworkServer(port=%d, ...)", port);
42+
}
43+
NetworkServer(const IPAddress& addr, uint16_t port=80, uint8_t max_clients=4):sockfd(-1),_accepted_sockfd(-1),_addr(addr),_port(port),_max_clients(max_clients),_listening(false),_noDelay(false) {
44+
log_v("NetworkServer::NetworkServer(addr=%s, port=%d, ...)", addr.toString().c_str(), port);
45+
}
46+
~NetworkServer(){ end();}
47+
WiFiClient available() __attribute__((deprecated("Renamed to accept().")));
48+
WiFiClient accept();
49+
void begin(uint16_t port=0);
50+
void begin(uint16_t port, int reuse_enable);
51+
void setNoDelay(bool nodelay);
52+
bool getNoDelay();
53+
bool hasClient();
54+
55+
void end();
56+
void close();
57+
void stop();
58+
operator bool(){return _listening;}
59+
int setTimeout(uint32_t seconds);
60+
};
61+
62+
#endif /* _WIFISERVER_H_ */

libraries/Networking/src/WiFiUdp.cpp renamed to libraries/Networking/src/NetworkUdp.cpp

+23-23
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
1919

20-
#include "WiFiUdp.h"
20+
#include "NetworkUdp.h"
2121
#include <new> //std::nothrow
2222
#include <lwip/sockets.h>
2323
#include <lwip/netdb.h>
@@ -26,7 +26,7 @@
2626
#undef write
2727
#undef read
2828

29-
WiFiUDP::WiFiUDP()
29+
NetworkUDP::NetworkUDP()
3030
: udp_server(-1)
3131
, server_port(0)
3232
, remote_port(0)
@@ -35,11 +35,11 @@ WiFiUDP::WiFiUDP()
3535
, rx_buffer(0)
3636
{}
3737

38-
WiFiUDP::~WiFiUDP(){
38+
NetworkUDP::~NetworkUDP(){
3939
stop();
4040
}
4141

42-
uint8_t WiFiUDP::begin(IPAddress address, uint16_t port){
42+
uint8_t NetworkUDP::begin(IPAddress address, uint16_t port){
4343
stop();
4444

4545
server_port = port;
@@ -100,11 +100,11 @@ uint8_t WiFiUDP::begin(IPAddress address, uint16_t port){
100100
return 1;
101101
}
102102

103-
uint8_t WiFiUDP::begin(uint16_t p){
103+
uint8_t NetworkUDP::begin(uint16_t p){
104104
return begin(IPAddress(), p);
105105
}
106106

107-
uint8_t WiFiUDP::beginMulticast(IPAddress address, uint16_t p){
107+
uint8_t NetworkUDP::beginMulticast(IPAddress address, uint16_t p){
108108
if(begin(IPAddress(), p)){
109109
ip_addr_t addr;
110110
address.to_ip_addr_t(&addr);
@@ -147,7 +147,7 @@ uint8_t WiFiUDP::beginMulticast(IPAddress address, uint16_t p){
147147
return 0;
148148
}
149149

150-
void WiFiUDP::stop(){
150+
void NetworkUDP::stop(){
151151
if(tx_buffer){
152152
free(tx_buffer);
153153
tx_buffer = NULL;
@@ -190,15 +190,15 @@ void WiFiUDP::stop(){
190190
udp_server = -1;
191191
}
192192

193-
int WiFiUDP::beginMulticastPacket(){
193+
int NetworkUDP::beginMulticastPacket(){
194194
if(!server_port || multicast_ip == IPAddress())
195195
return 0;
196196
remote_ip = multicast_ip;
197197
remote_port = server_port;
198198
return beginPacket();
199199
}
200200

201-
int WiFiUDP::beginPacket(){
201+
int NetworkUDP::beginPacket(){
202202
if(!remote_port)
203203
return 0;
204204

@@ -226,13 +226,13 @@ int WiFiUDP::beginPacket(){
226226
return 1;
227227
}
228228

229-
int WiFiUDP::beginPacket(IPAddress ip, uint16_t port){
229+
int NetworkUDP::beginPacket(IPAddress ip, uint16_t port){
230230
remote_ip = ip;
231231
remote_port = port;
232232
return beginPacket();
233233
}
234234

235-
int WiFiUDP::beginPacket(const char *host, uint16_t port){
235+
int NetworkUDP::beginPacket(const char *host, uint16_t port){
236236
struct hostent *server;
237237
server = gethostbyname(host);
238238
if (server == NULL){
@@ -242,7 +242,7 @@ int WiFiUDP::beginPacket(const char *host, uint16_t port){
242242
return beginPacket(IPAddress((const uint8_t *)(server->h_addr_list[0])), port);
243243
}
244244

245-
int WiFiUDP::endPacket(){
245+
int NetworkUDP::endPacket(){
246246
ip_addr_t addr;
247247
remote_ip.to_ip_addr_t(&addr);
248248

@@ -272,7 +272,7 @@ int WiFiUDP::endPacket(){
272272
return 1;
273273
}
274274

275-
size_t WiFiUDP::write(uint8_t data){
275+
size_t NetworkUDP::write(uint8_t data){
276276
if(tx_buffer_len == 1460){
277277
endPacket();
278278
tx_buffer_len = 0;
@@ -281,14 +281,14 @@ size_t WiFiUDP::write(uint8_t data){
281281
return 1;
282282
}
283283

284-
size_t WiFiUDP::write(const uint8_t *buffer, size_t size){
284+
size_t NetworkUDP::write(const uint8_t *buffer, size_t size){
285285
size_t i;
286286
for(i=0;i<size;i++)
287287
write(buffer[i]);
288288
return i;
289289
}
290290

291-
int WiFiUDP::parsePacket(){
291+
int NetworkUDP::parsePacket(){
292292
if(rx_buffer)
293293
return 0;
294294
struct sockaddr_storage si_other_storage; // enough storage for v4 and v6
@@ -338,12 +338,12 @@ int WiFiUDP::parsePacket(){
338338
return len;
339339
}
340340

341-
int WiFiUDP::available(){
341+
int NetworkUDP::available(){
342342
if(!rx_buffer) return 0;
343343
return rx_buffer->available();
344344
}
345345

346-
int WiFiUDP::read(){
346+
int NetworkUDP::read(){
347347
if(!rx_buffer) return -1;
348348
int out = rx_buffer->read();
349349
if(!rx_buffer->available()){
@@ -354,11 +354,11 @@ int WiFiUDP::read(){
354354
return out;
355355
}
356356

357-
int WiFiUDP::read(unsigned char* buffer, size_t len){
357+
int NetworkUDP::read(unsigned char* buffer, size_t len){
358358
return read((char *)buffer, len);
359359
}
360360

361-
int WiFiUDP::read(char* buffer, size_t len){
361+
int NetworkUDP::read(char* buffer, size_t len){
362362
if(!rx_buffer) return 0;
363363
int out = rx_buffer->read(buffer, len);
364364
if(!rx_buffer->available()){
@@ -369,22 +369,22 @@ int WiFiUDP::read(char* buffer, size_t len){
369369
return out;
370370
}
371371

372-
int WiFiUDP::peek(){
372+
int NetworkUDP::peek(){
373373
if(!rx_buffer) return -1;
374374
return rx_buffer->peek();
375375
}
376376

377-
void WiFiUDP::flush(){
377+
void NetworkUDP::flush(){
378378
if(!rx_buffer) return;
379379
cbuf *b = rx_buffer;
380380
rx_buffer = 0;
381381
delete b;
382382
}
383383

384-
IPAddress WiFiUDP::remoteIP(){
384+
IPAddress NetworkUDP::remoteIP(){
385385
return remote_ip;
386386
}
387387

388-
uint16_t WiFiUDP::remotePort(){
388+
uint16_t NetworkUDP::remotePort(){
389389
return remote_port;
390390
}

0 commit comments

Comments
 (0)