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 
111  {
112  return state == eTCS_Connected || state == eTCS_Connecting;
113  }
114 
116  {
117  return state;
118  }
119 
124  void setCloseAfterSent(bool ignoreIncomingData = false)
125  {
126  closeAfterSent = ignoreIncomingData ? eTCCASS_AfterSent_Ignore_Received : eTCCASS_AfterSent;
127  }
128 
133  void commit()
134  {
137  }
138 
139 protected:
140  err_t onConnected(err_t err) override;
141  err_t onReceive(pbuf* buf) override;
142  err_t onSent(uint16_t len) override;
143  void onError(err_t err) override;
144  void onClosed() override;
145 
146  void onReadyToSendData(TcpConnectionEvent sourceEvent) override;
147 
148  virtual void onFinished(TcpClientState finishState);
149 
150  void pushAsyncPart();
151  void freeStreams();
152 
153 protected:
155 
156  ReadWriteStream* buffer = nullptr;
157  IDataSourceStream* stream = nullptr;
158 
159 private:
160  TcpClientState state = eTCS_Ready;
161  TcpClientCompleteDelegate completed;
163  TcpClientDataDelegate receive;
164 
166  uint16_t totalSentConfirmedBytes = 0;
167  uint16_t totalSentBytes = 0;
168 };
169 
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:43
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:641
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:40
void close() override
void setBuffer(ReadWriteStream *stream)
Definition: TcpConnection.h:29
#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:25
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:237
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:133
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:110
bool useSsl
Definition: TcpConnection.h:225
ReadWriteStream * buffer
Used internally to buffer arbitrary data via send() methods.
Definition: TcpClient.h:156
Definition: TcpClient.h:32
TcpClientCloseAfterSentState
Definition: TcpClient.h:37
void setCloseAfterSent(bool ignoreIncomingData=false)
Definition: TcpClient.h:124
void freeStreams()
Definition: TcpClient.h:33
TcpClientState
Definition: TcpClient.h:29
IDataSourceStream * stream
The currently active stream being sent.
Definition: TcpClient.h:157
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:115