-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.c
48 lines (36 loc) · 848 Bytes
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "compat-header.h"
#include "utils.h"
int UtilsSetTimeouts(SOCKET Socket, uint32_t Miliseconds)
{
int ret = 0;
#ifdef _WIN32
uint32_t timeout;
#else
struct timeval timeout;
#endif
memset(&timeout, 0, sizeof(timeout));
#ifdef _WIN32
timeout = Miliseconds;
#else
timeout.tv_sec = Miliseconds / 1000;
timeout.tv_usec = (Miliseconds * 1000) % 1000000;
#endif
ret = setsockopt(Socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
if (ret != 0) {
ret = errno;
goto Exit;
}
ret = setsockopt(Socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
if (ret != 0) {
ret = errno;
goto Exit;
}
Exit:
return ret;
}
int UtilsSetKeepAlive(SOCKET Socket, int KeepAlive)
{
int ret = 0;
ret = setsockopt(Socket, SOL_SOCKET, SO_KEEPALIVE, (char *)&KeepAlive, sizeof(KeepAlive));
return ret;
}