diff options
author | Cristian Maglie <c.maglie@bug.st> | 2012-01-04 15:14:51 +0100 |
---|---|---|
committer | Cristian Maglie <c.maglie@bug.st> | 2012-01-04 15:14:51 +0100 |
commit | 8c2b5b979a75d109ae7cc306afc50d7ffe1e0366 (patch) | |
tree | 19ac8d91c7209397e7dc890b432a254ae9a7cd62 /libraries/Ethernet/EthernetServer.cpp | |
parent | be52c95820400d48e47615232d3c38236166fad3 (diff) |
Moved libraries folder inside platform folder. Now libraries and examples are searched per board/platform
Diffstat (limited to 'libraries/Ethernet/EthernetServer.cpp')
-rw-r--r-- | libraries/Ethernet/EthernetServer.cpp | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/libraries/Ethernet/EthernetServer.cpp b/libraries/Ethernet/EthernetServer.cpp new file mode 100644 index 0000000..0308b92 --- /dev/null +++ b/libraries/Ethernet/EthernetServer.cpp @@ -0,0 +1,91 @@ +#include "w5100.h" +#include "socket.h" +extern "C" { +#include "string.h" +} + +#include "Ethernet.h" +#include "EthernetClient.h" +#include "EthernetServer.h" + +EthernetServer::EthernetServer(uint16_t port) +{ + _port = port; +} + +void EthernetServer::begin() +{ + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { + EthernetClient client(sock); + if (client.status() == SnSR::CLOSED) { + socket(sock, SnMR::TCP, _port, 0); + listen(sock); + EthernetClass::_server_port[sock] = _port; + break; + } + } +} + +void EthernetServer::accept() +{ + int listening = 0; + + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { + EthernetClient client(sock); + + if (EthernetClass::_server_port[sock] == _port) { + if (client.status() == SnSR::LISTEN) { + listening = 1; + } + else if (client.status() == SnSR::CLOSE_WAIT && !client.available()) { + client.stop(); + } + } + } + + if (!listening) { + begin(); + } +} + +EthernetClient EthernetServer::available() +{ + accept(); + + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { + EthernetClient client(sock); + if (EthernetClass::_server_port[sock] == _port && + (client.status() == SnSR::ESTABLISHED || + client.status() == SnSR::CLOSE_WAIT)) { + if (client.available()) { + // XXX: don't always pick the lowest numbered socket. + return client; + } + } + } + + return EthernetClient(MAX_SOCK_NUM); +} + +size_t EthernetServer::write(uint8_t b) +{ + return write(&b, 1); +} + +size_t EthernetServer::write(const uint8_t *buffer, size_t size) +{ + size_t n = 0; + + accept(); + + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { + EthernetClient client(sock); + + if (EthernetClass::_server_port[sock] == _port && + client.status() == SnSR::ESTABLISHED) { + n += client.write(buffer, size); + } + } + + return n; +} |