forked from diysandbox/Wirefree
-
Notifications
You must be signed in to change notification settings - Fork 2
/
WifiServer.cpp
131 lines (105 loc) · 2.75 KB
/
WifiServer.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
WifiServer.cpp - network server class
Copyright (C) 2011 DIYSandbox LLC
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "gs.h"
#include "socket.h"
//extern "C" {
//#include "string.h"
//}
#include "Wirefree.h"
#include "WifiClient.h"
#include "WifiServer.h"
WifiServer::WifiServer(uint16_t port)
{
_port = port;
}
void WifiServer::begin()
{
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
WifiClient client(sock);
if (client.status() == SOCK_STATUS::CLOSED) {
socket(sock, IPPROTO::TCP, _port, 0);
listen(sock);
Wirefree::_server_port[sock] = _port;
break;
}
}
}
ARETTYPE WifiServer::write(uint8_t b)
{
}
ARETTYPE WifiServer::write(const char *str)
{
}
ARETTYPE WifiServer::write(const uint8_t *buffer, size_t size)
{
}
void WifiServer::accept()
{
int listening = 0;
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
WifiClient client(sock);
if (Wirefree::_server_port[sock] == _port) {
if (client.status() == SOCK_STATUS::LISTEN) {
listening = 1;
}
//else if (client.status() == SnSR::CLOSE_WAIT && !client.available()) {
// client.stop();
//}
}
}
if (!listening) {
begin();
}
}
WifiClient WifiServer::available()
{
GS.process();
accept();
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
WifiClient client(sock);
if (/*Wirefree::_server_port[sock] == _port &&*/
(client.status() == SOCK_STATUS::ESTABLISHED ||
client.status() == SOCK_STATUS::CLOSE_WAIT))
{
if (client.available()) {
// XXX: don't always pick the lowest numbered socket.
return client;
}
}
}
return WifiClient(MAX_SOCK_NUM);
}
#if 0
void WifiServer::write(uint8_t b)
{
write(&b, 1);
}
void WifiServer::write(const char *str)
{
write((const uint8_t *)str, strlen(str));
}
void WifiServer::write(const uint8_t *buffer, size_t size)
{
accept();
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
WifiClient client(sock);
if (EthernetClass::_server_port[sock] == _port &&
client.status() == SnSR::ESTABLISHED) {
client.write(buffer, size);
}
}
}
#endif