From 98cef5e9a772602d42acfcf233838c760424db9a Mon Sep 17 00:00:00 2001 From: Nicolas James Date: Thu, 13 Feb 2025 18:00:17 +1100 Subject: initial commit --- comp3331/server/src/shared/connection.hh | 60 ++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 comp3331/server/src/shared/connection.hh (limited to 'comp3331/server/src/shared/connection.hh') diff --git a/comp3331/server/src/shared/connection.hh b/comp3331/server/src/shared/connection.hh new file mode 100644 index 0000000..d7237cb --- /dev/null +++ b/comp3331/server/src/shared/connection.hh @@ -0,0 +1,60 @@ +#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 -- cgit v1.2.3