summaryrefslogtreecommitdiff
path: root/src/socket
diff options
context:
space:
mode:
Diffstat (limited to 'src/socket')
-rw-r--r--src/socket/socket.h20
-rw-r--r--src/socket/socket_linux.c28
2 files changed, 36 insertions, 12 deletions
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 <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>
+#include <poll.h>
#include <sys/un.h>
+#include <sys/eventfd.h>
#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);
}
-