1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-04 15:24:12 +01:00
Arduino/libraries/Ethernet/Server.cpp

93 lines
1.7 KiB
C++
Raw Normal View History

#include "w5100.h"
#include "socket.h"
2008-07-30 16:47:36 +02:00
extern "C" {
#include "string.h"
2008-07-30 16:47:36 +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);
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;
2008-07-30 16:47:36 +02:00
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
Client client(sock);
2008-07-30 16:47:36 +02:00
if (EthernetClass::_server_port[sock] == _port) {
if (client.status() == SnSR::LISTEN) {
2008-07-30 16:47:36 +02:00
listening = 1;
}
else if (client.status() == SnSR::CLOSE_WAIT && !client.available()) {
2008-07-30 16:47:36 +02:00
client.stop();
}
}
}
2008-07-30 16:47:36 +02:00
if (!listening) {
begin();
}
}
Client Server::available()
{
accept();
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 &&
(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;
}
}
}
return Client(MAX_SOCK_NUM);
2008-07-30 16:47:36 +02:00
}
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)
2008-07-30 16:47:36 +02:00
{
accept();
2008-07-30 16:47:36 +02:00
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
Client client(sock);
2008-07-30 16:47:36 +02:00
if (EthernetClass::_server_port[sock] == _port &&
client.status() == SnSR::ESTABLISHED) {
client.write(buffer, size);
2008-07-30 16:47:36 +02:00
}
}
}