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 
129 protected:
130  err_t onConnected(err_t err) override;
131  err_t onReceive(pbuf* buf) override;
132  err_t onSent(uint16_t len) override;
133  void onError(err_t err) override;
134  void onReadyToSendData(TcpConnectionEvent sourceEvent) override;
135 
136  virtual void onFinished(TcpClientState finishState);
137 
138  void pushAsyncPart();
139  void freeStreams();
140 
141 protected:
143 
144  ReadWriteStream* buffer = nullptr;
145  IDataSourceStream* stream = nullptr;
146 
147 private:
148  TcpClientState state = eTCS_Ready;
149  TcpClientCompleteDelegate completed;
151  TcpClientDataDelegate receive;
152 
154  uint16_t totalSentConfirmedBytes = 0;
155  uint16_t totalSentBytes = 0;
156 };
157 
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:199
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:600
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 data source stream.
Definition: DataSourceStream.h:39
void close() override
void setBuffer(ReadWriteStream *stream)
Delegate< void(TcpClient &client, TcpConnectionEvent sourceEvent)> TcpClientEventDelegate
Definition: TcpClient.h:23
#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
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:228
bool sendString(const String &data, bool forceCloseAfterSent=false)
Definition: TcpClient.h:105
Delegate< bool(TcpClient &client, char *data, int size)> TcpClientDataDelegate
Definition: TcpClient.h:27
err_t onReceive(pbuf *buf) override
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
Delegate< void(TcpClient &client, bool successful)> TcpClientCompleteDelegate
Definition: TcpClient.h:26
bool useSsl
Definition: TcpConnection.h:204
ReadWriteStream * buffer
Used internally to buffer arbitrary data via send() methods.
Definition: TcpClient.h:144
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:145
Definition: TcpClient.h:34
Base class for read/write stream.
Definition: ReadWriteStream.h:22
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