Program Listing for File netio3_receiver.hpp

Return to documentation for file (network/netio3_receiver.hpp)

#ifndef FELIX_NETIO3_RECEIVER_H_
#define FELIX_NETIO3_RECEIVER_H_

#include "config.hpp"
#include "netio3/NetioPublisher.hpp"
#include "netio3/NetioReceiver.hpp"
#include "netio3/Types.hpp"
#include "netio3_evloop.hpp"
#include "netio3_utility.hpp"
#include "receiver.hpp"
#include "bus.hpp"

class Netio3Receiver : public Receiver {

public:
    template<Netio3EventLoopType EventLoopType>
    Netio3Receiver(const std::string &ip, uint32_t port, NetworkMode network_mode,
        unsigned int netio_pn, unsigned int netio_ps, bool buffered, const std::string& receiver_info,
         Netio3EventLoop::type_tag<EventLoopType> type_tag) :
        m_ip{ip.substr(ip.find(':') + 1)},  // FIXME: Safer way to split string (or make IP and actual IP)
        m_port{static_cast<uint16_t>(port)},
        m_num_buffers{netio_pn},
        m_buffer_size{netio_ps},
        m_own_evloop{type_tag},
        m_evloop(&m_own_evloop.value()),
        m_receiver{netio3::NetioReceiverConfig{
                    network::utility::get_network_type(network_mode),
                    network::utility::get_network_mode(network_mode),
                    netio3::ThreadSafetyModel::UNSAFE},
                m_evloop->get_evloop()},
        m_buffered{buffered},
        m_tcp{network_mode == NetworkMode::tcp}
    {
        m_evloop->start_thread(std::format("rec{}", receiver_info));
        init_listen_socket();
    }

    Netio3Receiver(const std::string &ip, uint32_t port, NetworkMode network_mode,
        unsigned int netio_pn, unsigned int netio_ps, bool buffered, Netio3EventLoop& evloop);

    void set_conn_open_callback(OnConnOpen callback) override {m_on_conn_open = callback;};

    void set_conn_close_callback(OnConnClose callback) override {m_on_conn_close = callback;};

    void set_on_msg_callback(OnMsg callback) override {m_on_msg = callback;};

    int get_number_of_connections() override {return -1;} //TODO: need entries of liste_socket's socket_list.

    [[nodiscard]] uint16_t get_port() const override {return m_port;}

private:
    void init_listen_socket();

    [[nodiscard]] static netio3::SendMethod get_send_method(bool buffered) {
        if (buffered) {
            return netio3::SendMethod::BUFFERED;
        }
        return netio3::SendMethod::SEND;
    }

    std::string m_ip;
    uint16_t m_port{};
    unsigned int m_num_buffers{};
    unsigned int m_buffer_size{};
    std::optional<Netio3EventLoop> m_own_evloop;
    Netio3EventLoop* m_evloop{nullptr};
    netio3::NetioReceiver m_receiver;
    bool m_buffered{};
    bool m_tcp{};

    OnConnOpen m_on_conn_open;
    OnConnClose m_on_conn_close;
    OnMsg m_on_msg;
};

#endif /* FELIX_UNBUFFERED_RECEIVER_H_ */