TcpClientStream.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  * TcpClientStream.h
8  *
9  * @author 2021 Slavey Karadzhov <slav@attachix.com>
10  *
11  *
12  ****/
13 
14 #pragma once
15 
16 #include <Network/TcpServer.h>
18 
19 namespace Hosted
20 {
21 namespace Transport
22 {
23 class TcpClientStream : public Stream
24 {
25 public:
26  TcpClientStream(TcpClient& client, size_t cbufferSize = 1024, size_t threshold = 400)
27  : cBuffer(cbufferSize), client(client), pendingBytes(0), threshold(threshold)
28  {
29  client.setReceiveDelegate(TcpClientDataDelegate(&TcpClientStream::store, this));
30  }
31 
32  void setClient(TcpClient& client)
33  {
34  this->client = client;
35  }
36 
37  bool push(const uint8_t* buffer, size_t size)
38  {
39  size_t written = cBuffer.write(buffer, size);
40  return (written == size);
41  }
42 
43  size_t readBytes(char* buffer, size_t length) override
44  {
45  return cBuffer.readBytes(buffer, length);
46  }
47 
48  size_t write(const uint8_t* buffer, size_t size) override
49  {
50  if(client.send(reinterpret_cast<const char*>(buffer), size)) {
51  pendingBytes += size;
52  if(pendingBytes > threshold) {
53  pendingBytes = 0;
54  client.commit();
55  }
56  return size;
57  }
58 
59  return 0;
60  }
61 
62  size_t write(uint8_t c) override
63  {
64  return cBuffer.write(c);
65  }
66 
67  int available() override
68  {
69  return cBuffer.available();
70  }
71 
72  int peek() override
73  {
74  return cBuffer.peek();
75  }
76 
77  int read() override
78  {
79  return cBuffer.read();
80  }
81 
82  void flush() override
83  {
84  client.commit();
85  }
86 
87 private:
88  CircularBuffer cBuffer;
89  TcpClient& client;
90  size_t pendingBytes;
91  size_t threshold;
92 
93  bool store(TcpClient& client, char* data, int size)
94  {
95  return push(reinterpret_cast<const uint8_t*>(data), size);
96  }
97 };
98 
99 } // namespace Transport
100 
101 } // namespace Hosted
Circular stream class.
Definition: CircularBuffer.h:29
int available() override
Return the total length of the stream.
size_t write(uint8_t charToWrite) override
Writes a single character to output stream.
Definition: TcpClientStream.h:24
size_t write(const uint8_t *buffer, size_t size) override
Writes characters from a buffer to output stream.
Definition: TcpClientStream.h:48
int available() override
Definition: TcpClientStream.h:67
int peek() override
Definition: TcpClientStream.h:72
void setClient(TcpClient &client)
Definition: TcpClientStream.h:32
size_t write(uint8_t c) override
Writes a single character to output stream.
Definition: TcpClientStream.h:62
TcpClientStream(TcpClient &client, size_t cbufferSize=1024, size_t threshold=400)
Definition: TcpClientStream.h:26
void flush() override
Definition: TcpClientStream.h:82
bool push(const uint8_t *buffer, size_t size)
Definition: TcpClientStream.h:37
size_t readBytes(char *buffer, size_t length) override
Read chars from stream into buffer.
Definition: TcpClientStream.h:43
int read() override
Definition: TcpClientStream.h:77
int peek() override
Read a character without advancing the stream pointer.
int read() override
Read one character and moves the stream pointer.
size_t readBytes(char *buffer, size_t length) override
Read chars from stream into buffer.
Base Stream class.
Definition: Wiring/Stream.h:33
Definition: TcpClient.h:46
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
void commit()
Tries to send the pending data immediately.
Definition: TcpClient.h:144
Delegate< bool(TcpClient &client, char *data, int size)> TcpClientDataDelegate
Definition: TcpClient.h:26
Definition: Components/Hosted/include/Hosted/Client.h:32