mmmm mommy milk

main
Brett 2023-11-06 19:24:40 -05:00
parent f120057562
commit 69576352c4
8 changed files with 76 additions and 90 deletions

Binary file not shown.

View File

@ -28,3 +28,9 @@
1830 1932 1699313070127717082 AssignmentProject e35660c7830bbb12 1830 1932 1699313070127717082 AssignmentProject e35660c7830bbb12
2 1960 1699313077803712045 CMakeFiles/AssignmentProject.dir/src/main.cpp.o 7b18e037a23b35ad 2 1960 1699313077803712045 CMakeFiles/AssignmentProject.dir/src/main.cpp.o 7b18e037a23b35ad
1961 2064 1699313077907711977 AssignmentProject e35660c7830bbb12 1961 2064 1699313077907711977 AssignmentProject e35660c7830bbb12
1 1720 1699313297303548848 CMakeFiles/AssignmentProject.dir/src/main.cpp.o 7b18e037a23b35ad
1720 1818 1699313297407548764 AssignmentProject e35660c7830bbb12
1 1869 1699313337111515909 CMakeFiles/AssignmentProject.dir/src/main.cpp.o 7b18e037a23b35ad
1869 1975 1699313337219515819 AssignmentProject e35660c7830bbb12
1 1853 1699313450731417521 CMakeFiles/AssignmentProject.dir/src/main.cpp.o 7b18e037a23b35ad
1853 1952 1699313450831417432 AssignmentProject e35660c7830bbb12

Binary file not shown.

View File

@ -1,3 +1,3 @@
Start testing: Nov 06 18:24 EST Start testing: Nov 06 18:30 EST
---------------------------------------------------------- ----------------------------------------------------------
End testing: Nov 06 18:24 EST End testing: Nov 06 18:30 EST

View File

@ -0,0 +1,22 @@
This is some really large data that i am going to send to you.
This is some really large data that i am going to send to you 12;
This is some really large data that i am going to send to you 23;
This is some really large data that i am going to send to you 43;
This is some really large data that i am going to send to you 74;
This is some really large data that i am going to send to you 123;
This is some really large data that i am going to send to you 643;
This is some really large data that i am going to send to you 785;
This is some really large data that i am going to send to you 875;
This is some really large data that i am going to send to you 1000;
This is some really large data that i am going to send to you 1200;
This is some really large data that i am going to send to you 1300;
This is some really large data that i am going to send to you 1400;
This is some really large data that i am going to send to you 1500;
This is some really large data that i am going to send to you 1600;
This is some really large data that i am going to send to you 1700;
This is some really large data that i am going to send to you 1800;
This is some really large data that i am going to send to you 1900;
This is some really large data that i am going to send to you 2000;
This is some really large data that i am going to send to you 2100;
This is some really large data that i am going to send to you 2200;
This is some really large data that i am going to send to you 2300;

@ -1 +1 @@
Subproject commit 1a72728aeb9299d168868bb1c79f319031d9d8a1 Subproject commit a555b53a612ebbb5aabee61269f090a2ac06b731

View File

