From 8ae9659bf0a75d404841941374d6efcd1526b6eb Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Sun, 22 Mar 2020 22:43:17 +0000 Subject: [PATCH] VConnect, VDRServer, VDR::findServers to std::strings --- command.cc | 11 +++++---- command.h | 5 ++-- defines.h | 2 ++ main.cc | 14 ++++++++--- vconnect.cc | 60 ++++++++++++------------------------------------ vconnect.h | 4 +--- vdp6.cc | 17 ++++---------- vdr.cc | 43 ++++++++-------------------------- vdr.h | 20 ++++++++-------- vserverselect.cc | 4 ++-- wol.cc | 6 ++--- wol.h | 4 ++-- 12 files changed, 69 insertions(+), 121 deletions(-) diff --git a/command.cc b/command.cc index b38569d..4dcb98b 100644 --- a/command.cc +++ b/command.cc @@ -75,12 +75,11 @@ Command* Command::getInstance() return instance; } -int Command::init(bool tcrashed, char* tServer) +int Command::init(bool tcrashed) { if (initted) return 0; initted = true; crashed = tcrashed; - server = tServer; logger = Log::getInstance(); boxstack = BoxStack::getInstance(); @@ -183,7 +182,7 @@ void Command::run() } else { - VConnect* vconnect = new VConnect(server); + VConnect* vconnect = new VConnect(); boxstack->add(vconnect); vconnect->run(); } @@ -442,7 +441,7 @@ void Command::doPowerOn() InputMan::getInstance()->changePowerState(true); isStandby = false; - VConnect* vconnect = new VConnect(server); + VConnect* vconnect = new VConnect(); boxstack->add(vconnect); vconnect->run(); } @@ -509,7 +508,7 @@ void Command::doFromTheTop(bool which) // at this point, everything should be reset to first-go - VConnect* vconnect = new VConnect(server); + VConnect* vconnect = new VConnect(); boxstack->add(vconnect); vconnect->run(); } @@ -657,6 +656,8 @@ void Command::doJustConnected(VConnect* vconnect) boxstack->add(vi); boxstack->update(vi); + // FIXME make config system + VDR* vdr = VDR::getInstance(); char* config; diff --git a/command.h b/command.h index 847fd05..70c7a4c 100644 --- a/command.h +++ b/command.h @@ -28,7 +28,7 @@ #include #ifndef WIN32 -#include +#include // why? #endif #include @@ -62,7 +62,7 @@ class Command : public MessageQueue ~Command(); static Command* getInstance(); - int init(bool crashed = false, char* server = NULL); + int init(bool crashed = false); int shutdown(); void run(); void stop(); @@ -102,7 +102,6 @@ class Command : public MessageQueue WJpeg* wallpaper_pict{}; VInfo* connLost{}; bool crashed{}; - char* server{}; bool advMenus{}; ASLPrefList langcodes; diff --git a/defines.h b/defines.h index 82dbbaf..d705965 100644 --- a/defines.h +++ b/defines.h @@ -47,6 +47,8 @@ std::string tp2str(const std::chrono::time_point& tp) int getClockRealTime(struct timespec *tp); +const std::string& getCommandLineServer(); + //#define WINDOWS_LEGACY #ifdef WIN32 diff --git a/main.cc b/main.cc index c6f0c73..aca7fa1 100644 --- a/main.cc +++ b/main.cc @@ -71,6 +71,7 @@ void threadSignalReceiverFunction(); ULLONG htonll(ULLONG); ULLONG ntohll(ULLONG); void shutdown(int code); +const std::string& getCommandLineServer(); // Global variables -------------------------------------------------------------------------------------------------- Log* logger; @@ -86,6 +87,9 @@ Audio* audio; Wol* wol; Sleeptimer* sleeptimer; +// Temporary, will move to Config system +std::string argvServer; + #ifdef HANDLE_VT_SWITCHING int fdtty; struct vt_mode old_vtmode; @@ -98,7 +102,6 @@ int main(int argc, char** argv) bool daemonize = true; bool debugEnabled = false; bool crashed = false; - char* setServer = NULL; int c; while ((c = getopt(argc, argv, "cdns:")) != -1) @@ -115,7 +118,7 @@ int main(int argc, char** argv) daemonize = false; break; case 's': - setServer = optarg; + argvServer = optarg; break; case '?': printf("Unknown option\n"); @@ -320,7 +323,7 @@ int main(int argc, char** argv) shutdown(1); } - success = command->init(crashed, setServer); + success = command->init(crashed); if (success) { logger->log("Core", Log::INFO, "Command module initialised"); @@ -539,3 +542,8 @@ std::string tp2str(const std::chrono::time_point& tp) ss << std::put_time(stm, "%T") << "." << std::setfill('0') << std::setw(3) << ttm; return ss.str(); } + +const std::string& getCommandLineServer() +{ + return argvServer; +} diff --git a/vconnect.cc b/vconnect.cc index 3e4d99b..c31cb09 100644 --- a/vconnect.cc +++ b/vconnect.cc @@ -17,6 +17,7 @@ along with VOMP. If not, see . */ +#include "defines.h" #include "video.h" #include "colour.h" #include "command.h" @@ -30,8 +31,7 @@ #include "vconnect.h" -VConnect::VConnect(char* tServer) -: server(tServer) +VConnect::VConnect() { boxstack = BoxStack::getInstance(); vdr = VDR::getInstance(); @@ -98,35 +98,21 @@ void VConnect::threadMethod() std::unique_lock ul(threadMutex, std::defer_lock); + const std::string& commandLineServer = getCommandLineServer(); + do { - if (server) // Server is specified, fake a servers array + if (!commandLineServer.empty()) // Server is specified, fake a servers array { - VDRServer vdrserver; - vdrserver.ip = new char[strlen(server)+1]; - strcpy(vdrserver.ip, server); - vdrserver.name = new char[1]; - vdrserver.name[0] = '\0'; - vdrserver.port = 3024; // FIXME - servers.push_back(vdrserver); + servers.emplace_back(commandLineServer, "", 3024, 0); } else { setOneLiner(tr("Locating server")); draw(); boxstack->update(this); - vdr->findServers(servers); - if (threadReqQuit) - { - for(UINT k = 0; k < servers.size(); k++) - { - delete[] servers[k].ip; - delete[] servers[k].name; - } - servers.clear(); - return; - } + if (threadReqQuit) return; } if (servers.size() == 1) @@ -147,31 +133,13 @@ void VConnect::threadMethod() ul.unlock(); } - if (threadReqQuit) - { - for(UINT k = 0; k < servers.size(); k++) - { - delete[] servers[k].ip; - delete[] servers[k].name; - } - servers.clear(); - return; - } + if (threadReqQuit) return; - logger->log("VConnect", Log::NOTICE, "Connecting to server at %s %u", servers[selectedServer].ip, servers[selectedServer].port); - Wol::getInstance()->setWakeUpIP(servers[selectedServer].ip); - vdr->setServerIP(servers[selectedServer].ip); + logger->log("VConnect", Log::NOTICE, "Connecting to server at %s %u", servers[selectedServer].ip.c_str(), servers[selectedServer].port); + Wol::getInstance()->setWakeUpIP(servers[selectedServer].ip.c_str()); + vdr->setServerIP(servers[selectedServer].ip.c_str()); vdr->setServerPort(servers[selectedServer].port); - // Clear the serverIPs vector - for(UINT k = 0; k < servers.size(); k++) - { - delete[] servers[k].ip; - delete[] servers[k].name; - } - servers.clear(); - - setOneLiner(tr("Connecting to VDR")); draw(); boxstack->update(this); @@ -180,9 +148,9 @@ void VConnect::threadMethod() if (success) { logger->log("VConnect", Log::DEBUG, "Connected ok, doing login"); - unsigned int version_server_min,version_server_max,version_client; + unsigned int version_server_min, version_server_max, version_client; int subtitles; - success = vdr->doLogin(&version_server_min,&version_server_max,&version_client, Command::getInstance()->getASLList(), subtitles); + success = vdr->doLogin(&version_server_min, &version_server_max, &version_client, Command::getInstance()->getASLList(), subtitles); Command::getInstance()->setSubDefault(subtitles); if (!success) @@ -208,6 +176,8 @@ void VConnect::threadMethod() boxstack->update(this); MILLISLEEP(delay); + servers.clear(); // In case we're going around + } while(!success); logger->log("VConnect", Log::INFO, "Send VDR connected message"); diff --git a/vconnect.h b/vconnect.h index 0f4102b..3ea21ce 100644 --- a/vconnect.h +++ b/vconnect.h @@ -20,7 +20,6 @@ #ifndef VCONNECT_H #define VCONNECT_H -#include #include #include #include @@ -36,7 +35,7 @@ class Message; class VConnect : public VInfo { public: - VConnect(char* server); + VConnect(); ~VConnect(); int handleCommand(int command); @@ -54,7 +53,6 @@ class VConnect : public VInfo Log* logger; std::vector servers; int selectedServer; - char* server; std::thread connectThread; std::mutex threadMutex; diff --git a/vdp6.cc b/vdp6.cc index 7d321b6..0dfa21f 100644 --- a/vdp6.cc +++ b/vdp6.cc @@ -89,26 +89,19 @@ void VDP6::run() logger->log("VDP6", Log::ERR, "recvfrom error"); break; } - - VDRServer newServer; + // FIXME upgrade this to look for a return IP in the reply packet - newServer.ip = new char[40]; - inet_ntop(AF_INET6, &theirAddr.sin6_addr, newServer.ip, sizeof(theirAddr)); + char tempStringIP[40]; + inet_ntop(AF_INET6, &theirAddr.sin6_addr, tempStringIP, sizeof(theirAddr)); USHORT tempPort; memcpy(&tempPort, &vdpreply[26], 2); - newServer.port = ntohs(tempPort); ULONG newServerVersion; memcpy(&newServerVersion, &vdpreply[28], 4); - newServer.version = ntohl(newServerVersion); - - UINT newServerNameLength = static_cast(mlength - 32); - newServer.name = new char[newServerNameLength]; - strcpy(newServer.name, &vdpreply[32]); - logger->log("VDP6", Log::INFO, "Got response: %s %u %s %lx", newServer.ip, newServer.port, newServer.name, newServer.version); - servers.push_back(newServer); + //logger->log("VDP6", Log::INFO, "Got response: %s %u %s %lx", newServer.ip, newServer.port, newServer.name, newServer.version); + servers.emplace_back(tempStringIP, &vdpreply[32], ntohs(tempPort), ntohl(newServerVersion)); } }); diff --git a/vdr.cc b/vdr.cc index 8fda12f..db620a4 100644 --- a/vdr.cc +++ b/vdr.cc @@ -201,25 +201,18 @@ void VDR::findServers(std::vector& servers) const char* vdpreply = static_cast(ds.getData()); if ((ds.getDataLength() >= 24) && !strncmp(vdpreply, "VDP-0002", 8)) { - VDRServer newServer; // FIXME upgrade this to look for a return IP in the reply packet - newServer.ip = new char[16]; - strcpy(newServer.ip, ds.getFromIPA()); USHORT newServerPort; memcpy(&newServerPort, &vdpreply[26], 2); - newServer.port = ntohs(newServerPort); ULONG newServerVersion; memcpy(&newServerVersion, &vdpreply[28], 4); - newServer.version = ntohl(newServerVersion); - - int newServerNameLength = ds.getDataLength() - 32; - newServer.name = new char[newServerNameLength]; - strcpy(newServer.name, &vdpreply[32]); - - servers.push_back(newServer); haveAtLeastOne = 1; + + // FIXME - packet length > 24 not checked, end NULL not checked!! + // FIXME mutex protection ?? Nope this is a separate vector currently + servers.emplace_back(ds.getFromIPA(), &vdpreply[32], ntohs(newServerPort), ntohl(newServerVersion)); } } else @@ -231,15 +224,6 @@ void VDR::findServers(std::vector& servers) waitType = 1; firstloop = false; -/* For DEBUGGING * - { VDRServer newServer; - newServer.ip = new char[16]; - strcpy(newServer.ip, "192.168.1.7"); - newServer.name = new char[6]; - strcpy(newServer.name,"debug"); - servers.push_back(newServer); - waitType = 2; - haveAtLeastOne = 1;}/ * */ } } logger->log("VDR", Log::NOTICE, "END loop"); @@ -249,26 +233,17 @@ void VDR::findServers(std::vector& servers) std::vector* servers6 = vdp6.getServers(); // Add IPv6 found servers to servers vector, if not in servers already - // Free buffers from VDRServer objects if not taken. (Itching for that rewrite already). for(auto i6 = servers6->begin(); i6 != servers6->end(); i6++) { bool found = false; for(auto i4 = servers.begin(); i4 != servers.end(); i4++) - { - if (!strcmp(i4->name, i6->name)) { found = true; break; } - } + if (i4->name == i6->name) { found = true; break; } - if (found) - { - delete[] i6->name; - delete[] i6->ip; - } - else - { - servers.push_back(*i6); - } + if (found) continue; // Don't add + + servers.push_back(std::move(*i6)); // VDP6 is stopped, the vector owner only goes out of scope after this method } #endif @@ -281,7 +256,7 @@ void VDR::cancelFindingServer() findingServer = 0; } -void VDR::setServerIP(char* newIP) +void VDR::setServerIP(const char* newIP) { strcpy(serverIP, newIP); } diff --git a/vdr.h b/vdr.h index a30b24d..27ea09b 100644 --- a/vdr.h +++ b/vdr.h @@ -63,10 +63,13 @@ typedef std::vector RecTimerList; struct VDRServer { - char* ip; - char* name; + std::string ip; + std::string name; USHORT port; ULONG version; + + VDRServer(const std::string tip, const std::string tname, USHORT tport, ULONG tversion) + : ip(tip), name(tname), port(tport), version(tversion) {} }; struct RecTimerSorter // : public binary_function @@ -81,13 +84,10 @@ struct ServerSorter { bool operator() (const VDRServer& a, const VDRServer& b) { - if (strcmp(b.name, a.name) > 0) return true; - return false; + return a.name < b.name; } }; -class RecMan; - class StreamReceiver { public: @@ -103,16 +103,18 @@ class VDR_PacketReceiver : public EDReceiver // implementation in vdr.cc protected: // ULONG requestTime; ULONG receiverChannel; - + // If receiverChannel == 1: ULONG requestSerialNumber; // set by RequestResponse, used in ed_cb_find VDR_ResponsePacket* save_vresp; // set by ed_cb_call, used in RequestResponse - + // If receiverChannel == 2: ULONG streamID; StreamReceiver* streamReceiver; }; +class RecMan; + class VDR : public Thread_TYPE, public EventDispatcher, #ifdef VOMP_MEDIAPLAYER @@ -140,7 +142,7 @@ public ExternLogger void findServers(std::vector& servers); void cancelFindingServer(); - void setServerIP(char*); + void setServerIP(const char*); void setServerPort(USHORT); void setReceiveWindow(size_t size); int connect(); diff --git a/vserverselect.cc b/vserverselect.cc index a0c8792..09da0a3 100644 --- a/vserverselect.cc +++ b/vserverselect.cc @@ -55,10 +55,10 @@ VServerSelect::VServerSelect(std::vector& servers, void* treplyTo) sl.setSize(area.w - 20, area.h - 30 - 15); add(&sl); - sl.addOption(servers[0].name, 0, 1); + sl.addOption(servers[0].name.c_str(), 0, 1); for(UINT k = 1; k < servers.size(); k++) { - sl.addOption(servers[k].name, 0, 0); + sl.addOption(servers[k].name.c_str(), 0, 0); } replyTo = treplyTo; diff --git a/wol.cc b/wol.cc index 3f9a71a..881e6a5 100644 --- a/wol.cc +++ b/wol.cc @@ -77,7 +77,7 @@ void Wol::setEnabled(bool tEnabled) } #ifndef WIN32 /* Display the contents of the ARP cache in the kernel. */ -int Wol::find_ether(char *name) +int Wol::find_ether(const char *name) { char ip[100]; char line[200]; @@ -112,7 +112,7 @@ int Wol::find_ether(char *name) return 0; } #else -int Wol::find_ether(char *name) +int Wol::find_ether(const char *name) { sockaddr_in sock_address; int sockname_len=sizeof(sock_address); @@ -291,7 +291,7 @@ int Wol::doWakeUp() return (0); } -void Wol::setWakeUpIP(char* ip_addr) +void Wol::setWakeUpIP(const char* ip_addr) { if(find_ether(ip_addr)) { diff --git a/wol.h b/wol.h index 3020c40..10fab1f 100644 --- a/wol.h +++ b/wol.h @@ -38,12 +38,12 @@ class Wol ~Wol(); static Wol* getInstance(); int doWakeUp(); - void setWakeUpIP(char* ip_addr); + void setWakeUpIP(const char* ip_addr); void setEnabled(bool enabled); private: static Wol* instance; - int find_ether(char *name); + int find_ether(const char *name); int convertToBinary(char *bufp, unsigned char *addr); char ether_addr[100]; bool bEtherKnown; -- 2.39.2