mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2026-02-16 02:21:07 +00:00
Add vita environment (#22)
* vita: initial port * vita: implement events, enable input thread * vita: gethostbyname * vita: Enable audio * vita: Fix thread crash on discoonect process * vita: Handle cannot create thread * vita: now use newlib's socket apis * vita: Refactoring for moonlight-stream/moonlight-common-c * Fix review things https://github.com/moonlight-stream/moonlight-common-c/pull/22#pullrequestreview-2436093 - vita may not support IPv6; so add LC_ASSERT them. - define inet_ntop to sceNetInetNtop - guard about failure of sceKernelCreateMutex or sceKernelCreateCond - remove meanless macros https://github.com/moonlight-stream/moonlight-common-c/pull/22#pullrequestreview-2444677 - !*mutex to *mutex - remove useless LC_ASSERT then change to use inet_ntop in vita system not defined `sockaddr_in6`, so just use `sockaddr_in` instead this. https://github.com/moonlight-stream/moonlight-common-c/pull/22#pullrequestreview-2445642 - define sin6_addr
This commit is contained in:
committed by
Cameron Gutman
parent
293c6a7274
commit
ba27e97698
@@ -153,6 +153,7 @@ static void ClInternalConnectionTerminated(long errorCode)
|
||||
|
||||
static int resolveHostName(const char* host)
|
||||
{
|
||||
#ifndef __vita__
|
||||
struct addrinfo hints, *res;
|
||||
int err;
|
||||
|
||||
@@ -187,6 +188,21 @@ static int resolveHostName(const char* host)
|
||||
|
||||
freeaddrinfo(res);
|
||||
return 0;
|
||||
#else
|
||||
struct hostent *phost = gethostbyname(host);
|
||||
if (!phost) {
|
||||
Limelog("gethostbyname() failed for host %s\n", host);
|
||||
return -1;
|
||||
}
|
||||
struct sockaddr_in tmp = {0};
|
||||
tmp.sin_len = sizeof(tmp);
|
||||
tmp.sin_family = SCE_NET_AF_INET;
|
||||
memcpy(&tmp.sin_addr, phost->h_addr, phost->h_length);
|
||||
|
||||
memcpy(&RemoteAddr, &tmp, sizeof(tmp));
|
||||
RemoteAddrLen = sizeof(tmp);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Starts the connection to the streaming machine
|
||||
@@ -345,4 +361,4 @@ int LiStartConnection(const char* host, PSTREAM_CONFIGURATION streamConfig, PCON
|
||||
|
||||
Cleanup:
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,9 @@ void cleanupPlatformSockets(void);
|
||||
struct thread_context {
|
||||
ThreadEntry entry;
|
||||
void* context;
|
||||
#if defined(__vita__)
|
||||
PLT_THREAD* thread;
|
||||
#endif
|
||||
};
|
||||
|
||||
static int running_threads = 0;
|
||||
@@ -29,6 +32,9 @@ void LimelogWindows(char* Format, ...) {
|
||||
#if defined(LC_WINDOWS)
|
||||
DWORD WINAPI ThreadProc(LPVOID lpParameter) {
|
||||
struct thread_context* ctx = (struct thread_context*)lpParameter;
|
||||
#elif defined(__vita__)
|
||||
int ThreadProc(SceSize args, void *argp) {
|
||||
struct thread_context* ctx = (struct thread_context*)argp;
|
||||
#else
|
||||
void* ThreadProc(void* context) {
|
||||
struct thread_context* ctx = (struct thread_context*)context;
|
||||
@@ -36,9 +42,13 @@ void* ThreadProc(void* context) {
|
||||
|
||||
ctx->entry(ctx->context);
|
||||
|
||||
#if defined(__vita__)
|
||||
ctx->thread->alive = 0;
|
||||
#else
|
||||
free(ctx);
|
||||
#endif
|
||||
|
||||
#if defined(LC_WINDOWS)
|
||||
#if defined(LC_WINDOWS) || defined(__vita__)
|
||||
return 0;
|
||||
#else
|
||||
return NULL;
|
||||
@@ -48,6 +58,8 @@ void* ThreadProc(void* context) {
|
||||
void PltSleepMs(int ms) {
|
||||
#if defined(LC_WINDOWS)
|
||||
WaitForSingleObjectEx(GetCurrentThread(), ms, FALSE);
|
||||
#elif defined(__vita__)
|
||||
sceKernelDelayThread(ms * 1000);
|
||||
#else
|
||||
useconds_t usecs = ms * 1000;
|
||||
usleep(usecs);
|
||||
@@ -61,6 +73,12 @@ int PltCreateMutex(PLT_MUTEX* mutex) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
#elif defined(__vita__)
|
||||
*mutex = sceKernelCreateMutex("", 0, 0, NULL);
|
||||
if (*mutex < 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
#else
|
||||
return pthread_mutex_init(mutex, NULL);
|
||||
#endif
|
||||
@@ -69,6 +87,8 @@ int PltCreateMutex(PLT_MUTEX* mutex) {
|
||||
void PltDeleteMutex(PLT_MUTEX* mutex) {
|
||||
#if defined(LC_WINDOWS)
|
||||
CloseHandle(*mutex);
|
||||
#elif defined(__vita__)
|
||||
sceKernelDeleteMutex(*mutex);
|
||||
#else
|
||||
pthread_mutex_destroy(mutex);
|
||||
#endif
|
||||
@@ -81,6 +101,8 @@ void PltLockMutex(PLT_MUTEX* mutex) {
|
||||
if (err != WAIT_OBJECT_0) {
|
||||
LC_ASSERT(FALSE);
|
||||
}
|
||||
#elif defined(__vita__)
|
||||
sceKernelLockMutex(*mutex, 1, NULL);
|
||||
#else
|
||||
pthread_mutex_lock(mutex);
|
||||
#endif
|
||||
@@ -89,6 +111,8 @@ void PltLockMutex(PLT_MUTEX* mutex) {
|
||||
void PltUnlockMutex(PLT_MUTEX* mutex) {
|
||||
#if defined(LC_WINDOWS)
|
||||
ReleaseMutex(*mutex);
|
||||
#elif defined(__vita__)
|
||||
sceKernelUnlockMutex(*mutex, 1);
|
||||
#else
|
||||
pthread_mutex_unlock(mutex);
|
||||
#endif
|
||||
@@ -98,6 +122,12 @@ void PltJoinThread(PLT_THREAD* thread) {
|
||||
LC_ASSERT(thread->cancelled);
|
||||
#if defined(LC_WINDOWS)
|
||||
WaitForSingleObjectEx(thread->handle, INFINITE, FALSE);
|
||||
#elif defined(__vita__)
|
||||
while(thread->alive) {
|
||||
PltSleepMs(10);
|
||||
}
|
||||
if (thread->context != NULL)
|
||||
free(thread->context);
|
||||
#else
|
||||
pthread_join(thread->thread, NULL);
|
||||
#endif
|
||||
@@ -107,6 +137,8 @@ void PltCloseThread(PLT_THREAD* thread) {
|
||||
running_threads--;
|
||||
#if defined(LC_WINDOWS)
|
||||
CloseHandle(thread->handle);
|
||||
#elif defined(__vita__)
|
||||
sceKernelDeleteThread(thread->handle);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -139,6 +171,18 @@ int PltCreateThread(ThreadEntry entry, void* context, PLT_THREAD* thread) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
#elif defined(__vita__)
|
||||
{
|
||||
thread->alive = 1;
|
||||
thread->context = ctx;
|
||||
ctx->thread = thread;
|
||||
thread->handle = sceKernelCreateThread("", ThreadProc, 0, 0x10000, 0, 0, NULL);
|
||||
if (thread->handle < 0) {
|
||||
free(ctx);
|
||||
return -1;
|
||||
}
|
||||
sceKernelStartThread(thread->handle, sizeof(struct thread_context), ctx);
|
||||
}
|
||||
#else
|
||||
{
|
||||
int err = pthread_create(&thread->thread, NULL, ThreadProc, ctx);
|
||||
@@ -161,6 +205,18 @@ int PltCreateEvent(PLT_EVENT* event) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#elif defined(__vita__)
|
||||
event->mutex = sceKernelCreateMutex("", 0, 0, NULL);
|
||||
if (event->mutex < 0) {
|
||||
return -1;
|
||||
}
|
||||
event->cond = sceKernelCreateCond("", 0, event->mutex, NULL);
|
||||
if (event->cond < 0) {
|
||||
sceKernelDeleteMutex(event->mutex);
|
||||
return -1;
|
||||
}
|
||||
event->signalled = 0;
|
||||
return 0;
|
||||
#else
|
||||
pthread_mutex_init(&event->mutex, NULL);
|
||||
@@ -173,6 +229,9 @@ int PltCreateEvent(PLT_EVENT* event) {
|
||||
void PltCloseEvent(PLT_EVENT* event) {
|
||||
#if defined(LC_WINDOWS)
|
||||
CloseHandle(*event);
|
||||
#elif defined(__vita__)
|
||||
sceKernelDeleteCond(event->cond);
|
||||
sceKernelDeleteMutex(event->mutex);
|
||||
#else
|
||||
pthread_mutex_destroy(&event->mutex);
|
||||
pthread_cond_destroy(&event->cond);
|
||||
@@ -182,6 +241,9 @@ void PltCloseEvent(PLT_EVENT* event) {
|
||||
void PltSetEvent(PLT_EVENT* event) {
|
||||
#if defined(LC_WINDOWS)
|
||||
SetEvent(*event);
|
||||
#elif defined(__vita__)
|
||||
event->signalled = 1;
|
||||
sceKernelSignalCondAll(event->cond);
|
||||
#else
|
||||
event->signalled = 1;
|
||||
pthread_cond_broadcast(&event->cond);
|
||||
@@ -208,6 +270,14 @@ int PltWaitForEvent(PLT_EVENT* event) {
|
||||
LC_ASSERT(0);
|
||||
return -1;
|
||||
}
|
||||
#elif defined(__vita__)
|
||||
sceKernelLockMutex(event->mutex, 1, NULL);
|
||||
while (!event->signalled) {
|
||||
sceKernelWaitCond(event->cond, NULL);
|
||||
}
|
||||
sceKernelUnlockMutex(event->mutex, 1);
|
||||
|
||||
return PLT_WAIT_SUCCESS;
|
||||
#else
|
||||
pthread_mutex_lock(&event->mutex);
|
||||
while (!event->signalled) {
|
||||
@@ -253,4 +323,4 @@ void cleanupPlatform(void) {
|
||||
enet_deinitialize();
|
||||
|
||||
LC_ASSERT(running_threads == 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,11 @@
|
||||
#include <Windows.h>
|
||||
#include <Winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#elif defined(__vita__)
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include <netinet/in.h>
|
||||
#include <psp2/kernel/threadmgr.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
@@ -34,6 +39,8 @@
|
||||
void LimelogWindows(char* Format, ...);
|
||||
#define Limelog(s, ...) \
|
||||
LimelogWindows(s, ##__VA_ARGS__)
|
||||
#elif defined(__vita__)
|
||||
#define Limelog sceClibPrintf
|
||||
#else
|
||||
#define Limelog(s, ...) \
|
||||
fprintf(stderr, s, ##__VA_ARGS__)
|
||||
|
||||
@@ -79,7 +79,11 @@ SOCKET bindUdpSocket(int addrfamily, int bufferSize) {
|
||||
struct sockaddr_storage addr;
|
||||
int err;
|
||||
|
||||
#ifndef __vita__
|
||||
LC_ASSERT(addrfamily == AF_INET || addrfamily == AF_INET6);
|
||||
#else
|
||||
LC_ASSERT(addrfamily == AF_INET);
|
||||
#endif
|
||||
|
||||
s = socket(addrfamily, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (s == INVALID_SOCKET) {
|
||||
@@ -253,6 +257,8 @@ int initializePlatformSockets(void) {
|
||||
#if defined(LC_WINDOWS)
|
||||
WSADATA data;
|
||||
return WSAStartup(MAKEWORD(2, 0), &data);
|
||||
#elif defined(__vita__)
|
||||
return 0; // already initialized
|
||||
#elif defined(LC_POSIX) && !defined(LC_CHROME)
|
||||
// Disable SIGPIPE signals to avoid us getting
|
||||
// killed when a socket gets an EPIPE error
|
||||
@@ -275,4 +281,4 @@ void cleanupPlatformSockets(void) {
|
||||
WSACleanup();
|
||||
#else
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,16 @@
|
||||
typedef int SOCK_RET;
|
||||
typedef int SOCKADDR_LEN;
|
||||
|
||||
#else
|
||||
#if defined(__vita__)
|
||||
#include <psp2/net/net.h>
|
||||
#include <enet/enet.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/select.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <errno.h>
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
@@ -24,6 +34,7 @@ typedef int SOCKADDR_LEN;
|
||||
#include <netdb.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#endif
|
||||
|
||||
#define ioctlsocket ioctl
|
||||
#define LastSocketError() errno
|
||||
@@ -36,6 +47,17 @@ typedef ssize_t SOCK_RET;
|
||||
typedef socklen_t SOCKADDR_LEN;
|
||||
#endif
|
||||
|
||||
#if defined(__vita__)
|
||||
#define TCP_NODELAY SCE_NET_TCP_NODELAY
|
||||
#define INADDR_ANY SCE_NET_INADDR_ANY
|
||||
|
||||
#define sockaddr_in6 sockaddr_in
|
||||
#define sin6_addr sin_addr
|
||||
#define sin6_port sin_port
|
||||
#define INET6_ADDRSTRLEN 128
|
||||
#define inet_ntop sceNetInetNtop
|
||||
#endif
|
||||
|
||||
#define LastSocketFail() ((LastSocketError() != 0) ? LastSocketError() : -1)
|
||||
|
||||
// IPv6 addresses have 2 extra characters for URL escaping
|
||||
@@ -48,4 +70,4 @@ int enableNoDelay(SOCKET s);
|
||||
int recvUdpSocket(SOCKET s, char* buffer, int size);
|
||||
void shutdownTcpSocket(SOCKET s);
|
||||
void setRecvTimeout(SOCKET s, int timeoutSec);
|
||||
void closeSocket(SOCKET s);
|
||||
void closeSocket(SOCKET s);
|
||||
|
||||
@@ -12,6 +12,19 @@ typedef struct _PLT_THREAD {
|
||||
HANDLE handle;
|
||||
int cancelled;
|
||||
} PLT_THREAD;
|
||||
#elif defined(__vita__)
|
||||
typedef int PLT_MUTEX;
|
||||
typedef struct _PLT_EVENT {
|
||||
int mutex;
|
||||
int cond;
|
||||
int signalled;
|
||||
} PLT_EVENT;
|
||||
typedef struct _PLT_THREAD {
|
||||
int handle;
|
||||
int cancelled;
|
||||
void *context;
|
||||
int alive;
|
||||
} PLT_THREAD;
|
||||
#elif defined (LC_POSIX)
|
||||
typedef pthread_mutex_t PLT_MUTEX;
|
||||
typedef struct _PLT_EVENT {
|
||||
|
||||
@@ -243,4 +243,4 @@ int startVideoStream(void* rendererContext, int drFlags) {
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user