Skip to content

Commit

Permalink
Merge pull request #77 from fpistm/ArduinoAPI
Browse files Browse the repository at this point in the history
feat(server): add bool operator and end() api
  • Loading branch information
fpistm authored Nov 9, 2023
2 parents df78428 + 8803014 commit f000a6e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ Call `Ethernet::schedule()` performs an update of the LwIP stack.<br>

## Wiki

You can find information at https://github.com/stm32duino/wiki/wiki/STM32Ethernet
You can find information at https://github.com/stm32duino/Arduino_Core_STM32/wiki/STM32Ethernet
30 changes: 26 additions & 4 deletions src/EthernetServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ void EthernetServer::begin(uint16_t port)
begin();
}

void EthernetServer::end(void)
{
/* Free client */
for (int n = 0; n < MAX_CLIENT; n++) {
if (_tcp_client[n] != NULL) {
EthernetClient client(_tcp_client[n]);
client.stop();
_tcp_client[n] = NULL;
}
}
if (_tcp_server.pcb != NULL) {
tcp_close(_tcp_server.pcb);
_tcp_server.pcb = NULL;
}
}

void EthernetServer::accept()
{
/* Free client if disconnected */
Expand Down Expand Up @@ -93,10 +109,10 @@ size_t EthernetServer::write(const uint8_t *buffer, size_t size)

accept();

for (int n = 0; n < MAX_CLIENT; n++) {
if (_tcp_client[n] != NULL) {
if (_tcp_client[n]->pcb != NULL) {
EthernetClient client(_tcp_client[n]);
for (int i = 0; i < MAX_CLIENT; i++) {
if (_tcp_client[i] != NULL) {
if (_tcp_client[i]->pcb != NULL) {
EthernetClient client(_tcp_client[i]);
uint8_t s = client.status();
if (s == TCP_ACCEPTED) {
n += client.write(buffer, size);
Expand All @@ -107,3 +123,9 @@ size_t EthernetServer::write(const uint8_t *buffer, size_t size)

return n;
}

EthernetServer::operator bool()
{
// server is listening for incoming clients
return ((_tcp_server.pcb != NULL) && (_tcp_server.pcb->state == LISTEN));
}
2 changes: 2 additions & 0 deletions src/EthernetServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ class EthernetServer :
EthernetClient available();
virtual void begin();
virtual void begin(uint16_t port);
void end(void);
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buf, size_t size);
virtual operator bool();
using Print::write;
};

Expand Down

0 comments on commit f000a6e

Please sign in to comment.