OpenVPN 3 Core Library
Loading...
Searching...
No Matches
actionthread.hpp
Go to the documentation of this file.
1
// OpenVPN -- An application to securely tunnel IP networks
2
// over a single port, with support for SSL/TLS-based
3
// session authentication and key exchange,
4
// packet encryption, packet authentication, and
5
// packet compression.
6
//
7
// Copyright (C) 2012- OpenVPN Inc.
8
//
9
// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10
//
11
12
13
#ifndef OPENVPN_COMMON_ACTIONTHREAD_H
14
#define OPENVPN_COMMON_ACTIONTHREAD_H
15
16
#include <thread>
17
18
#include <
openvpn/common/rc.hpp
>
19
#include <
openvpn/common/action.hpp
>
20
#include <
openvpn/log/logthread.hpp
>
21
22
namespace
openvpn
{
23
24
class
ActionThread
:
public
RC
<thread_safe_refcount>
25
{
26
public
:
27
typedef
RCPtr<ActionThread>
Ptr
;
28
29
struct
Notify
30
{
31
virtual
void
action_thread_finished
(
const
ActionThread
*self,
bool
status) = 0;
32
};
33
34
ActionThread
(openvpn_io::io_context &io_context_arg,
35
const
ActionList::Ptr
&action_list,
36
Notify
*completion_handler_arg)
37
:
io_context
(io_context_arg),
38
thread
(nullptr),
39
actions
(action_list),
40
completion_handler
(completion_handler_arg)
41
{
42
if
(
actions
)
43
thread
=
new
std::thread(&
ActionThread::thread_func
,
this
);
44
}
45
46
void
stop
(
const
bool
halt)
47
{
48
if
(
thread
)
49
{
50
if
(halt)
51
actions
->
halt
();
52
thread
->join();
53
delete
thread
;
54
thread
=
nullptr
;
55
// Necessary because no guarantee that completion_handler
56
// obj will remain in scope during io_context.post delay.
57
completion_handler
=
nullptr
;
58
}
59
}
60
61
virtual
~ActionThread
()
62
{
63
stop
(
true
);
64
}
65
66
private
:
67
void
completion_post
(
bool
status)
68
{
69
Notify
*n =
completion_handler
;
70
completion_handler
=
nullptr
;
71
if
(n)
72
n->
action_thread_finished
(
this
, status);
73
}
74
75
void
thread_func
()
76
{
77
Log::Context
logctx(
logwrap
);
78
bool
status =
false
;
79
try
80
{
81
OPENVPN_LOG
(
"START THREAD..."
);
82
status =
actions
->
execute
();
83
OPENVPN_LOG
(
"END THREAD"
);
84
}
85
catch
(
const
std::exception &e)
86
{
87
OPENVPN_LOG
(
"ActionThread exception: "
<< e.what());
88
}
89
openvpn_io::post(
io_context
, [self =
Ptr
(
this
), status]()
90
{ self->completion_post(status); });
91
}
92
93
openvpn_io::io_context &
io_context
;
94
std::thread *
thread
;
95
ActionList::Ptr
actions
;
// actions to execute in child thread
96
Notify
*
completion_handler
;
// completion handler
97
Log::Context::Wrapper
logwrap
;
// used to carry forward the log context from parent thread
98
};
99
100
}
// namespace openvpn
101
102
#endif
action.hpp
openvpn::ActionList::halt
void halt()
Definition
action.hpp:146
openvpn::ActionList::execute
virtual std::unordered_set< std::string > execute(std::ostream &os)
Executes a sequence of actions and returns marks of failed actions.
Definition
action.hpp:98
openvpn::ActionThread
Definition
actionthread.hpp:25
openvpn::ActionThread::stop
void stop(const bool halt)
Definition
actionthread.hpp:46
openvpn::ActionThread::completion_handler
Notify * completion_handler
Definition
actionthread.hpp:96
openvpn::ActionThread::io_context
openvpn_io::io_context & io_context
Definition
actionthread.hpp:93
openvpn::ActionThread::Ptr
RCPtr< ActionThread > Ptr
Definition
actionthread.hpp:27
openvpn::ActionThread::~ActionThread
virtual ~ActionThread()
Definition
actionthread.hpp:61
openvpn::ActionThread::thread_func
void thread_func()
Definition
actionthread.hpp:75
openvpn::ActionThread::ActionThread
ActionThread(openvpn_io::io_context &io_context_arg, const ActionList::Ptr &action_list, Notify *completion_handler_arg)
Definition
actionthread.hpp:34
openvpn::ActionThread::completion_post
void completion_post(bool status)
Definition
actionthread.hpp:67
openvpn::ActionThread::actions
ActionList::Ptr actions
Definition
actionthread.hpp:95
openvpn::ActionThread::logwrap
Log::Context::Wrapper logwrap
Definition
actionthread.hpp:97
openvpn::ActionThread::thread
std::thread * thread
Definition
actionthread.hpp:94
openvpn::RCPtr
The smart pointer class.
Definition
rc.hpp:119
openvpn::RC
Reference count base class for objects tracked by RCPtr. Disallows copying and assignment.
Definition
rc.hpp:912
OPENVPN_LOG
#define OPENVPN_LOG(args)
Definition
logdatetime.hpp:23
logthread.hpp
openvpn
Definition
ovpncli.cpp:97
rc.hpp
openvpn::ActionThread::Notify
Definition
actionthread.hpp:30
openvpn::ActionThread::Notify::action_thread_finished
virtual void action_thread_finished(const ActionThread *self, bool status)=0
openvpn::Log::Context::Wrapper
Argument to construct a Context in a different thread.
Definition
logdatetime.hpp:35
openvpn::Log::Context
Scoped RAII for the global_log pointer.
Definition
logdatetime.hpp:33
openvpn
common
actionthread.hpp
Generated by
1.9.8