roo_transport
API Documentation for roo_transport
Loading...
Searching...
No Matches
receiver.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4
7
8namespace roo_transport {
9namespace internal {
10
11class Receiver {
12 public:
13 enum State {
14 // Connect was not locally called; no handshake shall be initialized.
15 kIdle = 0,
16
17 // Indicates that the connect has been called but we have not yet received
18 // the the peer's stream ID and seq.
20
21 // Indicates that we received the peer's stream ID and seq, allowing us
22 // to receive messages from it.
24
25 // Indicates that peer has abruptly terminated a previously valid
26 // connection.
28 };
29
30 Receiver(unsigned int recvbuf_log2);
31
32 State state() const { return state_; }
33 bool eos() const { return end_of_stream_; }
34
35 bool done() const;
36
37 void setConnected(SeqNum peer_seq_num, bool control_bit);
38 void setIdle();
39 void setBroken();
40
41 size_t tryRead(roo::byte* buf, size_t count, bool& outgoing_data_ready);
42
43 int peek();
44 size_t availableForRead() const;
45
46 void reset();
47 void init(uint32_t my_stream_id);
48
49 void markInputClosed(bool& outgoing_data_ready);
50
51 size_t ack(roo::byte* buf);
52 size_t updateRecvHimark(roo::byte* buf, long& next_send_micros);
53
54 bool handleDataPacket(bool control_bit, uint16_t seq_id,
55 const roo::byte* payload, size_t len, bool is_final,
56 bool& has_new_data_to_read);
57
58 bool empty() const { return in_ring_.empty(); }
59
60 uint32_t packets_received() const { return packets_received_; }
61
62 uint32_t my_stream_id() const { return my_stream_id_; }
63
64 // Used to communicate maximum offset of the recv himark to the sender.
65 unsigned int buffer_size_log2() const { return in_ring_.capacity_log2(); }
66
67 private:
68 InBuffer& getInBuffer(SeqNum seq) const {
69 return in_buffers_[in_ring_.offset_for(seq)];
70 }
71
72 uint32_t my_stream_id_;
73 State state_;
74
75 // Set when the input stream is closed on this process, indicating that the
76 // reader is not interested in the rest of the data. We will silently read and
77 // ignore it.
78 bool self_closed_;
79
80 // Set when we receive 'end of stream' notification (kFin packet) from the
81 // peer, indicating that there will be no more data to come after that final
82 // packet.
83 bool peer_closed_;
84
85 // Set when the stream is read till end without error.
86 bool end_of_stream_;
87
88 std::unique_ptr<InBuffer[]> in_buffers_;
89 mutable InBuffer* current_in_buffer_;
90 mutable uint8_t current_in_buffer_pos_;
91 RingBuffer in_ring_;
92
93 // Whether we need to send kDataAckPacket.
94 bool needs_ack_;
95
96 // Newest unacked seq ID.
97 uint16_t unack_seq_;
98
99 // The seq ID past the maximum we're able to receive. We track it on the
100 // receiver so that we can decide if it needs to be retransmitted if we
101 // don't see new data packets.
102 SeqNum recv_himark_;
103
104 // The deadline by which we will (re)send the recv-himark update (by means
105 // of a kControlFlotPacket), unless we receive evidence that the up-to-date
106 // one was delivered (by means of a data packet with a higher seq). When
107 // some receive buffers get freed, we reset it so that the update is sent
108 // immediately.
109 roo_time::Uptime recv_himark_update_expiration_;
110
111 uint32_t packets_received_;
112
113 bool control_bit_;
114};
115
116} // namespace internal
117} // namespace roo_transport
size_t tryRead(roo::byte *buf, size_t count, bool &outgoing_data_ready)
Definition receiver.cpp:54
void setConnected(SeqNum peer_seq_num, bool control_bit)
Definition receiver.cpp:25
size_t updateRecvHimark(roo::byte *buf, long &next_send_micros)
Definition receiver.cpp:174
void markInputClosed(bool &outgoing_data_ready)
Definition receiver.cpp:101
bool handleDataPacket(bool control_bit, uint16_t seq_id, const roo::byte *payload, size_t len, bool is_final, bool &has_new_data_to_read)
Definition receiver.cpp:231
uint32_t packets_received() const
Definition receiver.h:60
void init(uint32_t my_stream_id)
Definition receiver.cpp:163
unsigned int buffer_size_log2() const
Definition receiver.h:65
size_t ack(roo::byte *buf)
Definition receiver.cpp:195
uint32_t my_stream_id() const
Definition receiver.h:62
uint16_t offset_for(SeqNum seq) const
Definition ring_buffer.h:49