]> git.vomp.tv Git - vompclient.git/commitdiff
Mods for Windows
authorChris Tallon <chris@vomp.tv>
Tue, 7 Apr 2020 14:37:54 +0000 (15:37 +0100)
committerChris Tallon <chris@vomp.tv>
Tue, 7 Apr 2020 14:37:54 +0000 (15:37 +0100)
Since d3becd8bbaf12a5328585b429fad2472628da920 - 31 CWFs

13 files changed:
demuxer.cc
log.cc
tcp.cc
tcp.h
timers.cc
udp4.h
udp6.cc
udp6.h
vdpc.cc
vdpc.h
vvideorec.cc
winmain.cc
wol.cc

index a38b0de873f964fe7340c40e9a000b05d2845e1a..17121801906c9a7e052808039c6fd570d2ffcc72 100644 (file)
@@ -26,6 +26,7 @@
 #include "log.h"
 
 #include <cstdlib>
+#include <algorithm>
 
 #include <math.h>
 
@@ -97,7 +98,7 @@ NALUUnit::NALUUnit(const UCHAR *buf, UINT length_buf)
         pattern = ((pattern << 8) | buf[nalu_end])&0x00FFFFFF;
     }
     nalu_end-=3;
-    nalu_end=min(length_buf-1,nalu_end);
+    nalu_end=std::min(length_buf-1,nalu_end);
     if (nalu_end <= nalu_start) return; // input buffer too small. corrupt data? ignore.
     nalu_length=nalu_end-nalu_start;
     nalu_buf=(UCHAR*)malloc(nalu_length);
@@ -140,7 +141,7 @@ inline UINT NALUUnit::getBits(UINT num_bits)
             }
 
         }
-        UINT fetch_bits=min(remain_bits,8-bit_pos);
+        UINT fetch_bits=std::min(remain_bits, static_cast<UINT>(8-bit_pos));
         work=work <<fetch_bits;
         //work|=((working_byte>>bit_pos) & (0xFF>>(8-fetch_bits)));
         work|=(working_byte &(0xFF>>(bit_pos)))>>(8-fetch_bits-bit_pos);
