Skip to content

Commit

Permalink
Set socket timeout: Remove ifdef and move to psmove_port
Browse files Browse the repository at this point in the history
  • Loading branch information
thp committed May 21, 2016
1 parent f46e3f0 commit 6a12b87
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
18 changes: 1 addition & 17 deletions src/daemon/moved_client.c
Expand Up @@ -102,23 +102,7 @@ moved_client_create(const char *hostname)
client->socket = (int)socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
assert(client->socket != -1);

/**
* The receiving socket must have a timeout to not block indefinitely
*
* With Berkeley sockets, SO_RCVTIMEO takes a struct timeval, whereas
* Microsoft's WinSock takes a DWORD containing a milliseconds value.
**/
#ifdef _WIN32
DWORD receive_timeout = MOVED_TIMEOUT_MS;
#else
struct timeval receive_timeout = {
.tv_sec = MOVED_TIMEOUT_MS / 1000,
.tv_usec = (MOVED_TIMEOUT_MS % 1000) * 1000,
};
#endif
int result = setsockopt(client->socket, SOL_SOCKET, SO_RCVTIMEO,
(char*)&receive_timeout, sizeof(receive_timeout));
assert(result == 0);
psmove_port_set_socket_timeout_ms(client->socket, MOVED_TIMEOUT_MS);

client->moved_addr.sin_family = AF_INET;
client->moved_addr.sin_port = htons(MOVED_UDP_PORT);
Expand Down
11 changes: 11 additions & 0 deletions src/platform/psmove_port_linux.c
Expand Up @@ -28,6 +28,7 @@


#include "psmove_port.h"
#include "psmove_sockets.h"

#include <stdlib.h>
#include <unistd.h>
Expand Down Expand Up @@ -75,3 +76,13 @@ psmove_port_get_time_ms()

return (now - startup_time);
}

void
psmove_port_set_socket_timeout_ms(int socket, uint32_t timeout_ms)
{
struct timeval receive_timeout = {
.tv_sec = timeout_ms / 1000,
.tv_usec = (timeout_ms % 1000) * 1000,
};
setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&receive_timeout, sizeof(receive_timeout));
}
11 changes: 11 additions & 0 deletions src/platform/psmove_port_osx.c
Expand Up @@ -28,6 +28,7 @@


#include "psmove_port.h"
#include "psmove_sockets.h"

#include <sys/time.h>

Expand Down Expand Up @@ -64,3 +65,13 @@ psmove_port_get_time_ms()

return (now - startup_time);
}

void
psmove_port_set_socket_timeout_ms(int socket, uint32_t timeout_ms)
{
struct timeval receive_timeout = {
.tv_sec = timeout_ms / 1000,
.tv_usec = (timeout_ms % 1000) * 1000,
};
setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&receive_timeout, sizeof(receive_timeout));
}
8 changes: 8 additions & 0 deletions src/platform/psmove_port_windows.c
Expand Up @@ -28,6 +28,7 @@


#include "psmove_port.h"
#include "psmove_sockets.h"

#include <windows.h>

Expand Down Expand Up @@ -75,3 +76,10 @@ psmove_port_get_time_ms()

return (uint64_t)((now.QuadPart - startup_time.QuadPart) * 1000 / frequency.QuadPart);
}

void
psmove_port_set_socket_timeout_ms(int socket, uint32_t timeout_ms)
{
DWORD receive_timeout = timeout_ms;
setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&receive_timeout, sizeof(receive_timeout));
}
6 changes: 6 additions & 0 deletions src/psmove_port.h
Expand Up @@ -52,6 +52,12 @@ psmove_port_check_pairing_permissions();
uint64_t
psmove_port_get_time_ms();

/**
* Set the timeout to the specified time in milliseconds for a given socket
**/
void
psmove_port_set_socket_timeout_ms(int socket, uint32_t timeout_ms);

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 6a12b87

Please sign in to comment.