-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathconnection.cpp
More file actions
92 lines (79 loc) · 2.74 KB
/
connection.cpp
File metadata and controls
92 lines (79 loc) · 2.74 KB
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
#include "connection.h"
#include "log.h"
#include <sstream>
namespace media
{
using namespace boost;
using namespace boost::asio;
connection::connection(io_service::strand& strand)
: local(strand.get_io_service()), io_strand(strand)
{
}
void connection::open(const boost::asio::ip::address& iface, int& port)
{
ip::udp::endpoint ep(iface, port);
local.open(ep.protocol());
local.bind(ep);
port = local.local_endpoint().port();
}
bool connection::try_open(const boost::asio::ip::address& iface, int port)
{
system::error_code ec;
ip::udp::endpoint ep(iface, port);
local.open(ep.protocol());
local.bind(ep, ec);
return !ec;
}
void connection::set_peer(const boost::asio::ip::udp::endpoint& ep) { remote = ep; }
void connection::send(void* data, size_t size)
{
local.send_to(buffer(data, size), remote);
}
void connection::async_receive(std::function<void(void*, size_t)> cb)
{
void* data = buf;
local.async_receive_from(buffer(buf), remote, io_strand.wrap(
[cb, data, this](const boost::system::error_code& ec, size_t bytes_transferred)
{
if (!ec)
{
cb(data, bytes_transferred);
}
else if (ec != boost::asio::error::operation_aborted)
{
// TODO: If a socket error occurs, the callback won't be
// called and the receive won't be restarted. This is good
// for situations in which the socket is dead for some
// reason, so that we don't start spewing error messages
// uncontrollably. However, there may be error conditions
// that don't warrant giving up. These should be identified
// here and handled appropriately.
dbg_msg("Socket receive error: %d, %s", ec.value(), ec.message().c_str());
}
}));
}
void connection::close()
{
if (local.is_open()) local.close();
}
std::string connection::to_string()
{
std::ostringstream str;
if (local.is_open())
{
if (local.local_endpoint().address().is_v6())
{
str << '['
<< local.local_endpoint().address().to_string()
<< "]:" << local.local_endpoint().port();
}
else
{
str << local.local_endpoint().address().to_string()
<< ':' << local.local_endpoint().port();
}
}
else str << "(closed)";
return str.str();
}
}