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
|
//
// Copyright 2010 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 <uhd/transport/udp_simple.hpp>
#include <boost/thread.hpp>
#include <boost/format.hpp>
#include <iostream>
using namespace uhd::transport;
/***********************************************************************
* Helper Functions
**********************************************************************/
/*!
* A receive timeout for a socket:
*
* It seems that asio cannot have timeouts with synchronous io.
* However, we can implement a polling loop that will timeout.
* This is okay bacause this is the slow-path implementation.
*
* \param socket the asio socket
*/
static void reasonable_recv_timeout(
boost::asio::ip::udp::socket &socket
){
boost::asio::deadline_timer timer(socket.get_io_service());
timer.expires_from_now(boost::posix_time::milliseconds(50));
while (not (socket.available() or timer.expires_from_now().is_negative())){
boost::this_thread::sleep(boost::posix_time::milliseconds(1));
}
}
/***********************************************************************
* UDP connected implementation class
**********************************************************************/
class udp_connected_impl : public udp_simple{
public:
//structors
udp_connected_impl(const std::string &addr, const std::string &port);
~udp_connected_impl(void);
//send/recv
size_t send(const boost::asio::const_buffer &buff);
size_t recv(const boost::asio::mutable_buffer &buff);
private:
boost::asio::ip::udp::socket *_socket;
boost::asio::io_service _io_service;
};
udp_connected_impl::udp_connected_impl(const std::string &addr, const std::string &port){
//std::cout << boost::format("Creating udp transport for %s %s") % addr % port << std::endl;
// resolve the address
boost::asio::ip::udp::resolver resolver(_io_service);
boost::asio::ip::udp::resolver::query query(boost::asio::ip::udp::v4(), addr, port);
boost::asio::ip::udp::endpoint receiver_endpoint = *resolver.resolve(query);
// Create, open, and connect the socket
_socket = new boost::asio::ip::udp::socket(_io_service);
_socket->open(boost::asio::ip::udp::v4());
_socket->connect(receiver_endpoint);
}
udp_connected_impl::~udp_connected_impl(void){
delete _socket;
}
size_t udp_connected_impl::send(const boost::asio::const_buffer &buff){
return _socket->send(boost::asio::buffer(buff));
}
size_t udp_connected_impl::recv(const boost::asio::mutable_buffer &buff){
reasonable_recv_timeout(*_socket);
if (not _socket->available()) return 0;
return _socket->receive(boost::asio::buffer(buff));
}
/***********************************************************************
* UDP broadcast implementation class
**********************************************************************/
class udp_broadcast_impl : public udp_simple{
public:
//structors
udp_broadcast_impl(const std::string &addr, const std::string &port);
~udp_broadcast_impl(void);
//send/recv
size_t send(const boost::asio::const_buffer &buff);
size_t recv(const boost::asio::mutable_buffer &buff);
private:
boost::asio::ip::udp::socket *_socket;
boost::asio::ip::udp::endpoint _receiver_endpoint;
boost::asio::io_service _io_service;
};
udp_broadcast_impl::udp_broadcast_impl(const std::string &addr, const std::string &port){
//std::cout << boost::format("Creating udp transport for %s %s") % addr % port << std::endl;
// resolve the address
boost::asio::ip::udp::resolver resolver(_io_service);
boost::asio::ip::udp::resolver::query query(boost::asio::ip::udp::v4(), addr, port);
_receiver_endpoint = *resolver.resolve(query);
// Create and open the socket
_socket = new boost::asio::ip::udp::socket(_io_service);
_socket->open(boost::asio::ip::udp::v4());
// Allow broadcasting
boost::asio::socket_base::broadcast option(true);
_socket->set_option(option);
}
udp_broadcast_impl::~udp_broadcast_impl(void){
delete _socket;
}
size_t udp_broadcast_impl::send(const boost::asio::const_buffer &buff){
return _socket->send_to(boost::asio::buffer(buff), _receiver_endpoint);
}
size_t udp_broadcast_impl::recv(const boost::asio::mutable_buffer &buff){
reasonable_recv_timeout(*_socket);
if (not _socket->available()) return 0;
boost::asio::ip::udp::endpoint sender_endpoint;
return _socket->receive_from(boost::asio::buffer(buff), sender_endpoint);
}
/***********************************************************************
* UDP public make functions
**********************************************************************/
udp_simple::sptr udp_simple::make_connected(
const std::string &addr, const std::string &port
){
return sptr(new udp_connected_impl(addr, port));
}
udp_simple::sptr udp_simple::make_broadcast(
const std::string &addr, const std::string &port
){
return sptr(new udp_broadcast_impl(addr, port));
}
|