forked from chunlinhan/WiFiRM04
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiFiRM04Server.cpp
89 lines (73 loc) · 1.96 KB
/
WiFiRM04Server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <string.h>
#include "server_drv.h"
extern "C" {
#include "utility/debug.h"
}
#include "WiFiRM04.h"
#include "WiFiRM04Client.h"
#include "WiFiRM04Server.h"
WiFiRM04Server::WiFiRM04Server(uint16_t port)
{
_port = port;
}
void WiFiRM04Server::begin()
{
uint8_t _sock = WiFiRM04Class::getSocket();
if (_sock != NO_SOCKET_AVAIL)
{
ServerDrv::startServer(_port, _sock);
WiFiRM04Class::_server_port[_sock] = _port;
WiFiRM04Class::_state[_sock] = _sock;
}
}
WiFiRM04Client WiFiRM04Server::available(byte* status)
{
static int cycle_server_down = 0;
const int TH_SERVER_DOWN = 50;
for (int sock = 0; sock < MAX_SOCK_NUM; sock++)
{
if (WiFiRM04Class::_server_port[sock] == _port)
{
WiFiRM04Client client(sock);
uint8_t _status = client.status();
uint8_t _ser_status = this->status();
if (status != NULL)
*status = _status;
//server not in listen state, restart it
if ((_ser_status == 0)&&(cycle_server_down++ > TH_SERVER_DOWN))
{
ServerDrv::startServer(_port, sock);
cycle_server_down = 0;
}
if (_status == ESTABLISHED)
{
return client; //TODO
}
}
}
return WiFiRM04Client(255);
}
uint8_t WiFiRM04Server::status() {
return ServerDrv::getServerState(0);
}
size_t WiFiRM04Server::write(uint8_t b)
{
return write(&b, 1);
}
size_t WiFiRM04Server::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
for (int sock = 0; sock < MAX_SOCK_NUM; sock++)
{
if (WiFiRM04Class::_server_port[sock] != 0)
{
WiFiRM04Client client(sock);
if (WiFiRM04Class::_server_port[sock] == _port &&
client.status() == ESTABLISHED)
{
n+=client.write(buffer, size);
}
}
}
return n;
}