File: cancelable_handle.hpp

package info (click to toggle)
openvpn3-client 25%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,276 kB
  • sloc: cpp: 190,085; python: 7,218; ansic: 1,866; sh: 1,361; java: 402; lisp: 81; makefile: 17
file content (110 lines) | stat: -rw-r--r-- 3,304 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//    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.
//
//    Copyright (C) 2025- OpenVPN Inc.
//
//    SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//

#include <openvpn/io/io.hpp>
#include <openvpn/win/winerr.hpp>

namespace openvpn {

/**
 * @brief Wrapper for an asynchronous handle supporting cancellation and closure.
 *
 * Automatically manages lifecycle by canceling and closing the handle if not already done.
 */
class CancelableHandle
{
  public:
    /**
     * @brief Constructs with the given I/O context.
     * @param io_context I/O context for asynchronous operations.
     */
    CancelableHandle(openvpn_io::io_context &io_context)
        : handle_(io_context)
    {
    }

    /** @brief Destructor ensures handle cancellation and closure. */
    ~CancelableHandle()
    {
        cancel_and_close();
    }

    /**
     * @brief Checks if the handle's event is already signaled.
     * @throws Exception if event is signaled or abandoned, or on WaitForSingleObject failure.
     */
    void check_is_already_signalled()
    {
        DWORD status = ::WaitForSingleObject(handle_.native_handle(), 0);
        const Win::LastError err;
        switch (status)
        {
        case WAIT_TIMEOUT: // expected status
            break;
        case WAIT_OBJECT_0:
            throw Exception("CancelableHandle: destroy event is already signaled");
        case WAIT_ABANDONED:
            throw Exception("CancelableHandle: destroy event is abandoned");
        default:
            OPENVPN_THROW_EXCEPTION("CancelableHandle: WaitForSingleObject failed: " << err.message());
        }
    }

    /** @brief Cancels and closes the handle if not already closed. */
    void cancel_and_close()
    {
        if (!*is_closed_)
        {
            *is_closed_ = true;
            try
            {
                handle_.cancel();
            }
            catch (const openvpn_io::system_error &)
            {
                // this is fine, handle is likely not yet initialized
            }

            handle_.close();
        }
    }

    /**
     * @brief Assigns a native Windows handle.
     * @param handle The native HANDLE to manage.
     */
    void assign(HANDLE handle)
    {
        handle_.assign(handle);
        is_closed_ = std::make_shared<bool>(false);
    }

    /**
     * @brief Initiates an asynchronous wait on the handle.
     * @param handler Handler executed on completion, unless closed.
     */
    template <typename Handler>
    void async_wait(Handler &&handler)
    {
        handle_.async_wait([is_closed = is_closed_, handler = std::forward<Handler>(handler)](const openvpn_io::error_code &ec)
                           {
            if (!*is_closed)
            {
                handler(ec);
            } });
    }

  private:
    openvpn_io::windows::object_handle handle_;                       ///< Asynchronous Windows object handle.
    std::shared_ptr<bool> is_closed_ = std::make_shared<bool>(false); ///< Indicates if handle is closed.
};

} // namespace openvpn