blob: 50eee82ca068dc7ff19634be295829db6a3d1852 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
#ifndef WANDA_CONTROL_CONNECTION_HPP
#define WANDA_CONTROL_CONNECTION_HPP
#include "keyed.hpp"
#include <boost/asio.hpp>
#include <istream>
#include <memory>
#include <ostream>
#include <set>
#include <string>
namespace wanda
{
struct control_connection : keyed<control_connection>, std::enable_shared_from_this<control_connection>
{
using protocol = boost::asio::local::stream_protocol;
using pointer = std::shared_ptr<control_connection>;
struct listener
{
virtual void on_close(pointer connection) {}
virtual void on_received(pointer connection, std::string message) {}
virtual void on_error(pointer connection, boost::system::error_code) {}
};
/**
* @internal
* @brief Construct a new control connection object
*
* @param socket The socket connected to the remote control endpoint
*/
control_connection(key, protocol::socket socket);
/**
* @brief Add the given listener to this control connection's listener set
*
* @returns <code>true</code> iff. the listener was not already in the listener set
*/
bool add(std::shared_ptr<listener> listener);
/**
* @brief Remove the given listener from this control connection's listener set
*
* @return <code>true</code> iff. the listener was previously registered with this control connection
*/
bool remove(std::shared_ptr<listener> listener);
/**
* @brief Start I/O processing for this control connection
*/
void start();
/**
* @brief Close this control connection
*/
void close();
void send(std::string message);
private:
friend pointer make_control_connection(protocol::socket &&socket);
void perform_read();
protocol::socket m_socket;
boost::asio::streambuf m_in{};
boost::asio::streambuf m_out{};
std::istream m_input{&m_in};
std::ostream m_output{&m_out};
std::set<std::shared_ptr<listener>> m_listeners{};
bool m_running{};
};
control_connection::pointer make_control_connection(control_connection::protocol::socket &&socket);
} // namespace wanda
#endif
|