receiver.h 2.83 KB
Newer Older
1
// Copyright (C) 2018-2019 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6
//
// 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/.

7 8
#ifndef PERFDHCP_RECEIVER_H
#define PERFDHCP_RECEIVER_H
9

10
#include <perfdhcp/perf_socket.h>
Michal Nowikowski's avatar
Michal Nowikowski committed
11
#include <perfdhcp/command_options.h>
12

13
#include <dhcp/pkt.h>
14 15 16

#include <queue>
#include <thread>
17
#include <mutex>
18
#include <atomic>
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

namespace isc {
namespace perfdhcp {

/// \brief A receviving 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 backgroud.
/// 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 {
35
private:
36
    /// \brief Flag indicating if thread should run (true) or not (false).
37
    std::atomic_flag run_flag_;
38 39

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

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

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

48 49
    BasePerfSocket &socket_;

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

53 54
    uint8_t ip_version_;

55 56 57 58
public:
    /// \brief Receiver constructor.
    ///
    /// \param socket A socket for receiving packets.
59 60 61 62 63 64
    /// \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) {
65
        run_flag_.clear();
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    }

    /// \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.
85
    dhcp::PktPtr getPkt();
86 87 88 89 90 91 92 93 94 95 96

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.
97
    dhcp::PktPtr readPktFromSocket();
98 99 100 101
};

}
}
102 103

#endif /* PERFDHCP_RECEIVER_H */