Program Listing for File message_truncator.hpp

Return to documentation for file (message_truncator.hpp)

#ifndef FELIX_MESSAGE_TRUNCATOR_H_
#define FELIX_MESSAGE_TRUNCATOR_H_

#include <sys/uio.h>

#include <algorithm>
#include <cstdint>
#include <optional>
#include <span>

class MessageTruncator
{
    public:
        struct Settings
        {
            size_t max_msg_by_net{};
            uint32_t max_iov_len{};
        };

        explicit MessageTruncator(Settings settings) : m_settings{settings} {}

        [[nodiscard]] std::uint8_t truncate_msg_if_too_large(std::span<iovec>& iov, size_t bytes, std::uint8_t status,
            std::optional<size_t> max_msg_size = std::nullopt) const;

    private:
        [[nodiscard]] size_t get_max_msg_size(std::optional<size_t> max_msg_size) const {
            return max_msg_size ? std::min(*max_msg_size, m_settings.max_msg_by_net) : m_settings.max_msg_by_net;
        }

        Settings m_settings{};
};

#endif /* FELIX_MESSAGE_TRUNCATOR_H_ */