Skip to content

Commit

Permalink
EthernetServer - added end(), begin(port) and ctor without parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
JAndrassy committed Oct 14, 2023
1 parent 39103da commit fe702d1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -884,11 +884,12 @@ Tells the server to begin listening for incoming connections.

```
server.begin()
server.begin(port)
```

#### Parameters
None
- port (optional): the port to listen on (int)

#### Returns
None
Expand Down
3 changes: 3 additions & 0 deletions src/Ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,13 @@ class EthernetServer : public Server {
private:
uint16_t _port;
public:
EthernetServer() : _port(80) { }
EthernetServer(uint16_t port) : _port(port) { }
EthernetClient available();
EthernetClient accept();
virtual void begin();
void begin(uint16_t port);
void end(uint16_t timeout = 5000);
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buf, size_t size);
virtual operator bool();
Expand Down
37 changes: 37 additions & 0 deletions src/EthernetServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,43 @@ void EthernetServer::begin()
}
}

void EthernetServer::begin(uint16_t port)
{
end();
_port = port;
begin();
}

void EthernetServer::end(uint16_t timeout)
{
for (uint8_t i=0; i < MAX_SOCK_NUM; i++) {
if (server_port[i] == _port) {
Ethernet.socketDisconnect(i);
}
}
unsigned long start = millis();
bool allClosed = false;
while (!allClosed && millis() - start < timeout) {
allClosed = true;
for (uint8_t i=0; i < MAX_SOCK_NUM; i++) {
if (server_port[i] == _port) {
if (Ethernet.socketStatus(i) == SnSR::CLOSED) {
server_port[i] = 0;
} else {
allClosed = false;
}
}
}
delay(1);
}
for (uint8_t i=0; i < MAX_SOCK_NUM; i++) {
if (server_port[i] == _port) {
Ethernet.socketClose(i);
server_port[i] = 0;
}
}
}

EthernetClient EthernetServer::available()
{
bool listening = false;
Expand Down

0 comments on commit fe702d1

Please sign in to comment.