GNU Radio 3.6.5.1 C++ API
udp_source_impl.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007-2010,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_GR_UDP_SOURCE_IMPL_H
24 #define INCLUDED_GR_UDP_SOURCE_IMPL_H
25 
26 #include <blocks/udp_source.h>
27 #include <boost/asio.hpp>
28 #include <boost/format.hpp>
29 #include <gruel/thread.h>
30 
31 namespace gr {
32  namespace blocks {
33 
34  class udp_source_impl : public udp_source
35  {
36  private:
37  size_t d_itemsize;
38  int d_payload_size; // maximum transmission unit (packet length)
39  bool d_eof; // look for an EOF signal
40  bool d_connected; // are we connected?
41  char *d_rxbuf; // get UDP buffer items
42  char *d_residbuf; // hold buffer between calls
43  ssize_t d_residual; // hold information about number of bytes stored in residbuf
44  ssize_t d_sent; // track how much of d_residbuf we've outputted
45  size_t d_offset; // point to residbuf location offset
46 
47  std::string d_host;
48  unsigned short d_port;
49 
50  boost::asio::ip::udp::socket *d_socket;
51  boost::asio::ip::udp::endpoint d_endpoint;
52  boost::asio::ip::udp::endpoint d_endpoint_rcvd;
53  boost::asio::io_service d_io_service;
54 
55  gruel::condition_variable d_cond_wait;
56  gruel::mutex d_udp_mutex;
57  gruel::thread d_udp_thread;
58 
59  void start_receive();
60  void handle_read(const boost::system::error_code& error,
61  size_t bytes_transferred);
62  void run_io_service() { d_io_service.run(); }
63 
64  public:
66  const std::string &host, int port,
67  int payload_size, bool eof);
69 
70  void connect(const std::string &host, int port);
71  void disconnect();
72 
73  int payload_size() { return d_payload_size; }
74  int get_port();
75 
76  int work(int noutput_items,
77  gr_vector_const_void_star &input_items,
78  gr_vector_void_star &output_items);
79  };
80 
81  } /* namespace blocks */
82 } /* namespace gr */
83 
84 #endif /* INCLUDED_GR_UDP_SOURCE_H */