From 842fed76fdb6762e10d1571255229a80c8505b04 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Sun, 31 Dec 2023 17:29:19 -0500 Subject: Update - Add aki_poll and aki_fs_event - Various aki_file improvments - Add grow mode to aki_packet_pool - aki_packet_stream_pause/resume -> cork - Misc fixes and improvments Signed-off-by: Andrew Opalach --- src/socket/socket.h | 20 ++++++++++++++------ src/socket/socket_linux.c | 28 ++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 12 deletions(-) (limited to 'src/socket') diff --git a/src/socket/socket.h b/src/socket/socket.h index 4971c1c..c0cf796 100644 --- a/src/socket/socket.h +++ b/src/socket/socket.h @@ -1,11 +1,14 @@ #pragma once #ifndef _WIN32 +#include #include #include #include #include +#include #include +#include #else #include "../winwrap.h" #endif @@ -34,15 +37,20 @@ struct aki_socket { #ifndef _WIN32 #define aki_read read #define aki_write write -#define aki_htons htons -#define aki_htonl htonl -#define aki_ntohs ntohs #else #define aki_read _read #define aki_write _write -#define aki_htons(...) 0 -#define aki_htonl(...) 0 -#define aki_ntohs(...) 0 +#endif + +#define aki_htons htons +#define aki_htonl htonl +#define aki_ntohs ntohs + +#ifndef _WIN32 // Posix-only helpers. +#define aki_pollfd pollfd +#define aki_nfds nfds_t +void aki_fd_set_blocking(s32 fd, bool blocking); +s32 aki_poll_fds(struct aki_pollfd *fds, aki_nfds nfds, s64 timeout_ns); #endif bool aki_socket_init(struct aki_socket *s); diff --git a/src/socket/socket_linux.c b/src/socket/socket_linux.c index 7bc9ea9..3d141b7 100644 --- a/src/socket/socket_linux.c +++ b/src/socket/socket_linux.c @@ -9,6 +9,27 @@ #include "socket.h" +void aki_fd_set_blocking(s32 fd, bool blocking) +{ + s32 flags = fcntl(fd, F_GETFL, 0); + al_assert(flags != -1); + flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK); + s32 ret = fcntl(fd, F_SETFL, flags); + al_assert(ret == 0); +} + +#define AKI_TIME_S_TO_NS(s) ((s) * INT64_C(1000000000)) + +// https://github.com/mpv-player/mpv/blob/e575ec4fc3654387c7358bd3640877ef32628d2c/osdep/poll_wrapper.c#L29 +s32 aki_poll_fds(struct aki_pollfd *fds, aki_nfds nfds, s64 timeout_ns) +{ + struct timespec ts; + ts.tv_sec = timeout_ns / AKI_TIME_S_TO_NS(1); + ts.tv_nsec = timeout_ns % AKI_TIME_S_TO_NS(1); + struct timespec *tsp = timeout_ns >= 0 ? &ts : NULL; + return ppoll(fds, nfds, tsp, NULL); +} + bool aki_socket_init(struct aki_socket *s) { switch (s->type) { @@ -33,11 +54,7 @@ bool aki_socket_init(struct aki_socket *s) void aki_socket_set_blocking(struct aki_socket *s, bool blocking) { - s32 flags = fcntl(s->sock, F_GETFL, 0); - al_assert(flags != -1); - flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK); - s32 ret = fcntl(s->sock, F_SETFL, flags); - al_assert(ret == 0); + aki_fd_set_blocking(s->sock, blocking); } void aki_socket_set_no_delay(struct aki_socket *s, s32 no_delay) @@ -251,4 +268,3 @@ void aki_socket_close(struct aki_socket *s) { close(s->sock); } - -- cgit v1.2.3-101-g0448