Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): add bool operator and end() api #77

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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