Void Sentry
An AI-driven, real-time Sentry Turret platform leveraging asynchronous I/O and computer vision to deliver high-precision autonomous motion tracking on resource-constrained embedded hardware.
Loading...
Searching...
No Matches
viewer_set.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <array>
11#include <cstddef>
12#include <cstdint>
13#include <optional>
14
15namespace frames
16{
17constexpr size_t kMaxViewers = 2;
18
20struct ReadyList {
21 std::array<int, kMaxViewers> fds{};
22 size_t count = 0;
23};
24
26{
27public:
29 bool add(int fd)
30 {
31 if (index_of(fd))
32 {
33 return true;
34 }
35 for (Viewer& v : viewers_)
36 {
37 if (!v.used)
38 {
39 v = Viewer{true, fd, false, 0};
40 return true;
41 }
42 }
43 return false;
44 }
45
46 void remove(int fd)
47 {
48 if (auto i = index_of(fd))
49 {
50 viewers_[*i] = Viewer{};
51 }
52 }
53
55 ReadyList ready_for(uint32_t sequence) const
56 {
57 ReadyList ready;
58 for (const Viewer& v : viewers_)
59 {
60 if (v.used && !v.busy && v.last_sequence_sent < sequence)
61 {
62 ready.fds[ready.count++] = v.fd;
63 }
64 }
65 return ready;
66 }
67
68 void mark_sending(int fd, uint32_t sequence)
69 {
70 if (auto i = index_of(fd))
71 {
72 viewers_[*i].busy = true;
73 viewers_[*i].last_sequence_sent = sequence;
74 }
75 }
76
77 void mark_done(int fd)
78 {
79 if (auto i = index_of(fd))
80 {
81 viewers_[*i].busy = false;
82 }
83 }
84
85 std::optional<size_t> index_of(int fd) const
86 {
87 for (size_t i = 0; i < kMaxViewers; ++i)
88 {
89 if (viewers_[i].used && viewers_[i].fd == fd)
90 {
91 return i;
92 }
93 }
94 return std::nullopt;
95 }
96
97 size_t size() const
98 {
99 size_t n = 0;
100 for (const Viewer& v : viewers_)
101 {
102 n += v.used ? 1 : 0;
103 }
104 return n;
105 }
106
107private:
108 struct Viewer {
109 bool used = false;
110 int fd = -1;
111 bool busy = false;
112 uint32_t last_sequence_sent = 0;
113 };
114
115 std::array<Viewer, kMaxViewers> viewers_{};
116};
117} // namespace frames
Definition viewer_set.h:26
ReadyList ready_for(uint32_t sequence) const
Viewers that are idle and have not yet been sent this sequence (or newer).
Definition viewer_set.h:55
bool add(int fd)
Add a viewer by socket fd. Adding an existing fd is a no-op. False when full.
Definition viewer_set.h:29
Fixed-capacity result so the stream path never allocates per frame.
Definition viewer_set.h:20