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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <asiolink/asio_wrapper.h>
#include <asiolink/io_service.h>

#include <unistd.h>             // for some IPC/network system calls<--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <netinet/in.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/shared_ptr.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sys/socket.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace asiolink {

class IOServiceImpl {
    /// @brief Constructors and Destructor.
    ///
    /// @note The copy constructor and the assignment operator are
    /// intentionally defined as private, making this class non-copyable.
    //@{
private:
    IOServiceImpl(const IOService& source);
    IOServiceImpl& operator=(const IOService& source);
public:
    /// @brief The constructor.
    IOServiceImpl() :
        io_service_(),
        work_(new boost::asio::io_service::work(io_service_)) {
    };

    /// @brief The destructor.
    ~IOServiceImpl() = default;
    //@}

    /// @brief Start the underlying event loop.
    ///
    /// This method does not return control to the caller until
    /// the @ref stop() or @ref stopWork() method is called via some handler.
    void run() {
        io_service_.run();
    };

    /// @brief Run the underlying event loop for a single event.
    ///
    /// This method return control to the caller as soon as the
    /// first handler has completed.  (If no handlers are ready when
    /// it is run, it will block until one is.)
    ///
    /// @return The number of handlers that were executed.
    size_t runOne() {
        return (static_cast<size_t>(io_service_.run_one()));
    };

    /// @brief Run the underlying event loop for a ready events.
    ///
    /// This method executes handlers for all ready events and returns.
    /// It will return immediately if there are no ready events.
    ///
    /// @return The number of handlers that were executed.
    size_t poll() {
        return (static_cast<size_t>(io_service_.poll()));
    };

    /// @brief Run the underlying event loop for a ready events.
    ///
    /// This method executes handlers for all ready events and returns.
    /// It will return immediately if there are no ready events.
    ///
    /// @return The number of handlers that were executed.
    size_t pollOne() {
        return (static_cast<size_t>(io_service_.poll_one()));
    };

    /// @brief Stop the underlying event loop.
    ///
    /// This will return the control to the caller of the @ref run() method.
    void stop() {
        io_service_.stop();
    }

    /// @brief Indicates if the IOService has been stopped.
    ///
    /// @return true if the IOService has been stopped, false otherwise.
    bool stopped() const {
        return (io_service_.stopped());
    }

    /// @brief Restarts the IOService in preparation for a subsequent @ref run() invocation.
    void restart() {
        io_service_.reset();
    }

    /// @brief Removes IO service work object to let it finish running
    /// when all handlers have been invoked.
    void stopWork() {
        work_.reset();
    }

    /// @brief Return the native @c io_service object used in this wrapper.
    ///
    /// This is a short term work around to support other Kea modules
    /// that share the same @c io_service with the authoritative server.
    /// It will eventually be removed once the wrapper interface is
    /// generalized.
    boost::asio::io_service& getInternalIOService() {
        return (io_service_);
    }

    /// @brief Post a callback on the IO service.
    ///
    /// @param callback The callback to be run on the IO service.
    void post(const std::function<void ()>& callback) {
        io_service_.post(callback);
    }

private:
    boost::asio::io_service io_service_;
    boost::shared_ptr<boost::asio::io_service::work> work_;
};

IOService::IOService() : io_impl_(new IOServiceImpl()) {
}

IOService::~IOService() {
}

void
IOService::run() {
    io_impl_->run();
}

size_t
IOService::runOne() {
    return (io_impl_->runOne());
}

size_t
IOService::poll() {
    return (io_impl_->poll());
}

size_t
IOService::pollOne() {
    return (io_impl_->pollOne());
}

void
IOService::stop() {
    io_impl_->stop();
}

bool
IOService::stopped() const {
    return (io_impl_->stopped());
}

void
IOService::restart() {
    io_impl_->restart();
}

void
IOService::stopWork() {
    io_impl_->stopWork();
}

boost::asio::io_service&
IOService::getInternalIOService() {
    return (io_impl_->getInternalIOService());
}

void
IOService::post(const std::function<void ()>& callback) {
    return (io_impl_->post(callback));
}

void
IOService::stopAndPoll(bool ignore_errors) {
    stop();
    restart();
    if (ignore_errors) {
        try {
            poll();
        } catch (...) {
            // Ignore all exceptions.
        }
    } else {
        poll();
    }
}

}  // namespace asiolink
}  // namespace isc