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 ReadWriteStream;
23 class IpAddress;
24 
28 
35 };
36 
41 };
42 
43 // By default a TCP client connection has 70 seconds timeout
44 #define TCP_CLIENT_TIMEOUT 70
45 
46 class TcpClient : public TcpConnection
47 {
48 public:
49  TcpClient(bool autoDestruct) : TcpConnection(autoDestruct)
50  {
52  }
53 
54  TcpClient(tcp_pcb* clientTcp, TcpClientDataDelegate clientReceive, TcpClientCompleteDelegate onCompleted)
55  : TcpConnection(clientTcp, true), state(eTCS_Connected), completed(onCompleted), receive(clientReceive)
56  {
58  }
59 
62  : TcpConnection(false), completed(onCompleted), ready(onReadyToSend), receive(onReceive)
63  {
65  }
66 
68  : TcpConnection(false), completed(onCompleted), receive(onReceive)
69  {
71  }
72 
73  explicit TcpClient(TcpClientDataDelegate onReceive) : TcpConnection(false), receive(onReceive)
74  {
76  }
77 
79  {
80  freeStreams();
81  }
82 
83  bool connect(const String& server, int port, bool useSsl = false) override;
84  bool connect(IpAddress addr, uint16_t port, bool useSsl = false) override;
85  void close() override;
86 
90  void setReceiveDelegate(TcpClientDataDelegate receiveCb = nullptr)
91  {
92  receive = receiveCb;
93  }
94 
99  {
100  completed = completeCb;
101  }
102 
103  bool send(const char* data, uint16_t len, bool forceCloseAfterSent = false);
104 
105  bool sendString(const String& data, bool forceCloseAfterSent = false)
106  {
107  return send(data.c_str(), data.length(), forceCloseAfterSent);
108  }
109 
120  bool send(IDataSourceStream* source, bool forceCloseAfterSent = false);
121 
123  {
124  return state == eTCS_Connected || state == eTCS_Connecting;
125  }
126 
128  {
129  return state;
130  }
131 
136  void setCloseAfterSent(bool ignoreIncomingData = false)
137  {
138  closeAfterSent = ignoreIncomingData ? eTCCASS_AfterSent_Ignore_Received : eTCCASS_AfterSent;
139  }
140 
145  void commit()
146  {
149  }
150 
151 protected:
152  err_t onConnected(err_t err) override;
153  err_t onReceive(pbuf* buf) override;
154  err_t onSent(uint16_t len) override;
155  void onError(err_t err) override;
156  void onClosed() override;
157 
158  void onReadyToSendData(TcpConnectionEvent sourceEvent) override;
159 
160  virtual void onFinished(TcpClientState finishState);
161 
162  void pushAsyncPart();
163  void freeStreams();
164 
165 protected:
167 
168 private:
169  TcpClientState state = eTCS_Ready;
170  TcpClientCompleteDelegate completed;
172  TcpClientDataDelegate receive;
173 
175  uint16_t totalSentConfirmedBytes = 0;
176  uint16_t totalSentBytes = 0;
177 };
178 
void setReceiveDelegate(TcpClientDataDelegate receiveCb=nullptr)
Set or clear the callback for received data.
Definition: TcpClient.h:90
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:44
uint16_t timeOut
By default a TCP connection does not have a time out.
Definition: TcpConnection.h:220
Definition: TcpClient.h:30
~TcpClient()
Definition: TcpClient.h:78
void pushAsyncPart()
Definition: TcpClient.h:31
TcpClient(tcp_pcb *clientTcp, TcpClientDataDelegate clientReceive, TcpClientCompleteDelegate onCompleted)
Definition: TcpClient.h:54
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
TcpClient(TcpClientDataDelegate onReceive)
Definition: TcpClient.h:73
void setCompleteDelegate(TcpClientCompleteDelegate completeCb=nullptr)
Set or clear the callback for connection close.
Definition: TcpClient.h:98
Base class for read-only stream.
Definition: DataSourceStream.h:45
void close() override
Definition: TcpConnection.h:30
#define TCP_CLIENT_TIMEOUT
Definition: TcpClient.h:44
Definition: TcpConnection.h:39
The String class.
Definition: WString.h:136
void onReadyToSendData(TcpConnectionEvent sourceEvent) override
Definition: TcpClient.h:46
TcpConnectionEvent
Definition: TcpConnection.h:26
Definition: TcpClient.h:39
Server server
bool send(const char *data, uint16_t len, bool forceCloseAfterSent=false)
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:243
bool sendString(const String &data, bool forceCloseAfterSent=false)
Definition: TcpClient.h:105
void onClosed() override
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.
err_t onReceive(pbuf *buf) override
void commit()
Tries to send the pending data immediately.
Definition: TcpClient.h:145
err_t onSent(uint16_t len) override
TcpClient(TcpClientCompleteDelegate onCompleted, TcpClientDataDelegate onReceive=nullptr)
Definition: TcpClient.h:67
Definition: TcpClient.h:40
virtual void onFinished(TcpClientState finishState)
Definition: TcpClient.h:38
TcpClient(bool autoDestruct)
Definition: TcpClient.h:49
void onError(err_t err) override
bool isProcessing()
Definition: TcpClient.h:122
bool useSsl
Definition: TcpConnection.h:225
Definition: TcpClient.h:32
TcpClientCloseAfterSentState
Definition: TcpClient.h:37
void setCloseAfterSent(bool ignoreIncomingData=false)
Definition: TcpClient.h:136
void freeStreams()
Definition: TcpClient.h:33
TcpClientState
Definition: TcpClient.h:29
IDataSourceStream * stream
The currently active stream being sent.
Definition: TcpClient.h:166
Definition: TcpClient.h:34
Base class for read/write stream.
Definition: ReadWriteStream.h:19
bool connect(const String &server, int port, bool useSsl=false) override
err_t onConnected(err_t err) override
TcpClient(TcpClientCompleteDelegate onCompleted, TcpClientEventDelegate onReadyToSend, TcpClientDataDelegate onReceive=nullptr)
Definition: TcpClient.h:60
TcpClientState getConnectionState()
Definition: TcpClient.h:127