udp_stream::udp_stream( boost::asio::io_service &io_service, const boost::asio::ip::udp::endpoint &endpoint, const stream_config &config, std::size_t buffer_size) : udp_stream(boost::asio::ip::udp::socket(io_service, endpoint.protocol()), endpoint, config, buffer_size) { }
blocking_t(const std::string& host, std::uint16_t port) : io_service(), socket(io_service) { boost::asio::ip::udp::resolver resolver(io_service); boost::asio::ip::udp::resolver::query query(host, boost::lexical_cast<std::string>(port), boost::asio::ip::udp::resolver::query::flags::numeric_service); endpoint = *resolver.resolve(query); socket.open(endpoint.protocol()); }
void udp_multiplexor::send_to( const boost::asio::ip::udp::endpoint & ep, const char * buf, const std::size_t & len ) { m_bytes_sent += len; auto uptime = std::time(0) - send_time_; if (uptime > 0) { m_bps_sent = m_bytes_sent / uptime; log_none("m_bps_sent = " << m_bps_sent); if (uptime > 1) { send_time_ = std::time(0); uptime = std::time(0) - send_time_; m_bytes_sent = 0; } } if (ep.protocol() == boost::asio::ip::udp::v4()) { if (socket_ipv4_.is_open()) { boost::system::error_code ec; /** * Perform a blocking send_to. */ socket_ipv4_.send_to(boost::asio::buffer(buf, len), ep, 0, ec); if (ec) { log_debug("UDP v4 send failed " << ec.message() << "."); if (ec == boost::asio::error::broken_pipe) { std::uint16_t port = socket_ipv4_.local_endpoint().port(); close(); open(port); boost::system::error_code ignored_ec; /** * Perform a blocking send_to. */ socket_ipv4_.send_to( boost::asio::buffer(buf, len), ep, 0, ignored_ec ); } } } } else { if (socket_ipv6_.is_open()) { boost::system::error_code ec; /** * Perform a blocking send_to. */ socket_ipv6_.send_to(boost::asio::buffer(buf, len), ep, 0, ec); if (ec) { log_debug("UDP v6 send failed " << ec.message() << "."); if (ec == boost::asio::error::broken_pipe) { std::uint16_t port = socket_ipv6_.local_endpoint().port(); close(); open(port); boost::system::error_code ignored_ec; /** * Perform a blocking send_to. */ socket_ipv6_.send_to( boost::asio::buffer(buf, len), ep, 0, ignored_ec ); } } } } }
void udp_multiplexor::send_to( const boost::asio::ip::udp::endpoint & ep, const char * buf, const std::size_t & len ) { /** * If the length cannot fit within the MTU of the underlying * datagram protocol we can perform fragmentation and IP-layer * fragmentation is allowed to the size of 2 Kilobytes. */ if (len < 65535) { m_bytes_sent += len; auto uptime = std::time(0) - send_time_; if (uptime > 0) { m_bps_sent = m_bytes_sent / uptime; log_none("m_bps_sent = " << m_bps_sent); if (uptime > 1) { send_time_ = std::time(0); uptime = std::time(0) - send_time_; m_bytes_sent = 0; } } if (ep.protocol() == boost::asio::ip::udp::v4()) { if (socket_ipv4_.is_open()) { boost::system::error_code ec; /** * Perform a blocking send_to. */ socket_ipv4_.send_to(boost::asio::buffer(buf, len), ep, 0, ec); if (ec) { log_debug("UDP v4 send failed " << ec.message() << "."); if (ec == boost::asio::error::broken_pipe) { std::uint16_t port = socket_ipv4_.local_endpoint().port(); close(); open(port); boost::system::error_code ignored_ec; /** * Perform a blocking send_to. */ socket_ipv4_.send_to( boost::asio::buffer(buf, len), ep, 0, ignored_ec ); } } } } else { if (socket_ipv6_.is_open()) { boost::system::error_code ec; /** * Perform a blocking send_to. */ socket_ipv6_.send_to(boost::asio::buffer(buf, len), ep, 0, ec); if (ec) { log_debug("UDP v6 send failed " << ec.message() << "."); if (ec == boost::asio::error::broken_pipe) { std::uint16_t port = socket_ipv6_.local_endpoint().port(); close(); open(port); boost::system::error_code ignored_ec; /** * Perform a blocking send_to. */ socket_ipv6_.send_to( boost::asio::buffer(buf, len), ep, 0, ignored_ec ); } } } } } }