From 058ad2df23b1bb0b33be0ff7e4581759cf77df9f Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Fri, 31 Oct 2014 22:27:18 -0700 Subject: [PATCH] Add support for using a callback for writing debug messages --- limelight-common/Connection.c | 8 +++++--- limelight-common/Limelight.h | 2 ++ limelight-common/Platform.h | 8 +++++--- limelight-common/PlatformSockets.c | 2 +- limelight-common/PlatformSockets.h | 2 +- limelight-common/PlatformThreads.c | 22 +++++++++------------- limelight-common/PlatformThreads.h | 2 +- 7 files changed, 24 insertions(+), 22 deletions(-) diff --git a/limelight-common/Connection.c b/limelight-common/Connection.c index 4296bbc..a602c43 100644 --- a/limelight-common/Connection.c +++ b/limelight-common/Connection.c @@ -4,7 +4,9 @@ static int stage = STAGE_NONE; static CONNECTION_LISTENER_CALLBACKS listenerCallbacks; static CONNECTION_LISTENER_CALLBACKS originalCallbacks; -static PLATFORM_CALLBACKS platformCallbacks; + +// This is used for debug prints so it's not declared static +PLATFORM_CALLBACKS platformCallbacks; static int alreadyTerminated; @@ -161,13 +163,13 @@ int LiStartConnection(IP_ADDRESS host, PSTREAM_CONFIGURATION streamConfig, PCONN Limelog("Initializing platform..."); listenerCallbacks.stageStarting(STAGE_PLATFORM_INIT); - err = initializePlatformSockets(&platformCallbacks); + err = initializePlatformSockets(); if (err != 0) { Limelog("failed: %d\n", err); listenerCallbacks.stageFailed(STAGE_PLATFORM_INIT, err); goto Cleanup; } - err = initializePlatformThreads(&platformCallbacks); + err = initializePlatformThreads(); if (err != 0) { Limelog("failed: %d\n", err); listenerCallbacks.stageFailed(STAGE_PLATFORM_INIT, err); diff --git a/limelight-common/Limelight.h b/limelight-common/Limelight.h index e056425..e5d58f8 100644 --- a/limelight-common/Limelight.h +++ b/limelight-common/Limelight.h @@ -91,9 +91,11 @@ typedef struct _CONNECTION_LISTENER_CALLBACKS { } CONNECTION_LISTENER_CALLBACKS, *PCONNECTION_LISTENER_CALLBACKS; typedef void(*PlatformThreadStart)(void); +typedef void(*PlatformDebugPrint)(char* string); typedef struct _PLATFORM_CALLBACKS { PlatformThreadStart threadStart; + PlatformDebugPrint debugPrint; } PLATFORM_CALLBACKS, *PPLATFORM_CALLBACKS; int LiStartConnection(IP_ADDRESS host, PSTREAM_CONFIGURATION streamConfig, PCONNECTION_LISTENER_CALLBACKS clCallbacks, diff --git a/limelight-common/Platform.h b/limelight-common/Platform.h index 9a966f0..bde4761 100644 --- a/limelight-common/Platform.h +++ b/limelight-common/Platform.h @@ -27,11 +27,13 @@ #endif #include +#include "Limelight.h" #if defined(LC_WINDOWS_PHONE) || defined(LC_WINDOWS) -extern WCHAR DbgBuf[512]; +extern char DbgBuf[512]; +extern PLATFORM_CALLBACKS platformCallbacks; #define Limelog(s, ...) \ - swprintf(DbgBuf, sizeof(DbgBuf) / sizeof(WCHAR), L ## s, ##__VA_ARGS__); \ - OutputDebugStringW(DbgBuf) + sprintf(DbgBuf, s, ##__VA_ARGS__); \ + platformCallbacks.debugPrint(DbgBuf) #else #define Limelog printf #endif diff --git a/limelight-common/PlatformSockets.c b/limelight-common/PlatformSockets.c index 48df198..bc0e8bb 100644 --- a/limelight-common/PlatformSockets.c +++ b/limelight-common/PlatformSockets.c @@ -80,7 +80,7 @@ int enableNoDelay(SOCKET s) { return 0; } -int initializePlatformSockets(PPLATFORM_CALLBACKS plCallbacks) { +int initializePlatformSockets(void) { #if defined(LC_WINDOWS) || defined(LC_WINDOWS_PHONE) WSADATA data; return WSAStartup(MAKEWORD(2, 0), &data); diff --git a/limelight-common/PlatformSockets.h b/limelight-common/PlatformSockets.h index 5afc3be..f101872 100644 --- a/limelight-common/PlatformSockets.h +++ b/limelight-common/PlatformSockets.h @@ -33,5 +33,5 @@ typedef ssize_t SOCK_RET; SOCKET connectTcpSocket(IP_ADDRESS dstaddr, unsigned short port); SOCKET bindUdpSocket(void); int enableNoDelay(SOCKET s); -int initializePlatformSockets(PPLATFORM_CALLBACKS plCallbacks); +int initializePlatformSockets(void); void cleanupPlatformSockets(void); \ No newline at end of file diff --git a/limelight-common/PlatformThreads.c b/limelight-common/PlatformThreads.c index 9261631..78404b9 100644 --- a/limelight-common/PlatformThreads.c +++ b/limelight-common/PlatformThreads.c @@ -2,14 +2,13 @@ #include "Platform.h" #if defined(LC_WINDOWS_PHONE) || defined(LC_WINDOWS) -WCHAR DbgBuf[512]; +CHAR DbgBuf[512]; #endif #if defined(LC_WINDOWS) || defined(LC_WINDOWS_PHONE) -PLT_MUTEX thread_list_lock; -PLT_THREAD *pending_thread_head; -PLT_THREAD *thread_head; -PPLATFORM_CALLBACKS platformCallbacks; +static PLT_MUTEX thread_list_lock; +static PLT_THREAD *pending_thread_head; +static PLT_THREAD *thread_head; #else void* ThreadProc(void* context) { struct thread_context *ctx = (struct thread_context *)context; @@ -208,7 +207,7 @@ int PltCreateThread(ThreadEntry entry, void* context, PLT_THREAD *thread) { PltUnlockMutex(&thread_list_lock); // Make a callback to managed code to ask for a thread to grab this - platformCallbacks->threadStart(); + platformCallbacks.threadStart(); err = 0; } @@ -308,14 +307,8 @@ int PltWaitForEvent(PLT_EVENT *event) { #endif } -int initializePlatformThreads(PPLATFORM_CALLBACKS plCallbacks) { +int initializePlatformThreads(void) { #if defined(LC_WINDOWS) || defined(LC_WINDOWS_PHONE) - pending_thread_head = thread_head = NULL; - - // This data is stored in static memory in Connection.c, so we don't - // bother making our own copy - platformCallbacks = plCallbacks; - return PltCreateMutex(&thread_list_lock); #else return 0; @@ -324,6 +317,9 @@ int initializePlatformThreads(PPLATFORM_CALLBACKS plCallbacks) { void cleanupPlatformThreads(void) { #if defined(LC_WINDOWS) || defined(LC_WINDOWS_PHONE) + LC_ASSERT(pending_thread_head == NULL); + LC_ASSERT(thread_head == NULL); + PltDeleteMutex(&thread_list_lock); #else #endif diff --git a/limelight-common/PlatformThreads.h b/limelight-common/PlatformThreads.h index f597a8b..d12daac 100644 --- a/limelight-common/PlatformThreads.h +++ b/limelight-common/PlatformThreads.h @@ -35,7 +35,7 @@ typedef struct _PLT_EVENT { #error Unsupported platform #endif -int initializePlatformThreads(PPLATFORM_CALLBACKS plCallbacks); +int initializePlatformThreads(void); void cleanupPlatformThreads(void); int PltCreateMutex(PLT_MUTEX *mutex);