TcpServer.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * TcpServer.h
8  *
9  ****/
10 
18 #pragma once
19 
20 #include "TcpConnection.h"
21 #include "TcpClient.h"
22 
24 
25 // By default a TCP server will wait for a new remote client connection to get established for 20 seconds
26 #ifndef TCP_SERVER_TIMEOUT
27 #define TCP_SERVER_TIMEOUT 20
28 #endif
29 
30 class TcpServer : public TcpConnection
31 {
32 public:
34  {
36  }
37 
38  TcpServer(TcpClientConnectDelegate onClientHandler, TcpClientDataDelegate clientReceiveDataHandler,
39  TcpClientCompleteDelegate clientCompleteHandler)
40  : TcpConnection(false), clientConnectDelegate(onClientHandler), clientReceiveDelegate(clientReceiveDataHandler),
41  clientCompleteDelegate(clientCompleteHandler)
42  {
44  }
45 
46  TcpServer(TcpClientDataDelegate clientReceiveDataHandler, TcpClientCompleteDelegate clientCompleteHandler)
47  : TcpConnection(false), clientReceiveDelegate(clientReceiveDataHandler),
48  clientCompleteDelegate(clientCompleteHandler)
49  {
51  }
52 
53  TcpServer(TcpClientDataDelegate clientReceiveDataHandler)
54  : TcpConnection(false), clientReceiveDelegate(clientReceiveDataHandler)
55  {
57  }
58 
59  void setClientReceiveHandler(TcpClientDataDelegate clientReceiveDataHandler)
60  {
61  clientReceiveDelegate = clientReceiveDataHandler;
62  }
63 
65  {
66  debug_i("TcpServer destroyed");
67  }
68 
69  virtual bool listen(int port, bool useSsl = false);
70 
71  void setKeepAlive(uint16_t seconds);
72 
73  void shutdown();
74 
76  {
77  return connections;
78  }
79 
80 protected:
81  // Overload this method in your derived class!
82  virtual TcpConnection* createClient(tcp_pcb* clientTcp);
83 
84  virtual err_t onAccept(tcp_pcb* clientTcp, err_t err);
85  virtual void onClient(TcpClient* client);
86  virtual bool onClientReceive(TcpClient& client, char* data, int size);
87  virtual void onClientComplete(TcpClient& client, bool successful);
88  virtual void onClientDestroy(TcpConnection& connection);
89 
90 private:
91  static err_t staticAccept(void* arg, tcp_pcb* new_tcp, err_t err);
92 
93 public:
94  uint16_t activeClients = 0;
95 
96 protected:
97  size_t minHeapSize = 16384;
98  uint16_t maxConnections = 0;
99 
100  bool active = true;
102 
103 private:
104  uint16_t keepAlive = 70;
105  // coming or going to the client within that period the client connection will be closed
106  TcpClientConnectDelegate clientConnectDelegate = nullptr;
107  TcpClientDataDelegate clientReceiveDelegate = nullptr;
108  TcpClientCompleteDelegate clientCompleteDelegate = nullptr;
109 };
110 
Definition: TcpClient.h:46
Definition: TcpConnection.h:40
bool useSsl
Definition: TcpConnection.h:229
uint16_t timeOut
By default a TCP connection does not have a time out.
Definition: TcpConnection.h:224
Definition: TcpServer.h:31
virtual void onClientComplete(TcpClient &client, bool successful)
virtual bool onClientReceive(TcpClient &client, char *data, int size)
uint16_t activeClients
Definition: TcpServer.h:94
const Vector< TcpConnection * > & getConnections() const
Definition: TcpServer.h:75
~TcpServer()
Definition: TcpServer.h:64
bool active
Definition: TcpServer.h:100
Vector< TcpConnection * > connections
Definition: TcpServer.h:101
virtual bool listen(int port, bool useSsl=false)
void setKeepAlive(uint16_t seconds)
TcpServer(TcpClientConnectDelegate onClientHandler, TcpClientDataDelegate clientReceiveDataHandler, TcpClientCompleteDelegate clientCompleteHandler)
Definition: TcpServer.h:38
void shutdown()
TcpServer(TcpClientDataDelegate clientReceiveDataHandler, TcpClientCompleteDelegate clientCompleteHandler)
Definition: TcpServer.h:46
virtual void onClientDestroy(TcpConnection &connection)
uint16_t maxConnections
By default, don't limit connection count.
Definition: TcpServer.h:98
virtual void onClient(TcpClient *client)
TcpServer()
Definition: TcpServer.h:33
size_t minHeapSize
Definition: TcpServer.h:97
virtual err_t onAccept(tcp_pcb *clientTcp, err_t err)
virtual TcpConnection * createClient(tcp_pcb *clientTcp)
void setClientReceiveHandler(TcpClientDataDelegate clientReceiveDataHandler)
Definition: TcpServer.h:59
TcpServer(TcpClientDataDelegate clientReceiveDataHandler)
Definition: TcpServer.h:53
#define debug_i
Definition: debug_progmem.h:99
#define TCP_SERVER_TIMEOUT
Definition: TcpServer.h:27