From 124bad3938781af69e45db4a42bf43c3a7d79fea Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 1 Apr 2024 00:04:06 +0400 Subject: [PATCH] A function net_set_timeout() was introduced. --- src/net.c | 14 ++++++++++++++ src/net.h | 3 +++ 2 files changed, 17 insertions(+) diff --git a/src/net.c b/src/net.c index 67646c2..5201d69 100644 --- a/src/net.c +++ b/src/net.c @@ -1,6 +1,7 @@ #include "net.h" #include +#include #include #include @@ -99,6 +100,19 @@ void net_destroy(net_t *n) { n->inaddr_len = sizeof(n->inaddr); } +bool net_set_timeout(net_t *const n, int usec) { + struct timeval timeout; + timeout.tv_sec = 0; + timeout.tv_usec = usec; + + if (setsockopt(n->fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) + return false; + if (setsockopt(n->fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0) + return false; + + return true; +} + ssize_t net_send(net_t *const n, const char *const buf, int buf_len) { if (n->fd == -1 || n->raddr.sa_family == AF_UNSPEC) return -1; diff --git a/src/net.h b/src/net.h index 6eaaf0b..8e4bcf2 100644 --- a/src/net.h +++ b/src/net.h @@ -1,6 +1,7 @@ #ifndef _NET_H_ #define _NET_H_ +#include #include typedef struct net_t { @@ -13,6 +14,8 @@ int net_client_init(net_t *const n, const char *const raddr, const char *const r int net_server_init(net_t *const n, const char *const laddr, const char *const lport); void net_destroy(net_t *n); +bool net_set_timeout(net_t *const n, int usec); + ssize_t net_send(net_t *const n, const char *const buf, int buf_len); ssize_t net_recv(net_t *const n, void *const buf, size_t buf_len);