@@ -242,7 +243,7 @@ int PESPacket::write(const UCHAR *buf, int len)
   if (size + len > 0x10000) return 0;
   if (size + len > data_size)
   { // Reallocate
-    UINT new_data_size = max(data_size + data_size / 2, data_size + len);
+    UINT new_data_size = std::max(data_size + data_size / 2, data_size + len);
     if (new_data_size > 0x10000) new_data_size = 0x10000;
     data_size = new_data_size;
     data = (UCHAR*)realloc(data, data_size);
diff --git a/log.cc b/log.cc
index 867042ab9d9446457d8cbd1f652f07ce94807647..350db528bef3d278caa6ea1fda6962011fb79520 100644 (file)
--- a/log.cc
+++ b/log.cc
 */
 
 #include <sys/types.h>
-#include <unistd.h>
+
+#ifndef WIN32
 #include <sys/syscall.h>
+#include <unistd.h>
+#endif
 
 #include "vdr.h"
 
diff --git a/tcp.cc b/tcp.cc
index e23037428b06f4e37732c95a41b21291f6aa114f..57587704bc7fbb188b98a1e51903b63eae629ff7 100644 (file)
--- a/tcp.cc
+++ b/tcp.cc
 */
 
 #include <string.h> // memcmp
-#include <unistd.h> // pipe2
-#include <fcntl.h> // pipe2-O_NONBLOCK fcntl
+
+#ifdef WIN32
+  #include <WS2tcpip.h>
+  #include <iphlpapi.h>
+#else
+  #include <unistd.h> // pipe2
+  #include <fcntl.h> // pipe2-O_NONBLOCK fcntl
+  #include <sys/socket.h> // socket connect getaddrinfo
+  #include <netdb.h> // getaddrinfo
+  #include <sys/select.h> // select
+  #include <linux/if_packet.h> // for getMAC
+  #include <net/ethernet.h> // for getMAC
+  #include <ifaddrs.h> // getifaddrs
+#endif
+
 #include <sys/types.h>  // socket connect getaddrinfo
-#include <sys/socket.h> // socket connect getaddrinfo
 #include <string.h> // memset
-#include <netdb.h> // getaddrinfo
-#include <sys/select.h> // select
 #include <errno.h> // errno var
-#include <ifaddrs.h> // getifaddrs
-#include <linux/if_packet.h> // for getMAC
-#include <net/ethernet.h> // for getMAC
 
+#include "defines.h"
 #include "log.h"
 
 #include "tcp.h"
@@ -38,6 +46,9 @@ TCP::~TCP()
 {
   shutdown();
   
+#ifdef WIN32
+  CLOSESOCKET(abortSocket);
+#else
   if (abortPipe[0] != -1)
   {
     close(abortPipe[0]);
@@ -45,17 +56,27 @@ TCP::~TCP()
     abortPipe[0] = -1;
     abortPipe[1] = -1;
   }
+#endif
 }
 
 bool TCP::init()
 {
   logger = Log::getInstance();
   
+  #ifdef WIN32
+  abortSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+  if (abortSocket == INVALID_SOCKET)
+  {
+    logger->log("TCP", Log::CRIT, "socket error");
+    return false;
+  }
+  #else
   if (pipe2(abortPipe, O_NONBLOCK) == -1)
   {
     logger->log("TCP", Log::CRIT, "pipe2 error");
     return false;
   }
+  #endif
   
   return true;
 }
@@ -72,7 +93,7 @@ bool TCP::connect(const std::string& ip, USHORT port)
 
   struct addrinfo* aip;
   char portString[10];
-  std::snprintf(portString, 10, "%u", port);
+  SNPRINTF(portString, 10, "%u", port);
   int gaiResult = getaddrinfo(ip.c_str(), portString, &hints, &aip);
 
   if ((gaiResult != 0) || !aip)
@@ -84,7 +105,12 @@ bool TCP::connect(const std::string& ip, USHORT port)
   sockfd = socket(aip->ai_family, SOCK_STREAM, 0);
   if (sockfd == -1) { logger->log("TCP", Log::CRIT, "socket error"); return false; }
 
+#ifdef WIN32
+  ULONG param = 1;
+  ioctlsocket(sockfd, FIONBIO, &param);
+#else
   fcntl(sockfd, F_SETFL, O_NONBLOCK);
+#endif
 
   errno = 0;
   // There should only be one aip result..
@@ -99,7 +125,7 @@ bool TCP::connect(const std::string& ip, USHORT port)
 
   if (errno != EINPROGRESS)
   {
-    close(sockfd);
+    CLOSESOCKET(sockfd);
     sockfd = -1;
     logger->log("TCP", Log::CRIT, "connect error");
     return false;
@@ -109,13 +135,20 @@ bool TCP::connect(const std::string& ip, USHORT port)
   fd_set writefds;
   struct timeval tv;
   FD_ZERO(&writefds);
-  FD_SET(abortPipe[0], &writefds);
   FD_SET(sockfd, &writefds);
+  int maxfd = sockfd;
+
+#ifdef WIN32
+  FD_SET(abortSocket, &writefds);
+#else
+  FD_SET(abortPipe[0], &writefds);
+  if (abortPipe[0] > maxfd) maxfd = abortPipe[0];
+#endif
 
   tv.tv_sec = 5; // Allow 5s for a connect
   tv.tv_usec = 0;
 
-  int selectResult = select((sockfd > abortPipe[0] ? sockfd : abortPipe[0]) + 1, NULL, &writefds, NULL, &tv);
+  int selectResult = select(maxfd + 1, NULL, &writefds, NULL, &tv);
 
   if ((selectResult == 1) || FD_ISSET(sockfd, &writefds))
   {
@@ -126,7 +159,7 @@ bool TCP::connect(const std::string& ip, USHORT port)
   }
   else
   {
-    close(sockfd);
+    CLOSESOCKET(sockfd);
     sockfd = -1;
     logger->log("TCP", Log::CRIT, "connect/select error");
     return false;
@@ -137,7 +170,7 @@ void TCP::shutdown()
 {
   if (!connected) return;
   connected = false;
-  close(sockfd);
+  CLOSESOCKET(sockfd);
   sockfd = -1;
 }
 
@@ -148,7 +181,11 @@ bool TCP::status()
 
 void TCP::abortCall()
 {
+#ifdef WIN32
+  CLOSESOCKET(abortSocket);
+#else
   ::write(abortPipe[1], "X", 1);
+#endif
 }
 
 bool TCP::write(void* src, ULONG numBytes)
@@ -156,7 +193,7 @@ bool TCP::write(void* src, ULONG numBytes)
   if (!connected) return false;
 
   std::lock_guard<std::mutex> lg(writeMutex);
-  int result = send(sockfd, src, numBytes, 0);  // FIXME does send return < numBytes? Might need loop
+  int result = send(sockfd, reinterpret_cast<char*>(src), numBytes, 0);  // FIXME does send return < numBytes? Might need loop
   if (result < 0) return false;
   if (static_cast<ULONG>(result) != numBytes) return false;
 
@@ -172,7 +209,7 @@ bool TCP::read(void* dst, ULONG numBytes)
 
   ULONG totalReceived = 0;
   int abortCount = 0;
-  UCHAR* pointer = static_cast<UCHAR*>(dst);
+  char* pointer = static_cast<char*>(dst); // WIN32 requires char*
   
   do
   {
@@ -183,18 +220,30 @@ bool TCP::read(void* dst, ULONG numBytes)
     }
     
     FD_ZERO(&readfds);
-    FD_SET(abortPipe[0], &readfds);
     FD_SET(sockfd, &readfds);
+    int maxfd = sockfd; // WIN32 ignores
+
+#ifdef WIN32
+    FD_SET(abortSocket, &readfds);
+#else
+    FD_SET(abortPipe[0], &readfds);
+    if (abortPipe[0] > maxfd) maxfd = abortPipe[0];
+#endif
 
     tv.tv_sec = 2;
     tv.tv_usec = 0;
 
-    int selectResult = select((sockfd > abortPipe[0] ? sockfd : abortPipe[0]) + 1, &readfds, NULL, NULL, &tv);
+    int selectResult = select(maxfd + 1, &readfds, NULL, NULL, &tv);
 
     if (selectResult == -1) { shutdown(); return false; }
     if (selectResult == 0) return false;
-    if (FD_ISSET(abortPipe[0], &readfds)) { logger->log("TCP", Log::DEBUG, "aborting..."); return false; }
 
+#ifdef WIN32
+  if (FD_ISSET(abortSocket, &readfds)) { logger->log("TCP", Log::DEBUG, "aborting..."); return false; }
+#else
+  if (FD_ISSET(abortPipe[0], &readfds)) { logger->log("TCP", Log::DEBUG, "aborting..."); return false; }
+#endif
+  
     int recvResult = recv(sockfd, pointer, numBytes - totalReceived, 0);
     if (recvResult == -1) { shutdown(); return false; }
     totalReceived += recvResult;
@@ -207,8 +256,8 @@ bool TCP::read(void* dst, ULONG numBytes)
 
 MACAddress TCP::getMAC()
 {
+#ifndef WIN32
   MACAddress macerror{ 00, 00, 00, 00, 00, 00 };
-
   if (!connected) return macerror;
 
   /*
@@ -293,4 +342,34 @@ MACAddress TCP::getMAC()
 
   freeifaddrs(ifAddrs);
   return toReturn;
+
+#else // WIN32
+  PIP_ADAPTER_INFO daptinfo = NULL;
+  DWORD size = 0;
+  GetAdaptersInfo(daptinfo, &size);
+  daptinfo = (PIP_ADAPTER_INFO)new char[size + 1];
+  MACAddress macresult;
+  memcpy(&macresult, "ABCDEF", 6);//Dummy Address
+  sockaddr_in sock_address;
+  int sockname_len = sizeof(sock_address);
+  getsockname(sockfd, (sockaddr*)&sock_address, &sockname_len);
+  ULONG sockip = sock_address.sin_addr.s_addr;
+  if (GetAdaptersInfo(daptinfo, &size) == ERROR_SUCCESS)
+  {
+    PIP_ADAPTER_INFO daptinfo_it = daptinfo;
+    while (daptinfo_it != NULL)
+    {
+      ULONG ipaddress = inet_addr(daptinfo_it->IpAddressList.IpAddress.String);
+      if (ipaddress == sockip)
+      { //Is it our MAC?
+        memcpy(&macresult, daptinfo_it->Address, 6);
+        break;
+      }
+      daptinfo_it = daptinfo_it->Next;
+      if (daptinfo_it == daptinfo) break;
+    }
+  }
+  delete[] daptinfo;
+  return macresult;
+#endif
 }
diff --git a/tcp.h b/tcp.h
index c149e7471e1fd0e77b58f23cce66df69e7269678..01034bf9c2f9fe15d71535278d1b6779b757d7dc 100644 (file)
--- a/tcp.h
+++ b/tcp.h
 #ifndef TCP_H
 #define TCP_H
 
+#ifdef WIN32
+#include <WinSock2.h> // for SOCKET
+#endif
+
 #include <mutex>
 
 #include "defines.h"
@@ -49,9 +53,14 @@ class TCP
   private:
     Log* logger;
     int sockfd{-1};
-    int abortPipe[2]{-1, -1};
     bool connected{};
     std::mutex writeMutex;
+
+#ifdef WIN32
+    SOCKET abortSocket;
+#else
+    int abortPipe[2]{-1, -1};
+#endif
 };
 
 #endif
index 09ffa51fa7eaa7e692f2fbf091de132cbf585428..fe6328b782f35ef91b3781158178d8eb051e378a 100644 (file)
--- a/timers.cc
+++ b/timers.cc
@@ -95,14 +95,22 @@ bool Timers::setTimerD(TimerReceiver* client, int clientReference, long int requ
 {
   std::chrono::system_clock::time_point fireTime = std::chrono::system_clock::now();
   fireTime += std::chrono::seconds(requestedSecs);
+#ifdef WIN32
+  fireTime += std::chrono::microseconds(requestedNSecs / 1000);
+#else
   fireTime += std::chrono::nanoseconds(requestedNSecs);
+#endif
   return setTimerC(client, clientReference, fireTime);
 }
 
 bool Timers::setTimerT(TimerReceiver* client, int clientReference, long int requestedTime, long int requestedTimeNSecs)
 {
   std::chrono::system_clock::time_point fireTime = std::chrono::system_clock::from_time_t(requestedTime);
+#ifdef WIN32
+  fireTime += std::chrono::microseconds(requestedTimeNSecs / 1000);
+#else
   fireTime += std::chrono::nanoseconds(requestedTimeNSecs);
+#endif
   return setTimerC(client, clientReference, fireTime);
 }
 
diff --git a/udp4.h b/udp4.h
index 63e7da52a2b6813fd26c1c9bd2aaebae7b287119..783b596abe42eb148b748b4a82030a77544be899 100644 (file)
--- a/udp4.h
+++ b/udp4.h
 #ifndef DSOCK_H
 #define DSOCK_H
 
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netinet/ip.h>
+#ifdef WIN32
+  #include <winsock2.h>
+  #include <WS2tcpip.h>
+#else
+  #include <sys/socket.h>
+  #include <netinet/in.h>
+  #include <netinet/ip.h>
+#endif
 
 #include "defines.h"
 
diff --git a/udp6.cc b/udp6.cc
index 9e9e4021564cfa017bf95fdd6756da0f3f6e1521..0659facd1c06286c07f057bcefc511e7b936c7fc 100644 (file)
--- a/udp6.cc
+++ b/udp6.cc
@@ -23,6 +23,7 @@
 #include <arpa/inet.h>
 #include <sys/time.h>
 #define SOCKET_ERROR 0
+#include <net/if.h>
 #else
 #include <winsock2.h>
 #include <Ws2tcpip.h>
@@ -34,7 +35,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
-#include <net/if.h>
 #include <fcntl.h>
 
 #include "log.h"
diff --git a/udp6.h b/udp6.h
index bebd3bf591a5fdf2267a036c5bae034376ae2478..7e111cf835ed22b5df0848e86c17a99531fce688 100644 (file)
--- a/udp6.h
+++ b/udp6.h
 #ifndef UDP6_H
 #define UDP6_H
 
-#include <sys/socket.h>
-#include <netinet/in.h>
+#ifndef WIN32
+  #include <sys/socket.h>
+  #include <netinet/in.h>
+#endif
 
 #include "defines.h"
 
diff --git a/vdpc.cc b/vdpc.cc
index b2adeadd6ddc250b8503edf2834223d7856885e9..847ef0888bed7ca1503776f0e9ecfb70e92cbffa 100644 (file)
--- a/vdpc.cc
+++ b/vdpc.cc
     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
 */
 
-#include <unistd.h>
-#include <fcntl.h>
+#ifndef WIN32
+  #include <unistd.h>
+  #include <fcntl.h>
+#endif
 
 #include <cstdlib>
 #include <chrono>
@@ -181,28 +183,49 @@ void VDPC::VDPC4::stop()
   Log::getInstance()->log("VDPC4", Log::DEBUG, "stop");
 
   stopNow = true;
+
+#ifdef WIN32
+  CLOSESOCKET(quitPipe);
+#else
   write(pfdsUDP4[1], "X", 1);
+#endif
+
   receiveThread.join();
 
+#ifndef WIN32
   close(pfdsUDP4[0]);
   close(pfdsUDP4[1]);
+#endif
 }
 
 void VDPC::VDPC4::threadMethod()
 {
   Log* logger = Log::getInstance();
 
+#ifdef WIN32
+  quitPipe = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+  if (quitPipe == INVALID_SOCKET)
+  {
+    logger->log("VDPC4", Log::ERR, "socket() fail");
+    return;
+  }
+#else
   if (pipe2(pfdsUDP4, O_NONBLOCK) == -1)
   {
     logger->log("VDPC4", Log::ERR, "pipe2 error B");
     return;
   }
+#endif
 
   while (!stopNow)
   {
     Log::getInstance()->log("VDPC4", Log::DEBUG, "goto waitformessage");
 
+    #ifdef WIN32
+    UCHAR retval = udp4.waitforMessage(3, quitPipe);
+    #else
     UCHAR retval = udp4.waitforMessage(3, pfdsUDP4[0]);
+    #endif
     Log::getInstance()->log("VDPC4", Log::DEBUG, "backfrom waitformessage");
 
     if (retval == 2) // we got a reply
diff --git a/vdpc.h b/vdpc.h
index e1963025eb374d34631c29502d6dab6187ada9f4..9bb837febd3a0623cae87c8dc0f3b9e3636d3f1e 100644 (file)
--- a/vdpc.h
+++ b/vdpc.h
 #include <condition_variable>
 #include <functional>
 
+#ifdef WIN32
+  #include <WinSock2.h>
+#endif
+
 #include "defines.h"
 
 #ifdef IPV4
@@ -81,7 +85,11 @@ class VDPC
         void sendRequest();
 
       private:
-        int pfdsUDP4[2];
+        #ifdef WIN32
+          SOCKET quitPipe;
+        #else
+          int pfdsUDP4[2];
+        #endif
 
         std::mutex startProtect;
         std::thread receiveThread;
index 6709ef1e58417d669e08741ddca943c4156c4d54..b428ceaed68cf7b24246d644eb2ff21848d0da48 100644 (file)
@@ -857,7 +857,7 @@ void VVideoRec::doBar(int action_in)
     if ((playerState == PlayerVideoRec::S_PAUSE_P) || (playerState == PlayerVideoRec::S_PAUSE_I))
       timers->cancelTimer(this, 2);
     else
-      timers->setTimerD(this, 2, 0, 200'000'000);
+      timers->setTimerD(this, 2, 0, 200000000);
   }
 }
 
@@ -881,7 +881,7 @@ void VVideoRec::timercall(int clientReference)
 #else 
          doBar(-1);
 #endif
-      timers->setTimerD(this, 2, 0, 200'000'000);
+      timers->setTimerD(this, 2, 0, 200000000);
       break;
     }
   }
index 9d2fcba41860f743d9953e921f6c334c017a1901..7a24e2314f08da1ee5372e7c3a3e08417ad036de 100644 (file)
@@ -22,6 +22,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <signal.h>
+#include <sstream>
 
 #define _WIN32_WINNT 0x501
 #include <winsock2.h>
@@ -925,5 +926,24 @@ ULLONG ntohll(ULLONG a)
   return htonll(a);
 }
 
