]> git.vomp.tv Git - vompclient.git/commitdiff
Portability
authorChris Tallon <chris@vomp.tv>
Fri, 7 Apr 2006 00:08:17 +0000 (00:08 +0000)
committerChris Tallon <chris@vomp.tv>
Fri, 7 Apr 2006 00:08:17 +0000 (00:08 +0000)
12 files changed:
dsock.cc
dsock.h
i18n.h
log.cc
log.h
main.cc
stream.cc
stream.h
tcp.cc
threadwin.cc
timers.cc
timers.h

index 2e84bd5b36b746d8ddd70a6b23b4a81cb9daded1..0d65c77bc1290fd9eab2e115c7052618ffa839e1 100644 (file)
--- 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 d23fc2f45e0746e75e4b842250844595521e8b2b..15f3700576e5a2f131a3fb4b303d41032afd8b0b 100644 (file)
--- a/dsock.h
+++ b/dsock.h
 #ifndef DSOCK_H
 #define DSOCK_H
 
+#ifndef WIN32
+
 #include <netinet/in.h>
 #include <sys/socket.h>
 #include <unistd.h>
 #include <arpa/inet.h>
+#else
+#include <winsock2.h>
+#include <Ws2tcpip.h>
+#endif
+
 #include <sys/types.h>
+
+#ifndef WIN32
 #include <sys/time.h>
+#else
+#include <sys/timeb.h>
+#endif
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
diff --git a/i18n.h b/i18n.h
index cad5c9113757ea0e13b97236e289727de834eb6a..86a930e4b81dad642d52cbf96cc1db610a2475a5 100644 (file)
--- a/i18n.h
+++ b/i18n.h
@@ -28,6 +28,7 @@
 #include <stdio.h>
 #include <string.h>
 #ifdef WIN32
+#include <winsock2.h>
 #include <shlwapi.h>
 #endif
 
diff --git a/log.cc b/log.cc
index 481e418556912f49a39b91737a358b8e1650f494..812ad1351c4299e3c90c898e55cba5a9a1c88257 100644 (file)
--- 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 0b5c197bed65e4ef3b95b0a7cfea1413a668c213..4f8e80c3c4620bf388a710a4193bbc9c6d3b5851 100644 (file)
--- a/log.h
+++ b/log.h
@@ -26,6 +26,7 @@
 #ifndef WIN32
  #include <sys/time.h>
 #else
+ #include <winsock2.h>
  #include <sys/timeb.h>
 #endif
 
diff --git a/main.cc b/main.cc
index e5c9ae72637131890566bff61e30fdc82d915433..ddff33e84844c9c0631bca65e945868c53f6ac39 100644 (file)
--- a/main.cc
+++ b/main.cc
   #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);
index 785b11d39b7b8425e7e3bba6a265f619e48b079d..a04088b3bf3bcb9cf1501380da5fad58846a445d 100644 (file)
--- 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
+
 }
index cbe1c082840c6a8ab4fe8599b120175d83cf67a4..d7d02eab84be5c92ca000c718f200fc1f189c9d4 100644 (file)
--- a/stream.h
+++ b/stream.h
@@ -22,7 +22,9 @@
 #define STREAM_H
 
 #include <stdlib.h>
+#ifndef WIN32
 #include <unistd.h>
+#endif
 #include <memory.h>
 #include "defines.h"
 
diff --git a/tcp.cc b/tcp.cc
index 7ed022295163460274c3a536fc699516f7c7beac..6c56e383fb589c63635f8b2661c63c76f86da739 100644 (file)
--- 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)
     {
index e42b989a0d1cfa461f809da0bed5e0fe033eff32..cc9645ba3b4d32434529407157e2d05d3fb062be 100644 (file)
@@ -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
 }
index 5452240da49aa5550b85361d1a68f865eab40dfc..c94ee5889e3a6039f6ef8075a47428c90876f3e1 100755 (executable)
--- 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)\r
 {\r
   struct timespec currentTime;\r
+\r
+#ifndef WIN32\r
   clock_gettime(CLOCK_REALTIME, &currentTime);\r
+#else\r
+  SYSTEMTIME systime;\r
+  __int64  filetime;\r
+  __int64  test;\r
+  GetSystemTime(&systime);\r
+  SystemTimeToFileTime(&systime,(FILETIME*)&filetime);\r
+  currentTime.tv_sec=filetime/(10*1000*1000);\r
+  currentTime.tv_nsec=(filetime%(10*1000*1000))*100;\r
+#endif\r
 \r
   long int requestedTime;\r
   long int requestedTimeNSEC;\r
@@ -293,7 +304,7 @@ void Timers::threadMethod()
       threadUnlock();\r
       //logger->log("Timers", Log::DEBUG, "un-LOCKED -TIMERS- MUTEX (3)");\r
       //printf("\n\n\n WOOOOO \n\n\n The anti deadlock code is working!!! \n\n\n");\r
-      usleep(10000); // 10ms - too long?\r
+      MILLISLEEP(10); // 10ms - too long?\r
       //logger->log("Timers", Log::DEBUG, "Waiting for LOCK -TIMERS- MUTEX 7");\r
       threadLock();\r
       //logger->log("Timers", Log::DEBUG, "LOCKED -TIMERS- MUTEX 7");\r
index 7878b984a09dc6c3d342eb70baf885d62b09dded..09dddc07c7fcfbb8a9e19628639c13eb38e62c94 100755 (executable)
--- a/timers.h
+++ b/timers.h
@@ -22,7 +22,9 @@
 #define TIMERS_H
 
 #include <stdio.h>
+#ifndef WIN32
 #include <pthread.h>
+#endif
 #include <list>
 
 #include "defines.h"