Skip to content

Commit 7b06101

Browse files
committed
EthernetServerPrint class for write-to-all-clients server
1 parent 5a82eb8 commit 7b06101

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11

2-
EthernetENC is the Ethernet library for ENC28J60. It is a modern version of the UIPEthernet library.
2+
EthernetENC is the Ethernet library for ENC28J60. It is a modern version of [the UIPEthernet library](https://github.com/jandrassy/EthernetENC/wiki/UIPEthernet).
33

44
The modernization includes:
5-
* Ethernet 2.0 library functions
5+
* Ethernet 2.0.0 Arduino library functions
66
* compatible include file names EthernetClient.h, EthernetServer.h and EthernetUdp.h
77
* support of many Arduino architectures by using the SPI library
88
* SPI transactions to share the SPI bus with devices with different communication settings
99
* SPI communication at 20 MHz if the MCU supports it, else on the maximum supported by the MCU
1010
* client.flush() to send the packet immediately
1111
* calls yield() in blocking functions
12+
* Arduino 1.5 library format built with dot_a_linkage option for optimal build result
1213

1314
[The documentation of Arduino Ethernet library](https://www.arduino.cc/en/Reference/Ethernet) applies for classes and functions descriptions.
1415

1516
Limitations:
1617
* UDP.beginMulticast is not supported, because the uIP stack doesn't support multicast
1718
* UDB broadcasts receiving is turned off on ENC to lower the processing load on the library
18-
* EthernetServer doesn't support the print-to-all-clients functionality, because it takes flash memory space and RAM (10 bytes) and nobody uses it
1919

2020
This library doesn't have examples, because examples of the Arduino Ethernet library apply. You can find them in the Arduino IDE Examples menu Ethernet section. Only change `#include <Ethernet.h>` to `#include <EthernetENC.h>`. Some examples require [a little change](https://github.com/jandrassy/EthernetENC/wiki/Examples).
2121

22-
This library is based on the Norbert Truchsess's arduino-uip original source code repository and uses experience from the development of the multiarchitecture support by Cassy. Applicable fixes and enhancements from developed of EthernetENC were transfered to Cassy's UIPEthernet.
22+
This library is based on the Norbert Truchsess's arduino-uip original source code repository and uses experience from the development of the multi-architecture support by Cassy. Applicable fixes and enhancements from developed of EthernetENC were transfered to Cassy's UIPEthernet.
2323

2424
**You can find more information in project's [Wiki](https://github.com/jandrassy/EthernetENC/wiki).**
2525

src/EthernetServer.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,27 @@ void UIPServer::end() {
7272
UIPServer::operator bool() {
7373
return listening;
7474
}
75+
76+
size_t UIPServer::writeToAllClients(const uint8_t *buf, size_t size)
77+
{
78+
size_t ret = 0;
79+
for ( uip_userdata_t* data = &UIPClient::all_data[0]; data < &UIPClient::all_data[UIP_CONNS]; data++ )
80+
{
81+
if ((data->state & UIP_CLIENT_CONNECTED) && uip_conns[data->conn_index].lport ==_port)
82+
{
83+
EthernetClient client(data);
84+
ret = client.write(buf, size);
85+
}
86+
}
87+
return ret;
88+
}
89+
90+
size_t EthernetServerPrint::write(uint8_t c)
91+
{
92+
return write(&c,1);
93+
}
94+
95+
size_t EthernetServerPrint::write(const uint8_t *buf, size_t size)
96+
{
97+
return EthernetServer::writeToAllClients(buf, size);
98+
}

src/EthernetServer.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,25 @@ class EthernetServer {
3030
void begin();
3131
void end();
3232
operator bool();
33-
33+
34+
protected:
35+
size_t writeToAllClients(const uint8_t *buf, size_t size);
36+
3437
private:
3538
uint16_t _port;
36-
bool listening = false;
39+
bool listening = false;
40+
};
41+
42+
class EthernetServerPrint : public EthernetServer, public Print {
43+
44+
public:
45+
EthernetServerPrint(uint16_t port) : EthernetServer(port) {}
46+
47+
virtual size_t write(uint8_t);
48+
virtual size_t write(const uint8_t *buf, size_t size);
49+
50+
using Print::write;
51+
3752
};
3853

3954
#endif

0 commit comments

Comments
 (0)