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

EthernetServer - added end(), begin(port) and ctor without parameters #235

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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