You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
1.7 KiB
C++
93 lines
1.7 KiB
C++
14 years ago
|
#include "w5100.h"
|
||
|
#include "socket.h"
|
||
15 years ago
|
extern "C" {
|
||
14 years ago
|
#include "string.h"
|
||
15 years ago
|
}
|
||
|
|
||
|
#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);
|
||
14 years ago
|
if (client.status() == SnSR::CLOSED) {
|
||
|
socket(sock, SnMR::TCP, _port, 0);
|
||
15 years ago
|
listen(sock);
|
||
|
EthernetClass::_server_port[sock] = _port;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void Server::accept()
|
||
|
{
|
||
|
int listening = 0;
|
||
14 years ago
|
|
||
15 years ago
|
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
|
||
|
Client client(sock);
|
||
14 years ago
|
|
||
15 years ago
|
if (EthernetClass::_server_port[sock] == _port) {
|
||
14 years ago
|
if (client.status() == SnSR::LISTEN) {
|
||
15 years ago
|
listening = 1;
|
||
14 years ago
|
}
|
||
|
else if (client.status() == SnSR::CLOSE_WAIT && !client.available()) {
|
||
15 years ago
|
client.stop();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
14 years ago
|
|
||
15 years ago
|
if (!listening) {
|
||
|
begin();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Client Server::available()
|
||
|
{
|
||
|
accept();
|
||
14 years ago
|
|
||
15 years ago
|
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
|
||
|
Client client(sock);
|
||
|
if (EthernetClass::_server_port[sock] == _port &&
|
||
14 years ago
|
(client.status() == SnSR::ESTABLISHED ||
|
||
|
client.status() == SnSR::CLOSE_WAIT)) {
|
||
15 years ago
|
if (client.available()) {
|
||
|
// XXX: don't always pick the lowest numbered socket.
|
||
|
return client;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
14 years ago
|
|
||
|
return Client(MAX_SOCK_NUM);
|
||
15 years ago
|
}
|
||
|
|
||
|
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)
|
||
|
{
|
||
|
accept();
|
||
14 years ago
|
|
||
15 years ago
|
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
|
||
|
Client client(sock);
|
||
14 years ago
|
|
||
15 years ago
|
if (EthernetClass::_server_port[sock] == _port &&
|
||
14 years ago
|
client.status() == SnSR::ESTABLISHED) {
|
||
15 years ago
|
client.write(buffer, size);
|
||
|
}
|
||
|
}
|
||
|
}
|