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"
|
|
|
|
#include "Client.h"
|
|
|
|
#include "Server.h"
|
2008-07-30 16:47:36 +02:00
|
|
|
|
|
|
|
Server::Server(uint16_t port)
|
|
|
|
{
|
|
|
|
_port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Server::begin()
|
|
|
|
{
|
|
|
|
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
|
|
|
|
Client 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Server::accept()
|
|
|
|
{
|
|
|
|
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++) {
|
|
|
|
Client 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Client Server::available()
|
|
|
|
{
|
|
|
|
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++) {
|
|
|
|
Client client(sock);
|
|
|
|
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
|
|
|
|
2010-08-03 19:25:11 +02:00
|
|
|
return Client(MAX_SOCK_NUM);
|
2008-07-30 16:47:36 +02:00
|
|
|
}
|
|
|
|
|
2011-08-26 20:20:41 +02:00
|
|
|
ssize_t Server::write(uint8_t b)
|
2009-04-26 15:10:34 +02:00
|
|
|
{
|
|
|
|
write(&b, 1);
|
|
|
|
}
|
|
|
|
|
2011-08-26 20:20:41 +02:00
|
|
|
ssize_t Server::write(const char *str)
|
2009-04-26 15:10:34 +02:00
|
|
|
{
|
|
|
|
write((const uint8_t *)str, strlen(str));
|
|
|
|
}
|
|
|
|
|
2011-08-26 20:20:41 +02:00
|
|
|
ssize_t Server::write(const uint8_t *buffer, size_t size)
|
2008-07-30 16:47:36 +02:00
|
|
|
{
|
2011-08-26 20:20:41 +02:00
|
|
|
ssize_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++) {
|
|
|
|
Client 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
|
|
|
}
|