Pergunta

I have a server client app written in C++ on Linux. When a client connects to my server, the server spawns a thread that waits for the client to send the server commands to execute. the commands are OS dependent. the thread that the client is talking to the server on, calls global functions that perform the required command that the client wants. So I have to have two functions for every OS depended command the client sends to the server to be executed. All of these global functions are defined in the same header that the main thread function is. It's getting a bit messy with all of these functions for different OS's. My idea is to write two classes that are called WindowsFuncs and LinuxFuncs that have static member functions that perform the required command for that OS the class was designed for. What are some of stackoverflows ideas on how to clean my logic?

Foi útil?

Solução

This doesn't sound like a threading problem. Sounds like you can use simple inheritance.

Using something like

abstract class OSMethods {
  void listDir();
}

class OSMethodsLinux : OSMethods {
  void listDir() { system.exec("ls"); }
} 
class OSMethodsWin : OSMethods {
  void listDir() { system.exec("dir"); }
}

Then server client processing code has method like

  void accept(Socket s, OSMethods m) {
     s.readCommand();
     m.listDir();  // or whatever
  }

Make sure you pass correct instance to accept of either Linux or Win class. So no static methods.

Generally I've found that you will need no static methods in your programs (except main) unless you are doing clever stuff, most things just don't need them and they lead to less flexible design.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top