From d59bfa3da089948aa30a48272d90d8014cbc2df5 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Fri, 7 Apr 2006 00:08:17 +0000 Subject: [PATCH] Portability --- dsock.cc | 2 +- dsock.h | 13 ++++++ i18n.h | 1 + log.cc | 7 ++++ log.h | 1 + main.cc | 113 ++++++++++++++++++++++++--------------------------- stream.cc | 6 ++- stream.h | 2 + tcp.cc | 43 ++++++++++++++++---- threadwin.cc | 32 +++++++-------- timers.cc | 13 +++++- timers.h | 2 + 12 files changed, 148 insertions(+), 87 deletions(-) diff --git a/dsock.cc b/dsock.cc index 2e84bd5..0d65c77 100644 --- a/dsock.cc +++ b/dsock.cc @@ -43,7 +43,7 @@ DatagramSocket::DatagramSocket(short port) tv.tv_usec = 0; int allowed = 1; - setsockopt(socketnum, SOL_SOCKET, SO_BROADCAST, &allowed, sizeof(allowed)); + setsockopt(socketnum, SOL_SOCKET, SO_BROADCAST, (char*)&allowed, sizeof(allowed)); } DatagramSocket::~DatagramSocket() diff --git a/dsock.h b/dsock.h index d23fc2f..15f3700 100644 --- a/dsock.h +++ b/dsock.h @@ -21,12 +21,25 @@ #ifndef DSOCK_H #define DSOCK_H +#ifndef WIN32 + #include #include #include #include +#else +#include +#include +#endif + #include + +#ifndef WIN32 #include +#else +#include +#endif + #include #include #include diff --git a/i18n.h b/i18n.h index cad5c91..86a930e 100644 --- a/i18n.h +++ b/i18n.h @@ -28,6 +28,7 @@ #include #include #ifdef WIN32 +#include #include #endif diff --git a/log.cc b/log.cc index 481e418..812ad13 100644 --- a/log.cc +++ b/log.cc @@ -95,9 +95,16 @@ int Log::log(char *fromModule, int level, char* message, ...) char buffer[151]; int spaceLeft = 150; +#ifndef _MSC_VER struct timeval tv; gettimeofday(&tv, NULL); struct tm* tms = localtime(&tv.tv_sec); +#else + struct _timeb tb; + _ftime(&tb); + struct tm* tms = localtime(&tb.time); +#endif + spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tms); spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tv.tv_usec); diff --git a/log.h b/log.h index 0b5c197..4f8e80c 100644 --- a/log.h +++ b/log.h @@ -26,6 +26,7 @@ #ifndef WIN32 #include #else + #include #include #endif diff --git a/main.cc b/main.cc index e5c9ae7..ddff33e 100644 --- a/main.cc +++ b/main.cc @@ -50,7 +50,10 @@ #include "videomvp.h" #endif +#ifndef WIN32 void sighandler(int signalReceived); +#endif + void shutdown(int code); // Global variables -------------------------------------------------------------------------------------------------- @@ -67,6 +70,8 @@ VDR* vdr; Video* video; Audio* audio; +// Linux MVP main function and sighandler +#ifndef WIN32 int main(int argc, char** argv) { if ((argc > 1) && (!strcmp(argv[1], "-d"))) debugEnabled = 1; @@ -77,21 +82,12 @@ int main(int argc, char** argv) logger = new Log(); timers = new Timers(); vdr = new VDR(); -#ifdef WIN32 - mtd = new MtdWin(); - remote = new RemoteWin(); - led = new LedWin(); - osd = new OsdWin(); - audio = new AudioWin(); - video = new VideoWin(); -#else mtd = new MtdMVP(); remote = new RemoteMVP(); led = new LedMVP(); osd = new OsdMVP(); audio = new AudioMVP(); video = new VideoMVP(); -#endif viewman = new ViewMan(); command = new Command(); @@ -192,11 +188,7 @@ int main(int argc, char** argv) shutdown(1); } -#ifdef WIN32 - success = led->init(); -#else success = led->init(((RemoteMVP*)remote)->getDevice()); -#endif if (success) { logger->log("Core", Log::INFO, "LED module initialised"); @@ -317,6 +309,54 @@ int main(int argc, char** argv) // ------------------------------------------------------------------------------------------------------------------- +void sighandler(int signalReceived) +{ + logger->log("Core", Log::NOTICE, "Signal %i received", signalReceived); + + switch (signalReceived) + { + case SIGINT: + { + logger->log("Core", Log::NOTICE, "Interrupt signal, shutting down..."); + command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe? + break; + } + case SIGTERM: + { + logger->log("Core", Log::NOTICE, "Term signal, shutting down..."); + command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe? + break; + } + case SIGUSR1: + { + command->sig1(); + break; + } +/* + case SIGUSR1: + { + logger->log("Core", Log::DEBUG, "SIGUSR1 caught"); + logger->upLogLevel(); + break; + } + case SIGUSR2: + { + logger->log("Core", Log::DEBUG, "SIGUSR2 caught"); + logger->downLogLevel(); + break; + } +*/ + case SIGURG: + { + logger->log("Core", Log::DEBUG, "SIGURG caught"); + break; + } + } +} +#endif + +// ------------------------------------------------------------------------------------------------------------------- + void shutdown(int code) { if (viewman) @@ -401,53 +441,6 @@ void shutdown(int code) // ------------------------------------------------------------------------------------------------------------------- -void sighandler(int signalReceived) -{ - logger->log("Core", Log::NOTICE, "Signal %i received", signalReceived); - - switch (signalReceived) - { - case SIGINT: - { - logger->log("Core", Log::NOTICE, "Interrupt signal, shutting down..."); - command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe? - break; - } - case SIGTERM: - { - logger->log("Core", Log::NOTICE, "Term signal, shutting down..."); - command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe? - break; - } - case SIGUSR1: - { - command->sig1(); - break; - } -/* - case SIGUSR1: - { - logger->log("Core", Log::DEBUG, "SIGUSR1 caught"); - logger->upLogLevel(); - break; - } - case SIGUSR2: - { - logger->log("Core", Log::DEBUG, "SIGUSR2 caught"); - logger->downLogLevel(); - break; - } -*/ - case SIGURG: - { - logger->log("Core", Log::DEBUG, "SIGURG caught"); - break; - } - } -} - -// ------------------------------------------------------------------------------------------------------------------- - ULLONG ntohll(ULLONG a) { return htonll(a); diff --git a/stream.cc b/stream.cc index 785b11d..a04088b 100644 --- a/stream.cc +++ b/stream.cc @@ -101,7 +101,7 @@ int Stream::drain(int fd) int tail = bufferTail; int mark = bufferMark; int written; - +#ifndef WIN32 if (mark == -1 && tail > head) mark = bufferSize; if (mark >= 0) @@ -129,4 +129,8 @@ int Stream::drain(int fd) ret += written; bufferTail = tail + written; return ret; + #else + return 0; //to do! + #endif //Again this have to betransformed into abstract base class and derived class + } diff --git a/stream.h b/stream.h index cbe1c08..d7d02ea 100644 --- a/stream.h +++ b/stream.h @@ -22,7 +22,9 @@ #define STREAM_H #include +#ifndef WIN32 #include +#endif #include #include "defines.h" diff --git a/tcp.cc b/tcp.cc index 7ed0222..6c56e38 100644 --- a/tcp.cc +++ b/tcp.cc @@ -44,10 +44,15 @@ void TCP::disableTimeout() void TCP::getMAC(char* dest) { +#ifndef WIN32 struct ifreq ifr; strcpy(ifr.ifr_name, "eth0"); ioctl(sock, SIOCGIFHWADDR, &ifr); memcpy(dest, ifr.ifr_hwaddr.sa_data, 6); +#else + //TODO: Get MAC Address for windows + memcpy(dest, "ABCDEF", 6); +#endif } int TCP::connectTo(char* host, unsigned short port) @@ -59,18 +64,29 @@ int TCP::connectTo(char* host, unsigned short port) dest_addr.sin_family = AF_INET; dest_addr.sin_port = htons(port); +#ifndef WIN32 if (!inet_aton(host, &dest_addr.sin_addr)) +#else + dest_addr.sin_addr.s_addr = inet_addr(host); + if (dest_addr.sin_addr.s_addr == INADDR_NONE) +#endif { - close(sock); + CLOSESOCKET(sock); return 0; } memset(&(dest_addr.sin_zero), '\0', 8); // set non blocking +#ifndef WIN32 int oldflags = fcntl (sock, F_GETFL, 0); oldflags |= O_NONBLOCK; fcntl(sock, F_SETFL, oldflags); +#else + unsigned long flag=1; + ioctlsocket(sock,FIONBIO,&flag); + +#endif // setReceiveWindow(2048); @@ -86,9 +102,14 @@ int TCP::connectTo(char* host, unsigned short port) // first check errno for EINPROGRESS, otherwise it's a fail // this doesn't work? +#ifndef WIN32 if (errno != EINPROGRESS) +#else + int wsalasterr = WSAGetLastError(); + if ((wsalasterr != WSAEWOULDBLOCK) && (wsalasterr != WSAEINPROGRESS)) +#endif { - close(sock); + CLOSESOCKET(sock); return 0; } @@ -113,7 +134,7 @@ int TCP::connectTo(char* host, unsigned short port) int soError; socklen_t soErrorSize = sizeof(soError); - int gso = getsockopt(sock, SOL_SOCKET, SO_ERROR, &soError, &soErrorSize); + int gso = getsockopt(sock, SOL_SOCKET, SO_ERROR,(char*) &soError, &soErrorSize); if ((gso == 0) && (soError == 0)) { @@ -147,7 +168,7 @@ The full documentation: void TCP::setReceiveWindow(size_t rxBufferSize) { // Set receive window - int r = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rxBufferSize, sizeof(size_t)); + int r = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&rxBufferSize, sizeof(size_t)); Log::getInstance()->log("TCP", Log::DEBUG, "Set receive window to %i, success(=0): %i", rxBufferSize, r); } @@ -173,11 +194,14 @@ int TCP::sendPacket(void* bufR, size_t count) { do { +#ifndef WIN32 temp_write = this_write = write(sock, buf, count - bytes_sent); // Log::getInstance()->log("TCP", Log::DEBUG, "TCP has written %i bytes", temp_write); } while ( (this_write < 0) && (errno == EINTR) ); - - +#else + temp_write = this_write = send(sock,(char*) buf, count- bytes_sent,0); + } while ( (this_write == SOCKET_ERROR) && (WSAGetLastError() == WSAEINTR) ); +#endif if (this_write <= 0) { return(this_write); @@ -217,7 +241,7 @@ UCHAR* TCP::receivePacket() { Log::getInstance()->log("TCP", Log::ERR, "readData failed"); free(buffer); - close(sock); + CLOSESOCKET(sock); connected = 0; return NULL; } @@ -257,8 +281,11 @@ int TCP::readData(UCHAR* buffer, int totalBytes) Log::getInstance()->log("TCP", Log::ERR, "Error or timeout"); return 0; // error, or timeout } - +#ifndef WIN32 thisRead = read(sock, &buffer[bytesRead], totalBytes - bytesRead); +#else + thisRead = recv(sock,(char*) &buffer[bytesRead],totalBytes - bytesRead, 0); +#endif // printf("read %i\n", thisRead); if (!thisRead) { diff --git a/threadwin.cc b/threadwin.cc index e42b989..cc9645b 100644 --- a/threadwin.cc +++ b/threadwin.cc @@ -28,11 +28,11 @@ DWORD WINAPI threadInternalStart(void *arg) return 0; } -int ThreadP::threadStart() +int ThreadWin::threadStart() { threadCond = CreateEvent(NULL,FALSE,FALSE,NULL); if (threadCond == NULL) return 0; - threadCondMutex = CreateMutex(NULL,TRUE,NULL); + threadCondMutex = CreateMutex(NULL,FALSE,NULL); if (threadCondMutex == NULL) { CloseHandle(threadCond); @@ -51,7 +51,7 @@ int ThreadP::threadStart() return 1; } -void ThreadP::threadStop() +void ThreadWin::threadStop() { threadActive = 0; // Signal thread here in case it's waiting @@ -60,7 +60,7 @@ void ThreadP::threadStop() this->threadPostStopCleanup(); } -void ThreadP::threadCancel() +void ThreadWin::threadCancel() { threadActive = 0; TerminateThread(pthread, 0); @@ -68,51 +68,51 @@ void ThreadP::threadCancel() this->threadPostStopCleanup(); } -void ThreadP::threadCheckExit() +void ThreadWin::threadCheckExit() { if (!threadActive) ExitThread(NULL); } -void ThreadP::threadLock() +void ThreadWin::threadLock() { WaitForSingleObject(threadCondMutex, INFINITE); } -void ThreadP::threadUnlock() +void ThreadWin::threadUnlock() { ReleaseMutex(threadCondMutex); } -void ThreadP::threadSignal() +void ThreadWin::threadSignal() { WaitForSingleObject(threadCondMutex, INFINITE); - SetEvent(threadCond); + PulseEvent(threadCond); ReleaseMutex(threadCondMutex); } -void ThreadP::threadSignalNoLock() +void ThreadWin::threadSignalNoLock() { - SetEvent(threadCond); + PulseEvent(threadCond); } -void ThreadP::threadWaitForSignal() +void ThreadWin::threadWaitForSignal() { WaitForSingleObject(threadCond,INFINITE); } -void ThreadP::threadWaitForSignalTimed(struct timespec* ts) +void ThreadWin::threadWaitForSignalTimed(struct timespec* ts) { HANDLE handles[2] ={threadCond, NULL}; LARGE_INTEGER duration; duration.QuadPart=ts->tv_sec*1000*1000*10+ts->tv_nsec/100; handles[1]=CreateWaitableTimer(NULL,TRUE,NULL); - /* SetWaitableTimer(handles[1], &duration, 0, NULL, NULL, 0); - WaitForMultipleObject(2,handles,INFINITE);*/ + SetWaitableTimer(handles[1], &duration, 0, NULL, NULL, 0); + WaitForMultipleObjects(2,handles,FALSE,INFINITE); CloseHandle(handles[1]); } -void ThreadP::threadSetKillable() +void ThreadWin::threadSetKillable() { //WIN32:Ignore or use a separate Event Object to simulate this } diff --git a/timers.cc b/timers.cc index 5452240..c94ee58 100755 --- a/timers.cc +++ b/timers.cc @@ -130,7 +130,18 @@ int Timers::setTimerT(TimerReceiver* client, int clientReference, long int reque int Timers::setTimerD(TimerReceiver* client, int clientReference, long int requestedSecs, long int requestedNSecs) { struct timespec currentTime; + +#ifndef WIN32 clock_gettime(CLOCK_REALTIME, ¤tTime); +#else + SYSTEMTIME systime; + __int64 filetime; + __int64 test; + GetSystemTime(&systime); + SystemTimeToFileTime(&systime,(FILETIME*)&filetime); + currentTime.tv_sec=filetime/(10*1000*1000); + currentTime.tv_nsec=(filetime%(10*1000*1000))*100; +#endif long int requestedTime; long int requestedTimeNSEC; @@ -293,7 +304,7 @@ void Timers::threadMethod() threadUnlock(); //logger->log("Timers", Log::DEBUG, "un-LOCKED -TIMERS- MUTEX (3)"); //printf("\n\n\n WOOOOO \n\n\n The anti deadlock code is working!!! \n\n\n"); - usleep(10000); // 10ms - too long? + MILLISLEEP(10); // 10ms - too long? //logger->log("Timers", Log::DEBUG, "Waiting for LOCK -TIMERS- MUTEX 7"); threadLock(); //logger->log("Timers", Log::DEBUG, "LOCKED -TIMERS- MUTEX 7"); diff --git a/timers.h b/timers.h index 7878b98..09dddc0 100755 --- a/timers.h +++ b/timers.h @@ -22,7 +22,9 @@ #define TIMERS_H #include +#ifndef WIN32 #include +#endif #include #include "defines.h" -- 2.39.2