summaryrefslogtreecommitdiff
path: root/gr-blocks/lib/udp_sink_impl.cc
blob: 24370f322d295492e9cc910dbba0e067c3842070 (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
/* -*- c++ -*- */
/*
 * Copyright 2007-2010,2013 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

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

#include "udp_sink_impl.h"
#include <gnuradio/io_signature.h>
#include <gnuradio/thread/thread.h>
#include <stdio.h>
#include <string.h>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/format.hpp>
#include <memory>
#include <stdexcept>

namespace gr {
namespace blocks {

udp_sink::sptr udp_sink::make(
    size_t itemsize, const std::string& host, int port, int payload_size, bool eof)
{
    return gnuradio::make_block_sptr<udp_sink_impl>(
        itemsize, host, port, payload_size, eof);
}

udp_sink_impl::udp_sink_impl(
    size_t itemsize, const std::string& host, int port, int payload_size, bool eof)
    : sync_block(
          "udp_sink", io_signature::make(1, 1, itemsize), io_signature::make(0, 0, 0)),
      d_itemsize(itemsize),
      d_payload_size(payload_size),
      d_eof(eof),
      d_connected(false)
{
    // Get the destination address
    connect(host, port);
}

// public constructor that returns a shared_ptr
udp_sink_impl::~udp_sink_impl()
{
    if (d_connected)
        disconnect();
}

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

    std::string s_port = (boost::format("%d") % port).str();
    if (!host.empty()) {
        boost::asio::ip::udp::resolver resolver(d_io_service);
        boost::asio::ip::udp::resolver::query query(
            host, s_port, boost::asio::ip::resolver_query_base::passive);
        d_endpoint = *resolver.resolve(query);

        d_socket = std::make_unique<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_connected = true;
    }
}

void udp_sink_impl::disconnect()
{
    if (!d_connected)
        return;

    gr::thread::scoped_lock guard(d_mutex); // protect d_socket from work()

    // Send a few zero-length packets to signal receiver we are done
    boost::array<char, 0> send_buf;
    if (d_eof) {
        int i;
        for (i = 0; i < 3; i++)
            d_socket->send_to(boost::asio::buffer(send_buf), d_endpoint);
    }

    d_socket->close();
    d_socket.reset();

    d_connected = false;
}

int udp_sink_impl::work(int noutput_items,
                        gr_vector_const_void_star& input_items,
                        gr_vector_void_star& output_items)
{
    const char* in = (const char*)input_items[0];
    ssize_t r = 0, bytes_sent = 0, bytes_to_send = 0;
    ssize_t total_size = noutput_items * d_itemsize;

    gr::thread::scoped_lock guard(d_mutex); // protect d_socket

    while (bytes_sent < total_size) {
        bytes_to_send = std::min((ssize_t)d_payload_size, (total_size - bytes_sent));

        if (d_connected) {
            try {
                r = d_socket->send_to(
                    boost::asio::buffer((void*)(in + bytes_sent), bytes_to_send),
                    d_endpoint);
            } catch (std::exception& e) {
                GR_LOG_ERROR(d_logger, boost::format("send error: %s") % e.what());
                return -1;
            }
        } else
            r = bytes_to_send; // discarded for lack of connection
        bytes_sent += r;
    }

    return noutput_items;
}

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