Skip to content

Commit

Permalink
[udp] Add possibility to send to broadcast address
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Jul 30, 2023
1 parent e83e57c commit fb1abb4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/ossia/network/sockets/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ struct receive_fd_configuration : fd_configuration
struct socket_configuration
{
std::string host;
uint16_t port;
uint16_t port{};
bool broadcast{};
};

struct send_socket_configuration : socket_configuration
Expand Down
12 changes: 10 additions & 2 deletions src/ossia/network/sockets/udp_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class udp_send_socket
public:
udp_send_socket(const socket_configuration& conf, boost::asio::io_context& ctx)
: m_context{ctx}
, m_endpoint{boost::asio::ip::make_address(conf.host), conf.port}
, m_endpoint{conf.broadcast ? boost::asio::ip::address_v4::broadcast() : boost::asio::ip::make_address(conf.host), conf.port}
, m_socket{ctx}
{
}
Expand All @@ -102,7 +102,15 @@ class udp_send_socket
{
}

void connect() { m_socket.open(boost::asio::ip::udp::v4()); }
void connect()
{
m_socket.open(boost::asio::ip::udp::v4());

m_socket.set_option(boost::asio::ip::udp::socket::reuse_address(true));

if(m_endpoint.address() == boost::asio::ip::address_v4::broadcast())
m_socket.set_option(boost::asio::socket_base::broadcast(true));
}

void close()
{
Expand Down

0 comments on commit fb1abb4

Please sign in to comment.