#ifndef SHARED_CONNECTION_HH_ #define SHARED_CONNECTION_HH_ #include #include #include #include #include "shared/net.hh" namespace shared { // The connection class abstracts sending and receiving data, including reliable // transmission over UDP. class connection { private: shared::socket_t sock; sockaddr_in info; private: std::uint32_t seq_num = 0; // track packet sequence number std::uint32_t ack_num = 0; // for reliable transport, spawn a new thread which reads sent/received std::unique_ptr> should_thread_exit; std::unique_ptr lock; std::vector sent; std::vector received; std::shared_ptr reliable_transport_thread; void do_reliable_transport() noexcept; public: connection(const socket_t& sock, sockaddr_in&& info) : sock(sock), info(std::move(info)), should_thread_exit(std::make_unique>(false)), lock(std::make_unique()), reliable_transport_thread(std::make_shared( &connection::do_reliable_transport, this)) {} connection(const connection&) = delete; connection(connection&&) = default; ~connection() noexcept { *this->should_thread_exit = true; this->reliable_transport_thread->join(); } public: const sockaddr_in& get_info() const noexcept { return this->info; } const socket_t& get_socket() const noexcept { return this->sock; } public: // All unreliable packets should go through these functions so we may track // if our packets have been sent or received, making them reliable. void send_packet(packet&& packet) noexcept; bool should_discard_packet(const packet& packet) noexcept; }; } // namespace shared #endif