From 72e7559c68c78345c0d59d1207f40b569bfbd3b9 Mon Sep 17 00:00:00 2001 From: sotech117 Date: Sun, 24 Sep 2023 00:11:53 +0000 Subject: pass all control tests! --- protocol.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'protocol.c') diff --git a/protocol.c b/protocol.c index d350221..5c8127f 100644 --- a/protocol.c +++ b/protocol.c @@ -3,13 +3,13 @@ #include "protocol.h" -#define TIMEOUT 100000 // 100ms in microseconds +#define TCP_TIMEOUT 100000 // 100ms in microseconds int send_all(int sock, char *buf, int *len) { struct timeval timeout; timeout.tv_sec = 0; - timeout.tv_usec = 100000; + timeout.tv_usec = TCP_TIMEOUT; // if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, // sizeof timeout) < 0) // perror("setsockopt failed\n"); @@ -35,7 +35,7 @@ int recv_all(int sock, char *buf, int *len) // setup the timeout on the socket struct timeval timeout; timeout.tv_sec = 0; - timeout.tv_usec = TIMEOUT; + timeout.tv_usec = TCP_TIMEOUT; if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout) < 0) perror("setsockopt failed\n"); @@ -64,3 +64,30 @@ int recv_all(int sock, char *buf, int *len) return n==-1?-1:0; // return -1 on failure, 0 on success } + +int apply_timeout(int fd) { + // handle handshake + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = TCP_TIMEOUT; + if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) { + perror("setsockopt (in apply_timeout)"); + return -1; + } + + return 1; +} + +int remove_timeout(int fd) +{ + // handle handshake + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 0; + if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) { + perror("setsockopt (in remove_timeout)"); + return -1; + } + + return 1; +} \ No newline at end of file -- cgit v1.2.3-70-g09d2