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>
21
22namespace openvpn {
23
24class ActionThread : public RC<thread_safe_refcount>
25{
26 public:
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 {
70 completion_handler = nullptr;
71 if (n)
72 n->action_thread_finished(this, status);
73 }
74
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
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
void stop(const bool halt)
openvpn_io::io_context & io_context
RCPtr< ActionThread > Ptr
ActionThread(openvpn_io::io_context &io_context_arg, const ActionList::Ptr &action_list, Notify *completion_handler_arg)
void completion_post(bool status)
ActionList::Ptr actions
Log::Context::Wrapper logwrap
The smart pointer class.
Definition rc.hpp:119
Reference count base class for objects tracked by RCPtr. Disallows copying and assignment.
Definition rc.hpp:912
#define OPENVPN_LOG(args)
virtual void action_thread_finished(const ActionThread *self, bool status)=0
Argument to construct a Context in a different thread.
Scoped RAII for the global_log pointer.