commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] [gnuradio] 02/17: tcp_server_sink: implementation


From: git
Subject: [Commit-gnuradio] [gnuradio] 02/17: tcp_server_sink: implementation
Date: Mon, 1 Aug 2016 21:58:55 +0000 (UTC)

This is an automated email from the git hooks/post-receive script.

jcorgan pushed a commit to branch master
in repository gnuradio.

commit 950310a0cc632723358e1301ab76c4a58d545162
Author: Jiří Pinkava <address@hidden>
Date:   Thu Dec 4 16:36:11 2014 +0100

    tcp_server_sink: implementation
---
 gr-blocks/lib/CMakeLists.txt          |   1 +
 gr-blocks/lib/tcp_server_sink_impl.cc | 161 ++++++++++++++++++++++++++++++++++
 gr-blocks/lib/tcp_server_sink_impl.h  |  73 +++++++++++++++
 3 files changed, 235 insertions(+)

diff --git a/gr-blocks/lib/CMakeLists.txt b/gr-blocks/lib/CMakeLists.txt
index 643190c..1d69f27 100644
--- a/gr-blocks/lib/CMakeLists.txt
+++ b/gr-blocks/lib/CMakeLists.txt
@@ -199,6 +199,7 @@ list(APPEND gr_blocks_sources
     throttle_impl.cc
     transcendental_impl.cc
     tcp_connection.cc
+    tcp_server_sink_impl.cc
     tuntap_pdu_impl.cc
     tag_gate_impl.cc
     tagged_stream_align_impl.cc
diff --git a/gr-blocks/lib/tcp_server_sink_impl.cc 
b/gr-blocks/lib/tcp_server_sink_impl.cc
new file mode 100644
index 0000000..329e798
--- /dev/null
+++ b/gr-blocks/lib/tcp_server_sink_impl.cc
@@ -0,0 +1,161 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2007-2010,2013 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "tcp_server_sink_impl.h"
+#include <gnuradio/io_signature.h>
+#include <algorithm>
+#include <boost/array.hpp>
+#include <boost/asio.hpp>
+#include <boost/format.hpp>
+#include <gnuradio/thread/thread.h>
+#include <stdexcept>
+#include <stdio.h>
+#include <string.h>
+
+namespace gr {
+  namespace blocks {
+
+    tcp_server_sink::sptr
+      tcp_server_sink::make(size_t itemsize,
+          const std::string &host, int port,
+          bool noblock)
+      {
+        return gnuradio::get_initial_sptr
+          (new tcp_server_sink_impl(itemsize, host, port, noblock));
+      }
+
+    tcp_server_sink_impl::tcp_server_sink_impl(size_t itemsize,
+        const std::string &host, int port,
+        bool noblock)
+      : sync_block("tcp_server_sink",
+          io_signature::make(1, 1, itemsize),
+          io_signature::make(0, 0, 0)),
+      d_itemsize(itemsize),
+      d_acceptor(d_io_service),
+      d_buf(new uint8_t[BUF_SIZE]),
+      d_writing(0)
+      {
+        std::string s_port = (boost::format("%d") % port).str();
+        std::string s_host = host.empty() ? std::string("localhost") : host;
+        boost::asio::ip::tcp::resolver resolver(d_io_service);
+        boost::asio::ip::tcp::resolver::query query(s_host, s_port,
+            boost::asio::ip::resolver_query_base::passive);
+        d_endpoint = *resolver.resolve(query);
+
+        d_acceptor.open(d_endpoint.protocol());
+        
d_acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
+        d_acceptor.bind(d_endpoint);
+        d_acceptor.listen();
+
+        if (!noblock) {
+          d_socket.reset(new boost::asio::ip::tcp::socket(d_io_service));
+          d_acceptor.accept(*d_socket, d_endpoint);
+          d_sockets.insert(d_socket.release());
+        }
+
+        d_socket.reset(new boost::asio::ip::tcp::socket(d_io_service));
+        d_acceptor.async_accept(*d_socket, 
boost::bind(&tcp_server_sink_impl::do_accept,
+              this, boost::asio::placeholders::error));
+        d_io_serv_thread = boost::thread(
+            boost::bind(&boost::asio::io_service::run, &d_io_service));
+      }
+
+    void
+    tcp_server_sink_impl::do_accept(const boost::system::error_code& error)
+    {
+      if (!error) {
+        gr::thread::scoped_lock guard(d_writing_mut);
+        d_sockets.insert(d_socket.release());
+        d_socket.reset(new boost::asio::ip::tcp::socket(d_io_service));
+        d_acceptor.async_accept(*d_socket, 
boost::bind(&tcp_server_sink_impl::do_accept,
+              this, boost::asio::placeholders::error));
+      }
+    }
+
+    void
+    tcp_server_sink_impl::do_write(const boost::system::error_code& error,
+        size_t len, std::set<boost::asio::ip::tcp::socket *>::iterator i)
+    {
+      {
+        gr::thread::scoped_lock guard(d_writing_mut);
+        --d_writing;
+        if (error) {
+          delete *i;
+          d_sockets.erase(i);
+        }
+      }
+      d_writing_cond.notify_one();
+    }
+
+    tcp_server_sink_impl::~tcp_server_sink_impl()
+    {
+      gr::thread::scoped_lock guard(d_writing_mut);
+      while (d_writing) {
+        d_writing_cond.wait(guard);
+      }
+
+      for (std::set<boost::asio::ip::tcp::socket *>::iterator i = 
d_sockets.begin();
+          i != d_sockets.end(); ++i ) {
+        delete *i;
+      }
+      d_sockets.clear();
+
+      d_io_service.reset();
+      d_io_service.stop();
+      d_io_serv_thread.join();
+    }
+
+    int
+    tcp_server_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];
+
+      gr::thread::scoped_lock guard(d_writing_mut);
+      while (d_writing) {
+        d_writing_cond.wait(guard);
+      }
+
+      size_t data_len = std::min(size_t(BUF_SIZE), noutput_items * d_itemsize);
+      data_len -= data_len % d_itemsize;
+      memcpy(d_buf.get(), in, data_len);
+      for (std::set<boost::asio::ip::tcp::socket *>::iterator i = 
d_sockets.begin();
+          i != d_sockets.end(); ++i ) {
+        boost::asio::async_write(**i, boost::asio::buffer(d_buf.get(), 
data_len),
+            boost::bind(&tcp_server_sink_impl::do_write, this,
+              boost::asio::placeholders::error,
+              boost::asio::placeholders::bytes_transferred,
+              i));
+      }
+      d_writing = d_sockets.size();
+
+      return data_len / d_itemsize;
+    }
+
+  } /* namespace blocks */
+} /* namespace gr */
+
diff --git a/gr-blocks/lib/tcp_server_sink_impl.h 
b/gr-blocks/lib/tcp_server_sink_impl.h
new file mode 100644
index 0000000..d10f3b9
--- /dev/null
+++ b/gr-blocks/lib/tcp_server_sink_impl.h
@@ -0,0 +1,73 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2014 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef INCLUDED_GR_TCP_SERVER_SINK_IMPL_H
+#define INCLUDED_GR_TCP_SERVER_SINK_IMPL_H
+
+#include <gnuradio/blocks/tcp_server_sink.h>
+#include <boost/asio.hpp>
+#include <set>
+#include <boost/ptr_container/ptr_vector.hpp>
+
+namespace gr {
+  namespace blocks {
+
+    class tcp_server_sink_impl : public tcp_server_sink
+    {
+    private:
+      size_t d_itemsize;
+
+      boost::asio::io_service d_io_service;
+      gr::thread::thread d_io_serv_thread;
+      boost::asio::ip::tcp::endpoint d_endpoint;
+      std::auto_ptr<boost::asio::ip::tcp::socket> d_socket;
+      std::set<boost::asio::ip::tcp::socket *> d_sockets;
+      boost::asio::ip::tcp::acceptor d_acceptor;
+
+      boost::shared_ptr<uint8_t> d_buf;
+      enum {
+          BUF_SIZE = 256 * 1024,
+      };
+
+      int d_writing;
+      boost::condition_variable d_writing_cond;
+      boost::mutex d_writing_mut;
+
+      void do_accept(const boost::system::error_code& error);
+      void do_write(const boost::system::error_code& error, std::size_t len,
+              std::set<boost::asio::ip::tcp::socket *>::iterator);
+
+    public:
+      tcp_server_sink_impl(size_t itemsize,
+                    const std::string &host, int port,
+                    bool noblock);
+      ~tcp_server_sink_impl();
+
+      int work(int noutput_items,
+               gr_vector_const_void_star &input_items,
+               gr_vector_void_star &output_items);
+    };
+
+  } /* namespace blocks */
+} /* namespace gr */
+
+#endif /* INCLUDED_GR_TCP_SERVER_SINK_IMPL_H */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]