Commit e65ddf63 authored by Filip Hauzvic's avatar Filip Hauzvic
Browse files

Initialize Net Driver & Index

parent 019a111e
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
#pragma once
#include <com/context.hpp>
#include <net/net_driver.hpp>

namespace net
{
    struct Index final
    {
        static Index const &instance();

        com::Folder* root() const { return m_root; }
        NetDriver* driver() const { return m_net_driver; }

      private:
        Index();
        Index(Index const &) = delete;
        Index(Index &&) = delete;
        Index &operator=(Index const &) const = delete;
        Index &operator=(Index &&) const = delete;

        com::Folder* m_root;
        NetDriver* m_net_driver;
    };

    inline Index const &index() { return Index::instance(); }

    inline com::Folder* root() { return index().root(); }
    inline NetDriver* driver() { return index().driver(); }

}
+48 −0
Original line number Diff line number Diff line
#pragma once
#include <com/runner.hpp>
#include <net/network_types.hpp>
#include <net/socket/isocket.hpp>


#include <span>


namespace net
{
    struct NetDriver final : public com::Runner
    {
        static inline std::string self_name() { return "net_driver"; }

        NetDriver();
        ~NetDriver() override;

        void next_round() override;

        bool start_server(int port, int max_connections);
        bool start_client(std::string address, int port);
        SendResult send(
            ConnectionId peer, std::span<const uint8_t> data,
            ReliabilityMode reliability = ReliabilityMode::UNRELIABLE,
            OrderingMode ordering = OrderingMode::UNORDERED,
            int channel = 0);
        SendResult broadcast(
            std::span<const uint8_t> data,
            ReliabilityMode reliability = ReliabilityMode::UNRELIABLE,
            OrderingMode ordering = OrderingMode::UNORDERED,
            int channel = 0);

        void register_on_connected(const std::function<void()>& callback);
        void register_on_disconnected(const std::function<void()>& callback);
        void register_on_peer_connected(const std::function<void(ConnectionId)>& callback);
        void register_on_peer_disconnected(std::function<void(ConnectionId)> callback);

    private:
        void initialize() override;

        void add_connection(ConnectionId connection);
        void remove_connection(ConnectionId connection);

        std::unique_ptr<ISocket> socket;
        std::unordered_map<ConnectionId, Connection> connections;
    };
}
+10 −0
Original line number Diff line number Diff line
@@ -5,6 +5,16 @@ namespace net
{
    typedef uint32_t ConnectionId;

    struct Connection
    {
        ConnectionId id;
        bool active = false;
        double rtt = 0.0;

        Connection() = default;
        explicit Connection(const ConnectionId id) : id(id) {}
    };

    struct NetworkMessage
    {
        ConnectionId sender;

net/src/index.cpp

0 → 100644
+18 −0
Original line number Diff line number Diff line
#include <net/index.hpp>

namespace net {

    Index const &Index::instance() {
        static Index const idx;
        return idx;
    }

    Index::Index()
        : m_root { com::Folder::root()->find<com::Folder>("net") }
        , m_net_driver { m_root->find<NetDriver>(NetDriver::self_name()) }
    {
        ASSUMPTION(
            m_net_driver != nullptr
            );
    }
}
 No newline at end of file
+4 −0
Original line number Diff line number Diff line
#include <net/module.hpp>
#include <com/context.hpp>
#include <net/net_driver.hpp>

void net::boot(com::Folder *ctx_root)
{
	auto* const root_net = ctx_root->push_back<com::Folder>(folder_name());
	root_net->push_back<NetDriver>();

}

void net::shutdown(com::Folder *ctx_root)
{
	auto* const root_net = ctx_root->find<com::Folder>(folder_name());
	root_net->erase(NetDriver::self_name());

	ctx_root->erase(root_net);
}
Loading