Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
dhtnet
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
dhtnet
Commits
c36965cf
Commit
c36965cf
authored
1 year ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
upnp: debounce connectivityChanged
Change-Id: I6c7c73801febe3dc017ee58dfeff4d4ad692a3c8
parent
b04fbd7e
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/upnp/upnp_context.h
+7
-3
7 additions, 3 deletions
include/upnp/upnp_context.h
src/upnp/upnp_context.cpp
+35
-38
35 additions, 38 deletions
src/upnp/upnp_context.cpp
with
42 additions
and
41 deletions
include/upnp/upnp_context.h
+
7
−
3
View file @
c36965cf
...
@@ -277,6 +277,11 @@ private:
...
@@ -277,6 +277,11 @@ private:
UPnPContext
&
operator
=
(
UPnPContext
&&
)
=
delete
;
UPnPContext
&
operator
=
(
UPnPContext
&&
)
=
delete
;
UPnPContext
&
operator
=
(
const
UPnPContext
&
)
=
delete
;
UPnPContext
&
operator
=
(
const
UPnPContext
&
)
=
delete
;
void
_connectivityChanged
(
const
asio
::
error_code
&
ec
);
// Thread (io_context), destroyed last
std
::
unique_ptr
<
std
::
thread
>
ioContextRunner_
{};
bool
started_
{
false
};
bool
started_
{
false
};
// The known public address. The external addresses returned by
// The known public address. The external addresses returned by
...
@@ -284,6 +289,7 @@ private:
...
@@ -284,6 +289,7 @@ private:
IpAddr
knownPublicAddress_
{};
IpAddr
knownPublicAddress_
{};
// Set of registered controllers
// Set of registered controllers
std
::
mutex
mutable
controllerMutex_
;
std
::
set
<
void
*>
controllerList_
;
std
::
set
<
void
*>
controllerList_
;
// Map of available protocols.
// Map of available protocols.
...
@@ -300,6 +306,7 @@ private:
...
@@ -300,6 +306,7 @@ private:
std
::
shared_ptr
<
asio
::
io_context
>
ctx
;
std
::
shared_ptr
<
asio
::
io_context
>
ctx
;
std
::
shared_ptr
<
dht
::
log
::
Logger
>
logger_
;
std
::
shared_ptr
<
dht
::
log
::
Logger
>
logger_
;
asio
::
steady_timer
mappingListUpdateTimer_
;
asio
::
steady_timer
mappingListUpdateTimer_
;
asio
::
steady_timer
connectivityChangedTimer_
;
// Current preferred IGD. Can be null if there is no valid IGD.
// Current preferred IGD. Can be null if there is no valid IGD.
std
::
shared_ptr
<
IGD
>
preferredIgd_
;
std
::
shared_ptr
<
IGD
>
preferredIgd_
;
...
@@ -313,9 +320,6 @@ private:
...
@@ -313,9 +320,6 @@ private:
// Shutdown synchronization
// Shutdown synchronization
bool
shutdownComplete_
{
false
};
bool
shutdownComplete_
{
false
};
// Thread
std
::
unique_ptr
<
std
::
thread
>
ioContextRunner_
;
};
};
}
// namespace upnp
}
// namespace upnp
...
...
This diff is collapsed.
Click to expand it.
src/upnp/upnp_context.cpp
+
35
−
38
View file @
c36965cf
...
@@ -44,7 +44,10 @@ constexpr static uint16_t UPNP_UDP_PORT_MIN {20000};
...
@@ -44,7 +44,10 @@ constexpr static uint16_t UPNP_UDP_PORT_MIN {20000};
constexpr
static
uint16_t
UPNP_UDP_PORT_MAX
{
UPNP_UDP_PORT_MIN
+
5000
};
constexpr
static
uint16_t
UPNP_UDP_PORT_MAX
{
UPNP_UDP_PORT_MIN
+
5000
};
UPnPContext
::
UPnPContext
(
const
std
::
shared_ptr
<
asio
::
io_context
>&
ioContext
,
const
std
::
shared_ptr
<
dht
::
log
::
Logger
>&
logger
)
UPnPContext
::
UPnPContext
(
const
std
::
shared_ptr
<
asio
::
io_context
>&
ioContext
,
const
std
::
shared_ptr
<
dht
::
log
::
Logger
>&
logger
)
:
ctx
(
createIoContext
(
ioContext
,
logger
)),
mappingListUpdateTimer_
(
*
ioContext
),
logger_
(
logger
)
:
ctx
(
createIoContext
(
ioContext
,
logger
))
,
mappingListUpdateTimer_
(
*
ioContext
)
,
connectivityChangedTimer_
(
*
ioContext
)
,
logger_
(
logger
)
{
{
if
(
logger_
)
logger_
->
debug
(
"Creating UPnPContext instance [{}]"
,
fmt
::
ptr
(
this
));
if
(
logger_
)
logger_
->
debug
(
"Creating UPnPContext instance [{}]"
,
fmt
::
ptr
(
this
));
...
@@ -238,10 +241,16 @@ UPnPContext::generateRandomPort(PortType type, bool mustBeEven)
...
@@ -238,10 +241,16 @@ UPnPContext::generateRandomPort(PortType type, bool mustBeEven)
void
void
UPnPContext
::
connectivityChanged
()
UPnPContext
::
connectivityChanged
()
{
{
/*if (not isValidThread()) {
// Debounce the connectivity change notification.
ctx->post([this] { connectivityChanged(); });
connectivityChangedTimer_
.
expires_after
(
std
::
chrono
::
milliseconds
(
50
));
connectivityChangedTimer_
.
async_wait
(
std
::
bind
(
&
UPnPContext
::
_connectivityChanged
,
this
,
std
::
placeholders
::
_1
));
}
void
UPnPContext
::
_connectivityChanged
(
const
asio
::
error_code
&
ec
)
{
if
(
ec
==
asio
::
error
::
operation_aborted
)
return
;
return
;
}*/
auto
hostAddr
=
ip_utils
::
getLocalAddr
(
AF_INET
);
auto
hostAddr
=
ip_utils
::
getLocalAddr
(
AF_INET
);
...
@@ -384,11 +393,7 @@ UPnPContext::reserveMapping(Mapping& requestedMap)
...
@@ -384,11 +393,7 @@ UPnPContext::reserveMapping(Mapping& requestedMap)
void
void
UPnPContext
::
releaseMapping
(
const
Mapping
&
map
)
UPnPContext
::
releaseMapping
(
const
Mapping
&
map
)
{
{
/*if (not isValidThread()) {
ctx
->
dispatch
([
this
,
map
]
{
ctx->post([this, map] { releaseMapping(map); });
return;
}*/
auto
mapPtr
=
getMappingWithKey
(
map
.
getMapKey
());
auto
mapPtr
=
getMappingWithKey
(
map
.
getMapKey
());
if
(
not
mapPtr
)
{
if
(
not
mapPtr
)
{
...
@@ -405,6 +410,7 @@ UPnPContext::releaseMapping(const Mapping& map)
...
@@ -405,6 +410,7 @@ UPnPContext::releaseMapping(const Mapping& map)
// Remove it.
// Remove it.
requestRemoveMapping
(
mapPtr
);
requestRemoveMapping
(
mapPtr
);
unregisterMapping
(
mapPtr
);
unregisterMapping
(
mapPtr
);
});
}
}
void
void
...
@@ -416,18 +422,12 @@ UPnPContext::registerController(void* controller)
...
@@ -416,18 +422,12 @@ UPnPContext::registerController(void* controller)
if
(
logger_
)
logger_
->
warn
(
"UPnPContext already shut down"
);
if
(
logger_
)
logger_
->
warn
(
"UPnPContext already shut down"
);
return
;
return
;
}
}
}
/*if (not isValidThread()) {
ctx->post([this, controller] { registerController(controller); });
return;
}*/
auto
ret
=
controllerList_
.
emplace
(
controller
);
auto
ret
=
controllerList_
.
emplace
(
controller
);
if
(
not
ret
.
second
)
{
if
(
not
ret
.
second
)
{
if
(
logger_
)
logger_
->
warn
(
"Controller {} is already registered"
,
fmt
::
ptr
(
controller
));
if
(
logger_
)
logger_
->
warn
(
"Controller {} is already registered"
,
fmt
::
ptr
(
controller
));
return
;
return
;
}
}
}
if
(
logger_
)
logger_
->
debug
(
"Successfully registered controller {}"
,
fmt
::
ptr
(
controller
));
if
(
logger_
)
logger_
->
debug
(
"Successfully registered controller {}"
,
fmt
::
ptr
(
controller
));
if
(
not
started_
)
if
(
not
started_
)
...
@@ -437,11 +437,7 @@ UPnPContext::registerController(void* controller)
...
@@ -437,11 +437,7 @@ UPnPContext::registerController(void* controller)
void
void
UPnPContext
::
unregisterController
(
void
*
controller
)
UPnPContext
::
unregisterController
(
void
*
controller
)
{
{
/*if (not isValidThread()) {
std
::
unique_lock
<
std
::
mutex
>
lock
(
mappingMutex_
);
ctx->post([this, controller] { unregisterController(controller); });
return;
}*/
if
(
controllerList_
.
erase
(
controller
)
==
1
)
{
if
(
controllerList_
.
erase
(
controller
)
==
1
)
{
if
(
logger_
)
logger_
->
debug
(
"Successfully unregistered controller {}"
,
fmt
::
ptr
(
controller
));
if
(
logger_
)
logger_
->
debug
(
"Successfully unregistered controller {}"
,
fmt
::
ptr
(
controller
));
}
else
{
}
else
{
...
@@ -449,6 +445,7 @@ UPnPContext::unregisterController(void* controller)
...
@@ -449,6 +445,7 @@ UPnPContext::unregisterController(void* controller)
}
}
if
(
controllerList_
.
empty
())
{
if
(
controllerList_
.
empty
())
{
lock
.
unlock
();
stopUpnp
();
stopUpnp
();
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment