friendev EtherDune TCP/IP library
/home/jander/temp/etherdune/TCPListener.h
Go to the documentation of this file.
1 // EtherDune TCP Listener helper template class
2 // Author: Javier Peletier <jm@friendev.com>
3 // Summary: Maintains a list of available sockets, also listening and spawning sockets to
4 // serve multiple clients simultaneously.
5 //
6 // Copyright (c) 2015 All Rights Reserved, http://friendev.com
7 //
8 // This source is subject to the GPLv2 license.
9 // Please see the License.txt file for more information.
10 // All other rights reserved.
11 //
12 // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
13 // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14 // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
15 // PARTICULAR PURPOSE.
16 
37 #ifndef _TCPLISTENER_H_
38 #define _TCPLISTENER_H_
39 
40 #include "TCPSocket.h"
41 
42 
43 //SOCKET is expected to derive from TCPSocket
44 template <class SOCKET, uint8_t MAX_CLIENTS>
45 class TCPListener : public TCPSocket
46 {
47 protected:
48  SOCKET clients[MAX_CLIENTS];
52  virtual void onNoMoreConnections(){}
53 
54 protected:
55 
56  void onTerminate()
57  {
59  }
61  {
62 
63  for (int i = 0; i < MAX_CLIENTS; i++)
64  {
65  SOCKET& sck = clients[i];
66  if (sck.getState() == SCK_STATE_CLOSED)
67  {
68  sck.accept(*this);
69 
70  return;
71  }
72  }
73 
75 
76  }
77 
78 public:
83  void listen(uint16_t port)
84  {
85  localPort = port;
87  }
88 
89 
90 
91 };
92 
93 
94 #endif
nint16_t localPort
local TCP or UDP port
Definition: Socket.h:54
void listen(uint16_t port)
Starts to listen on the specified TCP port.
Definition: TCPListener.h:83
void onTerminate()
Called when the socket is ready to be reused.
Definition: TCPListener.h:56
void listen()
Starts listening on the local port indicated by the localPort property.
Definition: TCPSocket.cpp:91
static const uint8_t SCK_STATE_CLOSED
(both server and client) represents no connection state at all.
Definition: inet.h:550
SOCKET clients[MAX_CLIENTS]
Definition: TCPListener.h:48
void onConnectRequest()
Called when a listening socket receives a connection request.
Definition: TCPListener.h:60
virtual void onNoMoreConnections()
Called when there are no more sockets available
Definition: TCPListener.h:52
Maintains a list of available sockets, also listening and spawning sockets to serve multiple clients ...
Definition: TCPListener.h:45
Implements the TCP protocol.
Definition: TCPSocket.h:45