@ -40,19 +40,19 @@
constexpr size_t PACKET_SIZE = 512; constexpr size_t PACKET_SIZE = 512;
// header: // header:
// 1 byte type // 4 bytes for CRC32
// 4 byte type
// 0: data send // 0: data send
// 7: data resend // 7: data resend
// 15: data ack // 15: data ack
// 31: ack ack // 31: ack ack
// 3 unused
// 8 bytes send_id // 8 bytes send_id
// 8 bytes recv_id // 8 bytes recv_id
// 8 bytes unused // 8 bytes unused
// 4 bytes for CRC32
constexpr size_t HEADER_SIZE = 32; constexpr size_t HEADER_SIZE = 32;
bool running = true;
class packet class packet
{ {
protected: protected:
@ -220,16 +220,18 @@ class pipe
class transceiver class transceiver
{ {
private: private:
static constexpr size_t MAX_QUEUE_SIZE = 32;
// the pipe to send on // the pipe to send on
pipe& uplink; pipe& uplink;
// the pipe to receive on // the pipe to receive on
pipe& downlink; pipe& downlink;
size_t sendID = 0; std::mutex send_mutex;
size_t recvID = 0; std::mutex receive_mutex;
bool ack = true; std::condition_variable receive_cv;
bool ackack = false; std::condition_variable send_cv;
packet sendBuffer; std::queue<packet> send_queue;
std::queue<packet> receive_queue;
static DWORD crc(const packet& p) static DWORD crc(const packet& p)
{ {
@ -238,96 +240,48 @@ class transceiver
c = UPDC32(p[i], c); c = UPDC32(p[i], c);
return c; return c;
} }
void request_resend()
{
packet datagram{PACKET_SIZE + HEADER_SIZE};
std::memset(datagram.data(), 0, PACKET_SIZE + HEADER_SIZE);
std::uint32_t type = 2;
blt::mem::toBytes<true>(type, &datagram.data()[PACKET_SIZE]);
blt::mem::toBytes<true>(crc(datagram), &datagram.data()[PACKET_SIZE + 4 + 8 * 3]);
uplink.send(datagram);
}
void resend_packet()
{
uplink.send(sendBuffer);
}
public: public:
transceiver(pipe& uplink, pipe& downlink): uplink(uplink), downlink(downlink) transceiver(pipe& uplink, pipe& downlink): uplink(uplink), downlink(downlink)
{} { virtual_kernel_subsystem_init(); }
void send(const packet& p) void send(const packet& p)
{ {
while (!ack); std::unique_lock lock(send_mutex);
send_cv.wait(lock, [this] { return send_queue.size() < MAX_QUEUE_SIZE; });
packet datagram{PACKET_SIZE + HEADER_SIZE}; predicator pred{[&]() -> void { lock.unlock(); }};
// yes i am putting the header at the end because i don't want to deal with CRC too much
// my brain is actually ḗ̵͙m̷͕̩͛p̸͉͉͒t̷͔̘̕ÿ̸͕̟́̆ right now I think I am making this code worse. Sorry.
// I like writing clean code but there is no more passion for this assignment (blame naser)
std::memset(&datagram.data()[PACKET_SIZE], 0, HEADER_SIZE);
std::memcpy(datagram.data(), p.data(), PACKET_SIZE);
std::uint32_t type = 0; send_queue.push(p);
blt::mem::toBytes<true>(type, &datagram.data()[PACKET_SIZE]);
blt::mem::toBytes<true>(sendID++, &datagram.data()[PACKET_SIZE + 4]);
blt::mem::toBytes<true>(recvID, &datagram.data()[PACKET_SIZE + 12]);
blt::mem::toBytes<true>(crc(datagram), &datagram.data()[PACKET_SIZE + 4 + 8 * 3]);
uplink.send(datagram);
sendBuffer = p;
ack = false;
} }
bool receive(packet& p) bool receive(packet& p)
{ {
downlink.receive(p); std::unique_lock lock(receive_mutex);
receive_cv.wait(lock, [this] { return !receive_queue.empty(); });
std::uint32_t crc; predicator pred{[&]() -> void { lock.unlock(); }};
blt::mem::fromBytes<true>(&p.data()[PACKET_SIZE + 4 + 8 * 3], crc);
DWORD calc_crc = 0; p = receive_queue.front();
for (size_t i = 0; i < p.buffer_size() - 12; i++) receive_queue.pop();
calc_crc = updateCRC32(p[i], calc_crc); }
if (crc != calc_crc) /**
{ * Because the 'TCP' is normally handled by the kernel, which happens outside our process, we need a way of simulating that
BLT_DEBUG("CRC NOT THE SAME %d != %d", crc, calc_crc); * introducing the virtual kernel subsystem to handle all your reliability needs!
request_resend(); */
return false; void virtual_kernel_subsystem_init()
} {
std::thread kernel([this] {
uint32_t type = 0; while (running)
size_t r_sendID = 0; {
size_t r_recvID = 0; bool ack = false;
while (!ack)
blt::mem::fromBytes<true>(&p.data()[PACKET_SIZE], type); {
blt::mem::fromBytes<true>(&p.data()[PACKET_SIZE + 4], r_sendID);
blt::mem::fromBytes<true>(&p.data()[PACKET_SIZE + 12], r_recvID); }
}
BLT_TRACE("%d, %d : %d, %d", sendID, recvID, r_sendID, r_recvID); });
if (type == 7)
{
BLT_DEBUG("We need to resend the packet!");
resend_packet();
return false;
}
if (type == 15)
{
BLT_DEBUG("Message ACK");
ack = true;
return false;
}
recvID++;
return true;
} }
}; };
@ -344,6 +298,10 @@ void load_packets(const std::string& path, std::vector<packet>& packets)
size_t length = file.tellg(); size_t length = file.tellg();
file.seekg(0, std::ifstream::beg); file.seekg(0, std::ifstream::beg);
if (length <= 0)
return;
BLT_INFO("Loading file of length %d", length);
blt::scoped_buffer<std::uint8_t> data(length); blt::scoped_buffer<std::uint8_t> data(length);
file.read(reinterpret_cast<char*>(data.data()), static_cast<long>(length)); file.read(reinterpret_cast<char*>(data.data()), static_cast<long>(length));
@ -408,14 +366,14 @@ int main(int argc, const char** argv)
size_t send = 0; size_t send = 0;
transceiver server(uplink, downlink); transceiver server(uplink, downlink);
while (true) while (running)
{ {
send %= packets.size(); send %= packets.size();
server.send(packets[send++]); server.send(packets[send++]);
} }
}); });
while (true) while (running)
{ {
packet pack; packet pack;
//p.receive(pack); //p.receive(pack);