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::Transport
20 {
21 class TcpClientStream : public Stream
22 {
23 public:
24  TcpClientStream(TcpClient& client, size_t cbufferSize = 1024, size_t threshold = 400)
25  : cBuffer(cbufferSize), client(client), threshold(threshold)
26  {
27  client.setReceiveDelegate(TcpClientDataDelegate(&TcpClientStream::store, this));
28  }
29 
30  void setClient(TcpClient& client)
31  {
32  this->client = client;
33  }
34 
35  bool push(const uint8_t* buffer, size_t size)
36  {
37  size_t written = cBuffer.write(buffer, size);
38  return (written == size);
39  }
40 
41  size_t readBytes(char* buffer, size_t length) override
42  {
43  return cBuffer.readBytes(buffer, length);
44  }
45 
46  size_t write(const uint8_t* buffer, size_t size) override
47  {
48  if(client.send(reinterpret_cast<const char*>(buffer), size)) {
49  pendingBytes += size;
50  if(pendingBytes > threshold) {
51  pendingBytes = 0;
52  client.commit();
53  }
54  return size;
55  }
56 
57  return 0;
58  }
59 
60  size_t write(uint8_t c) override
61  {
62  return cBuffer.write(c);
63  }
64 
65  int available() override
66  {
67  return cBuffer.available();
68  }
69 
70  int peek() override
71  {
72  return cBuffer.peek();
73  }
74 
75  int read() override
76  {
77  return cBuffer.read();
78  }
79 
80  void flush() override
81  {
82  client.commit();
83  }
84 
85 private:
86  CircularBuffer cBuffer;
87  TcpClient& client;
88  size_t pendingBytes{0};
89  size_t threshold;
90 
91  bool store(TcpClient& client, char* data, int size)
92  {
93  return push(reinterpret_cast<const uint8_t*>(data), size);
94  }
95 };
96 
97 } // namespace Hosted::Transport
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:22
size_t write(const uint8_t *buffer, size_t size) override
Writes characters from a buffer to output stream.
Definition: TcpClientStream.h:46
int available() override
Definition: TcpClientStream.h:65
int peek() override
Definition: TcpClientStream.h:70
void setClient(TcpClient &client)
Definition: TcpClientStream.h:30
size_t write(uint8_t c) override
Writes a single character to output stream.
Definition: TcpClientStream.h:60
TcpClientStream(TcpClient &client, size_t cbufferSize=1024, size_t threshold=400)
Definition: TcpClientStream.h:24
void flush() override
Definition: TcpClientStream.h:80
bool push(const uint8_t *buffer, size_t size)
Definition: TcpClientStream.h:35
size_t readBytes(char *buffer, size_t length) override
Read chars from stream into buffer.
Definition: TcpClientStream.h:41
int read() override
Definition: TcpClientStream.h:75
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: BaseTransport.h:20