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/Ssl/Session.h>
19 #include <IpAddress.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;
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 
58  // return -1 on error
59  int writeString(const char* data, uint8_t apiflags = TCP_WRITE_FLAG_COPY)
60  {
61  return write(data, strlen(data), apiflags);
62  }
63 
64  // return -1 on error
65  int writeString(const String& data, uint8_t apiflags = TCP_WRITE_FLAG_COPY)
66  {
67  return write(data.c_str(), data.length(), apiflags);
68  }
69 
76  virtual int write(const char* data, int len, uint8_t apiflags = TCP_WRITE_FLAG_COPY);
77 
78  int write(IDataSourceStream* stream);
79 
81  {
82  return (canSend && tcp) ? tcp_sndbuf(tcp) : 0;
83  }
84 
85  void flush();
86 
87  void setTimeOut(uint16_t waitTimeOut);
88 
90  {
91  return (tcp == nullptr) ? INADDR_NONE : IpAddress(tcp->remote_ip);
92  }
93 
95  {
96  return (tcp == nullptr) ? 0 : tcp->remote_port;
97  }
98 
104  {
105  this->destroyedDelegate = destroyedDelegate;
106  }
107 
113  {
114  sslInit = handler;
115  }
116 
117  // Called by SSL Session when accepting a client connection
119  {
120  if(!sslCreateSession()) {
121  return false;
122  }
123  ssl->setConnection(connection);
124  useSsl = true;
125  return true;
126  }
127 
136  {
137  return ssl;
138  }
139 
140 protected:
141  void initialize(tcp_pcb* pcb);
142  bool internalConnect(IpAddress addr, uint16_t port);
143 
144  bool sslCreateSession();
145 
151  virtual void sslInitSession(Ssl::Session& session)
152  {
153  if(sslInit) {
154  sslInit(session);
155  }
156  }
157 
158  virtual err_t onConnected(err_t err);
159  virtual err_t onReceive(pbuf* buf);
160  virtual err_t onSent(uint16_t len);
161  virtual err_t onPoll();
162  virtual void onError(err_t err);
163  virtual void onReadyToSendData(TcpConnectionEvent sourceEvent);
164 
165  /*
166  * If there is space in the TCP output buffer, then don't wait for TCP
167  * sent confirmation but try to send more data now
168  * (Invoked from within other TCP callbacks.)
169  */
171  {
172  if(tcp != nullptr && getAvailableWriteSize() > 0) {
173  onReadyToSendData(event);
174  }
175  }
176 
177  // These methods are called via LWIP handlers
178  err_t internalOnConnected(err_t err);
179  err_t internalOnReceive(pbuf* p, err_t err);
180  err_t internalOnSent(uint16_t len);
181  err_t internalOnPoll();
182  void internalOnError(err_t err);
183  void internalOnDnsResponse(const char* name, LWIP_IP_ADDR_T* ipaddr, int port);
184 
185 private:
186  static err_t staticOnPoll(void* arg, tcp_pcb* tcp);
187  static void closeTcpConnection(tcp_pcb* tpcb);
188 
189  inline void checkSelfFree()
190  {
191  if(tcp == nullptr && autoSelfDestruct) {
192  delete this;
193  }
194  }
195 
196 protected:
197  tcp_pcb* tcp = nullptr;
199  uint16_t timeOut = USHRT_MAX;
200  bool canSend = true;
201  bool autoSelfDestruct = true;
202  Ssl::Session* ssl = nullptr;
204  bool useSsl = false;
205 
206 private:
207  TcpConnectionDestroyedDelegate destroyedDelegate = nullptr;
208 };
209 
int writeString(const String &data, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
Definition: TcpConnection.h:65
#define LWIP_IP_ADDR_T
Definition: IpAddress.h:30
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:37
Definition: TcpConnection.h:28
uint16_t timeOut
By default a TCP connection does not have a time out.
Definition: TcpConnection.h:199
bool internalConnect(IpAddress addr, uint16_t port)
IpAddress getRemoteIp() const
Definition: TcpConnection.h:89
virtual err_t onPoll()
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:600
void setSslInitHandler(Ssl::Session::InitDelegate handler)
Set the SSL session initialisation callback.
Definition: TcpConnection.h:112
Base class for data source stream.
Definition: DataSourceStream.h:39
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:94
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:135
Delegate< void(TcpConnection &)> TcpConnectionDestroyedDelegate
Definition: TcpConnection.h:35
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:200
bool setSslConnection(Ssl::Connection *connection)
Definition: TcpConnection.h:118
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
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:228
Ssl::Session::InitDelegate sslInit
Definition: TcpConnection.h:203
TcpConnection(bool autoDestruct)
Definition: TcpConnection.h:42
uint16_t getAvailableWriteSize()
Definition: TcpConnection.h:80
err_t internalOnConnected(err_t err)
Ssl::Session * ssl
Definition: TcpConnection.h:202
void setDestroyedDelegate(TcpConnectionDestroyedDelegate destroyedDelegate)
Sets a callback to be called when the object instance is destroyed.
Definition: TcpConnection.h:103
Occurs after connection establishment.
Definition: TcpConnection.h:26
bool useSsl
Definition: TcpConnection.h:204
tcp_pcb * tcp
Definition: TcpConnection.h:197
void internalOnError(err_t err)
virtual err_t onReceive(pbuf *buf)
err_t internalOnSent(uint16_t len)
bool autoSelfDestruct
Definition: TcpConnection.h:201
void initialize(tcp_pcb *pcb)
int writeString(const char *data, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
Definition: TcpConnection.h:59
uint16_t sleep
Definition: TcpConnection.h:198
virtual void onError(err_t err)
#define INADDR_NONE
Definition: IpAddress.h:181
void trySend(TcpConnectionEvent event)
Definition: TcpConnection.h:170
virtual void sslInitSession(Ssl::Session &session)
Override in inherited classes to perform custom session initialisation.
Definition: TcpConnection.h:151
err_t internalOnReceive(pbuf *p, err_t err)