mirror of
https://github.com/open-goal/jak-project.git
synced 2024-10-20 11:26:18 -04:00
Extract adding timeout to socket
This commit is contained in:
parent
871f1b43b0
commit
4b6223a6d0
|
@ -51,6 +51,17 @@ int set_socket_option(int socket, int level, int optname, const char* optval, in
|
|||
#endif
|
||||
}
|
||||
|
||||
int set_socket_timeout(int socket, int microSeconds) {
|
||||
#ifdef __linux
|
||||
timeval timeout = {};
|
||||
timeout.tv_sec = 0;
|
||||
timeout.tv_usec = microSeconds;
|
||||
#elif _WIN32
|
||||
unsigned long timeout = microSeconds / 1000; // milliseconds
|
||||
#endif
|
||||
return set_socket_option(socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
|
||||
}
|
||||
|
||||
int write_to_socket(int socket, const char* buf, int len) {
|
||||
#ifdef __linux
|
||||
return write(socket, buf, len);
|
||||
|
|
|
@ -15,5 +15,6 @@ const int TCP_SOCKET_LEVEL = IPPROTO_TCP;
|
|||
int open_socket(int af, int type, int protocol);
|
||||
void close_socket(int sock);
|
||||
int set_socket_option(int socket, int level, int optname, const char* optval, int optlen);
|
||||
int set_socket_timeout(int socket, int microSeconds);
|
||||
int write_to_socket(int socket, const char* buf, int len);
|
||||
int read_from_socket(int socket, char* buf, int len);
|
||||
|
|
|
@ -74,24 +74,10 @@ bool Deci2Server::init() {
|
|||
return false;
|
||||
}
|
||||
|
||||
// TODO - put in library
|
||||
#ifdef __linux
|
||||
timeval timeout = {};
|
||||
timeout.tv_sec = 0;
|
||||
timeout.tv_usec = 100000;
|
||||
if (set_socket_option(server_socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout)) <
|
||||
0) {
|
||||
if (set_socket_timeout(server_socket, 100000) < 0) {
|
||||
close_server_socket();
|
||||
return false;
|
||||
}
|
||||
#elif _WIN32
|
||||
unsigned long timeout = 100; // ms
|
||||
if (set_socket_option(server_socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout)) <
|
||||
0) {
|
||||
close_server_socket();
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
|
|
|
@ -71,25 +71,12 @@ bool Listener::connect_to_target(const std::string& ip, int port) {
|
|||
return false;
|
||||
}
|
||||
|
||||
// TODO - put in library
|
||||
#ifdef __linux
|
||||
timeval timeout = {};
|
||||
timeout.tv_sec = 0;
|
||||
timeout.tv_usec = 100000;
|
||||
if (set_socket_option(socket_fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout)) < 0) {
|
||||
close_socket(socket_fd);
|
||||
socket_fd = -1;
|
||||
return false;
|
||||
}
|
||||
#elif _WIN32
|
||||
unsigned long timeout = 100; // ms
|
||||
if (set_socket_option(socket_fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout)) < 0) {
|
||||
if (set_socket_timeout(socket_fd, 100000) < 0) {
|
||||
printf("[Listener] setsockopt failed\n");
|
||||
close_socket(socket_fd);
|
||||
socket_fd = -1;
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
// set nodelay, which makes small rapid messages faster, but large messages slower
|
||||
char one = 1;
|
||||
|
|
Loading…
Reference in a new issue