gr-baz Package
baz_udp_source.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2007,2008,2009,2010,2013 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 #if (!defined(INCLUDED_GR_UDP_SOURCE_H) && !defined(IN_GR_BAZ)) || (!defined(INCLUDED_BAZ_UDP_SOURCE_H) && defined(IN_GR_BAZ))
00024 #ifdef IN_GR_BAZ
00025 #define INCLUDED_BAZ_UDP_SOURCE_H
00026 #else
00027 #define INCLUDED_GR_UDP_SOURCE_H
00028 #endif // IN_GR_BAZ
00029 
00030 #include <gr_sync_block.h>
00031 #include <gruel/thread.h>
00032 
00033 #ifdef IN_GR_BAZ
00034 #define UDP_SOURCE_NAME   baz_udp_source
00035 #define UDP_SOURCE_MAKER  baz_make_udp_source
00036 #define UDP_SOURCE_SPTR   baz_udp_source_sptr
00037 #else
00038 #define UDP_SOURCE_NAME   gr_udp_source
00039 #define UDP_SOURCE_MAKER  gr_make_udp_source
00040 #define UDP_SOURCE_SPTR   gr_udp_source_sptr
00041 #endif // IN_GR_BAZ
00042 
00043 #ifndef _TO_STR
00044 #define _TO_STR(x)        #x
00045 #endif // _TO_STR
00046 #define UDP_SOURCE_STRING _TO_STR(UDP_SOURCE_NAME)
00047 
00048 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
00049 #include <cstddef>
00050 typedef ptrdiff_t ssize_t;
00051 #endif
00052 
00053 #include <stdio.h>
00054 
00055 class BAZ_API UDP_SOURCE_NAME;
00056 typedef boost::shared_ptr<UDP_SOURCE_NAME> UDP_SOURCE_SPTR;
00057 
00058 BAZ_API UDP_SOURCE_SPTR UDP_SOURCE_MAKER(size_t itemsize, const char *host, 
00059                                                 unsigned short port,
00060                                                 int payload_size=1472,
00061                                                 bool eof=true, bool wait=true, bool bor=false, bool verbose=false);
00062 
00063 /*! 
00064  * \brief Read stream from an UDP socket.
00065  * \ingroup source_blk
00066  *
00067  * \param itemsize     The size (in bytes) of the item datatype
00068  * \param host         The name or IP address of the receiving host; can be
00069  *                     NULL, None, or "0.0.0.0" to allow reading from any
00070  *                     interface on the host
00071  * \param port         The port number on which to receive data; use 0 to
00072  *                     have the system assign an unused port number
00073  * \param payload_size UDP payload size by default set to 1472 =
00074  *                     (1500 MTU - (8 byte UDP header) - (20 byte IP header))
00075  * \param eof          Interpret zero-length packet as EOF (default: true)
00076  * \param wait         Wait for data if not immediately available
00077  *                     (default: true)
00078  *
00079 */
00080 
00081 class BAZ_API UDP_SOURCE_NAME : public gr_sync_block
00082 {
00083   friend BAZ_API UDP_SOURCE_SPTR UDP_SOURCE_MAKER(size_t itemsize,
00084                                                const char *host, 
00085                                                unsigned short port,
00086                                                int payload_size,
00087                                                bool eof, bool wait, bool bor, bool verbose);
00088 
00089  private:
00090   size_t        d_itemsize;
00091   int           d_payload_size;  // maximum transmission unit (packet length)
00092   bool          d_eof;           // zero-length packet is EOF
00093   bool          d_wait;          // wait if data if not immediately available
00094   int           d_socket;        // handle to socket
00095   char *d_temp_buff;    // hold buffer between calls
00096   ssize_t d_residual;   // hold information about number of bytes stored in the temp buffer
00097   size_t d_temp_offset; // point to temp buffer location offset
00098   bool                  d_bor;
00099   unsigned short d_bor_counter;
00100   bool                  d_bor_first;
00101   bool                  d_verbose;
00102   bool                  d_eos;
00103 
00104  protected:
00105   /*!
00106    * \brief UDP Source Constructor
00107    * 
00108    * \param itemsize     The size (in bytes) of the item datatype
00109    * \param host         The name or IP address of the receiving host; can be
00110    *                     NULL, None, or "0.0.0.0" to allow reading from any
00111    *                     interface on the host
00112    * \param port         The port number on which to receive data; use 0 to
00113    *                     have the system assign an unused port number
00114    * \param payload_size UDP payload size by default set to 1472 =
00115    *                     (1500 MTU - (8 byte UDP header) - (20 byte IP header))
00116    * \param eof          Interpret zero-length packet as EOF (default: true)
00117    * \param wait         Wait for data if not immediately available
00118    *                     (default: true)
00119    * \param bor          Enable BorIP encapsulation
00120    * \param verbose      Output BorIP packet debug messages (helpful to judge packet loss)
00121    */
00122   UDP_SOURCE_NAME(size_t itemsize, const char *host, unsigned short port,
00123                 int payload_size, bool eof, bool wait, bool bor, bool verbose);
00124 
00125  public:
00126   ~UDP_SOURCE_NAME();
00127 
00128   /*! \brief return the PAYLOAD_SIZE of the socket */
00129   int payload_size() { return d_payload_size; }
00130 
00131   /*! \brief return the port number of the socket */
00132   int get_port();
00133   
00134   void signal_eos();
00135 
00136   // should we export anything else?
00137 
00138   int work(int noutput_items,
00139            gr_vector_const_void_star &input_items,
00140            gr_vector_void_star &output_items);
00141 };
00142 
00143 
00144 #endif /* INCLUDED_GR_UDP_SOURCE_H */