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 "EventListener.hh"
6 #include "Socket.hh"
7 #include "CliComm.hh"
8 #include "AdhocCliCommParser.hh"
9 #include "Poller.hh"
10 #include <mutex>
11 #include <string>
12 #include <thread>
13 
14 namespace openmsx {
15 
16 class CommandController;
17 class EventDistributor;
18 
19 class CliConnection : public CliListener, private EventListener
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  std::thread thread; // TODO: Possible to make this private?
63 
64 private:
65  virtual void run() = 0;
66 
67  void execute(const std::string& command);
68 
69  // CliListener
70  void log(CliComm::LogLevel level, string_ref message) override;
71  void update(CliComm::UpdateType type, string_ref machine,
72  string_ref name, string_ref value) override;
73 
74  // EventListener
75  int signalEvent(const std::shared_ptr<const Event>& event) override;
76 
77  CommandController& commandController;
78  EventDistributor& eventDistributor;
79 
80  bool updateEnabled[CliComm::NUM_UPDATES];
81 };
82 
83 class StdioConnection final : public CliConnection
84 {
85 public:
86  StdioConnection(CommandController& commandController,
87  EventDistributor& eventDistributor);
88  ~StdioConnection();
89 
90  void output(string_ref message) override;
91 
92 private:
93  void close() override;
94  void run() override;
95 
96  Poller poller;
97 };
98 
99 #ifdef _WIN32
100 class PipeConnection final : public CliConnection
101 {
102 public:
103  PipeConnection(CommandController& commandController,
104  EventDistributor& eventDistributor,
105  string_ref name);
106  ~PipeConnection();
107 
108  void output(string_ref message) override;
109 
110 private:
111  void close() override;
112  void run() override;
113 
114  HANDLE pipeHandle;
115  HANDLE shutdownEvent;
116 };
117 #endif
118 
119 class SocketConnection final : public CliConnection
120 {
121 public:
122  SocketConnection(CommandController& commandController,
123  EventDistributor& eventDistributor,
124  SOCKET sd);
125  ~SocketConnection();
126 
127  void output(string_ref message) override;
128 
129 private:
130  void close() override;
131  void run() override;
132  void closeSocket();
133 
134  std::mutex sdMutex;
135  SOCKET sd;
136  Poller poller;
137  bool established;
138 };
139 
140 } // namespace openmsx
141 
142 #endif
virtual void output(string_ref message)=0
AdhocCliCommParser parser
int SOCKET
Definition: Socket.hh:22
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.
virtual void close()=0
Close the connection.
CliConnection(CommandController &commandController, EventDistributor &eventDistributor)
void end()
End this connection by sending the closing tag and then closing the stream.
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
Polls for events on a given file descriptor.
Definition: Poller.hh:13
bool getUpdateEnable(CliComm::UpdateType type) const
void startOutput()
Send opening XML tag, should be called exactly once by a subclass shortly after opening a connection...
void setUpdateEnable(CliComm::UpdateType type, bool value)