1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-02 13:24:12 +01:00
Arduino/WiFi/Server.cpp

77 lines
1.5 KiB
C++
Executable File

#include <string.h>
#include "Server.h"
#include "Client.h"
#include "WiFi.h"
#include "server_drv.h"
#include "wiring.h"
Server::Server(uint16_t port)
{
_port = port;
}
void Server::begin()
{
uint8_t _sock = WiFiClass::getSocket();
if (_sock != NO_SOCKET_AVAIL)
{
ServerDrv::StartServer(_port, _sock);
WiFiClass::_server_port[_sock] = _port;
}
}
Client Server::available(byte* status)
{
//accept();
for (int sock = 0; sock < MAX_SOCK_NUM; sock++)
{
if (WiFiClass::_server_port[sock] != 0)
{
Client client(sock);
*status = client.status();
if (WiFiClass::_server_port[sock] == _port &&
*status == ESTABLISHED)
{
return client; //TODO
}else{
delayMicroseconds(100);
}
}
}
return Client(255);
}
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)
{
for (int sock = 0; sock < MAX_SOCK_NUM; sock++)
{
if (WiFiClass::_server_port[sock] != 0)
{
Client client(sock);
if (WiFiClass::_server_port[sock] == _port &&
client.status() == ESTABLISHED)
{
client.write(buffer, size);
}else{
delay(20);
}
}
}
}