aboutsummaryrefslogtreecommitdiff
path: root/libraries/Ethernet/Server.cpp
diff options
context:
space:
mode:
authorDavid A. Mellis <d.mellis@arduino.cc>2008-07-30 14:47:36 +0000
committerDavid A. Mellis <d.mellis@arduino.cc>2008-07-30 14:47:36 +0000
commiteac2983285b4bc40250771fb5141aa63c76a9a90 (patch)
tree9f51fab1b562f589d7ea6ce3dda53037ff2c929c /libraries/Ethernet/Server.cpp
parentf1a76708088659850b98a92779a2cb1aaf5793d8 (diff)
Adding ethernet library.
Diffstat (limited to 'libraries/Ethernet/Server.cpp')
-rw-r--r--libraries/Ethernet/Server.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/libraries/Ethernet/Server.cpp b/libraries/Ethernet/Server.cpp
new file mode 100644
index 0000000..351f838
--- /dev/null
+++ b/libraries/Ethernet/Server.cpp
@@ -0,0 +1,80 @@
+extern "C" {
+ #include "types.h"
+ #include "w5100.h"
+ #include "socket.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() == SOCK_CLOSED) {
+ socket(sock, Sn_MR_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() == SOCK_LISTEN) {
+ listening = 1;
+ } else if (client.status() == SOCK_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() == SOCK_ESTABLISHED) {
+ if (client.available()) {
+ // XXX: don't always pick the lowest numbered socket.
+ return client;
+ }
+ }
+ }
+
+ return Client(255);
+}
+
+void Server::write(uint8_t b)
+{
+ accept();
+
+ for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
+ Client client(sock);
+
+ if (EthernetClass::_server_port[sock] == _port &&
+ client.status() == SOCK_ESTABLISHED) {
+ client.write(b);
+ }
+ }
+} \ No newline at end of file