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
// Copyright (C) 2018-2021 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/.

#ifndef PERFDHCP_RECEIVER_H
#define PERFDHCP_RECEIVER_H

#include <perfdhcp/perf_socket.h>
#include <perfdhcp/command_options.h>

#include <dhcp/pkt.h>

#include <queue><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <thread><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <mutex><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <atomic><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace perfdhcp {

/// \brief A receiving DHCP packets class.
///
/// Receiver can be used in two modes: single-thread and multi-thread.
///
/// In single-thread mode the class directly reads packets from socket
/// and returns them to consumer using getPkt method.
///
/// In case of multi-thread mode the class starts a thread in the background.
/// The thread reads the packets and pushes them to pkt_queue_. Then
/// in main thread packets can be consumed from the queue using getPkt
/// method.
class Receiver {
private:
    /// \brief Flag indicating if thread should run (true) or not (false).
    std::atomic_flag run_flag_;

    /// \brief Thread for receiving packets.
    std::unique_ptr<std::thread> recv_thread_;

    /// \brief Queue for passing packets from receiver thread to main thread.
    std::queue<dhcp::PktPtr> pkt_queue_;

    /// \brief Mutex for controlling access to the queue.
    std::mutex pkt_queue_mutex_;

    BasePerfSocket &socket_;

    /// \brief Single- or thread-mode indicator.
    bool single_threaded_;

    uint8_t ip_version_;

public:
    /// \brief Receiver constructor.
    ///
    /// \param socket A socket for receiving packets.
    /// \param single_threaded A flag indicating running mode.
    /// \param ip_version An IP version: 4 or 6
    Receiver(BasePerfSocket &socket, bool single_threaded, uint8_t ip_version) :
        socket_(socket),
        single_threaded_(single_threaded),
        ip_version_(ip_version) {
        run_flag_.clear();
    }

    /// \brief Destructor.
    ~Receiver();

    /// \brief Start a receiving thread in multi-thread mode.
    ///
    /// In single-thread mode it does nothing.
    void start();

    /// \brief Stop a receiving thread in multi-thread mode.
    ///
    /// In single-thread mode it does nothing.
    void stop();

    /// \brief Get DHCP packet.
    ///
    /// In single-thread mode it reads directly from the socket.
    /// In multi-thread mode it reads packets from the queue.
    dhcp::PktPtr getPkt();

private:
    /// \brief Receiving thread main function.
    void run();

    /// \brief Receive packets from sockets and pushes them to the queue.
    ///
    /// It runs in a loop until socket is empty.
    void receivePackets();

    /// \brief Read a packet directly from the socket.
    dhcp::PktPtr readPktFromSocket();
};

}
}

#endif /* PERFDHCP_RECEIVER_H */