1
0
Fork 0

A function net_set_timeout() was introduced.

This commit is contained in:
Alexander Andreev 2024-04-01 00:04:06 +04:00
parent 67513cc729
commit 124bad3938
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
2 changed files with 17 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#include "net.h"
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <netdb.h>
@ -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;

View File

@ -1,6 +1,7 @@
#ifndef _NET_H_
#define _NET_H_
#include <stdbool.h>
#include <netinet/in.h>
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);