root / arduino-1.0 / libraries / Ethernet / EthernetServer.cpp @ 58d82c77
History | View | Annotate | Download (1.8 KB)
1 |
#include "w5100.h" |
---|---|
2 |
#include "socket.h" |
3 |
extern "C" { |
4 |
#include "string.h" |
5 |
} |
6 |
|
7 |
#include "Ethernet.h" |
8 |
#include "EthernetClient.h" |
9 |
#include "EthernetServer.h" |
10 |
|
11 |
EthernetServer::EthernetServer(uint16_t port) |
12 |
{ |
13 |
_port = port; |
14 |
} |
15 |
|
16 |
void EthernetServer::begin()
|
17 |
{ |
18 |
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { |
19 |
EthernetClient client(sock); |
20 |
if (client.status() == SnSR::CLOSED) {
|
21 |
socket(sock, SnMR::TCP, _port, 0);
|
22 |
listen(sock); |
23 |
EthernetClass::_server_port[sock] = _port; |
24 |
break;
|
25 |
} |
26 |
} |
27 |
} |
28 |
|
29 |
void EthernetServer::accept()
|
30 |
{ |
31 |
int listening = 0; |
32 |
|
33 |
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { |
34 |
EthernetClient client(sock); |
35 |
|
36 |
if (EthernetClass::_server_port[sock] == _port) {
|
37 |
if (client.status() == SnSR::LISTEN) {
|
38 |
listening = 1;
|
39 |
} |
40 |
else if (client.status() == SnSR::CLOSE_WAIT && !client.available()) { |
41 |
client.stop(); |
42 |
} |
43 |
} |
44 |
} |
45 |
|
46 |
if (!listening) {
|
47 |
begin(); |
48 |
} |
49 |
} |
50 |
|
51 |
EthernetClient EthernetServer::available() |
52 |
{ |
53 |
accept(); |
54 |
|
55 |
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { |
56 |
EthernetClient client(sock); |
57 |
if (EthernetClass::_server_port[sock] == _port &&
|
58 |
(client.status() == SnSR::ESTABLISHED || |
59 |
client.status() == SnSR::CLOSE_WAIT)) { |
60 |
if (client.available()) {
|
61 |
// XXX: don't always pick the lowest numbered socket.
|
62 |
return client;
|
63 |
} |
64 |
} |
65 |
} |
66 |
|
67 |
return EthernetClient(MAX_SOCK_NUM);
|
68 |
} |
69 |
|
70 |
size_t EthernetServer::write(uint8_t b) |
71 |
{ |
72 |
return write(&b, 1); |
73 |
} |
74 |
|
75 |
size_t EthernetServer::write(const uint8_t *buffer, size_t size)
|
76 |
{ |
77 |
size_t n = 0;
|
78 |
|
79 |
accept(); |
80 |
|
81 |
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { |
82 |
EthernetClient client(sock); |
83 |
|
84 |
if (EthernetClass::_server_port[sock] == _port &&
|
85 |
client.status() == SnSR::ESTABLISHED) { |
86 |
n += client.write(buffer, size); |
87 |
} |
88 |
} |
89 |
|
90 |
return n;
|
91 |
} |