GNU Radio 3.3.0 C++ API
gr_udp_source.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2007,2008,2009,2010 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef INCLUDED_GR_UDP_SOURCE_H
00024 #define INCLUDED_GR_UDP_SOURCE_H
00025 
00026 #include <gr_sync_block.h>
00027 #include <gruel/thread.h>
00028 
00029 class gr_udp_source;
00030 typedef boost::shared_ptr<gr_udp_source> gr_udp_source_sptr;
00031 
00032 gr_udp_source_sptr gr_make_udp_source(size_t itemsize, const char *host, 
00033                                       unsigned short port,
00034                                       int payload_size=1472,
00035                                       bool eof=true, bool wait=true);
00036 
00037 /*! 
00038  * \brief Read stream from an UDP socket.
00039  * \ingroup source_blk
00040  *
00041  * \param itemsize     The size (in bytes) of the item datatype
00042  * \param host         The name or IP address of the receiving host; can be
00043  *                     NULL, None, or "0.0.0.0" to allow reading from any
00044  *                     interface on the host
00045  * \param port         The port number on which to receive data; use 0 to
00046  *                     have the system assign an unused port number
00047  * \param payload_size UDP payload size by default set to 1472 =
00048  *                     (1500 MTU - (8 byte UDP header) - (20 byte IP header))
00049  * \param eof          Interpret zero-length packet as EOF (default: true)
00050  * \param wait         Wait for data if not immediately available
00051  *                     (default: true)
00052  *
00053 */
00054 
00055 class gr_udp_source : public gr_sync_block
00056 {
00057   friend gr_udp_source_sptr gr_make_udp_source(size_t itemsize,
00058                                                const char *host, 
00059                                                unsigned short port,
00060                                                int payload_size,
00061                                                bool eof, bool wait);
00062 
00063  private:
00064   size_t        d_itemsize;
00065   int           d_payload_size;  // maximum transmission unit (packet length)
00066   bool          d_eof;           // zero-length packet is EOF
00067   bool          d_wait;          // wait if data if not immediately available
00068   int           d_socket;        // handle to socket
00069   char *d_temp_buff;    // hold buffer between calls
00070   ssize_t d_residual;   // hold information about number of bytes stored in the temp buffer
00071   size_t d_temp_offset; // point to temp buffer location offset
00072 
00073  protected:
00074   /*!
00075    * \brief UDP Source Constructor
00076    * 
00077    * \param itemsize     The size (in bytes) of the item datatype
00078    * \param host         The name or IP address of the receiving host; can be
00079    *                     NULL, None, or "0.0.0.0" to allow reading from any
00080    *                     interface on the host
00081    * \param port         The port number on which to receive data; use 0 to
00082    *                     have the system assign an unused port number
00083    * \param payload_size UDP payload size by default set to 1472 =
00084    *                     (1500 MTU - (8 byte UDP header) - (20 byte IP header))
00085    * \param eof          Interpret zero-length packet as EOF (default: true)
00086    * \param wait         Wait for data if not immediately available
00087    *                     (default: true)
00088    */
00089   gr_udp_source(size_t itemsize, const char *host, unsigned short port,
00090                 int payload_size, bool eof, bool wait);
00091 
00092  public:
00093   ~gr_udp_source();
00094 
00095   /*! \brief return the PAYLOAD_SIZE of the socket */
00096   int payload_size() { return d_payload_size; }
00097 
00098   /*! \brief return the port number of the socket */
00099   int get_port();
00100 
00101   // should we export anything else?
00102 
00103   int work(int noutput_items,
00104            gr_vector_const_void_star &input_items,
00105            gr_vector_void_star &output_items);
00106 };
00107 
00108 
00109 #endif /* INCLUDED_GR_UDP_SOURCE_H */