mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2026-06-17 14:21:30 +00:00
Implement POSIX support
This commit is contained in:
@@ -45,7 +45,7 @@ int initializeControlStream(IP_ADDRESS host, PSTREAM_CONFIGURATION streamConfigP
|
|||||||
}
|
}
|
||||||
|
|
||||||
void requestIdrFrame(void) {
|
void requestIdrFrame(void) {
|
||||||
PltSetEvent(resyncEvent);
|
PltSetEvent(&resyncEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
static PNVCTL_PACKET_HEADER readNvctlPacket(void) {
|
static PNVCTL_PACKET_HEADER readNvctlPacket(void) {
|
||||||
@@ -143,7 +143,7 @@ static void resyncThreadFunc(void* context) {
|
|||||||
header.type = PTYPE_RESYNC;
|
header.type = PTYPE_RESYNC;
|
||||||
header.payloadLength = PPAYLEN_RESYNC;
|
header.payloadLength = PPAYLEN_RESYNC;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
PltWaitForEvent(resyncEvent);
|
PltWaitForEvent(&resyncEvent);
|
||||||
|
|
||||||
err = send(ctlSock, (char*) &header, sizeof(header), 0);
|
err = send(ctlSock, (char*) &header, sizeof(header), 0);
|
||||||
if (err != sizeof(header)) {
|
if (err != sizeof(header)) {
|
||||||
@@ -161,20 +161,20 @@ static void resyncThreadFunc(void* context) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
PltClearEvent(resyncEvent);
|
PltClearEvent(&resyncEvent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int stopControlStream(void) {
|
int stopControlStream(void) {
|
||||||
closesocket(ctlSock);
|
closesocket(ctlSock);
|
||||||
|
|
||||||
PltJoinThread(heartbeatThread);
|
PltJoinThread(&heartbeatThread);
|
||||||
PltJoinThread(jitterThread);
|
PltJoinThread(&jitterThread);
|
||||||
PltJoinThread(resyncThread);
|
PltJoinThread(&resyncThread);
|
||||||
|
|
||||||
PltCloseThread(heartbeatThread);
|
PltCloseThread(&heartbeatThread);
|
||||||
PltCloseThread(jitterThread);
|
PltCloseThread(&jitterThread);
|
||||||
PltCloseThread(resyncThread);
|
PltCloseThread(&resyncThread);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ int offerQueueItem(PLINKED_BLOCKING_QUEUE queueHead, void* data) {
|
|||||||
entry->next = NULL;
|
entry->next = NULL;
|
||||||
entry->data = data;
|
entry->data = data;
|
||||||
|
|
||||||
PltLockMutex(queueHead->mutex);
|
PltLockMutex(&queueHead->mutex);
|
||||||
|
|
||||||
if (queueHead->head == NULL) {
|
if (queueHead->head == NULL) {
|
||||||
queueHead->head = entry;
|
queueHead->head = entry;
|
||||||
@@ -43,9 +43,9 @@ int offerQueueItem(PLINKED_BLOCKING_QUEUE queueHead, void* data) {
|
|||||||
lastEntry->next = entry;
|
lastEntry->next = entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
PltSetEvent(queueHead->containsDataEvent);
|
PltSetEvent(&queueHead->containsDataEvent);
|
||||||
|
|
||||||
PltUnlockMutex(queueHead->mutex);
|
PltUnlockMutex(&queueHead->mutex);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -55,12 +55,12 @@ void* waitForQueueElement(PLINKED_BLOCKING_QUEUE queueHead) {
|
|||||||
void* data;
|
void* data;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
PltWaitForEvent(queueHead->containsDataEvent);
|
PltWaitForEvent(&queueHead->containsDataEvent);
|
||||||
|
|
||||||
PltLockMutex(queueHead->mutex);
|
PltLockMutex(&queueHead->mutex);
|
||||||
|
|
||||||
if (queueHead->head == NULL) {
|
if (queueHead->head == NULL) {
|
||||||
PltUnlockMutex(queueHead->mutex);
|
PltUnlockMutex(&queueHead->mutex);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,10 +72,10 @@ void* waitForQueueElement(PLINKED_BLOCKING_QUEUE queueHead) {
|
|||||||
free(entry);
|
free(entry);
|
||||||
|
|
||||||
if (queueHead->head == NULL) {
|
if (queueHead->head == NULL) {
|
||||||
PltClearEvent(queueHead->containsDataEvent);
|
PltClearEvent(&queueHead->containsDataEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
PltUnlockMutex(queueHead->mutex);
|
PltUnlockMutex(&queueHead->mutex);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,4 +3,8 @@
|
|||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#else
|
#else
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <pthread.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -20,7 +20,7 @@ SOCKET bindUdpSocket(unsigned short port) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val = 65536;
|
val = 65536;
|
||||||
int err = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char*) &val, sizeof(val));
|
setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char*) &val, sizeof(val));
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,11 @@
|
|||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#define LastSocketError() WSAGetLastError()
|
#define LastSocketError() WSAGetLastError()
|
||||||
#else
|
#else
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/tcp.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <errno.h>
|
||||||
#define SOCKET int
|
#define SOCKET int
|
||||||
#define LastSocketError() errno
|
#define LastSocketError() errno
|
||||||
#define INVALID_SOCKET -1
|
#define INVALID_SOCKET -1
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "PlatformThreads.h"
|
#include "PlatformThreads.h"
|
||||||
|
#include "Platform.h"
|
||||||
|
|
||||||
struct thread_context {
|
struct thread_context {
|
||||||
ThreadEntry entry;
|
ThreadEntry entry;
|
||||||
@@ -16,13 +17,23 @@ DWORD WINAPI ThreadProc(LPVOID lpParameter) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
#error POSIX threads not implemented
|
void* ThreadProc(void* context) {
|
||||||
|
struct thread_context *ctx = (struct thread_context *)context;
|
||||||
|
|
||||||
|
ctx->entry(ctx->context);
|
||||||
|
|
||||||
|
free(ctx);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void PltSleepMs(int ms) {
|
void PltSleepMs(int ms) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
Sleep(ms);
|
Sleep(ms);
|
||||||
#else
|
#else
|
||||||
|
long usecs = (long)ms * 1000;
|
||||||
|
usleep(usecs);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,40 +45,45 @@ int PltCreateMutex(PLT_MUTEX *mutex) {
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
#else
|
#else
|
||||||
|
return pthread_mutex_init(mutex, NULL);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void PltDeleteMutex(PLT_MUTEX mutex) {
|
void PltDeleteMutex(PLT_MUTEX *mutex) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
CloseHandle(mutex);
|
CloseHandle(*mutex);
|
||||||
#else
|
#else
|
||||||
|
pthread_mutex_destroy(mutex);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void PltLockMutex(PLT_MUTEX mutex) {
|
void PltLockMutex(PLT_MUTEX *mutex) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
WaitForSingleObject(mutex, INFINITE);
|
WaitForSingleObject(*mutex, INFINITE);
|
||||||
#else
|
#else
|
||||||
|
pthread_mutex_lock(mutex);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void PltUnlockMutex(PLT_MUTEX mutex) {
|
void PltUnlockMutex(PLT_MUTEX *mutex) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
ReleaseMutex(mutex);
|
ReleaseMutex(*mutex);
|
||||||
#else
|
#else
|
||||||
|
pthread_mutex_unlock(mutex);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void PltJoinThread(PLT_THREAD thread) {
|
void PltJoinThread(PLT_THREAD *thread) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
WaitForSingleObject(thread, INFINITE);
|
WaitForSingleObject(*thread, INFINITE);
|
||||||
#else
|
#else
|
||||||
|
pthread_join(*thread, NULL);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void PltCloseThread(PLT_THREAD thread) {
|
void PltCloseThread(PLT_THREAD *thread) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
CloseHandle(thread);
|
CloseHandle(*thread);
|
||||||
#else
|
#else
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -97,6 +113,12 @@ int PltCreateThread(ThreadEntry entry, void* context, PLT_THREAD *thread) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
{
|
||||||
|
err = pthread_create(thread, NULL, ThreadProc, ctx);
|
||||||
|
if (err != 0) {
|
||||||
|
free(ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
@@ -111,41 +133,62 @@ int PltCreateEvent(PLT_EVENT *event) {
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
#else
|
#else
|
||||||
|
pthread_mutex_init(&event->mutex, NULL);
|
||||||
|
pthread_cond_init(&event->cond, NULL);
|
||||||
|
event->signalled = 0;
|
||||||
|
return 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void PltCloseEvent(PLT_EVENT event) {
|
void PltCloseEvent(PLT_EVENT *event) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
CloseHandle(event);
|
CloseHandle(*event);
|
||||||
#else
|
#else
|
||||||
|
pthread_mutex_destroy(&event->mutex);
|
||||||
|
pthread_cond_destroy(&event->cond);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void PltSetEvent(PLT_EVENT event) {
|
void PltSetEvent(PLT_EVENT *event) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
SetEvent(event);
|
SetEvent(*event);
|
||||||
#else
|
#else
|
||||||
|
pthread_mutex_lock(&event->mutex);
|
||||||
|
event->signalled = 1;
|
||||||
|
pthread_cond_signal(&event->cond);
|
||||||
|
pthread_mutex_unlock(&event->mutex);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void PltClearEvent(PLT_EVENT event) {
|
void PltClearEvent(PLT_EVENT *event) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
ResetEvent(event);
|
ResetEvent(*event);
|
||||||
#else
|
#else
|
||||||
|
pthread_mutex_lock(&event->mutex);
|
||||||
|
event->signalled = 0;
|
||||||
|
pthread_mutex_unlock(&event->mutex);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void PltPulseEvent(PLT_EVENT event) {
|
void PltPulseEvent(PLT_EVENT *event) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
PulseEvent(event);
|
PulseEvent(*event);
|
||||||
#else
|
#else
|
||||||
|
pthread_mutex_lock(&event->mutex);
|
||||||
|
event->signalled = 1;
|
||||||
|
pthread_cond_signal(&event->cond);
|
||||||
|
event->signalled = 0;
|
||||||
|
pthread_mutex_unlock(&event->mutex);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void PltWaitForEvent(PLT_EVENT event) {
|
void PltWaitForEvent(PLT_EVENT *event) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
WaitForSingleObject(event, INFINITE);
|
WaitForSingleObject(*event, INFINITE);
|
||||||
#else
|
#else
|
||||||
|
while (!event->signalled) {
|
||||||
|
pthread_cond_wait(&event->cond, &event->mutex);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,22 +9,29 @@ typedef HANDLE PLT_THREAD;
|
|||||||
typedef HANDLE PLT_MUTEX;
|
typedef HANDLE PLT_MUTEX;
|
||||||
typedef HANDLE PLT_EVENT;
|
typedef HANDLE PLT_EVENT;
|
||||||
#else
|
#else
|
||||||
|
typedef pthread_t PLT_THREAD;
|
||||||
|
typedef pthread_mutex_t PLT_MUTEX;
|
||||||
|
typedef struct _PLT_EVENT {
|
||||||
|
pthread_mutex_t mutex;
|
||||||
|
pthread_cond_t cond;
|
||||||
|
int signalled;
|
||||||
|
} PLT_EVENT;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int PltCreateMutex(PLT_MUTEX *mutex);
|
int PltCreateMutex(PLT_MUTEX *mutex);
|
||||||
void PltDeleteMutex(PLT_MUTEX mutex);
|
void PltDeleteMutex(PLT_MUTEX *mutex);
|
||||||
void PltLockMutex(PLT_MUTEX mutex);
|
void PltLockMutex(PLT_MUTEX *mutex);
|
||||||
void PltUnlockMutex(PLT_MUTEX mutex);
|
void PltUnlockMutex(PLT_MUTEX *mutex);
|
||||||
|
|
||||||
int PltCreateThread(ThreadEntry entry, void* context, PLT_THREAD *thread);
|
int PltCreateThread(ThreadEntry entry, void* context, PLT_THREAD *thread);
|
||||||
void PltCloseThread(PLT_THREAD thread);
|
void PltCloseThread(PLT_THREAD *thread);
|
||||||
void PltJoinThread(PLT_THREAD thread);
|
void PltJoinThread(PLT_THREAD *thread);
|
||||||
|
|
||||||
int PltCreateEvent(PLT_EVENT *event);
|
int PltCreateEvent(PLT_EVENT *event);
|
||||||
void PltCloseEvent(PLT_EVENT event);
|
void PltCloseEvent(PLT_EVENT *event);
|
||||||
void PltSetEvent(PLT_EVENT event);
|
void PltSetEvent(PLT_EVENT *event);
|
||||||
void PltClearEvent(PLT_EVENT event);
|
void PltClearEvent(PLT_EVENT *event);
|
||||||
void PltPulseEvent(PLT_EVENT event);
|
void PltPulseEvent(PLT_EVENT *event);
|
||||||
void PltWaitForEvent(PLT_EVENT event);
|
void PltWaitForEvent(PLT_EVENT *event);
|
||||||
|
|
||||||
void PltSleepMs(int ms);
|
void PltSleepMs(int ms);
|
||||||
Reference in New Issue
Block a user