+std::string tp2str(const std::chrono::time_point<std::chrono::system_clock>& tp)
+{
+  auto tms = std::chrono::time_point_cast<std::chrono::milliseconds>(tp);
+  std::chrono::milliseconds e = tms.time_since_epoch();
+  long long c = e.count();
+  time_t tt = c / 1000;
+  int ttm = c % 1000;
+  auto stm = std::localtime(&tt);
+  std::stringstream ss;
+  ss << std::put_time(stm, "%T") << "." << std::setfill('0') << std::setw(3) << ttm;
+  return ss.str();
+}
+
+const std::string& getCommandLineServer()
+{
+  return std::string();
+}
+
+
 #endif
 
diff --git a/wol.cc b/wol.cc
index 881e6a5b6c4bd489148965866ca52b52ad20bc6c..50680eebf45048d0569c30cbe9e555c7ec98dae9 100644 (file)
--- a/wol.cc
+++ b/wol.cc
@@ -116,7 +116,7 @@ int Wol::find_ether(const char *name)
 {
   sockaddr_in sock_address;
   int sockname_len=sizeof(sock_address);
-  WSAStringToAddress(name,AF_INET,NULL,(sockaddr*)&sock_address,&sockname_len);
+  WSAStringToAddress(const_cast<LPSTR>(name),AF_INET,NULL,(sockaddr*)&sock_address,&sockname_len);
   
   DWORD ip=sock_address.sin_addr.s_addr;;
   ULONG size=0;