source: rtos_arduino/trunk/arduino_lib/libraries/Ethernet2/src/EthernetServer.cpp@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 1.8 KB
Line 
1#include "utility/w5500.h"
2#include "utility/socket.h"
3extern "C" {
4#include "string.h"
5}
6
7#include "Ethernet2.h"
8#include "EthernetClient.h"
9#include "EthernetServer.h"
10
11EthernetServer::EthernetServer(uint16_t port)
12{
13 _port = port;
14}
15
16void 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
29void 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
51EthernetClient 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
70size_t EthernetServer::write(uint8_t b)
71{
72 return write(&b, 1);
73}
74
75size_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}
Note: See TracBrowser for help on using the repository browser.