summaryrefslogtreecommitdiff
path: root/gr-blocks/lib/udp_source_impl.cc
blob: ea2f2b60a0bec5c849bdec22617fb738c3e4b743 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* -*- c++ -*- */
/*
 * Copyright 2007-2010,2013 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "udp_source_impl.h"
#include <gnuradio/io_signature.h>
#include <gnuradio/math.h>
#include <gnuradio/prefs.h>
#include <stdexcept>
#include <errno.h>
#include <stdio.h>
#include <string.h>

namespace gr {
  namespace blocks {

    const int udp_source_impl::BUF_SIZE_PAYLOADS =
        gr::prefs::singleton()->get_long("udp_blocks", "buf_size_payloads", 50);

    udp_source::sptr
    udp_source::make(size_t itemsize,
                     const std::string &ipaddr, int port,
                     int payload_size, bool eof)
    {
      return gnuradio::get_initial_sptr
        (new udp_source_impl(itemsize, ipaddr, port,
                             payload_size, eof));
    }

    udp_source_impl::udp_source_impl(size_t itemsize,
                                     const std::string &host, int port,
                                     int payload_size, bool eof)
      : sync_block("udp_source",
                      io_signature::make(0, 0, 0),
                      io_signature::make(1, 1, itemsize)),
        d_itemsize(itemsize), d_payload_size(payload_size),
        d_eof(eof), d_connected(false), d_residual(0), d_sent(0), d_offset(0)
    {
      // Give us some more room to play.
      d_rxbuf = new char[4*d_payload_size];
      d_residbuf = new char[BUF_SIZE_PAYLOADS*d_payload_size];

      connect(host, port);
    }

    udp_source_impl::~udp_source_impl()
    {
      if(d_connected)
        disconnect();

      delete [] d_rxbuf;
      delete [] d_residbuf;
    }

    void
    udp_source_impl::connect(const std::string &host, int port)
    {
      if(d_connected)
        disconnect();

      d_host = host;
      d_port = static_cast<unsigned short>(port);

      std::string s_port;
      s_port = (boost::format("%d")%d_port).str();

      if(host.size() > 0) {
        boost::asio::ip::udp::resolver resolver(d_io_service);
        boost::asio::ip::udp::resolver::query query(d_host, s_port,
                                                    boost::asio::ip::resolver_query_base::passive);
        d_endpoint = *resolver.resolve(query);

        d_socket = new boost::asio::ip::udp::socket(d_io_service);
        d_socket->open(d_endpoint.protocol());

        boost::asio::socket_base::reuse_address roption(true);
        d_socket->set_option(roption);

        d_socket->bind(d_endpoint);

        start_receive();
        d_udp_thread = gr::thread::thread(boost::bind(&udp_source_impl::run_io_service, this));
        d_connected = true;
      }
    }

    void
    udp_source_impl::disconnect()
    {
      gr::thread::scoped_lock lock(d_setlock);

      if(!d_connected)
        return;

      d_io_service.reset();
      d_io_service.stop();
      d_udp_thread.join();

      d_socket->close();
      delete d_socket;

      d_connected = false;
    }

    // Return port number of d_socket
    int
    udp_source_impl::get_port(void)
    {
      //return d_endpoint.port();
      return d_socket->local_endpoint().port();
    }

    void
    udp_source_impl::start_receive()
    {
      d_socket->async_receive_from(boost::asio::buffer((void*)d_rxbuf, d_payload_size), d_endpoint_rcvd,
                                   boost::bind(&udp_source_impl::handle_read, this,
                                               boost::asio::placeholders::error,
                                               boost::asio::placeholders::bytes_transferred));
    }

    void
    udp_source_impl::handle_read(const boost::system::error_code& error,
                                 size_t bytes_transferred)
    {
      if(!error) {
        {
          boost::lock_guard<gr::thread::mutex> lock(d_udp_mutex);
          if(d_eof && (bytes_transferred == 0)) {
            // If we are using EOF notification, test for it and don't
            // add anything to the output.
            d_residual = WORK_DONE;
            d_cond_wait.notify_one();
            return;
          }
          else {
            // Make sure we never go beyond the boundary of the
            // residual buffer.  This will just drop the last bit of
            // data in the buffer if we've run out of room.
            if((int)(d_residual + bytes_transferred) >= (BUF_SIZE_PAYLOADS*d_payload_size)) {
              GR_LOG_WARN(d_logger, "Too much data; dropping packet.");
            }
            else {
              // otherwise, copy received data into local buffer for
              // copying later.
              memcpy(d_residbuf+d_residual, d_rxbuf, bytes_transferred);
              d_residual += bytes_transferred;
            }
          }
          d_cond_wait.notify_one();
        }
      }
      start_receive();
    }

    int
    udp_source_impl::work(int noutput_items,
                          gr_vector_const_void_star &input_items,
                          gr_vector_void_star &output_items)
    {
      gr::thread::scoped_lock l(d_setlock);

      char *out = (char*)output_items[0];

      // Use async receive_from to get data from UDP buffer and wait
      // on a conditional signal before proceeding. We use this
      // because the conditional wait is interruptable while a
      // synchronous receive_from is not.
      boost::unique_lock<boost::mutex> lock(d_udp_mutex);

      //use timed_wait to avoid permanent blocking in the work function
      d_cond_wait.timed_wait(lock, boost::posix_time::milliseconds(10));

      if (d_residual < 0) {
        return d_residual;
      }

      int bytes_left_in_buffer = (int)(d_residual - d_sent);
      int bytes_to_send        = std::min<int>(d_itemsize * noutput_items, bytes_left_in_buffer);

      // Copy the received data in the residual buffer to the output stream
      memcpy(out, d_residbuf+d_sent, bytes_to_send);
      int nitems = bytes_to_send/d_itemsize;

      // Keep track of where we are if we don't have enough output
      // space to send all the data in the residbuf.
      if (bytes_to_send == bytes_left_in_buffer) {
        d_residual = 0;
        d_sent = 0;
      }
      else {
        d_sent += bytes_to_send;
      }

      return nitems;
    }

  } /* namespace blocks */
} /* namespace gr */