aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/transport/udp_simple.cpp
blob: 1ee036d52b6049d105075fa6cd98400f7b446b43 (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
//
// Copyright 2010-2011 Ettus Research LLC
//
// This program 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 of the License, or
// (at your option) any later version.
//
// This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
//

#include "udp_common.hpp"
#include <uhd/transport/udp_simple.hpp>
#include <boost/format.hpp>
#include <iostream>

using namespace uhd::transport;
namespace asio = boost::asio;

/***********************************************************************
 * UDP simple implementation: connected and broadcast
 **********************************************************************/
class udp_simple_impl : public udp_simple{
public:
    udp_simple_impl(
        const std::string &addr, const std::string &port, bool bcast, bool connect
    ):_connected(connect){
        //std::cout << boost::format("Creating udp transport for %s %s") % addr % port << std::endl;

        //resolve the address
        asio::ip::udp::resolver resolver(_io_service);
        asio::ip::udp::resolver::query query(asio::ip::udp::v4(), addr, port);
        _receiver_endpoint = *resolver.resolve(query);

        //create and open the socket
        _socket = socket_sptr(new asio::ip::udp::socket(_io_service));
        _socket->open(asio::ip::udp::v4());

        //allow broadcasting
        _socket->set_option(asio::socket_base::broadcast(bcast));

        //connect the socket
        if (connect) _socket->connect(_receiver_endpoint);

    }

    size_t send(const asio::const_buffer &buff){
        if (_connected) return _socket->send(asio::buffer(buff));
        return _socket->send_to(asio::buffer(buff), _receiver_endpoint);
    }

    size_t recv(const asio::mutable_buffer &buff, double timeout){
        if (not wait_for_recv_ready(_socket->native(), timeout)) return 0;
        return _socket->receive(asio::buffer(buff));
    }

private:
    bool                    _connected;
    asio::io_service        _io_service;
    socket_sptr             _socket;
    asio::ip::udp::endpoint _receiver_endpoint;
};

/***********************************************************************
 * UDP public make functions
 **********************************************************************/
udp_simple::sptr udp_simple::make_connected(
    const std::string &addr, const std::string &port
){
    return sptr(new udp_simple_impl(addr, port, false, true /* no bcast, connect */));
}

udp_simple::sptr udp_simple::make_broadcast(
    const std::string &addr, const std::string &port
){
    return sptr(new udp_simple_impl(addr, port, true, false /* bcast, no connect */));
}