mirror of
https://github.com/switchbrew/libnx.git
synced 2025-06-21 12:32:40 +02:00
Move tmem allocation in bsdInitalize...
...add bsdExit, change the default config's "version" field to 1.
This commit is contained in:
parent
fb9e126f3a
commit
5b0de6c054
@ -26,7 +26,8 @@ struct bsd_sockaddr_in {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const BsdConfig *bsdGetDefaultConfig(void);
|
const BsdConfig *bsdGetDefaultConfig(void);
|
||||||
Result bsdInitialize(TransferMemory* tmem, const BsdConfig *config);
|
Result bsdInitialize(const BsdConfig *config);
|
||||||
|
void bsdExit(void);
|
||||||
int bsdGetErrno(void);
|
int bsdGetErrno(void);
|
||||||
int bsdConnect(int sockfd, const void* addr, u32 addrlen);
|
int bsdConnect(int sockfd, const void* addr, u32 addrlen);
|
||||||
int bsdSocket(int domain, int type, int protocol);
|
int bsdSocket(int domain, int type, int protocol);
|
||||||
@ -38,30 +39,9 @@ int bsdRecv(int sockfd, void* buffer, size_t length, int flags);
|
|||||||
int bsdSetSockOpt(int sockfd, int level, int option_name, const void *option_value, size_t option_size);
|
int bsdSetSockOpt(int sockfd, int level, int option_name, const void *option_value, size_t option_size);
|
||||||
int bsdWrite(int sockfd, const void* buffer, size_t length);
|
int bsdWrite(int sockfd, const void* buffer, size_t length);
|
||||||
|
|
||||||
/**
|
static inline Result bsdInitializeDefault(void)
|
||||||
* @brief Computes the minimal size of the transfer memory to be passed to @ref bsdInitalize.
|
|
||||||
* Should the transfer memory be smaller than that, the BSD sockets service would only send
|
|
||||||
* ZeroWindow packets (for TCP), resulting in a transfer rate not exceeding 1 byte/s.
|
|
||||||
* @param config Pointer to the BSD sockets service configuration.
|
|
||||||
*/
|
|
||||||
static inline size_t bsdGetTransferMemSizeForConfig(const BsdConfig *config)
|
|
||||||
{
|
{
|
||||||
u32 tcp_tx_buf_max_size = config->tcp_tx_buf_max_size != 0 ? config->tcp_tx_buf_max_size : config->tcp_tx_buf_size;
|
return bsdInitialize(bsdGetDefaultConfig());
|
||||||
u32 tcp_rx_buf_max_size = config->tcp_rx_buf_max_size != 0 ? config->tcp_rx_buf_max_size : config->tcp_rx_buf_size;
|
|
||||||
u32 sum = tcp_tx_buf_max_size + tcp_rx_buf_max_size + config->udp_tx_buf_size + config->udp_rx_buf_size;
|
|
||||||
|
|
||||||
sum = ((sum + 0xFFF) >> 12) << 12; // page round-up
|
|
||||||
return (size_t)(config->sb_efficiency * sum);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline size_t bsdGetTransferMemSizeForDefaultConfig(void)
|
|
||||||
{
|
|
||||||
return bsdGetTransferMemSizeForConfig(bsdGetDefaultConfig());
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline Result bsdInitializeDefault(TransferMemory* tmem)
|
|
||||||
{
|
|
||||||
return bsdInitialize(tmem, bsdGetDefaultConfig());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define BSD_AF_INET 2
|
#define BSD_AF_INET 2
|
||||||
|
@ -73,4 +73,4 @@ typedef enum {
|
|||||||
#define DEPRECATED
|
#define DEPRECATED
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define INVALID_HANDLE ((Handle) -1)
|
#define INVALID_HANDLE ((Handle) 0)
|
||||||
|
@ -30,7 +30,7 @@ void envSetup(void* ctx, Handle main_thread, LoaderReturnFn saved_lr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Detect NSO environment.
|
// Detect NSO environment.
|
||||||
if (main_thread != INVALID_HANDLE)
|
if (main_thread != -1)
|
||||||
{
|
{
|
||||||
g_mainThreadHandle = main_thread;
|
g_mainThreadHandle = main_thread;
|
||||||
g_isNso = true;
|
g_isNso = true;
|
||||||
|
@ -7,13 +7,17 @@
|
|||||||
#include "kernel/shmem.h"
|
#include "kernel/shmem.h"
|
||||||
#include "kernel/rwlock.h"
|
#include "kernel/rwlock.h"
|
||||||
|
|
||||||
|
#define EPIPE 32
|
||||||
|
|
||||||
static Service g_bsdSrv;
|
static Service g_bsdSrv;
|
||||||
static Service g_bsdMonitor;
|
static Service g_bsdMonitor;
|
||||||
static u64 g_bsdClientPid = -1;
|
static u64 g_bsdClientPid = -1;
|
||||||
static int g_Errno = 0;
|
static int g_Errno = 0;
|
||||||
|
|
||||||
|
static TransferMemory g_bsdTmem;
|
||||||
|
|
||||||
static const BsdConfig g_defaultBsdConfig = {
|
static const BsdConfig g_defaultBsdConfig = {
|
||||||
.version = 2,
|
.version = 1,
|
||||||
|
|
||||||
.tcp_tx_buf_size = 0x8000,
|
.tcp_tx_buf_size = 0x8000,
|
||||||
.tcp_rx_buf_size = 0x10000,
|
.tcp_rx_buf_size = 0x10000,
|
||||||
@ -26,7 +30,20 @@ static const BsdConfig g_defaultBsdConfig = {
|
|||||||
.sb_efficiency = 4,
|
.sb_efficiency = 4,
|
||||||
};
|
};
|
||||||
|
|
||||||
#define EPIPE 32
|
/*
|
||||||
|
This function computes the minimal size of the transfer memory to be passed to @ref bsdInitalize.
|
||||||
|
Should the transfer memory be smaller than that, the BSD sockets service would only send
|
||||||
|
ZeroWindow packets (for TCP), resulting in a transfer rate not exceeding 1 byte/s.
|
||||||
|
*/
|
||||||
|
static size_t _bsdGetTransferMemSizeForConfig(const BsdConfig *config)
|
||||||
|
{
|
||||||
|
u32 tcp_tx_buf_max_size = config->tcp_tx_buf_max_size != 0 ? config->tcp_tx_buf_max_size : config->tcp_tx_buf_size;
|
||||||
|
u32 tcp_rx_buf_max_size = config->tcp_rx_buf_max_size != 0 ? config->tcp_rx_buf_max_size : config->tcp_rx_buf_size;
|
||||||
|
u32 sum = tcp_tx_buf_max_size + tcp_rx_buf_max_size + config->udp_tx_buf_size + config->udp_rx_buf_size;
|
||||||
|
|
||||||
|
sum = ((sum + 0xFFF) >> 12) << 12; // page round-up
|
||||||
|
return (size_t)(config->sb_efficiency * sum);
|
||||||
|
}
|
||||||
|
|
||||||
static Result _bsdRegisterClient(Service* srv, TransferMemory* tmem, const BsdConfig *config, u64* pid_out) {
|
static Result _bsdRegisterClient(Service* srv, TransferMemory* tmem, const BsdConfig *config, u64* pid_out) {
|
||||||
IpcCommand c;
|
IpcCommand c;
|
||||||
@ -108,28 +125,43 @@ const BsdConfig *bsdGetDefaultConfig(void) {
|
|||||||
return &g_defaultBsdConfig;
|
return &g_defaultBsdConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result bsdInitialize(TransferMemory* tmem, const BsdConfig *config) {
|
Result bsdInitialize(const BsdConfig *config) {
|
||||||
const char* bsd_srv = "bsd:s";
|
const char* bsd_srv = "bsd:s";
|
||||||
|
|
||||||
|
if(serviceIsActive(&g_bsdSrv) || serviceIsActive(&g_bsdMonitor))
|
||||||
|
return MAKERESULT(Module_Libnx, LibnxError_AlreadyInitialized);
|
||||||
|
|
||||||
Result rc = smGetService(&g_bsdSrv, bsd_srv);
|
Result rc = smGetService(&g_bsdSrv, bsd_srv);
|
||||||
|
|
||||||
if (R_FAILED(rc)) {
|
if (R_FAILED(rc)) {
|
||||||
bsd_srv = "bsd:u";
|
bsd_srv = "bsd:u";
|
||||||
rc = smGetService(&g_bsdSrv, bsd_srv);
|
rc = smGetService(&g_bsdSrv, bsd_srv);
|
||||||
}
|
}
|
||||||
|
if(R_FAILED(rc)) goto error;
|
||||||
|
|
||||||
if (R_SUCCEEDED(rc)) {
|
rc = smGetService(&g_bsdMonitor, bsd_srv);
|
||||||
rc = smGetService(&g_bsdMonitor, bsd_srv);
|
if(R_FAILED(rc)) goto error;
|
||||||
|
|
||||||
if (R_SUCCEEDED(rc)) {
|
rc = tmemCreate(&g_bsdTmem, _bsdGetTransferMemSizeForConfig(config), 0);
|
||||||
rc = _bsdRegisterClient(&g_bsdSrv, tmem, config, &g_bsdClientPid);
|
if(R_FAILED(rc)) goto error;
|
||||||
|
|
||||||
if (R_SUCCEEDED(rc)) {
|
rc = _bsdRegisterClient(&g_bsdSrv, &g_bsdTmem, config, &g_bsdClientPid);
|
||||||
rc = _bsdStartMonitor(&g_bsdMonitor, g_bsdClientPid);
|
if(R_FAILED(rc)) goto error;
|
||||||
}
|
|
||||||
}
|
rc = _bsdStartMonitor(&g_bsdMonitor, g_bsdClientPid);
|
||||||
}
|
if(R_FAILED(rc)) goto error;
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
|
|
||||||
|
error:
|
||||||
|
bsdExit();
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
void bsdExit(void) {
|
||||||
|
serviceClose(&g_bsdMonitor);
|
||||||
|
serviceClose(&g_bsdSrv);
|
||||||
|
tmemClose(&g_bsdTmem);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bsdGetErrno(void) {
|
int bsdGetErrno(void) {
|
||||||
|
Loading…
Reference in New Issue
Block a user