add Context::close()

This commit is contained in:
Michael Davidsaver
2022-11-27 08:20:13 -08:00
parent 722759416b
commit 0de17036f4
5 changed files with 43 additions and 1 deletions
+19
View File
@@ -312,6 +312,9 @@ std::shared_ptr<Channel> Channel::build(const std::shared_ptr<ContextImpl>& cont
const std::string& name,
const std::string& server)
{
if(context->state!=ContextImpl::Running)
throw std::logic_error("Context close()d");
SockAddr forceServer;
decltype (context->chanByName)::key_type namekey(name, server);
@@ -381,6 +384,14 @@ const Config& Context::config() const
return pvt->impl->effective;
}
void Context::close()
{
if(!pvt)
throw std::logic_error("NULL Context");
pvt->impl->close();
}
void Context::hurryUp()
{
if(!pvt)
@@ -584,6 +595,8 @@ ContextImpl::ContextImpl(const Config& conf, const evbase& tcp_loop)
log_err_printf(setup, "Error enabling beacon clean timer on\n%s", "");
if(event_add(cacheCleaner.get(), &channelCacheCleanInterval))
log_err_printf(setup, "Error enabling channel cache clean timer on\n%s", "");
state = Running;
}
ContextImpl::~ContextImpl() {}
@@ -609,8 +622,14 @@ void ContextImpl::startNS()
void ContextImpl::close()
{
log_debug_printf(setup, "context %p close\n", this);
// terminate all active connections
tcp_loop.call([this]() {
if(state == Stopped)
return;
state = Stopped;
(void)event_del(searchTimer.get());
(void)event_del(searchRx4.get());
(void)event_del(searchRx6.get());
+3
View File
@@ -46,6 +46,9 @@ Connection::~Connection()
std::shared_ptr<Connection> Connection::build(const std::shared_ptr<ContextImpl>& context,
const SockAddr& serv, bool reconn)
{
if(context->state!=ContextImpl::Running)
throw std::logic_error("Context close()d");
std::shared_ptr<Connection> ret;
auto it = context->connByAddr.find(serv);
if(it==context->connByAddr.end() || !(ret = it->second.lock())) {
+3
View File
@@ -82,6 +82,9 @@ std::shared_ptr<Operation> DiscoverBuilder::exec()
context->tcp_loop.dispatch([op, context, ping]() {
if(context->state!=ContextImpl::Running)
throw std::logic_error("Context close()d");
bool first = context->discoverers.empty();
context->discoverers[op.get()] = op;
+7 -1
View File
@@ -239,6 +239,12 @@ struct ContextImpl : public std::enable_shared_from_this<ContextImpl>
SockAttach attach;
IfaceMap& ifmap;
enum state_t {
Init,
Running,
Stopped,
} state = Init;
// "const" after ctor
Config effective;
@@ -334,7 +340,7 @@ struct Context::Pvt {
private:
evbase loop;
public:
std::shared_ptr<ContextImpl> impl;
const std::shared_ptr<ContextImpl> impl;
INST_COUNTER(ClientPvt);
+11
View File
@@ -309,6 +309,17 @@ public:
//! effective config of running client
const Config& config() const;
/** Force close the client.
*
* ~Context() will close() automatically. So an explicit call is optional.
*
* Aborts/interrupts all in progress network operations.
* Blocks until any in-progress callbacks have completed.
*
* @since UNRELEASED
*/
void close();
/** Request the present value of a PV
*
* Simple blocking