From 1bb5a21453ebae17b0a5f754c83fbaa98942dc17 Mon Sep 17 00:00:00 2001 From: fincs Date: Fri, 27 Oct 2023 23:46:40 +0200 Subject: [PATCH] socket: automatically detect and use latest bsdsockets version (fix #551) --- nx/include/switch/runtime/devices/socket.h | 2 -- nx/source/runtime/devices/socket.c | 26 +++++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/nx/include/switch/runtime/devices/socket.h b/nx/include/switch/runtime/devices/socket.h index 0f7af7ab..41a35cbb 100644 --- a/nx/include/switch/runtime/devices/socket.h +++ b/nx/include/switch/runtime/devices/socket.h @@ -10,8 +10,6 @@ typedef enum { /// Configuration structure for socketInitalize typedef struct { - u32 bsdsockets_version; ///< Observed 1 on 2.0 LibAppletWeb, 2 on 3.0. - u32 tcp_tx_buf_size; ///< Size of the TCP transfer (send) buffer (initial or fixed). u32 tcp_rx_buf_size; ///< Size of the TCP receive buffer (initial or fixed). u32 tcp_tx_buf_max_size; ///< Maximum size of the TCP transfer (send) buffer. If it is 0, the size of the buffer is fixed to its initial value. diff --git a/nx/source/runtime/devices/socket.c b/nx/source/runtime/devices/socket.c index 52d0cfb0..490c2943 100644 --- a/nx/source/runtime/devices/socket.c +++ b/nx/source/runtime/devices/socket.c @@ -62,8 +62,6 @@ static const devoptab_t g_socketDevoptab = { }; static const SocketInitConfig g_defaultSocketInitConfig = { - .bsdsockets_version = 1, - .tcp_tx_buf_size = 0x8000, .tcp_rx_buf_size = 0x10000, .tcp_tx_buf_max_size = 0x40000, @@ -82,13 +80,35 @@ const SocketInitConfig *socketGetDefaultInitConfig(void) { return &g_defaultSocketInitConfig; } +static u32 socketSelectVersion(void) { + if (hosversionBefore(3,0,0)) { + return 1; + } else if (hosversionBefore(4,0,0)) { + return 2; + } else if (hosversionBefore(5,0,0)) { + return 3; + } else if (hosversionBefore(6,0,0)) { + return 4; + } else if (hosversionBefore(8,0,0)) { + return 5; + } else if (hosversionBefore(9,0,0)) { + return 6; + } else if (hosversionBefore(13,0,0)) { + return 7; + } else if (hosversionBefore(16,0,0)) { + return 8; + } else /* latest known version */ { + return 9; + } +} + Result socketInitialize(const SocketInitConfig *config) { Result ret = 0; if (!config) config = &g_defaultSocketInitConfig; BsdInitConfig bcfg = { - .version = config->bsdsockets_version, + .version = socketSelectVersion(), .tcp_tx_buf_size = config->tcp_tx_buf_size, .tcp_rx_buf_size = config->tcp_rx_buf_size,