Merge remote branch 'gnuradio/wip/udp_source_sink'
[debian/gnuradio] / gnuradio-core / src / lib / io / gr_udp_sink.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008,2009,2010 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_SINK_H
24 #define INCLUDED_GR_UDP_SINK_H
25
26 #include <gr_sync_block.h>
27 #include <gruel/thread.h>
28
29 class gr_udp_sink;
30 typedef boost::shared_ptr<gr_udp_sink> gr_udp_sink_sptr;
31
32 gr_udp_sink_sptr
33 gr_make_udp_sink (size_t itemsize, 
34                   const char *host, unsigned short port,
35                   int payload_size=1472, bool eof=true);
36
37 /*!
38  * \brief Write stream to an UDP socket.
39  * \ingroup sink_blk
40  * 
41  * \param itemsize     The size (in bytes) of the item datatype
42  * \param host         The name or IP address of the receiving host; use
43  *                     NULL or None for no connection
44  * \param port         Destination port to connect to on receiving host
45  * \param payload_size UDP payload size by default set to 1472 =
46  *                     (1500 MTU - (8 byte UDP header) - (20 byte IP header))
47  * \param eof          Send zero-length packet on disconnect
48  */
49
50 class gr_udp_sink : public gr_sync_block
51 {
52   friend gr_udp_sink_sptr gr_make_udp_sink (size_t itemsize, 
53                                             const char *host,
54                                             unsigned short port,
55                                             int payload_size, bool eof);
56  private:
57   size_t        d_itemsize;
58
59   int           d_payload_size;    // maximum transmission unit (packet length)
60   bool          d_eof;             // send zero-length packet on disconnect
61   int           d_socket;          // handle to socket
62   bool          d_connected;       // are we connected?
63   gruel::mutex  d_mutex;           // protects d_socket and d_connected
64
65  protected:
66   /*!
67    * \brief UDP Sink Constructor
68    * 
69    * \param itemsize     The size (in bytes) of the item datatype
70    * \param host         The name or IP address of the receiving host; use
71    *                     NULL or None for no connection
72    * \param port         Destination port to connect to on receiving host
73    * \param payload_size UDP payload size by default set to 
74    *                     1472 = (1500 MTU - (8 byte UDP header) - (20 byte IP header))
75    * \param eof          Send zero-length packet on disconnect
76    */
77   gr_udp_sink (size_t itemsize, 
78                const char *host, unsigned short port,
79                int payload_size, bool eof);
80
81  public:
82   ~gr_udp_sink ();
83
84   /*! \brief return the PAYLOAD_SIZE of the socket */
85   int payload_size() { return d_payload_size; }
86
87   /*! \brief Change the connection to a new destination
88    *
89    * \param host         The name or IP address of the receiving host; use
90    *                     NULL or None to break the connection without closing
91    * \param port         Destination port to connect to on receiving host
92    *
93    * Calls disconnect() to terminate any current connection first.
94    */
95   void connect( const char *host, unsigned short port );
96
97   /*! \brief Send zero-length packet (if eof is requested) then stop sending
98    *
99    * Zero-byte packets can be interpreted as EOF by gr_udp_source.  Note that
100    * disconnect occurs automatically when the sink is destroyed, but not when
101    * its top_block stops.*/
102   void disconnect();
103
104   // should we export anything else?
105
106   int work (int noutput_items,
107             gr_vector_const_void_star &input_items,
108             gr_vector_void_star &output_items);
109 };
110
111 #endif /* INCLUDED_GR_UDP_SINK_H */