From 49948b4cc0f73d02a8932c525690a35e8efb6ac5 Mon Sep 17 00:00:00 2001 From: Lizzy Fleckenstein Date: Wed, 19 Jun 2024 18:07:47 +0200 Subject: add client Signed-off-by: Lizzy Fleckenstein --- include/net.h | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/peer.h | 41 ----------------------------------------- include/ticker.h | 23 +++++++++++++++++++++++ 3 files changed, 74 insertions(+), 41 deletions(-) create mode 100644 include/net.h delete mode 100644 include/peer.h create mode 100644 include/ticker.h (limited to 'include') diff --git a/include/net.h b/include/net.h new file mode 100644 index 0000000..fcb12a4 --- /dev/null +++ b/include/net.h @@ -0,0 +1,51 @@ +// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +#ifndef NET_H +#define NET_H + +#include +#include +#include +#include +#include + +#include "str.h" + +#define SET_NONBLOCK(X) fcntl((X), F_SETFL, fcntl((X), F_GETFL, 0) | O_NONBLOCK) + +#define PEER_INBUFFER_SIZE 0x100000 // 1MB +#define PEER_OUTBUFFER_SIZE 0x200000 // 2MB + +typedef uint32_t pkt_header; + +typedef struct { + int socket; + bool disco; + struct { + bool header; + size_t len; + size_t promised; + uint8_t *buffer; + } in; + // TODO: ring buffer + struct { + size_t avail; + size_t cursor; + uint8_t *buffer; + } out; +} peer; + +void invalid_pkt(str from, str pkt); + +int socket_create(const char *host, const char *port, bool server); +int socket_accept(int accept_fd); + +void peer_init(peer *p, int socket); +void peer_free(peer *p); +struct pollfd peer_prepare(peer *p); +str peer_recv(peer *p, struct pollfd pfd); +bool peer_send(peer *p, void *data, size_t len); + +#endif diff --git a/include/peer.h b/include/peer.h deleted file mode 100644 index f6baa4b..0000000 --- a/include/peer.h +++ /dev/null @@ -1,41 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein -// -// SPDX-License-Identifier: AGPL-3.0-or-later - -#ifndef PEER_H -#define PEER_H - -#include -#include -#include -#include - -#define PEER_INBUFFER_SIZE 0x100000 // 1MB -#define PEER_OUTBUFFER_SIZE 0x200000 // 2MB - -typedef uint32_t pkt_header; - -typedef struct { - int socket; - bool disco; - struct { - bool header; - size_t len; - size_t promised; - uint8_t *buffer; - } in; - // TODO: ring buffer - struct { - size_t avail; - size_t cursor; - uint8_t *buffer; - } out; -} peer; - -void peer_init(peer *p, int socket); -void peer_free(peer *p); -struct pollfd peer_prepare(peer *p); -bool peer_ready(peer *p, struct pollfd revents); -bool peer_send(peer *p, void *data, size_t len); - -#endif diff --git a/include/ticker.h b/include/ticker.h new file mode 100644 index 0000000..f6387c4 --- /dev/null +++ b/include/ticker.h @@ -0,0 +1,23 @@ +// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +#ifndef TICKER_H +#define TICKER_H + +#include +#include +#include + +#define NANOS 1000000000 + +typedef struct { + struct timespec timestamp; + uint64_t freq_nanos; +} ticker; + +void ticker_init(ticker *t, uint64_t f); +bool ticker_tick(ticker *t, uint64_t *dtime); +int ticker_timeout(ticker *t); + +#endif -- cgit v1.2.3