-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver-sink.cpp
More file actions
74 lines (61 loc) · 2.83 KB
/
server-sink.cpp
File metadata and controls
74 lines (61 loc) · 2.83 KB
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
// This example demonstrates how to implement a TCP server which acts as a data sink. When any
// streaming client connects, it subscribes to that client's signal "/Value", and prints messages
// to the console when data packets are received. The "client-source" sample provides a suitable
// client to connect to this demo. Press Ctrl+C to gracefully shut down the server.
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <boost/asio.hpp>
#include <ws-streaming/ws-streaming.hpp>
// Forward declarations, so we can order the code as it executes chronologically.
void on_data_received(std::int64_t domain_value, std::size_t sample_count, const void *data, std::size_t size);
void on_available(wss::connection_ptr connection, wss::remote_signal_ptr signal);
int main(int argc, char *argv[])
{
// Set up a single-threaded Boost.Asio execution context.
boost::asio::io_context ioc{1};
// Create the WebSocket Streaming server object. It will use the Boost.Asio execution context
// to accept incoming connections, generating Boost.Signals2 signals (events) we can react to.
wss::server server{ioc.get_executor()};
server.add_default_listeners();
server.run();
// Attach a Boost.Signals2 "slot" (event handler) which will be called whenever a
// signal becomes available from any client that has connected to the server.
server.on_available.connect(on_available);
// Set up a Boost.Asio signal handler to gracefully close the server when Ctrl+C is pressed.
boost::asio::signal_set signals{ioc, SIGINT};
signals.async_wait([&](const boost::system::error_code& ec, int signal)
{
if (!ec)
server.close();
});
// Enter the Boost.Asio event loop. This function will return when there are no more scheduled
// asynchronous work items - this happens when the server has been closed by the Ctrl+C signal
// handler above.
ioc.run();
}
void on_available(
wss::connection_ptr connection,
wss::remote_signal_ptr signal)
{
std::cout << "signal available from "
<< connection->socket().remote_endpoint().address()
<< ':' << connection->socket().remote_endpoint().port()
<< ": " << signal->id() << std::endl;
// Subscribe to the signal if its ID is "/Value".
if (signal->id() == "/Value")
{
signal->subscribe();
// If desired, we can std::bind() additional arguments to the data handler, for example
// to give it access to the wss::remote_signal or wss::connection objects.
signal->on_data_received.connect(on_data_received);
}
}
void on_data_received(
std::int64_t domain_value,
std::size_t sample_count,
const void *data,
std::size_t size)
{
std::cout << "received " << size << " data byte(s), " << sample_count << " sample(s) with domain value " << domain_value << std::endl;
}