]> git.vomp.tv Git - vompclient.git/commitdiff
VConnect, VDRServer, VDR::findServers to std::strings
authorChris Tallon <chris@vomp.tv>
Sun, 22 Mar 2020 22:43:17 +0000 (22:43 +0000)
committerChris Tallon <chris@vomp.tv>
Sun, 22 Mar 2020 22:43:17 +0000 (22:43 +0000)
12 files changed:
command.cc
command.h
defines.h
main.cc
vconnect.cc
vconnect.h
vdp6.cc
vdr.cc
vdr.h
vserverselect.cc
wol.cc
wol.h

index b38569d88cc40dfda61d080ae2efa19a877f5be2..4dcb98bfb880c5f4e1acaccafb7a30374e97de8f 100644 (file)
@@ -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;
 
index 847fd0572ccb255fd0088124cb7756c60ebc01b8..70c7a4caa8c02c640cc86ca81dbe440ee582e51d 100644 (file)
--- a/command.h
+++ b/command.h
@@ -28,7 +28,7 @@
 
 #include <time.h>
 #ifndef WIN32
-#include <pthread.h>
+#include <pthread.h>   // why?
 #endif
 
 #include <string>
@@ -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;
index 82dbbafdd915f6445e0f335a0f70f2b8c46bf262..d705965dae5155b7f01f0d1b1e09e58caf9a5eaf 100644 (file)
--- a/defines.h
+++ b/defines.h
@@ -47,6 +47,8 @@ std::string tp2str(const std::chrono::time_point<std::chrono::system_clock>& tp)
 
 int getClockRealTime(struct timespec *tp);
 
+const std::string& getCommandLineServer();
+
 //#define WINDOWS_LEGACY
 
 #ifdef WIN32
diff --git a/main.cc b/main.cc
index c6f0c736dd27d8a0bb46af3a0cb5715408cf2226..aca7fa156ab7be90cff790a3657f827285dbdb53 100644 (file)
--- 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<std::chrono::system_clock>& tp)
   ss << std::put_time(stm, "%T") << "." << std::setfill('0') << std::setw(3) << ttm;
   return ss.str();
 }
+
+const std::string& getCommandLineServer()
+{
+  return argvServer;
+}
index 3e4d99b14ba6565c63db93c280a81824c7df4142..c31cb09b6bf791d87821153fff72482c50ee3bea 100644 (file)
@@ -17,6 +17,7 @@
     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
 */
 
+#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<std::mutex> 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");
index 0f4102bd126f28f39b5364336b06723aafcf8986..3ea21ce06ed8adc806c3bc8d792479ca83f1e217 100644 (file)
@@ -20,7 +20,6 @@
 #ifndef VCONNECT_H
 #define VCONNECT_H
 
-#include <string.h>
 #include <vector>
 #include <mutex>
 #include <thread>
@@ -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<VDRServer> servers;
     int selectedServer;
-    char* server;
 
     std::thread connectThread;
     std::mutex threadMutex;
diff --git a/vdp6.cc b/vdp6.cc
index 7d321b6fae8ca016429b223daa79aa8b6a289f7a..0dfa21f119452829ff4d43df2c321e29f1262a23 100644 (file)
--- 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<UINT>(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 8fda12f3e66697ce34aaa4f631a8db3592531d61..db620a4698293f882ab4c1abe7e81e5ff2f9ebef 100644 (file)
--- a/vdr.cc
+++ b/vdr.cc
@@ -201,25 +201,18 @@ void VDR::findServers(std::vector<VDRServer>& servers)
       const char* vdpreply = static_cast<const char*>(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<VDRServer>& 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<VDRServer>& servers)
   std::vector<VDRServer>* 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 a30b24d927833cc3c3eeb0bbca62ca5aadd75e58..27ea09bfea5b036ae033f77959a6e0bacf7ac646 100644 (file)
--- a/vdr.h
+++ b/vdr.h
@@ -63,10 +63,13 @@ typedef std::vector<RecTimer*> 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<double, double, bool>
@@ -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<VDRServer>& servers);
     void cancelFindingServer();
-    void setServerIP(char*);
+    void setServerIP(const char*);
     void setServerPort(USHORT);
     void setReceiveWindow(size_t size);
     int connect();
index a0c8792137494e5ce39a8ed813167c38a03b0210..09da0a33ffb6e2d573d0a159fbaa1abaf754a86e 100644 (file)
@@ -55,10 +55,10 @@ VServerSelect::VServerSelect(std::vector<VDRServer>& 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 3f9a71aaeb446be8376524e0d5f578c8341ad0c6..881e6a5b6c4bd489148965866ca52b52ad20bc6c 100644 (file)
--- 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 3020c40b4e14ac8a466a3afe9fbb98a48d92dd95..10fab1f4291517d030bbcdeb6d68daa76c48557c 100644 (file)
--- 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;