GNU Radio 3.4.2 C++ API
gr_udp_sink.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_SINK_H
00024 #define INCLUDED_GR_UDP_SINK_H
00025 
00026 #include <gr_sync_block.h>
00027 #include <gruel/thread.h>
00028 
00029 class gr_udp_sink;
00030 typedef boost::shared_ptr<gr_udp_sink> gr_udp_sink_sptr;
00031 
00032 gr_udp_sink_sptr
00033 gr_make_udp_sink (size_t itemsize, 
00034                   const char *host, unsigned short port,
00035                   int payload_size=1472, bool eof=true);
00036 
00037 /*!
00038  * \brief Write stream to an UDP socket.
00039  * \ingroup sink_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; use
00043  *                     NULL or None for no connection
00044  * \param port         Destination port to connect to on receiving host
00045  * \param payload_size UDP payload size by default set to 1472 =
00046  *                     (1500 MTU - (8 byte UDP header) - (20 byte IP header))
00047  * \param eof          Send zero-length packet on disconnect
00048  */
00049 
00050 class gr_udp_sink : public gr_sync_block
00051 {
00052   friend gr_udp_sink_sptr gr_make_udp_sink (size_t itemsize, 
00053                                             const char *host,
00054                                             unsigned short port,
00055                                             int payload_size, bool eof);
00056  private:
00057   size_t        d_itemsize;
00058 
00059   int           d_payload_size;    // maximum transmission unit (packet length)
00060   bool          d_eof;             // send zero-length packet on disconnect
00061   int           d_socket;          // handle to socket
00062   bool          d_connected;       // are we connected?
00063   gruel::mutex  d_mutex;           // protects d_socket and d_connected
00064 
00065  protected:
00066   /*!
00067    * \brief UDP Sink Constructor
00068    * 
00069    * \param itemsize     The size (in bytes) of the item datatype
00070    * \param host         The name or IP address of the receiving host; use
00071    *                     NULL or None for no connection
00072    * \param port         Destination port to connect to on receiving host
00073    * \param payload_size UDP payload size by default set to 
00074    *                     1472 = (1500 MTU - (8 byte UDP header) - (20 byte IP header))
00075    * \param eof          Send zero-length packet on disconnect
00076    */
00077   gr_udp_sink (size_t itemsize, 
00078                const char *host, unsigned short port,
00079                int payload_size, bool eof);
00080 
00081  public:
00082   ~gr_udp_sink ();
00083 
00084   /*! \brief return the PAYLOAD_SIZE of the socket */
00085   int payload_size() { return d_payload_size; }
00086 
00087   /*! \brief Change the connection to a new destination
00088    *
00089    * \param host         The name or IP address of the receiving host; use
00090    *                     NULL or None to break the connection without closing
00091    * \param port         Destination port to connect to on receiving host
00092    *
00093    * Calls disconnect() to terminate any current connection first.
00094    */
00095   void connect( const char *host, unsigned short port );
00096 
00097   /*! \brief Send zero-length packet (if eof is requested) then stop sending
00098    *
00099    * Zero-byte packets can be interpreted as EOF by gr_udp_source.  Note that
00100    * disconnect occurs automatically when the sink is destroyed, but not when
00101    * its top_block stops.*/
00102   void disconnect();
00103 
00104   // should we export anything else?
00105 
00106   int work (int noutput_items,
00107             gr_vector_const_void_star &input_items,
00108             gr_vector_void_star &output_items);
00109 };
00110 
00111 #endif /* INCLUDED_GR_UDP_SINK_H */