Skip to content

Commit

Permalink
Generated from commit eab333628bcfd903fee1615aebe586a3c7b51407
Browse files Browse the repository at this point in the history
  • Loading branch information
Pusnow committed May 14, 2024
1 parent 755314e commit 019960d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 55 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.11)

project(e VERSION 3.3.5)
project(e VERSION 3.3.3)

# Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24:
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
Expand Down
1 change: 0 additions & 1 deletion include/E/E_System.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class System : private Log {
timerQueue;
std::unordered_map<UUID, TimerContainer> activeTimer;
std::unordered_set<UUID> activeUUID;
UUID currentUUID = 0;

UUID allocateUUID();
bool deallocateUUID(UUID uuid);
Expand Down
15 changes: 6 additions & 9 deletions include/E/Networking/E_Packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class NetworkSystem;
*/
class Packet : public Module::MessageBase {
private:
Packet(UUID uuid, size_t size);
Packet(UUID uuid, size_t maxSize);
std::vector<char> buffer;
size_t bufferSize;
size_t dataSize;

UUID packetID;

Expand Down Expand Up @@ -59,9 +61,9 @@ class Packet : public Module::MessageBase {
Packet &operator=(Packet &&other) noexcept;

/**
* @param size Packet size.
* @param maxSize Maximum packet size.
*/
Packet(size_t size);
Packet(size_t maxSize);

~Packet() override;

Expand Down Expand Up @@ -90,7 +92,7 @@ class Packet : public Module::MessageBase {

/**
* @brief Change the size of this Packet
* The size can be larger than the internal buffer.
* The size cannot be larger than the internal buffer.
* @param size New size.
* @return Actual changed size.
*/
Expand All @@ -101,11 +103,6 @@ class Packet : public Module::MessageBase {
*/
size_t getSize() const;

/**
* @return Packet UUID.
*/
UUID getUUID() const;

void clearContext();

friend class NetworkSystem;
Expand Down
19 changes: 19 additions & 0 deletions include/E/Networking/TCP/E_CCModule.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef E_CCModule_HPP_
#define E_CCModule_HPP_

#include <E/Networking/E_Packet.hpp>

namespace E {

constexpr int TCP_MSS = 512;


class CCModule {
public:
virtual int packetSent(Packet &&packet) { return TCP_MSS; }
virtual int packetArrived(Packet &&packet) { return TCP_MSS; }
virtual int packetTimeout(Packet &&packet) { return TCP_MSS; }
};

} // namespace E
#endif
18 changes: 10 additions & 8 deletions src/E/E_System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ UUID System::sendMessage(const ModuleID from, const ModuleID to,
return uuid;
}
UUID System::allocateUUID() {

UUID candidate = ++currentUUID;
if (activeUUID.find(candidate) == activeUUID.end()) {
activeUUID.insert(candidate);
return candidate;
} else {
assert(0);
}
UUID startID = currentID;
do {
UUID candidate = currentID++;
if (activeUUID.find(candidate) == activeUUID.end()) {
activeUUID.insert(candidate);
return candidate;
}
} while (startID != currentID);
assert(0);
return 0;
}

bool System::deallocateUUID(UUID candidate) {
Expand Down
36 changes: 21 additions & 15 deletions src/Networking/E_Packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,45 +27,53 @@ UUID Packet::allocatePacketUUID() {
}
void Packet::freePacketUUID(UUID uuid) { packetUUIDSet.erase(uuid); }

Packet::Packet(UUID uuid, size_t size) : buffer(size), packetID(uuid) {
Packet::Packet(UUID uuid, size_t maxSize)
: buffer(maxSize), bufferSize(maxSize), dataSize(maxSize), packetID(uuid) {

std::fill(this->buffer.begin(), this->buffer.end(), 0);
}

Packet::Packet(const Packet &other)
: buffer(other.buffer), packetID(other.packetID) {}
: buffer(other.buffer), bufferSize(other.bufferSize),
dataSize(other.dataSize), packetID(other.packetID) {}

Packet::Packet(Packet &&other) noexcept
: buffer(std::move(other.buffer)), packetID(other.packetID) {
other.buffer.clear();
: buffer(std::move(other.buffer)), bufferSize(other.bufferSize),
dataSize(other.dataSize), packetID(other.packetID) {
other.dataSize = 0;
}

Packet &Packet::operator=(const Packet &other) {
buffer = other.buffer;
bufferSize = other.bufferSize;
dataSize = other.dataSize;
packetID = other.packetID;
return *this;
}

Packet &Packet::operator=(Packet &&other) noexcept {
buffer = std::move(other.buffer);
bufferSize = std::move(other.bufferSize);
dataSize = std::move(other.dataSize);
packetID = std::move(other.packetID);
return *this;
}

Packet::Packet(size_t size) : Packet(allocatePacketUUID(), size) {}
Packet::Packet(size_t maxSize) : Packet(allocatePacketUUID(), maxSize) {}

Packet::~Packet() { freePacketUUID(this->packetID); }

Packet Packet::clone() const {

Packet pkt(this->buffer.size());
Packet pkt(this->bufferSize);
pkt.setSize(this->dataSize);
pkt.buffer = this->buffer;
return pkt;
}

size_t Packet::writeData(size_t offset, const void *data, size_t length) {
size_t actual_offset = std::min(offset, buffer.size());
size_t actual_write = std::min(length, buffer.size() - actual_offset);
size_t actual_offset = std::min(offset, dataSize);
size_t actual_write = std::min(length, dataSize - actual_offset);

if (actual_write == 0)
return 0;
Expand All @@ -75,8 +83,8 @@ size_t Packet::writeData(size_t offset, const void *data, size_t length) {
return actual_write;
}
size_t Packet::readData(size_t offset, void *data, size_t length) const {
size_t actual_offset = std::min(offset, buffer.size());
size_t actual_read = std::min(length, buffer.size() - actual_offset);
size_t actual_offset = std::min(offset, dataSize);
size_t actual_read = std::min(length, dataSize - actual_offset);

if (actual_read == 0)
return 0;
Expand All @@ -86,12 +94,10 @@ size_t Packet::readData(size_t offset, void *data, size_t length) const {
return actual_read;
}
size_t Packet::setSize(size_t size) {
buffer.resize(size);
return buffer.size();
this->dataSize = std::min(size, this->bufferSize);
return this->dataSize;
}
size_t Packet::getSize() const { return buffer.size(); }

UUID Packet::getUUID() const { return this->packetID; }
size_t Packet::getSize() const { return this->dataSize; }

void Packet::clearContext() {}

Expand Down
21 changes: 0 additions & 21 deletions src/Networking/Ethernet/E_Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,12 @@ void Ethernet::packetArrived(std::string fromModule, Packet &&packet) {
packet.readData(26, src_ip.data(), 4);
int port = this->getRoutingTable(src_ip);
auto src = this->getMACAddr(port);

if (!src.has_value()) {
printf("Unrecognized port: %d. Packet[%ld] is dropped.\n", port,
packet.getUUID());
return;
}

packet.writeData(0, mac_broadcast.data(), 6);
packet.writeData(6, src.value().data(), 6);
} else {
int port = this->getRoutingTable(dst_ip);
auto src = this->getMACAddr(port);
auto dst = this->getARPTable(dst_ip);

if (!src.has_value()) {
printf("Unrecognized port: %d. Packet[%ld] is dropped.\n", port,
packet.getUUID());
return;
}

if (!dst.has_value()) {
printf(
"Destination unreachable: %d.%d.%d.%d. Packet[%ld] is dropped.\n",
dst_ip[0], dst_ip[1], dst_ip[2], dst_ip[3], packet.getUUID());
return;
}

packet.writeData(0, dst.value().data(), 6);
packet.writeData(6, src.value().data(), 6);
}
Expand Down

0 comments on commit 019960d

Please sign in to comment.