d/remote: Add and use a multi-os r_socket_t typedef

On Linux, a socket descriptor as an int, while on Windows it is a
SOCKET (aka an unsigned long long).

This patch introduces a r_socket_t typedef set depending on the OS,
and uses it where needed.

The patch also reorders some header inclusions, to ensure that
winsock2.h is included before windows.h, or else the winsock API is
defined instead of the winsock2 API and a warning is emitted during
compilation.

Also, the "#pragma comment(lib, 'ws2_32.lib')" directive used in r_hub.c
is a VC++-ism, not compatible wth Mingw64 (gcc or clang toolchains).
This patch replaces the pragma with a cmake link directive.
This commit is contained in:
sdegrande 2024-03-12 15:41:57 +01:00
parent 56a7e60374
commit 2e43c7b895
6 changed files with 67 additions and 44 deletions

View file

@ -219,6 +219,9 @@ if(XRT_BUILD_DRIVER_REMOTE)
remote/r_internal.h
)
target_link_libraries(drv_remote PRIVATE xrt-interfaces aux_util aux_vive)
if(WIN32)
target_link_libraries(drv_remote PRIVATE ws2_32)
endif()
list(APPEND ENABLED_HEADSET_DRIVERS remote)
endif()

View file

@ -7,6 +7,8 @@
* @ingroup drv_remote
*/
#include "r_internal.h"
#include "os/os_time.h"
#include "util/u_var.h"
@ -19,7 +21,6 @@
#include "math/m_api.h"
#include "r_internal.h"
#include "util/u_hand_simulation.h"
#include <stdio.h>

View file

@ -7,6 +7,8 @@
* @ingroup drv_remote
*/
#include "r_internal.h"
#include "os/os_time.h"
#include "util/u_var.h"
@ -18,8 +20,6 @@
#include "math/m_api.h"
#include "math/m_mathinclude.h"
#include "r_internal.h"
#include <stdio.h>

View file

