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 #define TCP_SERVER_TIMEOUT 20
27 
28 class TcpServer : public TcpConnection
29 {
30 public:
32  {
34  }
35 
36  TcpServer(TcpClientConnectDelegate onClientHandler, TcpClientDataDelegate clientReceiveDataHandler,
37  TcpClientCompleteDelegate clientCompleteHandler)
38  : TcpConnection(false), clientConnectDelegate(onClientHandler), clientReceiveDelegate(clientReceiveDataHandler),
39  clientCompleteDelegate(clientCompleteHandler)
40  {
42  }
43 
44  TcpServer(TcpClientDataDelegate clientReceiveDataHandler, TcpClientCompleteDelegate clientCompleteHandler)
45  : TcpConnection(false), clientReceiveDelegate(clientReceiveDataHandler),
46  clientCompleteDelegate(clientCompleteHandler)
47  {
49  }
50 
51  TcpServer(TcpClientDataDelegate clientReceiveDataHandler)
52  : TcpConnection(false), clientReceiveDelegate(clientReceiveDataHandler)
53  {
55  }
56 
58  {
59  debug_i("TcpServer destroyed");
60  }
61 
62  virtual bool listen(int port, bool useSsl = false);
63 
64  void setKeepAlive(uint16_t seconds);
65 
66  void shutdown();
67 
69  {
70  return connections;
71  }
72 
73 protected:
74  // Overload this method in your derived class!
75  virtual TcpConnection* createClient(tcp_pcb* clientTcp);
76 
77  virtual err_t onAccept(tcp_pcb* clientTcp, err_t err);
78  virtual void onClient(TcpClient* client);
79  virtual bool onClientReceive(TcpClient& client, char* data, int size);
80  virtual void onClientComplete(TcpClient& client, bool successful);
81  virtual void onClientDestroy(TcpConnection& connection);
82 
83 private:
84  static err_t staticAccept(void* arg, tcp_pcb* new_tcp, err_t err);
85 
86 public:
88 
89 protected:
90  size_t minHeapSize = 16384;
92 
93  bool active = true;
95 
96 private:
97  uint16_t keepAlive = 70;
98  // coming or going to the client within that period the client connection will be closed
99  TcpClientConnectDelegate clientConnectDelegate = nullptr;
100  TcpClientDataDelegate clientReceiveDelegate = nullptr;
101  TcpClientCompleteDelegate clientCompleteDelegate = nullptr;
102 };
103 
~TcpServer()
Definition: TcpServer.h:57
size_t minHeapSize
Definition: TcpServer.h:90
uint16_t timeOut
By default a TCP connection does not have a time out.
Definition: TcpConnection.h:199
uint16_t activeClients
Definition: TcpServer.h:87
virtual void onClient(TcpClient *client)
TcpServer(TcpClientDataDelegate clientReceiveDataHandler, TcpClientCompleteDelegate clientCompleteHandler)
Definition: TcpServer.h:44
void shutdown()
Vector class template.
Definition: WVector.h:29
virtual err_t onAccept(tcp_pcb *clientTcp, err_t err)
bool active
Definition: TcpServer.h:93
Definition: TcpConnection.h:39
TcpServer(TcpClientDataDelegate clientReceiveDataHandler)
Definition: TcpServer.h:51
Definition: TcpClient.h:46
virtual void onClientDestroy(TcpConnection &connection)
virtual bool onClientReceive(TcpClient &client, char *data, int size)
Definition: TcpServer.h:28
void setKeepAlive(uint16_t seconds)
#define debug_i
Definition: debug_progmem.h:99
bool useSsl
Definition: TcpConnection.h:204
virtual bool listen(int port, bool useSsl=false)
Vector< TcpConnection * > connections
Definition: TcpServer.h:94
TcpServer(TcpClientConnectDelegate onClientHandler, TcpClientDataDelegate clientReceiveDataHandler, TcpClientCompleteDelegate clientCompleteHandler)
Definition: TcpServer.h:36
virtual void onClientComplete(TcpClient &client, bool successful)
TcpServer()
Definition: TcpServer.h:31
#define TCP_SERVER_TIMEOUT
Definition: TcpServer.h:26
uint16_t maxConnections
By default, don&#39;t limit connection count.
Definition: TcpServer.h:91
Delegate< void(TcpClient *client)> TcpClientConnectDelegate
Definition: TcpServer.h:23
const Vector< TcpConnection * > & getConnections() const
Definition: TcpServer.h:68
virtual TcpConnection * createClient(tcp_pcb *clientTcp)