From 8f24472da89c8187ae6ac682face00501a654c4d Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Tue, 7 Apr 2020 15:37:54 +0100 Subject: [PATCH] Mods for Windows Since d3becd8bbaf12a5328585b429fad2472628da920 - 31 CWFs --- demuxer.cc | 7 +-- log.cc | 5 ++- tcp.cc | 119 ++++++++++++++++++++++++++++++++++++++++++--------- tcp.h | 11 ++++- timers.cc | 8 ++++ udp4.h | 11 +++-- udp6.cc | 2 +- udp6.h | 6 ++- vdpc.cc | 27 +++++++++++- vdpc.h | 10 ++++- vvideorec.cc | 4 +- winmain.cc | 20 +++++++++ wol.cc | 2 +- 13 files changed, 195 insertions(+), 37 deletions(-) diff --git a/demuxer.cc b/demuxer.cc index a38b0de..1712180 100644 --- a/demuxer.cc +++ b/demuxer.cc @@ -26,6 +26,7 @@ #include "log.h" #include +#include #include @@ -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(8-bit_pos)); work=work <>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 867042a..350db52 100644 --- a/log.cc +++ b/log.cc @@ -20,8 +20,11 @@ */ #include -#include + +#ifndef WIN32 #include +#include +#endif #include "vdr.h" diff --git a/tcp.cc b/tcp.cc index e230374..5758770 100644 --- a/tcp.cc +++ b/tcp.cc @@ -18,18 +18,26 @@ */ #include // memcmp -#include // pipe2 -#include // pipe2-O_NONBLOCK fcntl + +#ifdef WIN32 + #include + #include +#else + #include // pipe2 + #include // pipe2-O_NONBLOCK fcntl + #include // socket connect getaddrinfo + #include // getaddrinfo + #include // select + #include // for getMAC + #include // for getMAC + #include // getifaddrs +#endif + #include // socket connect getaddrinfo -#include // socket connect getaddrinfo #include // memset -#include // getaddrinfo -#include // select #include // errno var -#include // getifaddrs -#include // for getMAC -#include // 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, ¶m); +#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 lg(writeMutex); - int result = send(sockfd, src, numBytes, 0); // FIXME does send return < numBytes? Might need loop + int result = send(sockfd, reinterpret_cast(src), numBytes, 0); // FIXME does send return < numBytes? Might need loop if (result < 0) return false; if (static_cast(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(dst); + char* pointer = static_cast(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(¯esult, "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(¯esult, 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 c149e74..01034bf 100644 --- a/tcp.h +++ b/tcp.h @@ -20,6 +20,10 @@ #ifndef TCP_H #define TCP_H +#ifdef WIN32 +#include // for SOCKET +#endif + #include #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 diff --git a/timers.cc b/timers.cc index 09ffa51..fe6328b 100644 --- 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 63e7da5..783b596 100644 --- a/udp4.h +++ b/udp4.h @@ -20,9 +20,14 @@ #ifndef DSOCK_H #define DSOCK_H -#include -#include -#include +#ifdef WIN32 + #include + #include +#else + #include + #include + #include +#endif #include "defines.h" diff --git a/udp6.cc b/udp6.cc index 9e9e402..0659fac 100644 --- a/udp6.cc +++ b/udp6.cc @@ -23,6 +23,7 @@ #include #include #define SOCKET_ERROR 0 +#include #else #include #include @@ -34,7 +35,6 @@ #include #include #include -#include #include #include "log.h" diff --git a/udp6.h b/udp6.h index bebd3bf..7e111cf 100644 --- a/udp6.h +++ b/udp6.h @@ -20,8 +20,10 @@ #ifndef UDP6_H #define UDP6_H -#include -#include +#ifndef WIN32 + #include + #include +#endif #include "defines.h" diff --git a/vdpc.cc b/vdpc.cc index b2adead..847ef08 100644 --- a/vdpc.cc +++ b/vdpc.cc @@ -17,8 +17,10 @@ along with VOMP. If not, see . */ -#include -#include +#ifndef WIN32 + #include + #include +#endif #include #include @@ -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 e196302..9bb837f 100644 --- a/vdpc.h +++ b/vdpc.h @@ -29,6 +29,10 @@ #include #include +#ifdef WIN32 + #include +#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; diff --git a/vvideorec.cc b/vvideorec.cc index 6709ef1..b428cea 100644 --- a/vvideorec.cc +++ b/vvideorec.cc @@ -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; } } diff --git a/winmain.cc b/winmain.cc index 9d2fcba..7a24e23 100644 --- a/winmain.cc +++ b/winmain.cc @@ -22,6 +22,7 @@ #include #include #include +#include #define _WIN32_WINNT 0x501 #include @@ -925,5 +926,24 @@ ULLONG ntohll(ULLONG a) return htonll(a); } +std::string tp2str(const std::chrono::time_point& tp) +{ + auto tms = std::chrono::time_point_cast(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 881e6a5..50680ee 100644 --- 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(name),AF_INET,NULL,(sockaddr*)&sock_address,&sockname_len); DWORD ip=sock_address.sin_addr.s_addr;; ULONG size=0; -- 2.39.2