diff --git a/cmake-build-debug/.ninja_deps b/cmake-build-debug/.ninja_deps index e051af1..2eef2cc 100644 Binary files a/cmake-build-debug/.ninja_deps and b/cmake-build-debug/.ninja_deps differ diff --git a/cmake-build-debug/.ninja_log b/cmake-build-debug/.ninja_log index 357fa0e..be82ddf 100644 --- a/cmake-build-debug/.ninja_log +++ b/cmake-build-debug/.ninja_log @@ -73,3 +73,7 @@ 1177 1269 1699239246742802185 AssignmentProject b2c28a88f620cb6b 1 1215 1699239290003321502 CMakeFiles/AssignmentProject.dir/src/main.cpp.o 2b6a871f4782c17 1215 1306 1699239290095322610 AssignmentProject b2c28a88f620cb6b +1 332 1699249408404357031 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/filesystem.cpp.o 9cc43f95baec43fd +1 1078 1699249409148366620 CMakeFiles/AssignmentProject.dir/src/main.cpp.o 2b6a871f4782c17 +333 1522 1699249409584372239 libraries/BLT/libBLT.a fff5a821b8ed7db +1522 1615 1699249409684373529 AssignmentProject b2c28a88f620cb6b diff --git a/cmake-build-debug/AssignmentProject b/cmake-build-debug/AssignmentProject index 6fb6bf9..61556ef 100755 Binary files a/cmake-build-debug/AssignmentProject and b/cmake-build-debug/AssignmentProject differ diff --git a/cmake-build-debug/CMakeFiles/AssignmentProject.dir/src/main.cpp.o b/cmake-build-debug/CMakeFiles/AssignmentProject.dir/src/main.cpp.o index 05025e4..7dd8c38 100644 Binary files a/cmake-build-debug/CMakeFiles/AssignmentProject.dir/src/main.cpp.o and b/cmake-build-debug/CMakeFiles/AssignmentProject.dir/src/main.cpp.o differ diff --git a/cmake-build-debug/Testing/Temporary/LastTest.log b/cmake-build-debug/Testing/Temporary/LastTest.log index b8141ae..083ecea 100644 --- a/cmake-build-debug/Testing/Temporary/LastTest.log +++ b/cmake-build-debug/Testing/Temporary/LastTest.log @@ -1,3 +1,3 @@ -Start testing: Nov 05 21:54 EST +Start testing: Nov 06 00:43 EST ---------------------------------------------------------- -End testing: Nov 05 21:54 EST +End testing: Nov 06 00:43 EST diff --git a/libraries/BLT b/libraries/BLT index 288076e..1a72728 160000 --- a/libraries/BLT +++ b/libraries/BLT @@ -1 +1 @@ -Subproject commit 288076ed02c7735cef394d10e923a246ef9a769c +Subproject commit 1a72728aeb9299d168868bb1c79f319031d9d8a1 diff --git a/src/main.cpp b/src/main.cpp index ee17852..2fb7dda 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,63 +35,55 @@ constexpr size_t PACKET_SIZE = 512; +// header: +// 1 byte type +// 0: data send +// 1: data ack +// 2: data resend +// 3: +// 8 bytes send_id +// 8 bytes recv_id + +constexpr size_t HEADER_SIZE = 32; + class packet { - private: - std::uint8_t* buffer = new std::uint8_t[PACKET_SIZE]; + protected: + blt::scoped_buffer buffer; public: + packet(): buffer(PACKET_SIZE) + {} + + explicit packet(size_t size): buffer(size) + {} + size_t used = 0; - packet() = default; + packet(const packet& copy) = default; - packet(const packet& copy) - { - for (size_t i = 0; i < PACKET_SIZE; i++) - buffer[i] = copy[i]; - used = copy.used; - } + packet(packet&& move) noexcept = default; - packet(packet&& move) noexcept - { - delete[] buffer; - buffer = move.buffer; - used = move.used; - move.buffer = nullptr; - } + packet& operator=(const packet& copy) = default; - packet& operator=(const packet& copy) - { - if (© == this) - return *this; - for (size_t i = 0; i < PACKET_SIZE; i++) - buffer[i] = copy[i]; - used = copy.used; - return *this; - } - - packet& operator=(packet&& move) noexcept - { - delete[] buffer; - buffer = move.buffer; - move.buffer = nullptr; - used = move.used; - return *this; - } + packet& operator=(packet&& move) noexcept = default; std::uint8_t* data() { - return buffer; + return buffer.data(); + } + + [[nodiscard]] const std::uint8_t* data() const + { + return buffer.data(); } std::uint8_t& operator[](size_t index) { - BLT_ASSERT(index < PACKET_SIZE); return buffer[index]; } [[nodiscard]] const std::uint8_t& operator[](size_t index) const { - BLT_ASSERT(index < PACKET_SIZE); return buffer[index]; } @@ -100,10 +92,7 @@ class packet return used; } - ~packet() - { - delete[] buffer; - } + ~packet() = default; }; void mangle(packet& packet) @@ -164,7 +153,7 @@ class pipe { // wait until the receiver has received std::unique_lock lock(mutex); - cv.wait(lock, [this]{return !is_written.load(std::memory_order::memory_order_acquire); }); + cv.wait(lock, [this] { return !is_written.load(std::memory_order::memory_order_acquire); }); // we now own the backing memory and can write to it // RAII is fun, we should use it here due to including a return statement predicator pred{[&]() -> void { @@ -190,7 +179,7 @@ class pipe void receive(packet& out) { std::unique_lock lock(mutex); - cv.wait(lock, [this]{return is_written.load(std::memory_order::memory_order_acquire); }); + cv.wait(lock, [this] { return is_written.load(std::memory_order::memory_order_acquire); }); out = packet(buffer); @@ -203,6 +192,31 @@ class pipe } }; +class transceiver +{ + private: + // the pipe to send on + pipe& uplink; + // the pipe to receive on + pipe& downlink; + public: + transceiver(pipe& uplink, pipe& downlink): uplink(uplink), downlink(downlink) + {} + + void send(const packet& p) + { + packet datagram{PACKET_SIZE + HEADER_SIZE}; + std::memcpy(&datagram.data()[HEADER_SIZE], p.data(), PACKET_SIZE); + + uplink.send(datagram); + } +}; + +/** + * Load a file and split it into data segments + * @param path + * @param packets + */ void load_packets(const std::string& path, std::vector& packets) { std::ifstream file(path);