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);
39  ~CliConnection() override;
40 
41  virtual void output(std::string_view message) = 0;
42 
47  void end();
48 
52  virtual void close() = 0;
53 
59  void startOutput();
60 
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, std::string_view message) override;
71  void update(CliComm::UpdateType type, std::string_view machine,
72  std::string_view name, std::string_view 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  std::thread thread;
81 
82  bool updateEnabled[CliComm::NUM_UPDATES];
83 };
84 
85 class StdioConnection final : public CliConnection
86 {
87 public:
88  StdioConnection(CommandController& commandController,
89  EventDistributor& eventDistributor);
90  ~StdioConnection() override;
91 
92  void output(std::string_view message) override;
93 
94 private:
95  void close() override;
96  void run() override;
97 };
98 
99 #ifdef _WIN32
100 class PipeConnection final : public CliConnection
101 {
102 public:
103  PipeConnection(CommandController& commandController,
104  EventDistributor& eventDistributor,
105  std::string_view name);
106  ~PipeConnection() override;
107 
108  void output(std::string_view 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() override;
126 
127  void output(std::string_view 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  bool established;
137 };
138 
139 } // namespace openmsx
140 
141 #endif
openmsx::CliConnection::output
virtual void output(std::string_view message)=0
openmsx::SOCKET
int SOCKET
Definition: Socket.hh:22
openmsx::CommandController
Definition: CommandController.hh:18
openmsx::StdioConnection
Definition: CliConnection.hh:86
openmsx::StdioConnection::~StdioConnection
~StdioConnection() override
Definition: CliConnection.cc:169
openmsx::CliComm::UpdateType
UpdateType
Definition: CliComm.hh:20
openmsx::CliConnection::close
virtual void close()=0
Close the connection.
openmsx::CliListener
Definition: CliListener.hh:9
EventListener.hh
openmsx::CliConnection::startOutput
void startOutput()
Send opening XML tag, should be called exactly once by a subclass shortly after opening a connection.
Definition: CliConnection.cc:108
openmsx::CliConnection::CliConnection
CliConnection(CommandController &commandController, EventDistributor &eventDistributor)
Definition: CliConnection.cc:67
openmsx::SocketConnection::SocketConnection
SocketConnection(CommandController &commandController, EventDistributor &eventDistributor, SOCKET sd)
Definition: CliConnection.cc:308
openmsx::CliConnection::poller
Poller poller
Definition: CliConnection.hh:62
Socket.hh
openmsx::EventDistributor
Definition: EventDistributor.hh:17
openmsx::CliConnection
Definition: CliConnection.hh:20
openmsx::CliConnection::~CliConnection
~CliConnection() override
Definition: CliConnection.cc:78
openmsx::SocketConnection::output
void output(std::string_view message) override
Definition: CliConnection.cc:363
openmsx::CliComm::LogLevel
LogLevel
Definition: CliComm.hh:13
openmsx::SocketConnection::~SocketConnection
~SocketConnection() override
Definition: CliConnection.cc:316
openmsx::StdioConnection::StdioConnection
StdioConnection(CommandController &commandController, EventDistributor &eventDistributor)
Definition: CliConnection.cc:162
openmsx::CliConnection::start
void start()
Starts the helper thread.
Definition: CliConnection.cc:113
openmsx::Poller
Polls for events on a given file descriptor.
Definition: Poller.hh:14
CliListener.hh
openmsx::SocketConnection
Definition: CliConnection.hh:120
openmsx::CliComm::NUM_UPDATES
@ NUM_UPDATES
Definition: CliComm.hh:31
openmsx::EventListener
Definition: EventListener.hh:11
Poller.hh
openmsx::CliConnection::setUpdateEnable
void setUpdateEnable(CliComm::UpdateType type, bool value)
Definition: CliConnection.hh:22
AdhocCliCommParser
Definition: AdhocCliCommParser.hh:9
openmsx::CliConnection::parser
AdhocCliCommParser parser
Definition: CliConnection.hh:61
openmsx::StdioConnection::output
void output(std::string_view message) override
Definition: CliConnection.cc:193
CliComm.hh
openmsx::CliConnection::end
void end()
End this connection by sending the closing tag and then closing the stream.
Definition: CliConnection.cc:118
openmsx::CliConnection::getUpdateEnable
bool getUpdateEnable(CliComm::UpdateType type) const
Definition: CliConnection.hh:25
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
AdhocCliCommParser.hh