COSA
An Object-Oriented Platform for Arduino Programming
INET_Server.cpp
Go to the documentation of this file.
1 
21 #include "Cosa/INET.hh"
22 #include "Cosa/Watchdog.hh"
23 #include "Cosa/Socket.hh"
24 
25 void
27 {
28  Socket* sock = socket();
29  if (UNLIKELY(sock == NULL)) return;
30  sock->src(addr);
31 }
32 
33 bool
35 {
36  // Sanity check parameter
37  if (UNLIKELY(sock == NULL)) return (false);
38 
39  // Bind to io-stream
40  m_ios.device(sock);
41 
42  // Set socket to listen mode
43  return (sock->listen() == 0);
44 }
45 
46 int
47 INET::Server::run(uint32_t ms)
48 {
49  // Sanity check server state
50  Socket* sock = socket();
51  if (UNLIKELY(sock == NULL)) return (ENOTSOCK);
52 
53  // When not connected; Check incoming connect requests
54  uint32_t start = Watchdog::millis();
55  int res;
56  if (!m_connected) {
57  while (((res = sock->accept()) != 0) &&
58  ((ms == 0L) || (Watchdog::since(start) < ms)))
59  yield();
60  if (res != 0) return (ETIME);
61  // Check if application accepts the connection
62  if (!on_accept(m_ios)) goto error;
63  // Run application connect
65  // Flush response message
66  sock->flush();
67  m_connected = true;
68  return (0);
69  }
70 
71  // Client has been accepted; check for incoming requests
72  while (((res = sock->available()) == 0) &&
73  ((ms == 0L) || (Watchdog::since(start) < ms)))
74  yield();
75  // If a message is available call application request handling
76  if (res > 0) {
78  res = sock->flush();
79  }
80  if (res == 0) return (0);
81 
82  error:
83  // Error handling; close and restart listen mode
84  on_disconnect();
85  m_connected = false;
86  sock->disconnect();
87  sock->listen();
88  return (res);
89 }
90 
91 bool
93 {
94  // Sanity check server state
95  Socket* sock = socket();
96  if (UNLIKELY(sock == NULL)) return (false);
97 
98  // Close the socket and mark as disconnected
99  sock->close();
100  m_connected = false;
101  return (true);
102 }
virtual int listen()=0
Definition: Socket.hh:31
static uint32_t millis()
Definition: Watchdog.hh:51
#define NULL
Definition: Types.h:101
static uint32_t since(uint32_t start)
Definition: Watchdog.hh:108
virtual bool on_accept(IOStream &ios)
Definition: INET.hh:248
virtual bool end()
Definition: INET_Server.cpp:92
IOStream & m_ios
Definition: INET.hh:281
#define ENOTSOCK
Definition: Errno.h:115
virtual bool begin(Socket *sock)
Definition: INET_Server.cpp:34
void client(INET::addr_t &addr)
Definition: INET_Server.cpp:26
Socket * socket()
Definition: INET.hh:201
virtual void on_request(IOStream &ios)=0
#define ETIME
Definition: Errno.h:89
bool m_connected
Definition: INET.hh:284
void(* yield)()
void src(INET::addr_t &addr) const
Definition: Socket.hh:60
virtual int close()=0
virtual int flush()
virtual int run(uint32_t ms=0L)
Definition: INET_Server.cpp:47
Device * device() const
Definition: IOStream.hh:265
virtual int disconnect()=0
virtual int available()
virtual void on_connect(IOStream &ios)
Definition: INET.hh:260
virtual void on_disconnect()
Definition: INET.hh:277
virtual int accept()=0
#define UNLIKELY(x)
Definition: Types.h:153