reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
TurnLoadGenAsyncSocketHandler.cxx
Go to the documentation of this file.
1
2#include <iostream>
3#include <string>
4#include <asio.hpp>
5#ifdef USE_SSL
6#include <asio/ssl.hpp>
7#endif
8
10#include "../TurnSocket.hxx"
11#include <rutil/Logger.hxx>
13
14#ifdef BOOST_ASIO_HAS_STD_CHRONO
15using namespace std::chrono;
16#else
17#include <chrono>
18using namespace std::chrono;
19#endif
20
21using namespace reTurn;
22using namespace std;
23using namespace resip;
24
25#define RESIPROCATE_SUBSYSTEM resip::Subsystem::TEST
26
27#define LOG_PREFIX << "[" << mClientNum << "] "
28
30
32 int clientNum,
33 asio::io_context& ioService,
34 const Data& localAddress,
35 const Data& turnServerAddress,
36 unsigned short turnServerPort,
37 unsigned short relayPort,
38 const ConfigParse& config) :
39 mClientNum(clientNum),
40 mTimer(ioService),
41 mLocalAddress(asio::ip::make_address(localAddress.c_str())),
42 mTurnServerAddress(turnServerAddress),
43 mTurnServerPort(turnServerPort),
44 mRelayPort(relayPort),
45 mConfig(config),
46 mNumSends(0),
47 mNumSendFailures(0),
48 mNumReceiveSuccesses(0),
49 mNumReceiveFailures(0),
50 mDelayBetweenClientStartsMs(config.getConfigInt("DelayBetweenClientStartsMs", 2000)),
51 mAllocationTimeSecs(config.getConfigInt("AllocationTimeSecs", 60)),
52 mAllocationLifetimeSecs(config.getConfigInt("AllocationLifetimeSecs", TurnSocket::UnspecifiedLifetime)),
53 mTimeBetweenAllocationsSecs(config.getConfigInt("TimeBetweenAllocationsSecs", 2)),
54 mPayloadIntervalMs(config.getConfigInt("PayloadIntervalMs", 20))
55{
56}
57
59{
60 InfoLog(LOG_PREFIX << "Destructing...");
61 if (mTurnAsyncSocket)
62 {
63 mTurnAsyncSocket->close();
64 }
65}
66
67void TurnLoadGenAsyncSocketHandler::setTurnAsyncSocket(std::shared_ptr<TurnAsyncSocket>& turnAsyncSocket)
68{
69 mTurnAsyncSocket = turnAsyncSocket;
70
71 // Connect to TurnServer based on delay setting
72 if (mDelayBetweenClientStartsMs > 0)
73 {
74 mTimer.expires_after(milliseconds(mDelayBetweenClientStartsMs * (mClientNum-1)));
75 mTimer.async_wait(std::bind(&TurnLoadGenAsyncSocketHandler::connect, this));
76 }
77 else
78 {
79 connect();
80 }
81}
82
84{
85 InfoLog(LOG_PREFIX << "Connecting to turn server...");
86 // Connect to Stun/Turn Server
87 mTurnAsyncSocket->connect(mTurnServerAddress.c_str(), mTurnServerPort);
88}
89
91{
92 mTurnAsyncSocket->bindRequest();
93}
94
96{
97 mNumSends = 0;
98 mNumSendFailures = 0;
99 mNumReceiveSuccesses = 0;
100 mNumReceiveFailures = 0;
101
102 mTurnAsyncSocket->createAllocation(mAllocationLifetimeSecs,
107}
108
110{
111 DebugLog(LOG_PREFIX << "Sending RTP payload...");
112 mStartTime = time(0);
113 sendPayload();
114}
115
117{
118 time_t secondsElapsed = time(0) - mStartTime;
119 if (secondsElapsed < mAllocationTimeSecs)
120 {
121 mTimer.expires_after(milliseconds(mPayloadIntervalMs));
122 mTimer.async_wait(std::bind(&TurnLoadGenAsyncSocketHandler::sendPayload, this));
123 mTurnAsyncSocket->send(g_Payload->data(), g_Payload->size());
124 ++mNumSends;
125 }
126 else
127 {
128 DebugLog(LOG_PREFIX << "Configured AllocationTimeSecs has expired, configured=" << mAllocationTimeSecs << ", secondsElapsed=" << secondsElapsed << " destroying allocation.");
129 mTurnAsyncSocket->destroyAllocation();
130 }
131}
132
133void TurnLoadGenAsyncSocketHandler::onConnectSuccess(unsigned int socketDesc, const asio::ip::address& address, unsigned short port)
134{
135 DebugLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onConnectSuccess: socketDest=" << socketDesc << ", address=" << address.to_string() << ", turnPort=" << port);
136
137 if (mConfig.getConfigBool("SendInitialBind", true))
138 {
140 }
141 else
142 {
144 }
145}
146
147void TurnLoadGenAsyncSocketHandler::onConnectFailure(unsigned int socketDesc, const asio::error_code& e)
148{
149 ErrLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onConnectFailure: socketDest=" << socketDesc << " error=" << e.value() << "(" << e.message() << ").");
150}
151
152void TurnLoadGenAsyncSocketHandler::onSharedSecretSuccess(unsigned int socketDesc, const char* username, unsigned int usernameSize, const char* password, unsigned int passwordSize)
153{
154 InfoLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onSharedSecretSuccess: socketDest=" << socketDesc << ", username=" << username << ", password=" << password);
155}
156
157void TurnLoadGenAsyncSocketHandler::onSharedSecretFailure(unsigned int socketDesc, const asio::error_code& e)
158{
159 ErrLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onSharedSecretFailure: socketDest=" << socketDesc << " error=" << e.value() << "(" << e.message() << ").");
160}
161
162void TurnLoadGenAsyncSocketHandler::onBindSuccess(unsigned int socketDesc, const StunTuple& reflexiveTuple, const StunTuple& stunServerTuple)
163{
164 InfoLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onBindingSuccess: socketDest=" << socketDesc << ", reflexive=" << reflexiveTuple << ", stunServerTuple=" << stunServerTuple);
166}
167
168void TurnLoadGenAsyncSocketHandler::onBindFailure(unsigned int socketDesc, const asio::error_code& e, const StunTuple& stunServerTuple)
169{
170 ErrLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onBindingFailure: socketDest=" << socketDesc << " error=" << e.value() << "(" << e.message() << "), stunServerTuple=" << stunServerTuple);
171}
172
173void TurnLoadGenAsyncSocketHandler::onAllocationSuccess(unsigned int socketDesc, const StunTuple& reflexiveTuple, const StunTuple& relayTuple, unsigned int lifetimeSecs, unsigned int bandwidth, uint64_t reservationToken)
174{
175 DebugLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onAllocationSuccess: socketDest=" << socketDesc <<
176 ", reflexive=" << reflexiveTuple <<
177 ", relay=" << relayTuple <<
178 ", lifetimeSecs=" << lifetimeSecs <<
179 ", bandwidth=" << bandwidth <<
180 ", reservationToken=" << reservationToken);
181
182 DebugLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onAllocationSuccess: setting active destination to " << mLocalAddress << ":" << mRelayPort);
183 mTurnAsyncSocket->setActiveDestination(mLocalAddress, mRelayPort);
184}
185
186void TurnLoadGenAsyncSocketHandler::onAllocationFailure(unsigned int socketDesc, const asio::error_code& e)
187{
188 ErrLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onAllocationFailure: socketDest=" << socketDesc << " error=" << e.value() << "(" << e.message() << ").");
189
190 // Retry allocation after timer expires
191 mTimer.expires_after(seconds(mTimeBetweenAllocationsSecs));
192 mTimer.async_wait(std::bind(&TurnLoadGenAsyncSocketHandler::sendAllocationRequest, this));
193}
194
195void TurnLoadGenAsyncSocketHandler::onRefreshSuccess(unsigned int socketDesc, unsigned int lifetimeSecs)
196{
197 DebugLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onRefreshSuccess: socketDest=" << socketDesc << ", lifetimeSecs=" << lifetimeSecs);
198 if (lifetimeSecs == 0)
199 {
200 InfoLog(LOG_PREFIX << "Allocation was up for " << (time(0) - mStartTime) << " seconds, numSends=" << mNumSends <<
201 ", numReceiveSuccesses=" << mNumReceiveSuccesses <<
202 ", numSendFailures=" << mNumSendFailures <<
203 ", numReceiveFailures=" << mNumReceiveFailures <<
204 ", creating new allocation in " << mTimeBetweenAllocationsSecs << " secs.");
205
206 mTimer.expires_after(seconds(mTimeBetweenAllocationsSecs));
207 mTimer.async_wait(std::bind(&TurnLoadGenAsyncSocketHandler::sendAllocationRequest, this));
208 }
209}
210
211void TurnLoadGenAsyncSocketHandler::onRefreshFailure(unsigned int socketDesc, const asio::error_code& e)
212{
213 ErrLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onRefreshFailure: socketDest=" << socketDesc << " error=" << e.value() << "(" << e.message() << ").");
214
215 // Retry allocation after timer expires
216 mTimer.expires_after(seconds(mTimeBetweenAllocationsSecs));
217 mTimer.async_wait(std::bind(&TurnLoadGenAsyncSocketHandler::sendAllocationRequest, this));
218}
219
221{
222 DebugLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onSetActiveDestinationSuccess: socketDest=" << socketDesc);
224}
225
226void TurnLoadGenAsyncSocketHandler::onSetActiveDestinationFailure(unsigned int socketDesc, const asio::error_code& e)
227{
228 ErrLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onSetActiveDestinationFailure: socketDest=" << socketDesc << " error=" << e.value() << "(" << e.message() << ").");
229}
230
232{
233 InfoLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onClearActiveDestinationSuccess: socketDest=" << socketDesc);
234}
235
236void TurnLoadGenAsyncSocketHandler::onClearActiveDestinationFailure(unsigned int socketDesc, const asio::error_code& e)
237{
238 ErrLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onClearActiveDestinationFailure: socketDest=" << socketDesc << " error=" << e.value() << "(" << e.message() << ").");
239}
240
241void TurnLoadGenAsyncSocketHandler::onChannelBindRequestSent(unsigned int socketDesc, unsigned short channelNumber)
242{
243 DebugLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onChannelBindRequestSent: socketDest=" << socketDesc << " channelNumber=" << channelNumber);
244}
245
246void TurnLoadGenAsyncSocketHandler::onChannelBindSuccess(unsigned int socketDesc, unsigned short channelNumber)
247{
248 DebugLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onChannelBindSuccess: socketDest=" << socketDesc << " channelNumber=" << channelNumber);
249}
250
251void TurnLoadGenAsyncSocketHandler::onChannelBindFailure(unsigned int socketDesc, const asio::error_code& e)
252{
253 ErrLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onChannelBindFailure: socketDest=" << socketDesc << " error=" << e.value() << "(" << e.message() << ").");
254}
255
257{
258 //InfoLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onSendSuccess: socketDest=" << socketDesc);
259}
260
261void TurnLoadGenAsyncSocketHandler::onSendFailure(unsigned int socketDesc, const asio::error_code& e)
262{
263 ErrLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onSendFailure: socketDest=" << socketDesc << " error=" << e.value() << "(" << e.message() << ").");
264 ++mNumSendFailures;
265}
266
267void TurnLoadGenAsyncSocketHandler::onReceiveSuccess(unsigned int socketDesc, const asio::ip::address& address, unsigned short port, const std::shared_ptr<reTurn::DataBuffer>& data)
268{
269 //InfoLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onReceiveSuccess: socketDest=" << socketDesc << ", fromAddress=" << address << ", fromPort=" << turnPort << ", size=" << data->size() << ", data=" << data->data());
270 ++mNumReceiveSuccesses;
271}
272
273void TurnLoadGenAsyncSocketHandler::onReceiveFailure(unsigned int socketDesc, const asio::error_code& e)
274{
275 ErrLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onReceiveFailure: socketDest=" << socketDesc << " error=" << e.value() << "(" << e.message() << ").");
276 ++mNumReceiveFailures;
277}
278
279void TurnLoadGenAsyncSocketHandler::onIncomingBindRequestProcessed(unsigned int socketDesc, const StunTuple& sourceTuple)
280{
281 InfoLog(LOG_PREFIX << "MyTurnAsyncSocketHandler::onIncomingBindRequestProcessed: socketDest=" << socketDesc << " sourceTuple=" << sourceTuple);
282}
283
284
285/* ====================================================================
286
287 Copyright (c) 2024 SIP Spectrum, Inc http://www.sipspectrum.com
288 All rights reserved.
289
290 Redistribution and use in source and binary forms, with or without
291 modification, are permitted provided that the following conditions are
292 met:
293
294 1. Redistributions of source code must retain the above copyright
295 notice, this list of conditions and the following disclaimer.
296
297 2. Redistributions in binary form must reproduce the above copyright
298 notice, this list of conditions and the following disclaimer in the
299 documentation and/or other materials provided with the distribution.
300
301 3. Neither the name of Plantronics nor the names of its contributors
302 may be used to endorse or promote products derived from this
303 software without specific prior written permission.
304
305 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
306 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
307 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
308 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
309 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
310 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
311 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
312 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
313 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
314 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
316
317 ==================================================================== */
#define DebugLog(args_)
Definition Logger.hxx:128
#define InfoLog(args_)
Definition Logger.hxx:131
#define ErrLog(args_)
Definition Logger.hxx:137
resip::Data * g_Payload
virtual void onSharedSecretSuccess(unsigned int socketDesc, const char *username, unsigned int usernameSize, const char *password, unsigned int passwordSize) override
virtual void onChannelBindRequestSent(unsigned int socketDesc, unsigned short channelNumber) override
virtual void onConnectSuccess(unsigned int socketDesc, const asio::ip::address &address, unsigned short port) override
virtual void onReceiveSuccess(unsigned int socketDesc, const asio::ip::address &address, unsigned short port, const std::shared_ptr< reTurn::DataBuffer > &data) override
virtual void onRefreshSuccess(unsigned int socketDesc, unsigned int lifetime) override
virtual void onAllocationSuccess(unsigned int socketDesc, const StunTuple &reflexiveTuple, const StunTuple &relayTuple, unsigned int lifetime, unsigned int bandwidth, uint64_t reservationToken) override
virtual void onSendFailure(unsigned int socketDesc, const asio::error_code &e) override
virtual void onSetActiveDestinationFailure(unsigned int socketDesc, const asio::error_code &e) override
virtual void onChannelBindSuccess(unsigned int socketDesc, unsigned short channelNumber) override
virtual void onChannelBindFailure(unsigned int socketDesc, const asio::error_code &e) override
virtual void onAllocationFailure(unsigned int socketDesc, const asio::error_code &e) override
virtual void onRefreshFailure(unsigned int socketDesc, const asio::error_code &e) override
virtual void onIncomingBindRequestProcessed(unsigned int socketDesc, const StunTuple &sourceTuple) override
virtual void onSendSuccess(unsigned int socketDesc) override
virtual void onReceiveFailure(unsigned int socketDesc, const asio::error_code &e) override
virtual void onClearActiveDestinationSuccess(unsigned int socketDesc) override
void setTurnAsyncSocket(std::shared_ptr< TurnAsyncSocket > &turnAsyncSocket)
TurnLoadGenAsyncSocketHandler(int clientNum, asio::io_context &ioService, const Data &localAddress, const Data &turnServerAddress, unsigned short turnServerPort, unsigned short relayPort, const ConfigParse &config)
virtual void onClearActiveDestinationFailure(unsigned int socketDesc, const asio::error_code &e) override
virtual void onBindFailure(unsigned int socketDesc, const asio::error_code &e, const StunTuple &stunServerTuple) override
virtual void onBindSuccess(unsigned int socketDesc, const StunTuple &reflexiveTuple, const StunTuple &stunServerTuple) override
virtual void onSetActiveDestinationSuccess(unsigned int socketDesc) override
virtual void onConnectFailure(unsigned int socketDesc, const asio::error_code &e) override
virtual void onSharedSecretFailure(unsigned int socketDesc, const asio::error_code &e) override
static const uint8_t PropsPortPair
static unsigned short UnspecifiedToken
static unsigned int UnspecifiedBandwidth
bool getConfigBool(const resip::Data &name, bool defaultValue) const
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
size_type size() const noexcept
Returns the number of bytes in this Data.
Definition Data.hxx:599
const char * data() const noexcept
Returns a pointer to the contents of this Data.
Definition Data.hxx:608
dcm! – add UnusedChecking(_enum) below;
unsigned long long uint64_t
Definition Stun.hxx:39