openMSX
CliConnection.hh
Go to the documentation of this file.
1 #ifndef CLICONNECTION_HH
2 #define CLICONNECTION_HH
3 
4 #include "CliListener.hh"
5 #include "Thread.hh"
6 #include "Semaphore.hh"
7 #include "EventListener.hh"
8 #include "Socket.hh"
9 #include "CliComm.hh"
10 #include "AdhocCliCommParser.hh"
11 #include <string>
12 
13 namespace openmsx {
14 
15 class CommandController;
16 class EventDistributor;
17 
18 class CliConnection : public CliListener, private EventListener
19  , protected Runnable
20 {
21 public:
22  void setUpdateEnable(CliComm::UpdateType type, bool value) {
23  updateEnabled[type] = value;
24  }
26  return updateEnabled[type];
27  }
28 
34  void start();
35 
36 protected:
37  CliConnection(CommandController& commandController,
38  EventDistributor& eventDistributor);
40 
41  virtual void output(string_ref message) = 0;
42 
47  void end();
48 
52  virtual void close() = 0;
53 
59  void startOutput();
60 
62  Thread thread; // TODO: Possible to make this private?
63 
64 private:
65  void execute(const std::string& command);
66 
67  // CliListener
68  void log(CliComm::LogLevel level, string_ref message) override;
69  void update(CliComm::UpdateType type, string_ref machine,
70  string_ref name, string_ref value) override;
71 
72  // EventListener
73  int signalEvent(const std::shared_ptr<const Event>& event) override;
74 
75  CommandController& commandController;
76  EventDistributor& eventDistributor;
77 
78  bool updateEnabled[CliComm::NUM_UPDATES];
79 };
80 
81 class StdioConnection final : public CliConnection
82 {
83 public:
87 
88  void output(string_ref message) override;
89 
90 private:
91  void close() override;
92  void run() override;
93 
94  bool ok;
95 };
96 
97 #ifdef _WIN32
98 class PipeConnection final : public CliConnection
99 {
100 public:
101  PipeConnection(CommandController& commandController,
103  string_ref name);
104  ~PipeConnection();
105 
106  void output(string_ref message) override;
107 
108 private:
109  void close() override;
110  void run() override;
111 
112  HANDLE pipeHandle;
113  HANDLE shutdownEvent;
114 };
115 #endif
116 
117 class SocketConnection final : public CliConnection
118 {
119 public:
122  SOCKET sd);
124 
125  void output(string_ref message) override;
126 
127 private:
128  void close() override;
129  void run() override;
130 
131  Semaphore sem;
132  SOCKET sd;
133  bool established;
134 };
135 
136 } // namespace openmsx
137 
138 #endif
virtual void output(string_ref message)=0
AdhocCliCommParser parser
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
void start()
Starts the helper thread.
int SOCKET
Definition: Socket.hh:22
StdioConnection(CommandController &commandController, EventDistributor &eventDistributor)
bool getUpdateEnable(CliComm::UpdateType type) const
virtual void close()=0
Close the connection.
CliConnection(CommandController &commandController, EventDistributor &eventDistributor)
void output(string_ref message) override
void end()
End this connection by sending the closing tag and then closing the stream.
SocketConnection(CommandController &commandController, EventDistributor &eventDistributor, SOCKET sd)
eventDistributor(eventDistributor_)
commandController(commandController_)
void startOutput()
Send opening XML tag, should be called exactly once by a subclass shortly after opening a connection...
void output(string_ref message) override
void setUpdateEnable(CliComm::UpdateType type, bool value)