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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
// license:BSD-3-Clause
// copyright-holders:Miodrag Milanovic
/***************************************************************************
network.cpp
Network output interface.
***************************************************************************/
#include "output_module.h"
#include "modules/osdmodule.h"
#include <thread>
#include <set>
#include "asio.h"
class output_client
{
public:
virtual ~output_client() {}
virtual void deliver(std::string &msg) = 0;
};
using output_client_ptr = std::shared_ptr<output_client>;
using client_set = std::set<output_client_ptr>;
class output_session : public output_client,
public std::enable_shared_from_this<output_session>
{
public:
output_session(asio::ip::tcp::socket socket, client_set *clients) :
m_socket(std::move(socket)),
m_clients(clients)
{
}
void start()
{
m_clients->insert(shared_from_this());
do_read();
}
private:
void deliver(std::string &msg)
{
std::strncpy(m_data, msg.c_str(), max_length);
do_write(msg.size());
}
void do_read()
{
auto self(shared_from_this());
m_socket.async_read_some(asio::buffer(m_input_m_data, max_length),
[this, self](std::error_code ec, std::size_t length)
{
if (!ec)
{
do_read();
}
else
{
m_clients->erase(shared_from_this());
}
});
}
void do_write(std::size_t length)
{
auto self(shared_from_this());
asio::async_write(m_socket, asio::buffer(m_data, length),
[this, self](std::error_code ec, std::size_t /*length*/)
{
if (ec)
{
m_clients->erase(shared_from_this());
}
});
}
asio::ip::tcp::socket m_socket;
enum { max_length = 1024 };
char m_data[max_length];
char m_input_m_data[max_length];
client_set *m_clients;
};
class output_network_server
{
public:
output_network_server(asio::io_context& io_context, short port) :
m_acceptor(io_context, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port))
{
do_accept();
}
void deliver_to_all(std::string msg)
{
for (auto client: m_clients)
client->deliver(msg);
}
private:
void do_accept()
{
m_acceptor.async_accept(
[this](std::error_code ec, asio::ip::tcp::socket socket)
{
if (!ec)
{
std::make_shared<output_session>(std::move(socket),&m_clients)->start();
}
do_accept();
});
}
asio::ip::tcp::acceptor m_acceptor;
client_set m_clients;
};
class output_network : public osd_module, public output_module
{
public:
output_network()
: osd_module(OSD_OUTPUT_PROVIDER, "network"),
output_module(),
m_io_context(nullptr), m_server(nullptr)
{
}
virtual ~output_network()
{
}
virtual int init(const osd_options &options) override
{
m_working_thread = std::thread([](output_network* self) { self->process_output(); }, this);
return 0;
}
virtual void exit() override
{
m_io_context->stop();
m_working_thread.join();
delete m_server;
delete m_io_context;
}
// output_module
virtual void notify(const char *outname, int32_t value) override
{
static char buf[256];
sprintf(buf, "%s = %d\n", ((outname==nullptr) ? "none" : outname), value);
m_server->deliver_to_all(buf);
}
// implementation
void process_output()
{
m_io_context = new asio::io_context();
m_server = new output_network_server(*m_io_context, 8000);
m_io_context->run();
}
private:
std::thread m_working_thread;
asio::io_context *m_io_context;
output_network_server *m_server;
};
MODULE_DEFINITION(OUTPUT_NETWORK, output_network)
|