diff options
author | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2024-06-19 18:07:47 +0200 |
---|---|---|
committer | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2024-06-19 18:07:55 +0200 |
commit | 49948b4cc0f73d02a8932c525690a35e8efb6ac5 (patch) | |
tree | 1ee33d5393046939a37fdef00b3484e87ff3ec6d /include/net.h | |
parent | ead2881be92d33076c2104dbd75bad3561f26088 (diff) | |
download | silly_game-49948b4cc0f73d02a8932c525690a35e8efb6ac5.tar.xz |
add client
Signed-off-by: Lizzy Fleckenstein <lizzy@vlhl.dev>
Diffstat (limited to 'include/net.h')
-rw-r--r-- | include/net.h | 51 |
1 files changed, 51 insertions, 0 deletions
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 <lizzy@vlhl.dev> +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +#ifndef NET_H +#define NET_H + +#include <stdint.h> +#include <stdbool.h> +#include <stddef.h> +#include <poll.h> +#include <fcntl.h> + +#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 |