1 /* -*- c++ -*- */
2 /*
3  * Copyright 2013 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifndef INCLUDED_BLOCKS_SOCKET_PDU_IMPL_H
24 #define INCLUDED_BLOCKS_SOCKET_PDU_IMPL_H
25 
26 #include "tcp_connection.h"
27 #include <gnuradio/blocks/socket_pdu.h>
28 
29 namespace gr {
30 namespace blocks {
31 
32 class socket_pdu_impl : public socket_pdu
33 {
34 private:
35     boost::asio::io_service d_io_service;
36     std::vector<char> d_rxbuf;
run_io_service()37     void run_io_service() { d_io_service.run(); }
38     gr::thread::thread d_thread;
39     bool d_started;
40 
41     // TCP specific
42     boost::asio::ip::tcp::endpoint d_tcp_endpoint;
43     std::vector<tcp_connection::sptr> d_tcp_connections;
44     void handle_tcp_read(const boost::system::error_code& error,
45                          size_t bytes_transferred);
46     bool d_tcp_no_delay;
47 
48     // TCP server specific
49     boost::shared_ptr<boost::asio::ip::tcp::acceptor> d_acceptor_tcp;
50     void start_tcp_accept();
51     void tcp_server_send(pmt::pmt_t msg);
52     void handle_tcp_accept(tcp_connection::sptr new_connection,
53                            const boost::system::error_code& error);
54 
55     // TCP client specific
56     boost::shared_ptr<boost::asio::ip::tcp::socket> d_tcp_socket;
57     void tcp_client_send(pmt::pmt_t msg);
58 
59     // UDP specific
60     boost::asio::ip::udp::endpoint d_udp_endpoint;
61     boost::asio::ip::udp::endpoint d_udp_endpoint_other;
62     boost::shared_ptr<boost::asio::ip::udp::socket> d_udp_socket;
63     void handle_udp_read(const boost::system::error_code& error,
64                          size_t bytes_transferred);
65     void udp_send(pmt::pmt_t msg);
66 
67 public:
68     socket_pdu_impl(std::string type,
69                     std::string addr,
70                     std::string port,
71                     int MTU = 10000,
72                     bool tcp_no_delay = false);
73     ~socket_pdu_impl();
74     bool stop();
75 };
76 
77 } /* namespace blocks */
78 } /* namespace gr */
79 
80 #endif /* INCLUDED_BLOCKS_SOCKET_PDU_IMPL_H */
81