roo_transport
API Documentation for roo_transport
Loading...
Searching...
No Matches
out_buffer.h
Go to the documentation of this file.
1#pragma once
2
3#include "roo_backport.h"
4#include "roo_backport/byte.h"
6#include "roo_logging.h"
7
8namespace roo_transport {
9namespace internal {
10
11class OutBuffer {
12 public:
14 : size_(0),
15 acked_(false),
16 flushed_(false),
17 finished_(false),
18 final_(false),
19 expiration_(roo_time::Uptime::Start()),
20 send_counter_(0) {}
21
22 void init(SeqNum seq_id, bool control_bit);
23
24 bool flushed() const { return flushed_; }
25 bool finished() const { return finished_; }
26 bool acked() const { return acked_; }
27
28 size_t write(const roo::byte* buf, size_t count) {
29 if (finished_) return 0;
30 size_t capacity = 248 - size_;
31 CHECK_GT(capacity, size_t{0});
32 if (count >= capacity) {
33 count = capacity;
34 flushed_ = true;
35 finished_ = true;
36 expiration_ = roo_time::Uptime::Start();
37 }
38 memcpy(payload_ + size_ + 2, buf, count);
39 size_ += count;
40 return count;
41 }
42
43 void flush() { flushed_ = true; }
44
45 void finish() {
46 flushed_ = true;
47 finished_ = true;
48 expiration_ = roo_time::Uptime::Start();
49 }
50
51 void markFinal();
52
53 void ack() { acked_ = true; }
54
55 const roo::byte* data() const { return payload_; }
56 const uint8_t size() const { return size_ + 2; }
57
58 roo_time::Uptime expiration() const { return expiration_; }
59
60 void markSent(roo_time::Uptime now);
61
62 // Updates the timeout of the (already sent) packet to be retransmitted
63 // immediately.
64 void rush() {
65 expiration_ = roo_time::Uptime::Start();
66 CHECK_GT(send_counter_, 0);
67 }
68
69 // How many times the packet has been already sent.
70 uint8_t send_counter() const { return send_counter_; }
71
72 private:
73 uint8_t size_;
74 bool acked_;
75
76 // Indicates that flush has been requested for this buffer, and therefore,
77 // the send loop should transmit it even if it has some more space left.
78
79 bool flushed_;
80 // Indicates that no more writes are permitted for this buffer, either
81 // because it is already full, or because it has already been transmitted.
82
83 bool finished_;
84 // Leave two front bytes for the header (incl. seq number).
85
86 // Indicates that this is an 'end-of-stream' packet.
87 bool final_;
88
89 roo::byte payload_[250];
90
91 // Set when sent, to indicate when the packet is due for retransmission.
92 roo_time::Uptime expiration_;
93
94 uint8_t send_counter_;
95};
96
97} // namespace internal
98} // namespace roo_transport
const roo::byte * data() const
Definition out_buffer.h:55
void init(SeqNum seq_id, bool control_bit)
size_t write(const roo::byte *buf, size_t count)
Definition out_buffer.h:28
const uint8_t size() const
Definition out_buffer.h:56
roo_time::Uptime expiration() const
Definition out_buffer.h:58
void markSent(roo_time::Uptime now)