summaryrefslogtreecommitdiff
path: root/gr-blocks/lib/udp_sink_impl.cc
diff options
context:
space:
mode:
authorMarcus Müller <mmueller@gnuradio.org>2019-08-07 21:45:12 +0200
committerMarcus Müller <marcus@hostalia.de>2019-08-09 23:04:28 +0200
commitf7bbf2c1d8d780294f3e016aff239ca35eb6516e (patch)
treee09ab6112e02b2215b2d59ac24d3d6ea2edac745 /gr-blocks/lib/udp_sink_impl.cc
parent78431dc6941e3acc67c858277dfe4a0ed583643c (diff)
Tree: clang-format without the include sorting
Diffstat (limited to 'gr-blocks/lib/udp_sink_impl.cc')
-rw-r--r--gr-blocks/lib/udp_sink_impl.cc164
1 files changed, 78 insertions, 86 deletions
diff --git a/gr-blocks/lib/udp_sink_impl.cc b/gr-blocks/lib/udp_sink_impl.cc
index b540a7da19..cb44ba0b6f 100644
--- a/gr-blocks/lib/udp_sink_impl.cc
+++ b/gr-blocks/lib/udp_sink_impl.cc
@@ -35,49 +35,45 @@
#include <string.h>
namespace gr {
- namespace blocks {
-
- udp_sink::sptr
- udp_sink::make(size_t itemsize,
- const std::string &host, int port,
- int payload_size, bool eof)
- {
- return gnuradio::get_initial_sptr
- (new udp_sink_impl(itemsize, host, port,
- payload_size, eof));
- }
-
- udp_sink_impl::udp_sink_impl(size_t itemsize,
- const std::string &host, int port,
- int payload_size, bool eof)
- : sync_block("udp_sink",
- io_signature::make(1, 1, itemsize),
- io_signature::make(0, 0, 0)),
- d_itemsize(itemsize), d_payload_size(payload_size), d_eof(eof),
- d_connected(false)
- {
- // Get the destination address
- connect(host, port);
- }
-
- // public constructor that returns a shared_ptr
- udp_sink_impl::~udp_sink_impl()
- {
- if(d_connected)
+namespace blocks {
+
+udp_sink::sptr udp_sink::make(
+ size_t itemsize, const std::string& host, int port, int payload_size, bool eof)
+{
+ return gnuradio::get_initial_sptr(
+ new udp_sink_impl(itemsize, host, port, payload_size, eof));
+}
+
+udp_sink_impl::udp_sink_impl(
+ size_t itemsize, const std::string& host, int port, int payload_size, bool eof)
+ : sync_block(
+ "udp_sink", io_signature::make(1, 1, itemsize), io_signature::make(0, 0, 0)),
+ d_itemsize(itemsize),
+ d_payload_size(payload_size),
+ d_eof(eof),
+ d_connected(false)
+{
+ // Get the destination address
+ connect(host, port);
+}
+
+// public constructor that returns a shared_ptr
+udp_sink_impl::~udp_sink_impl()
+{
+ if (d_connected)
disconnect();
- }
+}
- void
- udp_sink_impl::connect(const std::string &host, int port)
- {
- if(d_connected)
+void udp_sink_impl::connect(const std::string& host, int port)
+{
+ if (d_connected)
disconnect();
- std::string s_port = (boost::format("%d")%port).str();
- if(host.size() > 0) {
+ std::string s_port = (boost::format("%d") % port).str();
+ if (host.size() > 0) {
boost::asio::ip::udp::resolver resolver(d_io_service);
- boost::asio::ip::udp::resolver::query query(host, s_port,
- boost::asio::ip::resolver_query_base::passive);
+ boost::asio::ip::udp::resolver::query query(
+ host, s_port, boost::asio::ip::resolver_query_base::passive);
d_endpoint = *resolver.resolve(query);
d_socket = new boost::asio::ip::udp::socket(d_io_service);
@@ -87,63 +83,59 @@ namespace gr {
d_socket->set_option(roption);
d_connected = true;
- }
}
+}
- void
- udp_sink_impl::disconnect()
- {
- if(!d_connected)
+void udp_sink_impl::disconnect()
+{
+ if (!d_connected)
return;
- gr::thread::scoped_lock guard(d_mutex); // protect d_socket from work()
+ gr::thread::scoped_lock guard(d_mutex); // protect d_socket from work()
- // Send a few zero-length packets to signal receiver we are done
- boost::array<char, 0> send_buf;
- if(d_eof) {
+ // Send a few zero-length packets to signal receiver we are done
+ boost::array<char, 0> send_buf;
+ if (d_eof) {
int i;
- for(i = 0; i < 3; i++)
- d_socket->send_to(boost::asio::buffer(send_buf), d_endpoint);
- }
-
- d_socket->close();
- delete d_socket;
-
- d_connected = false;
+ for (i = 0; i < 3; i++)
+ d_socket->send_to(boost::asio::buffer(send_buf), d_endpoint);
}
- int
- udp_sink_impl::work (int noutput_items,
- gr_vector_const_void_star &input_items,
- gr_vector_void_star &output_items)
- {
- const char *in = (const char *) input_items[0];
- ssize_t r=0, bytes_sent=0, bytes_to_send=0;
- ssize_t total_size = noutput_items*d_itemsize;
-
- gr::thread::scoped_lock guard(d_mutex); // protect d_socket
-
- while(bytes_sent < total_size) {
- bytes_to_send = std::min((ssize_t)d_payload_size, (total_size-bytes_sent));
-
- if(d_connected) {
- try {
- r = d_socket->send_to(boost::asio::buffer((void*)(in+bytes_sent), bytes_to_send),
- d_endpoint);
- }
- catch(std::exception& e) {
- GR_LOG_ERROR(d_logger, boost::format("send error: %s") % e.what());
- return -1;
- }
- }
- else
- r = bytes_to_send; // discarded for lack of connection
+ d_socket->close();
+ delete d_socket;
+
+ d_connected = false;
+}
+
+int udp_sink_impl::work(int noutput_items,
+ gr_vector_const_void_star& input_items,
+ gr_vector_void_star& output_items)
+{
+ const char* in = (const char*)input_items[0];
+ ssize_t r = 0, bytes_sent = 0, bytes_to_send = 0;
+ ssize_t total_size = noutput_items * d_itemsize;
+
+ gr::thread::scoped_lock guard(d_mutex); // protect d_socket
+
+ while (bytes_sent < total_size) {
+ bytes_to_send = std::min((ssize_t)d_payload_size, (total_size - bytes_sent));
+
+ if (d_connected) {
+ try {
+ r = d_socket->send_to(
+ boost::asio::buffer((void*)(in + bytes_sent), bytes_to_send),
+ d_endpoint);
+ } catch (std::exception& e) {
+ GR_LOG_ERROR(d_logger, boost::format("send error: %s") % e.what());
+ return -1;
+ }
+ } else
+ r = bytes_to_send; // discarded for lack of connection
bytes_sent += r;
- }
-
- return noutput_items;
}
- } /* namespace blocks */
-} /* namespace gr */
+ return noutput_items;
+}
+} /* namespace blocks */
+} /* namespace gr */