00001 #include "w5100.h"
00002 #include "socket.h"
00003 extern "C" {
00004 #include "string.h"
00005 }
00006
00007 #include "Ethernet.h"
00008 #include "Client.h"
00009 #include "Server.h"
00010
00011 Server::Server(uint16_t port)
00012 {
00013 _port = port;
00014 }
00015
00016 void Server::begin()
00017 {
00018 for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
00019 Client client(sock);
00020 if (client.status() == SnSR::CLOSED) {
00021 socket(sock, SnMR::TCP, _port, 0);
00022 listen(sock);
00023 EthernetClass::_server_port[sock] = _port;
00024 break;
00025 }
00026 }
00027 }
00028
00029 void Server::accept()
00030 {
00031 int listening = 0;
00032
00033 for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
00034 Client client(sock);
00035
00036 if (EthernetClass::_server_port[sock] == _port) {
00037 if (client.status() == SnSR::LISTEN) {
00038 listening = 1;
00039 }
00040 else if (client.status() == SnSR::CLOSE_WAIT && !client.available()) {
00041 client.stop();
00042 }
00043 }
00044 }
00045
00046 if (!listening) {
00047 begin();
00048 }
00049 }
00050
00051 Client Server::available()
00052 {
00053 accept();
00054
00055 for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
00056 Client client(sock);
00057 if (EthernetClass::_server_port[sock] == _port &&
00058 (client.status() == SnSR::ESTABLISHED ||
00059 client.status() == SnSR::CLOSE_WAIT)) {
00060 if (client.available()) {
00061
00062 return client;
00063 }
00064 }
00065 }
00066
00067 return Client(MAX_SOCK_NUM);
00068 }
00069
00070 void Server::write(uint8_t b)
00071 {
00072 write(&b, 1);
00073 }
00074
00075 void Server::write(const char *str)
00076 {
00077 write((const uint8_t *)str, strlen(str));
00078 }
00079
00080 void Server::write(const uint8_t *buffer, size_t size)
00081 {
00082 accept();
00083
00084 for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
00085 Client client(sock);
00086
00087 if (EthernetClass::_server_port[sock] == _port &&
00088 client.status() == SnSR::ESTABLISHED) {
00089 client.write(buffer, size);
00090 }
00091 }
00092 }