From ccdb42d1509feea4f937d18f6fac914f55e4fda4 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Sun, 26 Mar 2006 17:43:10 +0000 Subject: [PATCH] Portability --- vdr.cc | 227 ++++++++++++++++++++++++++++++++++----------------------- vdr.h | 10 ++- 2 files changed, 144 insertions(+), 93 deletions(-) diff --git a/vdr.cc b/vdr.cc index 689d316..2db53f8 100644 --- a/vdr.cc +++ b/vdr.cc @@ -22,6 +22,15 @@ VDR* VDR::instance = NULL; +#ifndef WIN32 +#define MUTEX_LOCK(mutex) pthread_mutex_lock(mutex) +#define MUTEX_UNLOCK(mutex) pthread_mutex_unlock(mutex) +#else +#define MUTEX_LOCK(mutex) WaitForSingleObject(mutex, INFINITE ) +#define MUTEX_UNLOCK(mutex) ReleaseMutex(mutex) +#endif + + VDR::VDR() { if (instance) return; @@ -29,7 +38,11 @@ VDR::VDR() initted = 0; findingServer = 0; tcp = NULL; +#ifndef WIN32 pthread_mutex_init(&mutex, NULL); +#else + mutex=CreateMutex(NULL,TRUE,NULL); +#endif packetLength = 0; packetPos = 0; packet = NULL; @@ -38,6 +51,9 @@ VDR::VDR() VDR::~VDR() { +#ifdef WIN32 + CloseHandle(mutex); +#endif instance = NULL; if (initted) shutdown(); } @@ -233,14 +249,19 @@ int VDR::doLogin() tcp->getMAC((char*)&buffer[8]); - pthread_mutex_lock(&mutex); - if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + + MUTEX_LOCK(&mutex); + if (!connected) + { + MUTEX_UNLOCK(&mutex); + return 0; + } int a = tcp->sendPacket(buffer, 14); if (a != 14) { disconnect(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } @@ -248,7 +269,7 @@ int VDR::doLogin() if (!getPacket()) { - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } @@ -258,9 +279,11 @@ int VDR::doLogin() logger->log("VDR", Log::DEBUG, "offset = %i", vdrTimeOffset); freePacket(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); + // Set the time and zone on the MVP +#ifndef WIN32 struct timespec currentTime; currentTime.tv_sec = vdrTime; currentTime.tv_nsec = 0; @@ -291,6 +314,7 @@ int VDR::doLogin() setenv("TZ", newTZ, 1); logger->log("VDR", Log::DEBUG, "Timezone data: %s", newTZ); +#endif return 1; } @@ -302,14 +326,18 @@ Directory* VDR::getRecordingsList() *(unsigned long*)&buffer[0] = htonl(4); *(unsigned long*)&buffer[4] = htonl(VDR_GETRECORDINGLIST); - pthread_mutex_lock(&mutex); - if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + MUTEX_LOCK(&mutex); + if (!connected) + { + MUTEX_UNLOCK(&mutex); + return 0; + } int a = tcp->sendPacket(buffer, 8); if (a != 8) { disconnect(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return NULL; } @@ -317,7 +345,7 @@ Directory* VDR::getRecordingsList() if (!getPacket()) { - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return NULL; } @@ -367,8 +395,7 @@ Directory* VDR::getRecordingsList() } freePacket(); - pthread_mutex_unlock(&mutex); - + MUTEX_UNLOCK(&mutex); // Sort the directory order sort(recDir->dirList.begin(), recDir->dirList.end(), DirectorySorter()); @@ -391,32 +418,34 @@ Directory* VDR::getRecordingsList() int VDR::deleteRecording(char* fileName) { unsigned long totalLength = 8 + strlen(fileName) + 1; - UCHAR buffer[totalLength]; + UCHAR* buffer = new UCHAR[totalLength]; *(unsigned long*)&buffer[0] = htonl(totalLength - 4); *(unsigned long*)&buffer[4] = htonl(VDR_DELETERECORDING); strcpy((char*)&buffer[8], fileName); - pthread_mutex_lock(&mutex); - if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + MUTEX_LOCK(&mutex); + if (!connected) { MUTEX_UNLOCK(&mutex); return 0; } unsigned int a = tcp->sendPacket(buffer, totalLength); + delete []buffer; + if (a != totalLength) { disconnect(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } if (!getPacket()) { - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } int toReturn = (int)extractULONG(); freePacket(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return toReturn; } @@ -424,31 +453,33 @@ int VDR::deleteRecording(char* fileName) char* VDR::getRecordingSummary(char* fileName) { unsigned long totalLength = 8 + strlen(fileName) + 1; - UCHAR buffer[totalLength]; + UCHAR* buffer = new UCHAR[totalLength]; *(unsigned long*)&buffer[0] = htonl(totalLength - 4); *(unsigned long*)&buffer[4] = htonl(VDR_GETSUMMARY); strcpy((char*)&buffer[8], fileName); - pthread_mutex_lock(&mutex); - if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + MUTEX_LOCK(&mutex); + if (!connected) { MUTEX_UNLOCK(&mutex); return 0; } unsigned int a = tcp->sendPacket(buffer, totalLength); + delete []buffer; + if (a != totalLength) { disconnect(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return NULL; } if (!getPacket()) { - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return NULL; } char* toReturn = extractString(); freePacket(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return toReturn; } @@ -460,14 +491,14 @@ ChannelList* VDR::getChannelsList(ULONG type) *(unsigned long*)&buffer[0] = htonl(4); *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELLIST); - pthread_mutex_lock(&mutex); - if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + MUTEX_LOCK(&mutex); + if (!connected) { MUTEX_UNLOCK(&mutex); return 0; } int a = tcp->sendPacket(buffer, 8); if (a != 8) { disconnect(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return NULL; } @@ -475,7 +506,7 @@ ChannelList* VDR::getChannelsList(ULONG type) if (!getPacket()) { - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return NULL; } @@ -500,7 +531,7 @@ ChannelList* VDR::getChannelsList(ULONG type) } freePacket(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return chanList; } @@ -513,27 +544,27 @@ int VDR::streamChannel(ULONG number) *(unsigned long*)&buffer[4] = htonl(VDR_STREAMCHANNEL); *(unsigned long*)&buffer[8] = htonl(number); - pthread_mutex_lock(&mutex); - if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + MUTEX_LOCK(&mutex); + if (!connected) { MUTEX_UNLOCK(&mutex); return 0; } int a = tcp->sendPacket(buffer, 12); if (a != 12) { disconnect(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } if (!getPacket()) { - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } int toReturn = (int)extractULONG(); freePacket(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return toReturn; } @@ -545,27 +576,27 @@ int VDR::stopStreaming() *(unsigned long*)&buffer[0] = htonl(4); *(unsigned long*)&buffer[4] = htonl(VDR_STOPSTREAMING); - pthread_mutex_lock(&mutex); - if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + MUTEX_LOCK(&mutex); + if (!connected) { MUTEX_UNLOCK(&mutex); return 0; } int a = tcp->sendPacket(buffer, 8); if (a != 8) { disconnect(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } if (!getPacket()) { - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } int toReturn = (int)extractULONG(); freePacket(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return toReturn; } @@ -579,20 +610,20 @@ UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived) *(ULLONG*)&buffer[8] = htonll(position); *(unsigned long*)&buffer[16] = htonl(maxAmount); - pthread_mutex_lock(&mutex); - if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + MUTEX_LOCK(&mutex); + if (!connected) { MUTEX_UNLOCK(&mutex); return 0; } int a = tcp->sendPacket(buffer, 20); if (a != 20) { disconnect(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return NULL; } if (!getPacket()) { - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return NULL; } @@ -600,7 +631,7 @@ UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived) { Log::getInstance()->log("VDR", Log::DEBUG, "Detected getblock 0"); freePacket(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return NULL; } @@ -610,7 +641,8 @@ UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived) packet = NULL; packetLength = 0; packetPos = 0; - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); + return toReturn; } @@ -618,32 +650,34 @@ UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived) ULLONG VDR::streamRecording(Recording* rec) { unsigned long totalLength = 8 + strlen(rec->fileName) + 1; - UCHAR buffer[totalLength]; + UCHAR* buffer = new UCHAR[totalLength]; *(unsigned long*)&buffer[0] = htonl(totalLength - 4); *(unsigned long*)&buffer[4] = htonl(VDR_STREAMRECORDING); strcpy((char*)&buffer[8], rec->fileName); - pthread_mutex_lock(&mutex); - if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + MUTEX_LOCK(&mutex); + if (!connected) { MUTEX_UNLOCK(&mutex); return 0; } unsigned int a = tcp->sendPacket(buffer, totalLength); + delete []buffer; + if (a != totalLength) { disconnect(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } if (!getPacket()) { - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } ULLONG recordingLength = extractULLONG(); freePacket(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength); @@ -653,31 +687,33 @@ ULLONG VDR::streamRecording(Recording* rec) ULLONG VDR::rescanRecording() { unsigned long totalLength = 8; - UCHAR buffer[totalLength]; + UCHAR* buffer = new UCHAR[totalLength]; *(unsigned long*)&buffer[0] = htonl(totalLength - 4); *(unsigned long*)&buffer[4] = htonl(VDR_RESCANRECORDING); - pthread_mutex_lock(&mutex); - if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + MUTEX_LOCK(&mutex); + if (!connected) { MUTEX_UNLOCK(&mutex); return 0; } unsigned int a = tcp->sendPacket(buffer, totalLength); + delete []buffer; + if (a != totalLength) { disconnect(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } if (!getPacket()) { - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } ULLONG recordingLength = extractULLONG(); freePacket(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength); @@ -687,32 +723,34 @@ ULLONG VDR::rescanRecording() ULLONG VDR::positionFromFrameNumber(ULONG frameNumber) { unsigned long totalLength = 12; - UCHAR buffer[totalLength]; + UCHAR* buffer = new UCHAR[totalLength]; *(unsigned long*)&buffer[0] = htonl(totalLength - 4); *(unsigned long*)&buffer[4] = htonl(VDR_POSFROMFRAME); *(unsigned long*)&buffer[8] = htonl(frameNumber); - pthread_mutex_lock(&mutex); - if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + MUTEX_LOCK(&mutex); + if (!connected) { MUTEX_UNLOCK(&mutex); return 0; } unsigned int a = tcp->sendPacket(buffer, totalLength); + delete []buffer; + if (a != totalLength) { disconnect(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } if (!getPacket()) { - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } ULLONG position = extractULLONG(); freePacket(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); Log::getInstance()->log("VDR", Log::DEBUG, "VDR said new position is: %llu", position); @@ -737,21 +775,21 @@ EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration) *(unsigned long*)&buffer[12] = htonl(start); *(unsigned long*)&buffer[16] = htonl(duration); - pthread_mutex_lock(&mutex); - if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + MUTEX_LOCK(&mutex); + if (!connected) { MUTEX_UNLOCK(&mutex); return 0; } int a = tcp->sendPacket(buffer, 20); if (a != 20) { disconnect(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return NULL; } if (!getPacket()) { - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return NULL; } @@ -759,11 +797,10 @@ EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration) if (serverError()) { freePacket(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return NULL; } - EventList* eventList = new EventList(); while (packetPos < packetLength) @@ -780,7 +817,7 @@ EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration) } freePacket(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); Log::getInstance()->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule"); @@ -818,7 +855,7 @@ ULLONG VDR::getResumePoint(char* fileName) int VDR::configSave(char* section, char* key, const char* value) { ULONG totalLength = 8 + strlen(section) + strlen(key) + strlen(value) + 3; // 8 for headers, 3 for nulls - UCHAR buffer[totalLength]; + UCHAR* buffer = new UCHAR[totalLength]; *(unsigned long*)&buffer[0] = htonl(totalLength - 4); *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGSAVE); @@ -830,26 +867,28 @@ int VDR::configSave(char* section, char* key, const char* value) position += strlen(key) + 1; strcpy((char*)&buffer[position], value); - pthread_mutex_lock(&mutex); - if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + MUTEX_LOCK(&mutex); + if (!connected) { MUTEX_UNLOCK(&mutex); return 0; } unsigned int a = tcp->sendPacket(buffer, totalLength); + delete[] buffer; + if (a != totalLength) { disconnect(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } if (!getPacket()) { - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } int toReturn = (int)extractULONG(); freePacket(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return toReturn; } @@ -857,7 +896,7 @@ int VDR::configSave(char* section, char* key, const char* value) char* VDR::configLoad(char* section, char* key) { ULONG totalLength = 8 + strlen(section) + strlen(key) + 2; // 8 for headers, 2 for nulls - UCHAR buffer[totalLength]; + UCHAR* buffer = new UCHAR[totalLength]; *(unsigned long*)&buffer[0] = htonl(totalLength - 4); *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGLOAD); @@ -867,25 +906,27 @@ char* VDR::configLoad(char* section, char* key) position += strlen(section) + 1; strcpy((char*)&buffer[position], key); - pthread_mutex_lock(&mutex); - if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + MUTEX_LOCK(&mutex); + if (!connected) { MUTEX_UNLOCK(&mutex); return 0; } unsigned int a = tcp->sendPacket(buffer, totalLength); + delete[] buffer; + if (a != totalLength) { disconnect(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return NULL; } if (!getPacket()) { - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return NULL; } char* toReturn = extractString(); freePacket(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return toReturn; } @@ -897,14 +938,14 @@ RecTimerList* VDR::getRecTimersList() *(unsigned long*)&buffer[0] = htonl(4); *(unsigned long*)&buffer[4] = htonl(VDR_GETTIMERS); - pthread_mutex_lock(&mutex); - if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + MUTEX_LOCK(&mutex); + if (!connected) { MUTEX_UNLOCK(&mutex); return 0; } int a = tcp->sendPacket(buffer, 8); if (a != 8) { disconnect(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return NULL; } @@ -912,7 +953,7 @@ RecTimerList* VDR::getRecTimersList() if (!getPacket()) { - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return NULL; } @@ -948,7 +989,7 @@ RecTimerList* VDR::getRecTimersList() } freePacket(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); // Sort the list @@ -960,32 +1001,34 @@ RecTimerList* VDR::getRecTimersList() ULONG VDR::setEventTimer(char* timerString) { unsigned long totalLength = 8 + strlen(timerString) + 1; - UCHAR buffer[totalLength]; + UCHAR* buffer = new UCHAR[totalLength]; *(unsigned long*)&buffer[0] = htonl(totalLength - 4); *(unsigned long*)&buffer[4] = htonl(VDR_SETTIMER); strcpy((char*)&buffer[8], timerString); - pthread_mutex_lock(&mutex); - if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + MUTEX_LOCK(&mutex); + if (!connected) { MUTEX_UNLOCK(&mutex); return 0; } unsigned int a = tcp->sendPacket(buffer, totalLength); + delete []buffer; + if (a != totalLength) { disconnect(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } if (!getPacket()) { - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return 0; } ULONG toReturn = extractULONG(); freePacket(); - pthread_mutex_unlock(&mutex); + MUTEX_UNLOCK(&mutex); return toReturn; } diff --git a/vdr.h b/vdr.h index 616c308..7bb4170 100644 --- a/vdr.h +++ b/vdr.h @@ -23,7 +23,11 @@ #include #include -#include +#ifndef WIN32 + #include +#else + //Find threading replacements +#endif #include #include @@ -165,7 +169,11 @@ class VDR int port; char serverIP[16]; bool connected; +#ifndef WIN32 pthread_mutex_t mutex; +#else + HANDLE mutex; +#endif UCHAR* packet; ULONG packetLength; -- 2.39.5