2015-11-21 02:58:13 +01:00
|
|
|
// OpenVPN -- An application to securely tunnel IP networks
|
|
|
|
// over a single port, with support for SSL/TLS-based
|
|
|
|
// session authentication and key exchange,
|
|
|
|
// packet encryption, packet authentication, and
|
|
|
|
// packet compression.
|
|
|
|
//
|
2016-09-04 07:29:23 +02:00
|
|
|
// Copyright (C) 2012-2016 OpenVPN Technologies, Inc.
|
2015-11-21 02:58:13 +01:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2017-03-16 09:41:16 +01:00
|
|
|
// it under the terms of the GNU General Public License Version 3
|
2015-11-21 02:58:13 +01:00
|
|
|
// as published by the Free Software Foundation.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-03-16 09:41:16 +01:00
|
|
|
// GNU General Public License for more details.
|
2015-11-21 02:58:13 +01:00
|
|
|
//
|
2017-03-16 09:41:16 +01:00
|
|
|
// You should have received a copy of the GNU General Public License
|
2015-11-21 02:58:13 +01:00
|
|
|
// along with this program in the COPYING file.
|
|
|
|
// If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#ifndef OPENVPN_COMMON_STOP_H
|
|
|
|
#define OPENVPN_COMMON_STOP_H
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
|
|
|
#include <utility>
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
namespace openvpn {
|
|
|
|
class Stop
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class Scope
|
|
|
|
{
|
|
|
|
friend Stop;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Scope(Stop* stop_arg, std::function<void()>&& method_arg)
|
|
|
|
: stop(stop_arg),
|
2016-05-05 08:33:35 +02:00
|
|
|
method(std::move(method_arg)),
|
|
|
|
index(-1)
|
2015-11-21 02:58:13 +01:00
|
|
|
{
|
|
|
|
if (stop)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(stop->mutex);
|
2016-05-05 08:33:35 +02:00
|
|
|
if (stop->stop_called)
|
|
|
|
{
|
|
|
|
// stop already called, call method immediately
|
|
|
|
method();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
index = stop->scopes.size();
|
|
|
|
stop->scopes.push_back(this);
|
|
|
|
}
|
2015-11-21 02:58:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~Scope()
|
|
|
|
{
|
|
|
|
if (stop)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(stop->mutex);
|
2016-05-05 08:33:35 +02:00
|
|
|
if (index >= 0 && index < stop->scopes.size() && stop->scopes[index] == this)
|
2015-11-21 02:58:13 +01:00
|
|
|
{
|
|
|
|
stop->scopes[index] = nullptr;
|
|
|
|
stop->prune();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Scope(const Scope&) = delete;
|
|
|
|
Scope& operator=(const Scope&) = delete;
|
|
|
|
|
2016-05-05 08:33:35 +02:00
|
|
|
Stop *const stop;
|
|
|
|
const std::function<void()> method;
|
|
|
|
int index;
|
2015-11-21 02:58:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Stop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(mutex);
|
2016-05-05 08:33:35 +02:00
|
|
|
stop_called = true;
|
2015-11-21 02:58:13 +01:00
|
|
|
while (scopes.size())
|
|
|
|
{
|
|
|
|
Scope* scope = scopes.back();
|
|
|
|
scopes.pop_back();
|
|
|
|
if (scope)
|
|
|
|
{
|
2016-05-05 08:33:35 +02:00
|
|
|
scope->index = -1;
|
2015-11-21 02:58:13 +01:00
|
|
|
scope->method();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Stop(const Stop&) = delete;
|
|
|
|
Stop& operator=(const Stop&) = delete;
|
|
|
|
|
|
|
|
void prune()
|
|
|
|
{
|
|
|
|
while (scopes.size() && !scopes.back())
|
|
|
|
scopes.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::recursive_mutex mutex;
|
2016-05-05 08:33:35 +02:00
|
|
|
std::vector<Scope*> scopes;
|
|
|
|
bool stop_called = false;
|
2015-11-21 02:58:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|