TcpClient.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  * TcpClient.h
8  *
9  ****/
10 
17 #pragma once
18 
19 #include "TcpConnection.h"
20 
21 class TcpClient;
22 class IpAddress;
23 
24 using TcpClientEventDelegate = Delegate<void(TcpClient& client, TcpConnectionEvent sourceEvent)>;
25 using TcpClientCompleteDelegate = Delegate<void(TcpClient& client, bool successful)>;
26 using TcpClientDataDelegate = Delegate<bool(TcpClient& client, char* data, int size)>;
27 
34 };
35 
40 };
41 
42 // By default a TCP client connection has 70 seconds timeout
43 #define TCP_CLIENT_TIMEOUT 70
44 
45 class TcpClient : public TcpConnection
46 {
47 public:
48  TcpClient(bool autoDestruct) : TcpConnection(autoDestruct)
49  {
51  }
52 
53  TcpClient(tcp_pcb* clientTcp, TcpClientDataDelegate clientReceive, TcpClientCompleteDelegate onCompleted)
54  : TcpConnection(clientTcp, true), state(eTCS_Connected), completed(onCompleted), receive(clientReceive)
55  {
57  }
58 
61  : TcpConnection(false), completed(onCompleted), ready(onReadyToSend), receive(onReceive)
62  {
64  }
65 
67  : TcpConnection(false), completed(onCompleted), receive(onReceive)
68  {
70  }
71 
73  {
75  }
76 
78  {
79  freeStreams();
80  }
81 
82  bool connect(const String& server, int port, bool useSsl = false) override;
83  bool connect(IpAddress addr, uint16_t port, bool useSsl = false) override;
84  void close() override;
85 
89  void setReceiveDelegate(TcpClientDataDelegate receiveCb = nullptr)
90  {
91  receive = receiveCb;
92  }
93 
98  {
99  completed = completeCb;
100  }
101 
102  bool send(const char* data, uint16_t len, bool forceCloseAfterSent = false);
103 
104  bool sendString(const String& data, bool forceCloseAfterSent = false)
105  {
106  return send(data.c_str(), data.length(), forceCloseAfterSent);
107  }
108 
119  bool send(IDataSourceStream* source, bool forceCloseAfterSent = false);
120 
122  {
123  return state == eTCS_Connected || state == eTCS_Connecting;
124  }
125 
127  {
128  return state;
129  }
130 
135  void setCloseAfterSent(bool ignoreIncomingData = false)
136  {
137  closeAfterSent = ignoreIncomingData ? eTCCASS_AfterSent_Ignore_Received : eTCCASS_AfterSent;
138  }
139 
144  void commit()
145  {
148  }
149 
150 protected:
151  err_t onConnected(err_t err) override;
152  err_t onReceive(pbuf* buf) override;
153  err_t onSent(uint16_t len) override;
154  void onError(err_t err) override;
155  void onClosed() override;
156 
157  void onReadyToSendData(TcpConnectionEvent sourceEvent) override;
158 
159  virtual void onFinished(TcpClientState finishState);
160 
162  void freeStreams();
163 
164 protected:
166 
167 private:
168  TcpClientState state = eTCS_Ready;
169  TcpClientCompleteDelegate completed;
171  TcpClientDataDelegate receive;
172 
174  uint16_t totalSentConfirmedBytes = 0;
175  uint16_t totalSentBytes = 0;
176 };
177 
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
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: TcpClient.h:46
~TcpClient()
Definition: TcpClient.h:77
virtual void onFinished(TcpClientState finishState)
void pushAsyncPart()
void onError(err_t err) override
bool isProcessing()
Definition: TcpClient.h:121
TcpClient(bool autoDestruct)
Definition: TcpClient.h:48
TcpClient(TcpClientCompleteDelegate onCompleted, TcpClientDataDelegate onReceive=nullptr)
Definition: TcpClient.h:66
void onClosed() override
Gets called when there is/was a tcp connection, the latter does not have to be established,...
void close() override
err_t onSent(uint16_t len) override
bool send(IDataSourceStream *source, bool forceCloseAfterSent=false)
Sends data stream.
bool send(const char *data, uint16_t len, bool forceCloseAfterSent=false)
void setReceiveDelegate(TcpClientDataDelegate receiveCb=nullptr)
Set or clear the callback for received data.
Definition: TcpClient.h:89
TcpClientState getConnectionState()
Definition: TcpClient.h:126
bool sendString(const String &data, bool forceCloseAfterSent=false)
Definition: TcpClient.h:104
err_t onConnected(err_t err) override
void commit()
Tries to send the pending data immediately.
Definition: TcpClient.h:144
void freeStreams()
err_t onReceive(pbuf *buf) override
bool connect(const String &server, int port, bool useSsl=false) override
TcpClient(TcpClientCompleteDelegate onCompleted, TcpClientEventDelegate onReadyToSend, TcpClientDataDelegate onReceive=nullptr)
Definition: TcpClient.h:59
void setCloseAfterSent(bool ignoreIncomingData=false)
Definition: TcpClient.h:135
TcpClient(TcpClientDataDelegate onReceive)
Definition: TcpClient.h:72
TcpClient(tcp_pcb *clientTcp, TcpClientDataDelegate clientReceive, TcpClientCompleteDelegate onCompleted)
Definition: TcpClient.h:53
bool connect(IpAddress addr, uint16_t port, bool useSsl=false) override
void onReadyToSendData(TcpConnectionEvent sourceEvent) override
void setCompleteDelegate(TcpClientCompleteDelegate completeCb=nullptr)
Set or clear the callback for connection close.
Definition: TcpClient.h:97
IDataSourceStream * stream
The currently active stream being sent.
Definition: TcpClient.h:165
Definition: TcpConnection.h:40
bool useSsl
Definition: TcpConnection.h:229
uint16_t timeOut
By default a TCP connection does not have a time out.
Definition: TcpConnection.h:224
TcpConnectionEvent
Definition: TcpConnection.h:26
@ eTCE_Poll
Definition: TcpConnection.h:30
TcpClientCloseAfterSentState
Definition: TcpClient.h:36
#define TCP_CLIENT_TIMEOUT
Definition: TcpClient.h:43
TcpClientState
Definition: TcpClient.h:28
@ eTCCASS_None
Definition: TcpClient.h:37
@ eTCCASS_AfterSent
Definition: TcpClient.h:38
@ eTCCASS_AfterSent_Ignore_Received
Definition: TcpClient.h:39
@ eTCS_Failed
Definition: TcpClient.h:33
@ eTCS_Successful
Definition: TcpClient.h:32
@ eTCS_Connecting
Definition: TcpClient.h:30
@ eTCS_Connected
Definition: TcpClient.h:31
@ eTCS_Ready
Definition: TcpClient.h:29
Server server