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 
21 #define NETWORK_DEBUG
22 
23 #define NETWORK_SEND_BUFFER_SIZE 1024
24 
28  eTCE_Sent, //< Occurs when previous sending was completed
29  eTCE_Poll, //< Occurs on waiting
30 };
31 
32 struct pbuf;
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 public:
54  virtual bool connect(const String& server, int port, bool useSsl = false);
55  virtual bool connect(IpAddress addr, uint16_t port, bool useSsl = false);
56  virtual void close();
57 
63  int writeString(const char* data, uint8_t apiflags = TCP_WRITE_FLAG_COPY)
64  {
65  return write(data, strlen(data), apiflags);
66  }
67 
73  int writeString(const String& data, uint8_t apiflags = TCP_WRITE_FLAG_COPY)
74  {
75  return write(data.c_str(), data.length(), apiflags);
76  }
77 
84  virtual int write(const char* data, int len, uint8_t apiflags = TCP_WRITE_FLAG_COPY);
85 
91  int write(IDataSourceStream* stream);
92 
94  {
95  return (canSend && tcp) ? tcp_sndbuf(tcp) : 0;
96  }
97 
98  void flush();
99 
100  void setTimeOut(uint16_t waitTimeOut);
101 
103  {
104  return (tcp == nullptr) ? INADDR_NONE : IpAddress(tcp->remote_ip);
105  }
106 
108  {
109  return (tcp == nullptr) ? 0 : tcp->remote_port;
110  }
111 
117  {
118  this->destroyedDelegate = destroyedDelegate;
119  }
120 
126  {
127  sslInit = handler;
128  }
129 
130  // Called by SSL Session when accepting a client connection
132  {
133  if(!sslCreateSession()) {
134  return false;
135  }
136  ssl->setConnection(connection);
137  useSsl = true;
138  return true;
139  }
140 
149  {
150  return ssl;
151  }
152 
153 protected:
154  void initialize(tcp_pcb* pcb);
155  bool internalConnect(IpAddress addr, uint16_t port);
156 
157  bool sslCreateSession();
158 
164  virtual void sslInitSession(Ssl::Session& session)
165  {
166  if(sslInit) {
167  sslInit(session);
168  }
169  }
170 
171  virtual err_t onConnected(err_t err);
172  virtual err_t onReceive(pbuf* buf);
173  virtual err_t onSent(uint16_t len);
174  virtual err_t onPoll();
175  virtual void onError(err_t err);
176  virtual void onReadyToSendData(TcpConnectionEvent sourceEvent);
177 
182  virtual void onClosed()
183  {
184  }
185 
186  /*
187  * If there is space in the TCP output buffer, then don't wait for TCP
188  * sent confirmation but try to send more data now
189  * (Invoked from within other TCP callbacks.)
190  */
192  {
193  if(tcp != nullptr && getAvailableWriteSize() > 0) {
194  onReadyToSendData(event);
195  }
196  }
197 
198  // These methods are called via LWIP handlers
199  err_t internalOnConnected(err_t err);
200  err_t internalOnReceive(pbuf* p, err_t err);
201  err_t internalOnSent(uint16_t len);
202  err_t internalOnPoll();
203  void internalOnError(err_t err);
204  void internalOnDnsResponse(const char* name, LWIP_IP_ADDR_T* ipaddr, int port);
205 
206 private:
207  static err_t staticOnPoll(void* arg, tcp_pcb* tcp);
208  static void closeTcpConnection(tcp_pcb* tpcb);
209 
210  inline void checkSelfFree()
211  {
212  if(tcp == nullptr && autoSelfDestruct) {
213  delete this;
214  }
215  }
216 
217 protected:
218  tcp_pcb* tcp = nullptr;
220  uint16_t timeOut = USHRT_MAX;
221  bool canSend = true;
222  bool autoSelfDestruct = true;
223  Ssl::Session* ssl = nullptr;
225  bool useSsl = false;
226 
227 private:
228  TcpConnectionDestroyedDelegate destroyedDelegate = nullptr;
229 };
230 
int writeString(const String &data, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
Writes string data directly to the TCP buffer.
Definition: TcpConnection.h:73
#define LWIP_IP_ADDR_T
Definition: IpAddress.h:36
virtual void onReadyToSendData(TcpConnectionEvent sourceEvent)
void internalOnDnsResponse(const char *name, LWIP_IP_ADDR_T *ipaddr, int port)
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:43
Definition: TcpConnection.h:28
uint16_t timeOut
By default a TCP connection does not have a time out.
Definition: TcpConnection.h:220
bool internalConnect(IpAddress addr, uint16_t port)
IpAddress getRemoteIp() const
Definition: TcpConnection.h:102
virtual err_t onPoll()
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:641
void setSslInitHandler(Ssl::Session::InitDelegate handler)
Set the SSL session initialisation callback.
Definition: TcpConnection.h:125
Base class for read-only stream.
Definition: DataSourceStream.h:40
virtual err_t onSent(uint16_t len)
Implemented by SSL adapter to handle a connection.
Definition: Connection.h:35
uint16_t getRemotePort() const
Definition: TcpConnection.h:107
Occurs on data receive.
Definition: TcpConnection.h:27
virtual bool connect(const String &server, int port, bool useSsl=false)
virtual void close()
Definition: TcpConnection.h:29
Ssl::Session * getSsl()
Get a pointer to the current SSL session object.
Definition: TcpConnection.h:148
Definition: TcpConnection.h:39
The String class.
Definition: WString.h:136
bool sslCreateSession()
virtual ~TcpConnection()
err_t internalOnPoll()
void setConnection(Connection *connection)
Called by TcpConnection to set the established SSL connection.
Definition: Session.h:148
virtual err_t onConnected(err_t err)
bool canSend
Definition: TcpConnection.h:221
bool setSslConnection(Ssl::Connection *connection)
Definition: TcpConnection.h:131
void setTimeOut(uint16_t waitTimeOut)
TcpConnectionEvent
Definition: TcpConnection.h:25
virtual int write(const char *data, int len, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
Base write operation.
Handles all SSL activity for a TCP connection.
Definition: Session.h:76
Server server
TcpConnection(tcp_pcb *connection, bool autoDestruct)
Definition: TcpConnection.h:46
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:237
Ssl::Session::InitDelegate sslInit
Definition: TcpConnection.h:224
TcpConnection(bool autoDestruct)
Definition: TcpConnection.h:42
uint16_t getAvailableWriteSize()
Definition: TcpConnection.h:93
err_t internalOnConnected(err_t err)
Ssl::Session * ssl
Definition: TcpConnection.h:223
void setDestroyedDelegate(TcpConnectionDestroyedDelegate destroyedDelegate)
Sets a callback to be called when the object instance is destroyed.
Definition: TcpConnection.h:116
Occurs after connection establishment.
Definition: TcpConnection.h:26
bool useSsl
Definition: TcpConnection.h:225
tcp_pcb * tcp
Definition: TcpConnection.h:218
Definition: IpConnection.h:21
void internalOnError(err_t err)
virtual err_t onReceive(pbuf *buf)
err_t internalOnSent(uint16_t len)
bool autoSelfDestruct
Definition: TcpConnection.h:222
void initialize(tcp_pcb *pcb)
int writeString(const char *data, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
Writes string data directly to the TCP buffer.
Definition: TcpConnection.h:63
uint16_t sleep
Definition: TcpConnection.h:219
virtual void onError(err_t err)
#define INADDR_NONE
Definition: IpAddress.h:205
virtual void onClosed()
Gets called when there is/was a tcp connection, the latter does not have to be established, that is closed due to error or normal disconnect.
Definition: TcpConnection.h:182
void trySend(TcpConnectionEvent event)
Definition: TcpConnection.h:191
virtual void sslInitSession(Ssl::Session &session)
Override in inherited classes to perform custom session initialisation.
Definition: TcpConnection.h:164
err_t internalOnReceive(pbuf *p, err_t err)