The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

+67 -44
+3
src/xrt/drivers/CMakeLists.txt
··· 219 219 remote/r_internal.h 220 220 ) 221 221 target_link_libraries(drv_remote PRIVATE xrt-interfaces aux_util aux_vive) 222 + if(WIN32) 223 + target_link_libraries(drv_remote PRIVATE ws2_32) 224 + endif() 222 225 list(APPEND ENABLED_HEADSET_DRIVERS remote) 223 226 endif() 224 227
+2 -1
src/xrt/drivers/remote/r_device.c
··· 7 7 * @ingroup drv_remote 8 8 */ 9 9 10 + #include "r_internal.h" 11 + 10 12 #include "os/os_time.h" 11 13 12 14 #include "util/u_var.h" ··· 19 21 20 22 #include "math/m_api.h" 21 23 22 - #include "r_internal.h" 23 24 #include "util/u_hand_simulation.h" 24 25 25 26 #include <stdio.h>
+2 -2
src/xrt/drivers/remote/r_hmd.c
··· 7 7 * @ingroup drv_remote 8 8 */ 9 9 10 + #include "r_internal.h" 11 + 10 12 #include "os/os_time.h" 11 13 12 14 #include "util/u_var.h" ··· 17 19 18 20 #include "math/m_api.h" 19 21 #include "math/m_mathinclude.h" 20 - 21 - #include "r_internal.h" 22 22 23 23 #include <stdio.h> 24 24
+28 -34
src/xrt/drivers/remote/r_hub.c
··· 7 7 * @ingroup drv_remote 8 8 */ 9 9 10 + #include "r_internal.h" 11 + 10 12 #include "util/u_var.h" 11 13 #include "util/u_misc.h" 12 14 #include "util/u_debug.h" 13 15 #include "util/u_space_overseer.h" 14 16 15 - #include "r_interface.h" 16 - #include "r_internal.h" 17 - 18 17 #include <stdio.h> 19 18 #include <stdlib.h> 20 19 #include <string.h> ··· 22 21 #if defined(XRT_OS_WINDOWS) 23 22 #include <winsock2.h> 24 23 #include <ws2tcpip.h> 25 - #include <windows.h> 26 - 27 - #pragma comment(lib, "ws2_32.lib") 24 + #include "xrt/xrt_windows.h" 28 25 #else 29 26 #include <unistd.h> 30 27 #include <sys/socket.h> ··· 33 30 #include <arpa/inet.h> 34 31 #include <netinet/in.h> 35 32 #include <netinet/tcp.h> 33 + #endif 36 34 37 35 #ifndef _BSD_SOURCE 38 36 #define _BSD_SOURCE // same, but for musl // NOLINT 39 - #endif 40 - #endif 41 - 42 - #ifndef SOCKET 43 - #define SOCKET int 44 37 #endif 45 38 46 39 #ifndef __USE_MISC ··· 78 71 #if defined(XRT_OS_WINDOWS) 79 72 80 73 static inline void 81 - socket_close(SOCKET id) 74 + socket_close(r_socket_t id) 82 75 { 83 76 closesocket(id); 84 77 } 85 78 86 - static inline SOCKET 79 + static inline r_socket_t 87 80 socket_create(void) 88 81 { 89 82 return socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 90 83 } 91 84 92 85 static inline int 93 - socket_set_opt(SOCKET id, int flag) 86 + socket_set_opt(r_socket_t id, int flag) 94 87 { 95 88 return setsockopt(id, SOL_SOCKET, SO_REUSEADDR, (const char *)&flag, sizeof(flag)); 96 89 } 97 90 98 91 static inline ssize_t 99 - socket_read(SOCKET id, void *ptr, size_t size, size_t current) 92 + socket_read(r_socket_t id, void *ptr, size_t size, size_t current) 100 93 { 101 - return recv(id, (char *)ptr, size - current, 0); 94 + return recv(id, (char *)ptr, (int)(size - current), 0); 102 95 } 103 96 104 - static inline size_t 105 - socket_write(SOCKET id, void *ptr, size_t size, size_t current) 97 + static inline ssize_t 98 + socket_write(r_socket_t id, void *ptr, size_t size, size_t current) 106 99 { 107 - return send(id, (const char *)ptr, size - current, 0); 100 + return send(id, (const char *)ptr, (int)(size - current), 0); 108 101 } 109 102 110 103 #elif defined(XRT_OS_UNIX) 111 104 112 105 static inline void 113 - socket_close(SOCKET id) 106 + socket_close(r_socket_t id) 114 107 { 115 108 close(id); 116 109 } 117 110 118 - static inline SOCKET 111 + static inline r_socket_t 119 112 socket_create(void) 120 113 { 121 114 return socket(AF_INET, SOCK_STREAM, 0); 122 115 } 123 116 124 117 static inline int 125 - socket_set_opt(SOCKET id, int flag) 118 + socket_set_opt(r_socket_t id, int flag) 126 119 { 127 120 return setsockopt(id, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)); 128 121 } 129 122 130 123 static inline ssize_t 131 - socket_read(SOCKET id, void *ptr, size_t size, size_t current) 124 + socket_read(r_socket_t id, void *ptr, size_t size, size_t current) 132 125 { 133 126 return read(id, ptr, size - current); 134 127 } 135 128 136 129 static inline ssize_t 137 - socket_write(SOCKET id, void *ptr, size_t size, size_t current) 130 + socket_write(r_socket_t id, void *ptr, size_t size, size_t current) 138 131 { 139 132 return write(id, ptr, size - current); 140 133 } ··· 148 141 * 149 142 */ 150 143 151 - static int 144 + static r_socket_t 152 145 setup_accept_fd(struct r_hub *r) 153 146 { 154 147 struct sockaddr_in server_address = {0}; ··· 204 197 } 205 198 206 199 static bool 207 - wait_for_read_and_to_continue(struct r_hub *r, SOCKET socket) 200 + wait_for_read_and_to_continue(struct r_hub *r, r_socket_t socket) 208 201 { 209 202 fd_set set; 210 203 int ret = 0; ··· 222 215 FD_ZERO(&set); 223 216 FD_SET(socket, &set); 224 217 225 - ret = select(socket + 1, &set, NULL, NULL, &timeout); 218 + ret = select((int)socket + 1, &set, NULL, NULL, &timeout); 226 219 } 227 220 228 221 if (ret < 0) { ··· 235 228 } 236 229 } 237 230 238 - static int 231 + static r_socket_t 239 232 do_accept(struct r_hub *r) 240 233 { 241 234 struct sockaddr_in addr = {0}; 242 - int ret = 0; 235 + r_socket_t ret = 0; 243 236 if (!wait_for_read_and_to_continue(r, r->accept_fd)) { 244 237 R_ERROR(r, "Failed to wait for id %d", r->accept_fd); 245 238 return -1; ··· 252 245 return ret; 253 246 } 254 247 255 - SOCKET conn_fd = ret; 248 + r_socket_t conn_fd = ret; 256 249 257 250 int flags = 1; 258 251 ret = socket_set_opt(r->accept_fd, flags); ··· 269 262 return 0; 270 263 } 271 264 272 - static int 265 + static ssize_t 273 266 read_one(struct r_hub *r, struct r_remote_data *data) 274 267 { 275 268 struct r_remote_connection *rc = &r->rc; ··· 307 300 run_thread(void *ptr) 308 301 { 309 302 struct r_hub *r = (struct r_hub *)ptr; 310 - int ret; 303 + r_socket_t ret; 311 304 312 305 ret = setup_accept_fd(r); 313 306 if (ret < 0) { ··· 539 532 * 540 533 */ 541 534 542 - int 535 + r_socket_t 543 536 r_remote_connection_init(struct r_remote_connection *rc, const char *ip_addr, uint16_t port) 544 537 { 545 538 struct sockaddr_in addr = {0}; 546 - int conn_fd; 539 + r_socket_t sock_fd; 540 + r_socket_t conn_fd; 547 541 int ret; 548 542 549 543 // Set log level.
+29 -3
src/xrt/drivers/remote/r_interface.h
··· 9 9 10 10 #pragma once 11 11 12 + // winsock2.h must be included before windows.h, or the winsock interface will be 13 + // defined instead of the winsock2 interface. 14 + // Given that some of the Monado headers could include windows.h, winsock2 is to be 15 + // included before anything else. 16 + // As a consequence, this header must also be the first included in the file using 17 + // it. 18 + #include "xrt/xrt_config_os.h" 19 + #ifdef XRT_OS_WINDOWS 20 + #include <winsock2.h> // For SOCKET 21 + #endif 22 + 12 23 #include "xrt/xrt_defines.h" 13 24 #include "util/u_logging.h" 14 - 15 25 16 26 #ifdef __cplusplus 17 27 extern "C" { ··· 34 44 * 35 45 * @brief @ref drv_remote files. 36 46 */ 47 + 48 + #ifdef XRT_OS_WINDOWS 49 + /*! 50 + * The type for a socket descriptor 51 + * 52 + * On Windows, this is a SOCKET. 53 + */ 54 + typedef SOCKET r_socket_t; 55 + #else 56 + /*! 57 + * The type for a socket descriptor 58 + * 59 + * On non-Windows, this is a file descriptor. 60 + */ 61 + typedef int r_socket_t; 62 + #endif 37 63 38 64 /*! 39 65 * Header value to be set in the packet. ··· 129 155 enum u_logging_level log_level; 130 156 131 157 //! Socket. 132 - int fd; 158 + r_socket_t fd; 133 159 }; 134 160 135 161 /*! ··· 148 174 * 149 175 * @ingroup drv_remote 150 176 */ 151 - int 177 + r_socket_t 152 178 r_remote_connection_init(struct r_remote_connection *rc, const char *addr, uint16_t port); 153 179 154 180 int
+3 -4
src/xrt/drivers/remote/r_internal.h
··· 9 9 10 10 #pragma once 11 11 12 + #include "r_interface.h" 13 + 12 14 #include "xrt/xrt_device.h" 13 15 #include "xrt/xrt_system.h" 14 16 #include "xrt/xrt_tracking.h" ··· 17 19 18 20 #include "util/u_hand_tracking.h" 19 21 20 - #include "r_interface.h" 21 - 22 22 23 23 #ifdef __cplusplus 24 24 extern "C" { 25 25 #endif 26 - 27 26 28 27 /*! 29 28 * Central object remote object. ··· 48 47 struct r_remote_data latest; 49 48 50 49 //! Incoming connection socket. 51 - int accept_fd; 50 + r_socket_t accept_fd; 52 51 53 52 uint16_t port; 54 53