2010-08-02 20:59:44 +02:00
|
|
|
#include "w5100.h"
|
|
|
|
#include "socket.h"
|
2008-07-30 16:47:36 +02:00
|
|
|
extern "C" {
|
2010-08-02 20:59:44 +02:00
|
|
|
#include "string.h"
|
2008-07-30 16:47:36 +02:00
|
|
|
}
|
|
|
|
|
2010-08-03 15:36:47 +02:00
|
|
|
#include "Ethernet.h"
|
2011-08-29 23:36:28 +02:00
|
|
|
#include "EthernetClient.h"
|
|
|
|
#include "EthernetServer.h"
|
2008-07-30 16:47:36 +02:00
|
|
|
|
2011-08-29 23:36:28 +02:00
|
|
|
EthernetServer::EthernetServer(uint16_t port)
|
2008-07-30 16:47:36 +02:00
|
|
|
{
|
|
|
|
_port = port;
|
|
|
|
}
|
|
|
|
|
2011-08-29 23:36:28 +02:00
|
|
|
void EthernetServer::begin()
|
2008-07-30 16:47:36 +02:00
|
|
|
{
|
|
|
|
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
|
2011-08-29 23:36:28 +02:00
|
|
|
EthernetClient client(sock);
|
2010-08-02 20:59:44 +02:00
|
|
|
if (client.status() == SnSR::CLOSED) {
|
|
|
|
socket(sock, SnMR::TCP, _port, 0);
|
2008-07-30 16:47:36 +02:00
|
|
|
listen(sock);
|
|
|
|
EthernetClass::_server_port[sock] = _port;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-29 23:36:28 +02:00
|
|
|
void EthernetServer::accept()
|
2008-07-30 16:47:36 +02:00
|
|
|
{
|
|
|
|
int listening = 0;
|
2010-08-02 20:59:44 +02:00
|
|
|
|
2008-07-30 16:47:36 +02:00
|
|
|
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
|
2011-08-29 23:36:28 +02:00
|
|
|
EthernetClient client(sock);
|
2010-08-02 20:59:44 +02:00
|
|
|
|
2008-07-30 16:47:36 +02:00
|
|
|
if (EthernetClass::_server_port[sock] == _port) {
|
2010-08-02 20:59:44 +02:00
|
|
|
if (client.status() == SnSR::LISTEN) {
|
2008-07-30 16:47:36 +02:00
|
|
|
listening = 1;
|
2010-08-02 20:59:44 +02:00
|
|
|
}
|
|
|
|
else if (client.status() == SnSR::CLOSE_WAIT && !client.available()) {
|
2008-07-30 16:47:36 +02:00
|
|
|
client.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-02 20:59:44 +02:00
|
|
|
|
2008-07-30 16:47:36 +02:00
|
|
|
if (!listening) {
|
|
|
|
begin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-29 23:36:28 +02:00
|
|
|
EthernetClient EthernetServer::available()
|
2008-07-30 16:47:36 +02:00
|
|
|
{
|
|
|
|
accept();
|
2010-08-02 20:59:44 +02:00
|
|
|
|
2008-07-30 16:47:36 +02:00
|
|
|
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
|
2011-08-29 23:36:28 +02:00
|
|
|
EthernetClient client(sock);
|
2008-07-30 16:47:36 +02:00
|
|
|
if (EthernetClass::_server_port[sock] == _port &&
|
2010-09-16 02:59:12 +02:00
|
|
|
(client.status() == SnSR::ESTABLISHED ||
|
|
|
|
client.status() == SnSR::CLOSE_WAIT)) {
|
2008-07-30 16:47:36 +02:00
|
|
|
if (client.available()) {
|
|
|
|
// XXX: don't always pick the lowest numbered socket.
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-02 20:59:44 +02:00
|
|
|
|
2011-08-29 23:36:28 +02:00
|
|
|
return EthernetClient(MAX_SOCK_NUM);
|
2008-07-30 16:47:36 +02:00
|
|
|
}
|
|
|
|
|
2011-08-29 23:36:28 +02:00
|
|
|
size_t EthernetServer::write(uint8_t b)
|
2009-04-26 15:10:34 +02:00
|
|
|
{
|
2011-10-02 16:17:33 +02:00
|
|
|
return write(&b, 1);
|
2009-04-26 15:10:34 +02:00
|
|
|
}
|
|
|
|
|
2011-08-29 23:36:28 +02:00
|
|
|
size_t EthernetServer::write(const uint8_t *buffer, size_t size)
|
2008-07-30 16:47:36 +02:00
|
|
|
{
|
2011-08-26 22:08:14 +02:00
|
|
|
size_t n = 0;
|
2011-08-24 01:12:03 +02:00
|
|
|
|
2008-07-30 16:47:36 +02:00
|
|
|
accept();
|
2010-08-02 20:59:44 +02:00
|
|
|
|
2008-07-30 16:47:36 +02:00
|
|
|
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
|
2011-08-29 23:36:28 +02:00
|
|
|
EthernetClient client(sock);
|
2010-08-02 20:59:44 +02:00
|
|
|
|
2008-07-30 16:47:36 +02:00
|
|
|
if (EthernetClass::_server_port[sock] == _port &&
|
2010-08-02 20:59:44 +02:00
|
|
|
client.status() == SnSR::ESTABLISHED) {
|
2011-08-24 01:12:03 +02:00
|
|
|
n += client.write(buffer, size);
|
2008-07-30 16:47:36 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-24 01:12:03 +02:00
|
|
|
|
|
|
|
return n;
|
2009-04-26 15:10:34 +02:00
|
|
|
}
|