Skip to content

Commit 79b7657

Browse files
Add NXDK (Xbox) support
1 parent 7b026e7 commit 79b7657

8 files changed

Lines changed: 76 additions & 46 deletions

File tree

enet

nanors/deps/obl/oblas_lite.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void obl_axpyb32_ref(u8 *a, u32 *b, u8 u, unsigned k)
5858
for (unsigned idx = 0, p = 0; idx < k; idx += 8 * sizeof(u32), p++) {
5959
u32 tmp = b[p];
6060
while (tmp > 0) {
61-
#ifdef _MSC_VER
61+
#if defined(_MSC_VER) && !defined(NXDK)
6262
unsigned long index = 0;
6363
_BitScanForward(&index, tmp);
6464
unsigned tz = (unsigned int)index;

src/Platform.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static int activeMutexes = 0;
2020
static int activeEvents = 0;
2121
static int activeCondVars = 0;
2222

23-
#if defined(LC_WINDOWS)
23+
#if defined(LC_WINDOWS) && !defined(NXDK)
2424

2525
#pragma pack(push, 8)
2626
typedef struct tagTHREADNAME_INFO
@@ -65,7 +65,9 @@ void setThreadNameWin32(const char* name) {
6565
}
6666
#endif
6767
}
68+
#endif
6869

70+
#if defined(LC_WINDOWS)
6971
DWORD WINAPI ThreadProc(LPVOID lpParameter) {
7072
struct thread_context* ctx = (struct thread_context*)lpParameter;
7173
#elif defined(__WIIU__)
@@ -76,7 +78,7 @@ void* ThreadProc(void* context) {
7678
struct thread_context* ctx = (struct thread_context*)context;
7779
#endif
7880

79-
#if defined(LC_WINDOWS)
81+
#if defined(LC_WINDOWS) && !defined(NXDK)
8082
setThreadNameWin32(ctx->name);
8183
#elif defined(__linux__) || defined(__FreeBSD__)
8284
pthread_setname_np(pthread_self(), ctx->name);
@@ -298,7 +300,7 @@ int PltCreateThread(const char* name, ThreadEntry entry, void* context, PLT_THRE
298300
free(ctx);
299301
return err;
300302
}
301-
303+
302304
}
303305
#endif
304306

@@ -309,7 +311,7 @@ int PltCreateThread(const char* name, ThreadEntry entry, void* context, PLT_THRE
309311

310312
int PltCreateEvent(PLT_EVENT* event) {
311313
#if defined(LC_WINDOWS)
312-
*event = CreateEventEx(NULL, NULL, CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS);
314+
*event = CreateEvent(NULL, TRUE, FALSE, NULL);
313315
if (!*event) {
314316
return -1;
315317
}
@@ -578,7 +580,7 @@ bool PltSafeStrcpy(char* dest, size_t dest_size, const char* src) {
578580
memset(dest, 0xFE, dest_size);
579581
#endif
580582

581-
#ifdef _MSC_VER
583+
#if defined(_MSC_VER) && !defined(NXDK)
582584
// strncpy_s() with _TRUNCATE does what we need for MSVC.
583585
// We use this rather than strcpy_s() because we don't want
584586
// the invalid parameter handler invoked upon failure.

src/Platform.h

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#ifdef _WIN32
3+
#if defined(_WIN32) && !defined(NXDK)
44
// Prevent bogus definitions of error codes
55
// that are incompatible with Winsock errors.
66
#define _CRT_NO_POSIX_ERROR_CODES
@@ -13,11 +13,23 @@
1313
#include <string.h>
1414
#include <stdint.h>
1515

16-
#ifdef _WIN32
16+
#if defined(_WIN32) && !defined(NXDK)
1717
#define WIN32_LEAN_AND_MEAN
1818
#include <windows.h>
1919
#include <winsock2.h>
2020
#include <ws2tcpip.h>
21+
#elif defined(NXDK)
22+
// nxdk exposes Win32-style threading primitives, but its network stack is
23+
// lwIP with POSIX/BSD socket compatibility headers. Pull in the socket-facing
24+
// headers from the POSIX side so moonlight-common-c uses the Unix backend.
25+
#define WIN32_LEAN_AND_MEAN
26+
#include <windows.h>
27+
#include <unistd.h>
28+
#include <sys/time.h>
29+
#include <sys/ioctl.h>
30+
#include <arpa/inet.h>
31+
#include <netinet/in.h>
32+
#include <fcntl.h>
2133
#elif defined(__APPLE__)
2234
#include <mach/mach_time.h>
2335
#include <unistd.h>
@@ -54,7 +66,7 @@
5466
#include <fcntl.h>
5567
#endif
5668

57-
#ifdef _WIN32
69+
#if defined(_WIN32)
5870
# define LC_WINDOWS
5971
#else
6072
# define LC_POSIX
@@ -64,26 +76,31 @@
6476
#endif
6577

6678
#ifdef LC_WINDOWS
79+
# if !defined(NXDK)
6780
// Windows doesn't have strtok_r() but it has the same
6881
// function named strtok_s().
6982
#define strtok_r strtok_s
83+
# endif
7084

71-
# if defined(WINAPI_FAMILY) && WINAPI_FAMILY==WINAPI_FAMILY_APP
72-
# define LC_UWP
73-
# else
74-
# define LC_WINDOWS_DESKTOP
75-
#endif
85+
# if !defined(NXDK)
86+
# if defined(WINAPI_FAMILY) && WINAPI_FAMILY==WINAPI_FAMILY_APP
87+
# define LC_UWP
88+
# else
89+
# define LC_WINDOWS_DESKTOP
90+
# endif
91+
# endif
7692

7793
#endif
7894

95+
7996
#include <stdio.h>
8097
#include "Limelight.h"
8198

8299
#define Limelog(s, ...) \
83100
if (ListenerCallbacks.logMessage) \
84101
ListenerCallbacks.logMessage(s, ##__VA_ARGS__)
85102

86-
#if defined(LC_WINDOWS)
103+
#if defined(LC_WINDOWS) && !defined(NXDK)
87104
#include <crtdbg.h>
88105
#ifdef LC_DEBUG
89106
#define LC_ASSERT(x) __analysis_assume(x); \
@@ -116,21 +133,22 @@
116133
#define LC_ASSERT_VT(x) LC_ASSERT(x)
117134
#endif
118135

119-
#ifdef _MSC_VER
136+
#if defined(__has_builtin) && __has_builtin(__builtin_bswap16)
137+
#define LC_HAS_BUILTIN_BSWAP
138+
#endif
139+
140+
#if defined(LC_HAS_BUILTIN_BSWAP) || \
141+
(defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))
142+
#define BSWAP16(x) __builtin_bswap16(x)
143+
#define BSWAP32(x) __builtin_bswap32(x)
144+
#define BSWAP64(x) __builtin_bswap64(x)
145+
#elif defined(_MSC_VER)
120146
#pragma intrinsic(_byteswap_ushort)
121147
#define BSWAP16(x) _byteswap_ushort(x)
122148
#pragma intrinsic(_byteswap_ulong)
123149
#define BSWAP32(x) _byteswap_ulong(x)
124150
#pragma intrinsic(_byteswap_uint64)
125151
#define BSWAP64(x) _byteswap_uint64(x)
126-
#elif (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
127-
#define BSWAP16(x) __builtin_bswap16(x)
128-
#define BSWAP32(x) __builtin_bswap32(x)
129-
#define BSWAP64(x) __builtin_bswap64(x)
130-
#elif defined(__has_builtin) && __has_builtin(__builtin_bswap16)
131-
#define BSWAP16(x) __builtin_bswap16(x)
132-
#define BSWAP32(x) __builtin_bswap32(x)
133-
#define BSWAP64(x) __builtin_bswap64(x)
134152
#else
135153
#error Please define your platform byteswap macros!
136154
#endif

src/PlatformSockets.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#endif
1313
#define TCPv6_MSS 1220
1414

15-
#if defined(LC_WINDOWS)
15+
#if defined(LC_WINDOWS) && !defined(NXDK)
1616

1717
#ifndef SIO_UDP_CONNRESET
1818
#define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR, 12)
@@ -70,7 +70,7 @@ void shutdownTcpSocket(SOCKET s) {
7070
}
7171

7272
int setNonFatalRecvTimeoutMs(SOCKET s, int timeoutMs) {
73-
#if defined(LC_WINDOWS)
73+
#if defined(LC_WINDOWS) && !defined(NXDK)
7474
// Windows says that SO_RCVTIMEO puts the socket into an indeterminate state
7575
// when a timeout occurs. MSDN doesn't go into it any more than that, but it
7676
// seems likely that they are referring to the inability to know whether a
@@ -93,7 +93,7 @@ int setNonFatalRecvTimeoutMs(SOCKET s, int timeoutMs) {
9393
}
9494

9595
int pollSockets(struct pollfd* pollFds, int pollFdsCount, int timeoutMs) {
96-
#if defined(LC_WINDOWS)
96+
#if defined(LC_WINDOWS) && !defined(NXDK)
9797
// We could have used WSAPoll() but it has some nasty bugs
9898
// https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
9999
//
@@ -206,7 +206,7 @@ int recvUdpSocket(SOCKET s, char* buffer, int size, bool useSelect) {
206206
(LastSocketError() == EWOULDBLOCK ||
207207
LastSocketError() == EINTR ||
208208
LastSocketError() == EAGAIN ||
209-
#if defined(LC_WINDOWS)
209+
#if defined(LC_WINDOWS) && !defined(NXDK)
210210
// This error is specific to overlapped I/O which isn't even
211211
// possible to perform with recvfrom(). It seems to randomly
212212
// be returned instead of WSAETIMEDOUT on certain systems.
@@ -221,7 +221,7 @@ int recvUdpSocket(SOCKET s, char* buffer, int size, bool useSelect) {
221221
// We may receive an error due to a previous ICMP Port Unreachable error received
222222
// by this socket. We want to ignore those and continue reading. If the remote party
223223
// is really dead, ENet or TCP connection failures will trigger connection teardown.
224-
#if defined(LC_WINDOWS)
224+
#if defined(LC_WINDOWS) && !defined(NXDK)
225225
} while (err < 0 && LastSocketError() == WSAECONNRESET);
226226
#else
227227
} while (err < 0 && LastSocketError() == ECONNREFUSED);
@@ -231,7 +231,7 @@ int recvUdpSocket(SOCKET s, char* buffer, int size, bool useSelect) {
231231
}
232232

233233
void closeSocket(SOCKET s) {
234-
#if defined(LC_WINDOWS)
234+
#if defined(LC_WINDOWS) && !defined(NXDK)
235235
closesocket(s);
236236
#else
237237
close(s);
@@ -335,7 +335,7 @@ SOCKET bindUdpSocket(int addressFamily, struct sockaddr_storage* localAddr, SOCK
335335
int val = 1;
336336
setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (char*)&val, sizeof(val));
337337
}
338-
#elif defined(LC_WINDOWS)
338+
#elif defined(LC_WINDOWS) && !defined(NXDK)
339339
{
340340
// Disable WSAECONNRESET for UDP sockets on Windows
341341
BOOL val = FALSE;
@@ -409,10 +409,10 @@ int setSocketNonBlocking(SOCKET s, bool enabled) {
409409
#if defined(__vita__) || defined(__HAIKU__)
410410
int val = enabled ? 1 : 0;
411411
return setsockopt(s, SOL_SOCKET, SO_NONBLOCK, (char*)&val, sizeof(val));
412-
#elif defined(O_NONBLOCK)
412+
#elif defined(O_NONBLOCK) && !defined(NXDK)
413413
return fcntl(s, F_SETFL, (enabled ? O_NONBLOCK : 0) | (fcntl(s, F_GETFL) & ~O_NONBLOCK));
414414
#elif defined(FIONBIO)
415-
#ifdef LC_WINDOWS
415+
#if defined(LC_WINDOWS) && !defined(NXDK)
416416
u_long val = enabled ? 1 : 0;
417417
#else
418418
int val = enabled ? 1 : 0;
@@ -452,7 +452,6 @@ SOCKET connectTcpSocket(struct sockaddr_storage* dstaddr, SOCKADDR_LEN addrlen,
452452
LC_SOCKADDR addr;
453453
struct pollfd pfd;
454454
int err;
455-
int val;
456455

457456
// Create a non-blocking TCP socket
458457
s = createSocket(dstaddr->ss_family, SOCK_STREAM, IPPROTO_TCP, true);
@@ -471,7 +470,9 @@ SOCKET connectTcpSocket(struct sockaddr_storage* dstaddr, SOCKADDR_LEN addrlen,
471470
// Note: This only changes the max packet size we can *receive* from the host PC.
472471
// We still must split our own sends into smaller chunks with TCP_NODELAY enabled to
473472
// avoid MTU issues on the way out to to the target.
474-
#if defined(LC_WINDOWS)
473+
#if defined(LC_WINDOWS) && !defined(NXDK)
474+
int val;
475+
475476
// Windows doesn't support setting TCP_MAXSEG but IP_PMTUDISC_DONT forces the MSS to the protocol
476477
// minimum which is what we want here. Linux doesn't do this (disabling PMTUD just avoids setting DF).
477478
if (dstaddr->ss_family == AF_INET) {
@@ -491,12 +492,13 @@ SOCKET connectTcpSocket(struct sockaddr_storage* dstaddr, SOCKADDR_LEN addrlen,
491492
// restrict MSS to the minimum. It strips all options out of the SYN packet which
492493
// forces the remote party to fall back to the minimum MSS. TCP_MAXSEG doesn't seem
493494
// to work correctly for outbound connections on macOS/iOS.
494-
val = 1;
495+
int val = 1;
495496
if (setsockopt(s, IPPROTO_TCP, TCP_NOOPT, (char*)&val, sizeof(val)) < 0) {
496497
Limelog("setsockopt(TCP_NOOPT, %d) failed: %d\n", val, (int)LastSocketError());
497498
}
498499
#elif defined(TCP_MAXSEG)
499-
val = dstaddr->ss_family == AF_INET ? TCPv4_MSS : TCPv6_MSS;
500+
int val = dstaddr->ss_family == AF_INET ? TCPv4_MSS : TCPv6_MSS;
501+
500502
if (setsockopt(s, IPPROTO_TCP, TCP_MAXSEG, (char*)&val, sizeof(val)) < 0) {
501503
Limelog("setsockopt(TCP_MAXSEG, %d) failed: %d\n", val, (int)LastSocketError());
502504
}
@@ -1004,12 +1006,12 @@ void exitLowLatencyMode(void) {
10041006
}
10051007

10061008
int initializePlatformSockets(void) {
1007-
#if defined(LC_WINDOWS)
1009+
#if defined(LC_WINDOWS) && !defined(NXDK)
10081010
WSADATA data;
10091011
return WSAStartup(MAKEWORD(2, 0), &data);
10101012
#elif defined(__vita__) || defined(__WIIU__) || defined(__3DS__)
10111013
return 0; // already initialized
1012-
#elif defined(LC_POSIX) && !defined(LC_CHROME)
1014+
#elif defined(LC_POSIX) && !defined(LC_CHROME) && !defined(NXDK)
10131015
// Disable SIGPIPE signals to avoid us getting
10141016
// killed when a socket gets an EPIPE error
10151017
struct sigaction sa;
@@ -1027,7 +1029,7 @@ int initializePlatformSockets(void) {
10271029
}
10281030

10291031
void cleanupPlatformSockets(void) {
1030-
#if defined(LC_WINDOWS)
1032+
#if defined(LC_WINDOWS) && !defined(NXDK)
10311033
WSACleanup();
10321034
#else
10331035
#endif

src/PlatformSockets.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extern in_port_t n3ds_udp_port;
1818
#endif
1919
#endif
2020

21-
#ifdef _WIN32
21+
#if defined(_WIN32) && !defined(NXDK)
2222
#define WIN32_LEAN_AND_MEAN
2323
#include <windows.h>
2424
#include <wlanapi.h>
@@ -83,11 +83,19 @@ typedef int SOCKADDR_LEN;
8383
#include <signal.h>
8484
#include <poll.h>
8585

86+
#ifndef ioctlsocket
8687
#define ioctlsocket ioctl
88+
#endif
8789
#define LastSocketError() errno
8890
#define SetLastSocketError(x) errno = x
91+
92+
#ifndef INVALID_SOCKET
8993
#define INVALID_SOCKET -1
94+
#endif
95+
96+
#ifndef SOCKET_ERROR
9097
#define SOCKET_ERROR -1
98+
#endif
9199

92100
typedef int SOCKET;
93101
typedef ssize_t SOCK_RET;

src/PlatformThreads.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ typedef struct _PLT_THREAD {
3737
#error Unsupported platform
3838
#endif
3939

40-
#ifdef LC_WINDOWS
40+
#if defined(LC_WINDOWS)
4141
typedef HANDLE PLT_EVENT;
4242
#else
4343
typedef struct _PLT_EVENT {

src/rswrapper.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#define gemm DECORATE_FUNC(gemm, ISA_SUFFIX)
4040
#define invert_mat DECORATE_FUNC(invert_mat, ISA_SUFFIX)
4141

42-
#if defined(__x86_64__) || defined(__i386__) || (defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64)))
42+
#if !defined(NXDK) && (defined(__x86_64__) || defined(__i386__) || (defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))))
4343

4444
// Compile a variant for SSSE3
4545
#if defined(__clang__)
@@ -117,7 +117,7 @@ reed_solomon_release_t reed_solomon_release_fn;
117117
reed_solomon_encode_t reed_solomon_encode_fn;
118118
reed_solomon_decode_t reed_solomon_decode_fn;
119119

120-
#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))
120+
#if defined(_MSC_VER) && !defined(NXDK) && (defined(_M_IX86) || defined(_M_AMD64))
121121

122122
#if defined(_M_AMD64)
123123
// For some reason this is needed to avoid a "C1189 No target architecture" error from winnt.h
@@ -135,7 +135,7 @@ reed_solomon_decode_t reed_solomon_decode_fn;
135135
* @details The streaming code will directly invoke these function pointers during encoding.
136136
*/
137137
void reed_solomon_init(void) {
138-
#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))
138+
#if defined(_MSC_VER) && !defined(NXDK) && (defined(_M_IX86) || defined(_M_AMD64))
139139
// Visual Studio
140140
if (_msc_supports_avx512f()) {
141141
reed_solomon_new_fn = reed_solomon_new_avx512;

0 commit comments

Comments
 (0)