2#include <ossia/network/context.hpp>
3#include <ossia/network/sockets/configuration.hpp>
5#include <boost/asio/io_context.hpp>
6#include <boost/asio/ip/tcp.hpp>
7#include <boost/asio/ip/udp.hpp>
8#include <boost/asio/local/datagram_protocol.hpp>
9#include <boost/asio/placeholders.hpp>
10#include <boost/asio/strand.hpp>
11#include <boost/asio/write.hpp>
13#include <nano_signal_slot.hpp>
20 using proto = boost::asio::ip::tcp;
21 using socket =
typename proto::socket;
23 tcp_listener() =
delete;
24 tcp_listener(
const tcp_listener&) =
delete;
25 tcp_listener& operator=(
const tcp_listener&) =
delete;
26 tcp_listener(tcp_listener&&) =
default;
27 tcp_listener& operator=(tcp_listener&&) =
default;
28 explicit tcp_listener(proto::socket sock)
29 : m_socket{std::move(sock)}
33 void close() { m_socket.close(); }
35 void write(
const boost::asio::const_buffer& buf) { boost::asio::write(m_socket, buf); }
41 proto::socket m_socket;
47 using proto = boost::asio::ip::tcp;
48 using socket =
typename proto::socket;
49 using listener = tcp_listener;
51 tcp_server(
const inbound_socket_configuration& conf, boost::asio::io_context& ctx)
54 boost::asio::make_strand(ctx),
55 proto::endpoint{boost::asio::ip::make_address(conf.bind), conf.port}}
60 const inbound_socket_configuration& conf, ossia::net::network_context_ptr ctx)
61 : tcp_server{conf, ctx->context}
65 boost::asio::io_context& m_context;
66 proto::acceptor m_acceptor;
72 using proto = boost::asio::ip::tcp;
73 using socket =
typename proto::socket;
75 tcp_client(
const outbound_socket_configuration& conf, boost::asio::io_context& ctx)
77 , m_endpoint{boost::asio::ip::make_address(conf.host), conf.port}
78 , m_socket{boost::asio::make_strand(ctx)}
84 m_socket.connect(m_endpoint);
88 bool connected()
const {
return m_connected; }
92 boost::asio::post(m_context, [
this] {
98 void write(
const char* data, std::size_t sz)
100 boost::asio::write(m_socket, boost::asio::buffer(data, sz));
103 Nano::Signal<void()> on_open;
104 Nano::Signal<void()> on_close;
105 Nano::Signal<void()> on_fail;
107 boost::asio::io_context& m_context;
108 proto::endpoint m_endpoint;
109 proto::socket m_socket;
110 bool m_connected{
false};