diff options
author | Thibaut VIARD <thibaut.viard@atmel.com> | 2011-06-21 00:20:43 +0200 |
---|---|---|
committer | Thibaut VIARD <thibaut.viard@atmel.com> | 2011-06-21 00:20:43 +0200 |
commit | 0887b98f627500271b5ad8b3c4f6c7b90bc227ee (patch) | |
tree | ef8626954567ba22e392c68c8f3606416b07b5b5 /libraries/Ethernet/Server.cpp | |
parent | 90c487402cefadb6a2aab907ab07075cbb759e34 (diff) |
Moving all AVR specific libraries to hardware/avr
Diffstat (limited to 'libraries/Ethernet/Server.cpp')
-rw-r--r-- | libraries/Ethernet/Server.cpp | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/libraries/Ethernet/Server.cpp b/libraries/Ethernet/Server.cpp new file mode 100644 index 0000000..4271741 --- /dev/null +++ b/libraries/Ethernet/Server.cpp @@ -0,0 +1,92 @@ +#include "w5100.h" +#include "socket.h" +extern "C" { +#include "string.h" +} + +#include "Ethernet.h" +#include "Client.h" +#include "Server.h" + +Server::Server(uint16_t port) +{ + _port = port; +} + +void Server::begin() +{ + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { + Client client(sock); + if (client.status() == SnSR::CLOSED) { + socket(sock, SnMR::TCP, _port, 0); + listen(sock); + EthernetClass::_server_port[sock] = _port; + break; + } + } +} + +void Server::accept() +{ + int listening = 0; + + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { + Client 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(); + } +} + +Client Server::available() +{ + accept(); + + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { + Client 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 Client(MAX_SOCK_NUM); +} + +void Server::write(uint8_t b) +{ + write(&b, 1); +} + +void Server::write(const char *str) +{ + write((const uint8_t *)str, strlen(str)); +} + +void Server::write(const uint8_t *buffer, size_t size) +{ + accept(); + + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { + Client client(sock); + + if (EthernetClass::_server_port[sock] == _port && + client.status() == SnSR::ESTABLISHED) { + client.write(buffer, size); + } + } +} |