roo_transport
API Documentation for roo_transport
Loading...
Searching...
No Matches
link_stream_transport.h
Go to the documentation of this file.
1#pragma once
2
3#if (defined ARDUINO)
4
5#include "Arduino.h"
6#include "roo_io.h"
7#include "roo_io/stream/arduino/stream_input_stream.h"
8#include "roo_io/stream/arduino/stream_output_stream.h"
9#include "roo_transport.h"
14
15namespace roo_transport {
16
17// Generic implementation of the link stream transport, over an arbitrary
18// Arduino Stream. It uses default Arduino APIs to read from and to write to the
19// stream. The user needs to keep calling tryReceive() or receive() to process
20// incoming packets.
21//
22// The class does not provide a stream interface itself. Instead, use LinkStream
23// returned by connect() or connectAsync() methods to read and write data over
24// the reliable link.
25class LinkStreamTransport {
26 public:
27 LinkStreamTransport(Stream& stream, LinkBufferSize sendbuf = kBufferSize4KB,
28 LinkBufferSize recvbuf = kBufferSize4KB);
29
30 void begin();
31
32 // Establishes a new connection and returns the LinkStream object representing
33 // it. The optional function parameter will be called when the link gets
34 // disconnected.
35 LinkStream connect(std::function<void()> disconnect_fn = nullptr);
36
37 // Establishes a new connection asynchronously and returns the LinkStream
38 // object representing it. Until the connection is established, the link will
39 // be in the "connecting" state. The optional function parameter will be
40 // called when the link gets disconnected.
41 LinkStream connectAsync(std::function<void()> disconnect_fn = nullptr);
42
43 // Establishes a new connection and returns the LinkStream object representing
44 // it. If the peer attempts reconnection (e.g. after a reset), the program
45 // will terminate (usually to reconnect after reboot).
46 LinkStream connectOrDie();
47
48 LinkTransport& transport() { return transport_; }
49
50 // Allow implicit conversion to LinkTransport&, so that this Arduino wrapper
51 // can be used seamlessly in place of LinkTransport when a reference to the
52 // latter is needed (e.g., when constructing LinkMessaging).
53 operator LinkTransport&() { return transport_; }
54
55 // Attempts to receive one or more packets without blocking. Returns the
56 // number of packets received.
57 size_t tryReceive();
58
59 // Attempts to receive one or more packets, blocking if necessary until at
60 // least one packet is received. Returns the number of packets received.
61 size_t receive();
62
63 LinkTransport::StatsMonitor statsMonitor() {
64 return LinkTransport::StatsMonitor(transport_);
65 }
66
67 private:
68 Stream& stream_;
69 roo_io::ArduinoStreamOutputStream output_;
70 roo_io::ArduinoStreamInputStream input_;
71 PacketSenderOverStream sender_;
72 PacketReceiverOverStream receiver_;
73
74 LinkTransport transport_;
75};
76
77} // namespace roo_transport
78
79#endif // defined(ARDUINO)