-
Notifications
You must be signed in to change notification settings - Fork 294
/
AsyncUdpSocketBase.cxx
166 lines (137 loc) · 5.3 KB
/
AsyncUdpSocketBase.cxx
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
#include <boost/bind.hpp>
#include "AsyncUdpSocketBase.hxx"
#include "AsyncSocketBaseHandler.hxx"
#include <rutil/Logger.hxx>
#include "ReTurnSubsystem.hxx"
#define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
using namespace std;
namespace reTurn {
AsyncUdpSocketBase::AsyncUdpSocketBase(asio::io_service& ioService)
: AsyncSocketBase(ioService),
mSocket(ioService),
mResolver(ioService)
{
}
AsyncUdpSocketBase::~AsyncUdpSocketBase()
{
}
unsigned int
AsyncUdpSocketBase::getSocketDescriptor()
{
return (unsigned int)mSocket.native();
}
asio::error_code
AsyncUdpSocketBase::bind(const asio::ip::address& address, unsigned short port)
{
asio::error_code errorCode;
mSocket.open(address.is_v6() ? asio::ip::udp::v6() : asio::ip::udp::v4(), errorCode);
if(!errorCode)
{
mSocket.set_option(asio::ip::udp::socket::reuse_address(true), errorCode);
mSocket.set_option(asio::socket_base::receive_buffer_size(66560));
//mSocket.set_option(asio::socket_base::send_buffer_size(66560));
mSocket.bind(asio::ip::udp::endpoint(address, port), errorCode);
}
return errorCode;
}
void
AsyncUdpSocketBase::connect(const std::string& address, unsigned short port, bool allowV6)
{
// Start an asynchronous resolve to translate the address
// into a list of endpoints.
resip::Data service(port);
#ifdef USE_IPV6
asio::ip::udp::resolver::query query((allowV6 ? asio::ip::udp::v6() : asio::ip::udp::v4()), address, service.c_str());
#else
asio::ip::udp::resolver::query query(asio::ip::udp::v4(), address, service.c_str());
#endif
mResolver.async_resolve(query,
boost::bind(&AsyncSocketBase::handleUdpResolve, shared_from_this(),
asio::placeholders::error,
asio::placeholders::iterator));
}
void
AsyncUdpSocketBase::handleUdpResolve(const asio::error_code& ec,
asio::ip::udp::resolver::iterator endpoint_iterator)
{
if (!ec)
{
// Use the first endpoint in the list.
// Nothing to do for UDP except store the connected address/port
mConnected = true;
mConnectedAddress = endpoint_iterator->endpoint().address();
mConnectedPort = endpoint_iterator->endpoint().port();
onConnectSuccess();
}
else
{
onConnectFailure(ec);
}
}
const asio::ip::address
AsyncUdpSocketBase::getSenderEndpointAddress()
{
return mSenderEndpoint.address();
}
unsigned short
AsyncUdpSocketBase::getSenderEndpointPort()
{
return mSenderEndpoint.port();
}
void
AsyncUdpSocketBase::transportSend(const StunTuple& destination, std::vector<asio::const_buffer>& buffers)
{
//InfoLog(<< "AsyncUdpSocketBase::transportSend " << buffers.size() << " buffer(s) to " << destination << " - buf1 size=" << buffer_size(buffers.front()));
mSocket.async_send_to(buffers,
asio::ip::udp::endpoint(destination.getAddress(), destination.getPort()),
boost::bind(&AsyncUdpSocketBase::handleSend, shared_from_this(), asio::placeholders::error));
}
void
AsyncUdpSocketBase::transportReceive()
{
mSocket.async_receive_from(asio::buffer((void*)mReceiveBuffer->data(), RECEIVE_BUFFER_SIZE), mSenderEndpoint,
boost::bind(&AsyncUdpSocketBase::handleReceive, shared_from_this(), asio::placeholders::error, asio::placeholders::bytes_transferred));
}
void
AsyncUdpSocketBase::transportFramedReceive()
{
// For UDP these two functions are the same
transportReceive();
}
void
AsyncUdpSocketBase::transportClose()
{
if (mOnBeforeSocketCloseFp)
{
mOnBeforeSocketCloseFp((unsigned int)mSocket.native());
}
asio::error_code ec;
mSocket.close(ec);
}
}
/* ====================================================================
Copyright (c) 2007-2008, Plantronics, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of Plantronics nor the names of its contributors
may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
==================================================================== */