reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
AsyncUdpSocketBase.cxx
Go to the documentation of this file.
1#include <functional>
2
5#include <rutil/Logger.hxx>
6#include "ReTurnSubsystem.hxx"
7
8#define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
9
10using namespace std;
11
12namespace reTurn {
13
14AsyncUdpSocketBase::AsyncUdpSocketBase(asio::io_context& ioService)
15 : AsyncSocketBase(ioService),
16 mSocket(ioService),
17 mResolver(ioService)
18{
19}
20
21unsigned int
23{
24 return (unsigned int)mSocket.native_handle();
25}
26
27asio::error_code
28AsyncUdpSocketBase::bind(const asio::ip::address& address, unsigned short port)
29{
30 asio::error_code errorCode;
31 mSocket.open(address.is_v6() ? asio::ip::udp::v6() : asio::ip::udp::v4(), errorCode);
32 if(!errorCode)
33 {
34#ifdef USE_IPV6
35#ifdef __linux__
36 if(address.is_v6())
37 {
38 asio::ip::v6_only v6_opt(true);
39 mSocket.set_option(v6_opt);
40 }
41#endif
42#endif
44 mSocket.set_option(asio::socket_base::receive_buffer_size(66560));
45 //mSocket.set_option(asio::socket_base::send_buffer_size(66560));
46 mSocket.bind(asio::ip::udp::endpoint(address, port), errorCode);
47 }
48 return errorCode;
49}
50
51void
52AsyncUdpSocketBase::connect(const std::string& address, unsigned short port)
53{
54 // Start an asynchronous resolve to translate the address
55 // into a list of endpoints.
56 resip::Data service(port);
57 mResolver.async_resolve(mSocket.local_endpoint().protocol(), address, service.c_str(),
58 std::bind(&AsyncSocketBase::handleUdpResolve, shared_from_this(),
59 std::placeholders::_1,
60 std::placeholders::_2));
61}
62
63void
64AsyncUdpSocketBase::handleUdpResolve(const asio::error_code& ec,
65 const asio::ip::udp::resolver::results_type& endpoints)
66{
67 if (!ec && !endpoints.empty())
68 {
69 // Use the first endpoint in the list.
70 // Nothing to do for UDP except store the connected address/port
71 asio::ip::udp::resolver::results_type::const_iterator endpoint_iterator = endpoints.begin();
72 mConnected = true;
73 mConnectedAddress = endpoint_iterator->endpoint().address();
74 mConnectedPort = endpoint_iterator->endpoint().port();
75
77 }
78 else
79 {
81 }
82}
83
84asio::ip::address
89
90unsigned short
95
96void
97AsyncUdpSocketBase::transportSend(const StunTuple& destination, std::vector<asio::const_buffer>& buffers)
98{
99 //InfoLog(<< "AsyncUdpSocketBase::transportSend " << buffers.size() << " buffer(s) to " << destination << " - buf1 size=" << buffer_size(buffers.front()));
100 mSocket.async_send_to(buffers,
101 asio::ip::udp::endpoint(destination.getAddress(), destination.getPort()),
102 std::bind(&AsyncUdpSocketBase::handleSend, shared_from_this(), std::placeholders::_1));
103}
104
105void
107{
108 mSocket.async_receive_from(asio::buffer((void*)mReceiveBuffer->data(), RECEIVE_BUFFER_SIZE), mSenderEndpoint,
109 std::bind(&AsyncUdpSocketBase::handleReceive, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
110}
111
112void
114{
115 // For UDP these two functions are the same
117}
118
119void
121{
123 {
124 mOnBeforeSocketCloseFp((unsigned int)mSocket.native_handle());
125 }
126
127 asio::error_code ec;
128 mSocket.close(ec);
129}
130
131}
132
133
134/* ====================================================================
135
136 Copyright (c) 2008-2026 SIP Spectrum, Inc. https://www.sipspectrum.com
137 Copyright (c) 2007-2008, Plantronics, Inc.
138 All rights reserved.
139
140 Redistribution and use in source and binary forms, with or without
141 modification, are permitted provided that the following conditions are
142 met:
143
144 1. Redistributions of source code must retain the above copyright
145 notice, this list of conditions and the following disclaimer.
146
147 2. Redistributions in binary form must reproduce the above copyright
148 notice, this list of conditions and the following disclaimer in the
149 documentation and/or other materials provided with the distribution.
150
151 3. Neither the name of Plantronics nor the names of its contributors
152 may be used to endorse or promote products derived from this
153 software without specific prior written permission.
154
155 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
156 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
157 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
158 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
159 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
160 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
161 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
162 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
163 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
164 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
165 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
166
167 ==================================================================== */
168
constexpr size_t RECEIVE_BUFFER_SIZE
virtual void handleReceive(const asio::error_code &e, size_t bytesTransferred)
BeforeClosedHandler mOnBeforeSocketCloseFp
Provides an opportunity for the app to clean up, e.g., QoS-related data or resources just before the ...
virtual void onConnectFailure(const asio::error_code &e)
virtual void onConnectSuccess()
Class override callbacks.
virtual void handleSend(const asio::error_code &e)
Handle completion of a sendData operation.
std::shared_ptr< DataBuffer > mReceiveBuffer
Receive Buffer and state.
static void setExclusiveAddressUse(asio::ip::udp::socket &socket)
For a UDP socket, between open() and bind().
asio::ip::address mConnectedAddress
Connected Info and State.
virtual void handleUdpResolve(const asio::error_code &ec, const asio::ip::udp::resolver::results_type &endpoints)
void handleUdpResolve(const asio::error_code &ec, const asio::ip::udp::resolver::results_type &endpoints) override
asio::ip::udp::socket mSocket
void transportSend(const StunTuple &destination, std::vector< asio::const_buffer > &buffers) override
asio::ip::address getSenderEndpointAddress() override
AsyncUdpSocketBase(asio::io_context &ioService)
void connect(const std::string &address, unsigned short port) override
asio::error_code bind(const asio::ip::address &address, unsigned short port) override
Note: The following API's are thread safe and queue the request to be handled by the ioService thread...
asio::ip::udp::resolver mResolver
unsigned short getSenderEndpointPort() override
asio::ip::udp::endpoint mSenderEndpoint
Endpoint info for current sender.
unsigned int getSocketDescriptor() override
const asio::ip::address & getAddress() const
Definition StunTuple.hxx:44
unsigned int getPort() const
Definition StunTuple.hxx:47
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
const char * c_str() const
Returns a null-terminated string representing.
Definition Data.cxx:1097