TcpConnection.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  * TcpConnection.h
8  *
9  ****/
10 
16 #pragma once
17 
18 #include <Network/IpConnection.h>
19 #include <Network/Ssl/Session.h>
20 #include <lwip/tcp.h>
21 
22 #define NETWORK_DEBUG
23 
24 #define NETWORK_SEND_BUFFER_SIZE 1024
25 
29  eTCE_Sent, //< Occurs when previous sending was completed
30  eTCE_Poll, //< Occurs on waiting
31 };
32 
33 class String;
34 class IDataSourceStream;
35 class TcpConnection;
36 
38 
40 {
41 public:
42  TcpConnection(bool autoDestruct) : autoSelfDestruct(autoDestruct)
43  {
44  }
45 
46  TcpConnection(tcp_pcb* connection, bool autoDestruct) : autoSelfDestruct(autoDestruct)
47  {
48  initialize(connection);
49  }
50 
51  virtual ~TcpConnection();
52 
53  virtual bool connect(const String& server, int port, bool useSsl = false);
54  virtual bool connect(IpAddress addr, uint16_t port, bool useSsl = false);
55  virtual void close();
56 
57  void setAutoSelfDestruct(bool state)
58  {
59  autoSelfDestruct = state;
60  }
61 
67  int writeString(const char* data, uint8_t apiflags = TCP_WRITE_FLAG_COPY)
68  {
69  return write(data, strlen(data), apiflags);
70  }
71 
77  int writeString(const String& data, uint8_t apiflags = TCP_WRITE_FLAG_COPY)
78  {
79  return write(data.c_str(), data.length(), apiflags);
80  }
81 
88  virtual int write(const char* data, int len, uint8_t apiflags = TCP_WRITE_FLAG_COPY);
89 
95  int write(IDataSourceStream* stream);
96 
98  {
99  return (canSend && tcp) ? tcp_sndbuf(tcp) : 0;
100  }
101 
102  void flush();
103 
104  void setTimeOut(uint16_t waitTimeOut);
105 
107  {
108  return (tcp == nullptr) ? INADDR_NONE : IpAddress(tcp->remote_ip);
109  }
110 
111  uint16_t getRemotePort() const
112  {
113  return (tcp == nullptr) ? 0 : tcp->remote_port;
114  }
115 
121  {
122  this->destroyedDelegate = destroyedDelegate;
123  }
124 
130  {
131  sslInit = handler;
132  }
133 
134  // Called by SSL Session when accepting a client connection
136  {
137  if(!sslCreateSession()) {
138  return false;
139  }
140  ssl->setConnection(connection);
141  useSsl = true;
142  return true;
143  }
144 
153  {
154  return ssl;
155  }
156 
157 protected:
158  void initialize(tcp_pcb* pcb);
159  bool internalConnect(IpAddress addr, uint16_t port);
160 
162 
168  virtual void sslInitSession(Ssl::Session& session)
169  {
170  if(sslInit) {
171  sslInit(session);
172  }
173  }
174 
175  virtual err_t onConnected(err_t err);
176  virtual err_t onReceive(pbuf* buf);
177  virtual err_t onSent(uint16_t len);
178  virtual err_t onPoll();
179  virtual void onError(err_t err);
180  virtual void onReadyToSendData(TcpConnectionEvent sourceEvent);
181 
186  virtual void onClosed()
187  {
188  }
189 
190  /*
191  * If there is space in the TCP output buffer, then don't wait for TCP
192  * sent confirmation but try to send more data now
193  * (Invoked from within other TCP callbacks.)
194  */
196  {
197  if(tcp != nullptr && getAvailableWriteSize() > 0) {
198  onReadyToSendData(event);
199  }
200  }
201 
202  // These methods are called via LWIP handlers
203  err_t internalOnConnected(err_t err);
204  err_t internalOnReceive(pbuf* p, err_t err);
205  err_t internalOnSent(uint16_t len);
206  err_t internalOnPoll();
207  void internalOnError(err_t err);
208  void internalOnDnsResponse(const char* name, LWIP_IP_ADDR_T* ipaddr, int port);
209 
210 private:
211  static err_t staticOnPoll(void* arg, tcp_pcb* tcp);
212  static void closeTcpConnection(tcp_pcb* tpcb);
213 
214  void checkSelfFree()
215  {
216  if(tcp == nullptr && autoSelfDestruct) {
217  delete this;
218  }
219  }
220 
221 protected:
222  tcp_pcb* tcp = nullptr;
223  uint16_t sleep = 0;
224  uint16_t timeOut = USHRT_MAX;
225  bool canSend = true;
226  bool autoSelfDestruct = true;
227  Ssl::Session* ssl = nullptr;
229  bool useSsl = false;
230 
231 private:
232  TcpConnectionDestroyedDelegate destroyedDelegate = nullptr;
233 };
234 
#define INADDR_NONE
Definition: IpAddress.h:203
#define LWIP_IP_ADDR_T
Definition: IpAddress.h:37
Base class for read-only stream.
Definition: DataSourceStream.h:46
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:45
Definition: IpConnection.h:22
Implemented by SSL adapter to handle a connection.
Definition: Connection.h:37
Handles all SSL activity for a TCP connection.
Definition: Session.h:78
void setConnection(Connection *connection)
Called by TcpConnection to set the established SSL connection.
Definition: Session.h:148
The String class.
Definition: WString.h:137
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:243
Definition: TcpConnection.h:40
virtual bool connect(const String &server, int port, bool useSsl=false)
void internalOnError(err_t err)
virtual err_t onSent(uint16_t len)
Ssl::Session::InitDelegate sslInit
Definition: TcpConnection.h:228
bool sslCreateSession()
virtual int write(const char *data, int len, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
Base write operation.
bool useSsl
Definition: TcpConnection.h:229
void setDestroyedDelegate(TcpConnectionDestroyedDelegate destroyedDelegate)
Sets a callback to be called when the object instance is destroyed.
Definition: TcpConnection.h:120
void setTimeOut(uint16_t waitTimeOut)
uint16_t timeOut
By default a TCP connection does not have a time out.
Definition: TcpConnection.h:224
int writeString(const char *data, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
Writes string data directly to the TCP buffer.
Definition: TcpConnection.h:67
void setAutoSelfDestruct(bool state)
Definition: TcpConnection.h:57
virtual err_t onPoll()
uint16_t sleep
Definition: TcpConnection.h:223
Ssl::Session * getSsl()
Get a pointer to the current SSL session object.
Definition: TcpConnection.h:152
virtual ~TcpConnection()
bool autoSelfDestruct
Definition: TcpConnection.h:226
virtual err_t onReceive(pbuf *buf)
int write(IDataSourceStream *stream)
Writes stream data directly to the TCP buffer.
tcp_pcb * tcp
Definition: TcpConnection.h:222
void trySend(TcpConnectionEvent event)
Definition: TcpConnection.h:195
err_t internalOnPoll()
TcpConnection(bool autoDestruct)
Definition: TcpConnection.h:42
uint16_t getRemotePort() const
Definition: TcpConnection.h:111
bool canSend
Definition: TcpConnection.h:225
TcpConnection(tcp_pcb *connection, bool autoDestruct)
Definition: TcpConnection.h:46
virtual void onReadyToSendData(TcpConnectionEvent sourceEvent)
void setSslInitHandler(Ssl::Session::InitDelegate handler)
Set the SSL session initialisation callback.
Definition: TcpConnection.h:129
int writeString(const String &data, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
Writes string data directly to the TCP buffer.
Definition: TcpConnection.h:77
IpAddress getRemoteIp() const
Definition: TcpConnection.h:106
virtual void close()
virtual void sslInitSession(Ssl::Session &session)
Override in inherited classes to perform custom session initialisation.
Definition: TcpConnection.h:168
bool internalConnect(IpAddress addr, uint16_t port)
virtual void onError(err_t err)
uint16_t getAvailableWriteSize()
Definition: TcpConnection.h:97
void internalOnDnsResponse(const char *name, LWIP_IP_ADDR_T *ipaddr, int port)
Ssl::Session * ssl
Definition: TcpConnection.h:227
err_t internalOnReceive(pbuf *p, err_t err)
void initialize(tcp_pcb *pcb)
err_t internalOnConnected(err_t err)
virtual bool connect(IpAddress addr, uint16_t port, bool useSsl=false)
virtual void onClosed()
Gets called when there is/was a tcp connection, the latter does not have to be established,...
Definition: TcpConnection.h:186
bool setSslConnection(Ssl::Connection *connection)
Definition: TcpConnection.h:135
err_t internalOnSent(uint16_t len)
virtual err_t onConnected(err_t err)
TcpConnectionEvent
Definition: TcpConnection.h:26
@ eTCE_Sent
Definition: TcpConnection.h:29
@ eTCE_Received
Occurs on data receive.
Definition: TcpConnection.h:28
@ eTCE_Poll
Definition: TcpConnection.h:30
@ eTCE_Connected
Occurs after connection establishment.
Definition: TcpConnection.h:27
Server server