@ -7,14 +7,13 @@
* @ingroup drv_remote
*/
#include "r_internal.h"
#include "util/u_var.h"
#include "util/u_misc.h"
#include "util/u_debug.h"
#include "util/u_space_overseer.h"
#include "r_interface.h"
#include "r_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -22,9 +21,7 @@
#if defined(XRT_OS_WINDOWS)
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
#include "xrt/xrt_windows.h"
#else
#include <unistd.h>
#include <sys/socket.h>
@ -33,15 +30,11 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#endif
#ifndef _BSD_SOURCE
#define _BSD_SOURCE // same, but for musl // NOLINT
#endif
#endif
#ifndef SOCKET
#define SOCKET int
#endif
#ifndef __USE_MISC
#define __USE_MISC // SOL_TCP on C11
@ -78,63 +71,63 @@ DEBUG_GET_ONCE_LOG_OPTION(remote_log, "REMOTE_LOG", U_LOGGING_INFO)
#if defined(XRT_OS_WINDOWS)
static inline void
socket_close(SOCKET id)
socket_close(r_socket_t id)
{
closesocket(id);
}
static inline SOCKET
static inline r_socket_t
socket_create(void)
{
return socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
}
static inline int
socket_set_opt(SOCKET id, int flag)
socket_set_opt(r_socket_t id, int flag)
{
return setsockopt(id, SOL_SOCKET, SO_REUSEADDR, (const char *)&flag, sizeof(flag));
}
static inline ssize_t
socket_read(SOCKET id, void *ptr, size_t size, size_t current)
socket_read(r_socket_t id, void *ptr, size_t size, size_t current)
{
return recv(id, (char *)ptr, size - current, 0);
return recv(id, (char *)ptr, (int)(size - current), 0);
}
static inline size_t
socket_write(SOCKET id, void *ptr, size_t size, size_t current)
static inline ssize_t
socket_write(r_socket_t id, void *ptr, size_t size, size_t current)
{
return send(id, (const char *)ptr, size - current, 0);
return send(id, (const char *)ptr, (int)(size - current), 0);
}
#elif defined(XRT_OS_UNIX)
static inline void
socket_close(SOCKET id)
socket_close(r_socket_t id)
{
close(id);
}
static inline SOCKET
static inline r_socket_t
socket_create(void)
{
return socket(AF_INET, SOCK_STREAM, 0);
}
static inline int
socket_set_opt(SOCKET id, int flag)
socket_set_opt(r_socket_t id, int flag)
{
return setsockopt(id, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
}
static inline ssize_t
socket_read(SOCKET id, void *ptr, size_t size, size_t current)
socket_read(r_socket_t id, void *ptr, size_t size, size_t current)
{
return read(id, ptr, size - current);
}
static inline ssize_t
socket_write(SOCKET id, void *ptr, size_t size, size_t current)
socket_write(r_socket_t id, void *ptr, size_t size, size_t current)
{
return write(id, ptr, size - current);
}
@ -148,7 +141,7 @@ socket_write(SOCKET id, void *ptr, size_t size, size_t current)
*
*/
static int
static r_socket_t
setup_accept_fd(struct r_hub *r)
{
struct sockaddr_in server_address = {0};
@ -204,7 +197,7 @@ cleanup:
}
static bool
wait_for_read_and_to_continue(struct r_hub *r, SOCKET socket)
wait_for_read_and_to_continue(struct r_hub *r, r_socket_t socket)
{
fd_set set;
int ret = 0;
@ -222,7 +215,7 @@ wait_for_read_and_to_continue(struct r_hub *r, SOCKET socket)
FD_ZERO(&set);
FD_SET(socket, &set);
ret = select(socket + 1, &set, NULL, NULL, &timeout);
ret = select((int)socket + 1, &set, NULL, NULL, &timeout);
}
if (ret < 0) {
@ -235,11 +228,11 @@ wait_for_read_and_to_continue(struct r_hub *r, SOCKET socket)
}
}
static int
static r_socket_t
do_accept(struct r_hub *r)
{
struct sockaddr_in addr = {0};
int ret = 0;
r_socket_t ret = 0;
if (!wait_for_read_and_to_continue(r, r->accept_fd)) {
R_ERROR(r, "Failed to wait for id %d", r->accept_fd);
return -1;
@ -252,7 +245,7 @@ do_accept(struct r_hub *r)
return ret;
}
SOCKET conn_fd = ret;
r_socket_t conn_fd = ret;
int flags = 1;
ret = socket_set_opt(r->accept_fd, flags);
@ -269,7 +262,7 @@ do_accept(struct r_hub *r)
return 0;
}
static int
static ssize_t
read_one(struct r_hub *r, struct r_remote_data *data)
{
struct r_remote_connection *rc = &r->rc;
@ -307,7 +300,7 @@ static void *
run_thread(void *ptr)
{
struct r_hub *r = (struct r_hub *)ptr;
int ret;
r_socket_t ret;
ret = setup_accept_fd(r);
if (ret < 0) {
@ -539,11 +532,12 @@ r_create_devices(uint16_t port,
*
*/
int
r_socket_t
r_remote_connection_init(struct r_remote_connection *rc, const char *ip_addr, uint16_t port)
{
struct sockaddr_in addr = {0};
int conn_fd;
r_socket_t sock_fd;
r_socket_t conn_fd;
int ret;
// Set log level.

View file

@ -9,10 +9,20 @@
#pragma once
// winsock2.h must be included before windows.h, or the winsock interface will be
// defined instead of the winsock2 interface.
// Given that some of the Monado headers could include windows.h, winsock2 is to be
// included before anything else.
// As a consequence, this header must also be the first included in the file using
// it.
#include "xrt/xrt_config_os.h"
#ifdef XRT_OS_WINDOWS
#include <winsock2.h> // For SOCKET
#endif
#include "xrt/xrt_defines.h"
#include "util/u_logging.h"
#ifdef __cplusplus
extern "C" {
#endif
@ -35,6 +45,22 @@ struct xrt_session_event_sink;
* @brief @ref drv_remote files.
*/
#ifdef XRT_OS_WINDOWS
/*!
* The type for a socket descriptor
*
* On Windows, this is a SOCKET.
*/
typedef SOCKET r_socket_t;
#else
/*!
* The type for a socket descriptor
*
* On non-Windows, this is a file descriptor.
*/
typedef int r_socket_t;
#endif
/*!
* Header value to be set in the packet.
*
@ -129,7 +155,7 @@ struct r_remote_connection
enum u_logging_level log_level;
//! Socket.
int fd;
r_socket_t fd;
};
/*!
@ -148,7 +174,7 @@ r_create_devices(uint16_t port,
*
* @ingroup drv_remote
*/
int
r_socket_t
r_remote_connection_init(struct r_remote_connection *rc, const char *addr, uint16_t port);
int

View file

@ -9,6 +9,8 @@
#pragma once
#include "r_interface.h"
#include "xrt/xrt_device.h"
#include "xrt/xrt_system.h"
#include "xrt/xrt_tracking.h"
@ -17,14 +19,11 @@
#include "util/u_hand_tracking.h"
#include "r_interface.h"
#ifdef __cplusplus
extern "C" {
#endif
/*!
* Central object remote object.
*
@ -48,7 +47,7 @@ struct r_hub
struct r_remote_data latest;
//! Incoming connection socket.
int accept_fd;
r_socket_t accept_fd;
uint16_t port;