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 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  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 char *data, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
Writes string data directly to the TCP buffer.
Definition: TcpConnection.h:63
Base class for read-only stream.
Definition: DataSourceStream.h:45
tcp_pcb * tcp
Definition: TcpConnection.h:218
virtual void sslInitSession(Ssl::Session &session)
Override in inherited classes to perform custom session initialisation.
Definition: TcpConnection.h:164
err_t internalOnConnected(err_t err)
virtual err_t onPoll()
The String class.
Definition: WString.h:136
void initialize(tcp_pcb *pcb)
bool canSend
Definition: TcpConnection.h:221
void setDestroyedDelegate(TcpConnectionDestroyedDelegate destroyedDelegate)
Sets a callback to be called when the object instance is destroyed.
Definition: TcpConnection.h:116
TcpConnectionEvent
Definition: TcpConnection.h:26
void setTimeOut(uint16_t waitTimeOut)
Server server
err_t internalOnPoll()
int writeString(const String &data, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
Writes string data directly to the TCP buffer.
Definition: TcpConnection.h:73
virtual err_t onReceive(pbuf *buf)
uint16_t sleep
Definition: TcpConnection.h:219
Ssl::Session * getSsl()
Get a pointer to the current SSL session object.
Definition: TcpConnection.h:148
virtual int write(const char *data, int len, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
Base write operation.
void internalOnError(err_t err)
uint16_t timeOut
By default a TCP connection does not have a time out.
Definition: TcpConnection.h:220
bool autoSelfDestruct
Definition: TcpConnection.h:222
uint16_t getAvailableWriteSize()
Definition: TcpConnection.h:93
bool internalConnect(IpAddress addr, uint16_t port)
IpAddress getRemoteIp() const
Definition: TcpConnection.h:102
virtual err_t onConnected(err_t err)
TcpConnection(tcp_pcb *connection, bool autoDestruct)
Definition: TcpConnection.h:46
err_t internalOnReceive(pbuf *p, err_t err)
bool setSslConnection(Ssl::Connection *connection)
Definition: TcpConnection.h:131
virtual void close()
Implemented by SSL adapter to handle a connection.
Definition: Connection.h:46
bool useSsl
Definition: TcpConnection.h:225
#define INADDR_NONE
Definition: IpAddress.h:203
Definition: IpConnection.h:21
err_t internalOnSent(uint16_t len)
Ssl::Session * ssl
Definition: TcpConnection.h:223
void internalOnDnsResponse(const char *name, LWIP_IP_ADDR_T *ipaddr, int port)
virtual void onError(err_t err)
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
@ eTCE_Sent
Definition: TcpConnection.h:29
bool sslCreateSession()
uint16_t getRemotePort() const
Definition: TcpConnection.h:107
Handles all SSL activity for a TCP connection.
Definition: Session.h:77
virtual bool connect(const String &server, int port, bool useSsl=false)
virtual void onReadyToSendData(TcpConnectionEvent sourceEvent)
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:44
@ eTCE_Poll
Definition: TcpConnection.h:30
Definition: TcpConnection.h:39
virtual err_t onSent(uint16_t len)
void setConnection(Connection *connection)
Called by TcpConnection to set the established SSL connection.
Definition: Session.h:148
virtual void onClosed()
Gets called when there is/was a tcp connection, the latter does not have to be established,...
Definition: TcpConnection.h:182
Ssl::Session::InitDelegate sslInit
Definition: TcpConnection.h:224
TcpConnection(bool autoDestruct)
Definition: TcpConnection.h:42
void trySend(TcpConnectionEvent event)
Definition: TcpConnection.h:191
@ eTCE_Received
Occurs on data receive.
Definition: TcpConnection.h:28
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:243
#define LWIP_IP_ADDR_T
Definition: IpAddress.h:37
@ eTCE_Connected
Occurs after connection establishment.
Definition: TcpConnection.h:27
virtual ~TcpConnection()
void setSslInitHandler(Ssl::Session::InitDelegate handler)
Set the SSL session initialisation callback.
Definition: TcpConnection.h:125