13 public Sink<Buffer<uint8_t>>,
14 public Source<Buffer<uint8_t>> {
16 static constexpr size_t BLEN = 512;
20 TTY (
const char *path) {
21 fd = open(path, O_RDWR | O_NONBLOCK);
31 tx.
push(std::move(b));
35 int l = read(fd, wrk.buf, wrk.
size);
37 if (errno != EAGAIN) {
38 perror(
"reading on fd error");
43 rx.
push(std::move(wrk));
53 auto msg = tx.
front();
54 int l = write(fd, msg.buf, msg.len);
56 if (errno != EAGAIN) {
57 perror(
"writing to fd error");
60 }
else if ((
size_t)l < msg.len) {
61 for (
size_t i = 0; i < msg.len-l; i++){
62 msg.buf[i] = msg.buf[i+l];
77 public Sink<Buffer<uint8_t>>,
78 public Source<Buffer<uint8_t>> {
81 struct sockaddr addr {};
82 socklen_t len {
sizeof (
struct sockaddr)};
84 static constexpr size_t BLEN = 128;
88 UDP (
const char *ip, uint16_t port) {
89 struct sockaddr_in addr = {
90 .sin_family = AF_INET,
91 .sin_port = htons(port),
93 inet_pton(AF_INET, ip, &addr.sin_addr);
95 fd = socket(PF_INET, SOCK_DGRAM, 0);
96 if (bind(fd, (sockaddr*)&addr,
sizeof addr) == -1 ) {
97 perror(
"cannot bind socket to address");
100 if (fcntl(fd, F_SETFL,
101 fcntl(fd, F_GETFL, 0) | O_NONBLOCK) == -1) {
102 perror(
"cannot set O_NONBLOCK on socket");
114 tx.
push(std::move(b));
118 int l = recvfrom(fd, wrk.buf, wrk.
size, 0,
119 &peer.addr, &peer.len);
121 if (errno != EAGAIN) {
122 perror(
"reading on socket error");
127 rx.
push(std::move(wrk));
136 while (!tx.
empty()) {
137 auto msg = tx.
front();
138 int l = sendto(fd, msg.buf, msg.len, 0,
139 &peer.addr, peer.len);
141 if (errno != EAGAIN) {
142 perror(
"writing to socket error");
145 }
else if ((
size_t)l < msg.len) {
146 for (
size_t i = 0; i < msg.len-l; i++){
147 msg.buf[i] = msg.buf[i+l];
simple Buffer backed queue implementation
Definition queue.h:12
T pop() override
remove front of queue and return it
Definition queue.h:65
bool empty() override
check if queue is empty
Definition queue.h:83
bool full() override
return true if queue is full
Definition queue.h:87
void push(T &&val) override
move element into queue
Definition queue.h:49
T & front()
return reference to first element in queue
Definition queue.h:55
dynamically allocated, but fixed-size buffer template
Definition buffer.h:18
size_t size
total capacity of buffer
Definition buffer.h:23
size_t len
number of items stored in buffer
Definition buffer.h:21
simple wrapper around TTY & Logger useful for creating simple user interactions
Definition comm.h:160
simple printf-style logging infrastructure
Definition logger.h:21
generic object sink, i.e.
Definition streams.h:15
virtual void push(const T &t)
copy semantics does not check for space. guard by using 'if (!full) { ... }'
Definition streams.h:20
generic object source, i.e.
Definition streams.h:40
TTY communication backend.
Definition comm.h:14
Buffer< uint8_t > pop() override
pull object from source does not check for data. guard by using 'if (!empty) { ......
Definition comm.h:47
TTY(const char *path)
open given path
Definition comm.h:20
void push(Buffer< uint8_t > &&b) override
move semantics does not check for space. guard by using 'if (!full) { ... }'
Definition comm.h:30
bool empty() override
check if source is empty
Definition comm.h:34
bool full() override
check if sink is full
Definition comm.h:26
UDP Server communication backend.
Definition comm.h:78
bool empty() override
check if source is empty
Definition comm.h:117
UDP(const char *ip, uint16_t port)
create UDP Server listening on ip/port
Definition comm.h:88
void push(Buffer< uint8_t > &&b) override
move semantics does not check for space. guard by using 'if (!full) { ... }'
Definition comm.h:113
Buffer< uint8_t > pop() override
pull object from source does not check for data. guard by using 'if (!empty) { ......
Definition comm.h:131
bool full() override
check if sink is full
Definition comm.h:109