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.cc | 54 ++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 comp3331/server/src/shared/connection.cc (limited to 'comp3331/server/src/shared/connection.cc') diff --git a/comp3331/server/src/shared/connection.cc b/comp3331/server/src/shared/connection.cc new file mode 100644 index 0000000..1063823 --- /dev/null +++ b/comp3331/server/src/shared/connection.cc @@ -0,0 +1,54 @@ +#include "shared/connection.hh" + +namespace shared { + +void connection::send_packet(packet&& packet) noexcept { + packet.header.sequence = this->seq_num; + shared::send_packet(packet, this->sock, this->info); + ++this->seq_num; + + std::lock_guard guard{*this->lock}; + this->sent.push_back(packet); +} + +bool connection::should_discard_packet(const packet& packet) noexcept { + std::lock_guard guard{*this->lock}; + + // ack case + if (packet.header.command[0] == '\0') { + this->sent.erase(std::remove_if(std::begin(this->sent), + std::end(this->sent), + [&](const auto& p) { + return p.header.sequence <= + packet.header.sequence; + }), + std::end(this->sent)); + return true; + } + + // Send an ack for the packet if it's not an ack itself + auto ack_pkt = shared::contents_to_packet({}, "\0\0\0"); + ack_pkt.header.sequence = packet.header.sequence; + shared::send_packet(ack_pkt, this->sock, this->info); + + if (packet.header.sequence != this->ack_num) { + return true; + } + ++this->ack_num; + return false; +} + +// Reliable transport reads our packets on a different thread and resends if +// necessary. +void connection::do_reliable_transport() noexcept { + while (!*this->should_thread_exit) { + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + std::lock_guard guard{*this->lock}; + for (const auto& packet : this->sent) { + shared::send_packet(packet, this->sock, this->info); + } + } +} + +} // namespace shared -- cgit v1.2.3