Skip to content

Commit

Permalink
Merge pull request #36 from ucsd-cse125-sp24/refactor/event-packet-type
Browse files Browse the repository at this point in the history
Refactor PacketType for Events
  • Loading branch information
Tyler-Lentz authored Apr 20, 2024
2 parents b2b5e86 + cb4f480 commit a10cce8
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 41 deletions.
14 changes: 2 additions & 12 deletions include/shared/network/packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ enum class PacketType: uint16_t {
ServerAssignEID, ///< Sent by the server after TCP handshake, giving client its EID

// Gameplay
ClientRequestEvent = 2000, ///< Client requesting server to perform specific input
ServerDoEvent, ///< Server telling clients what events have occurred.
Event = 2000, ///< Client requesting server to perform specific input
};

/**
Expand Down Expand Up @@ -129,23 +128,14 @@ struct ServerAssignEIDPacket {
}
};

struct ClientRequestEventPacket {
struct EventPacket {
Event event;

DEF_SERIALIZE(Archive& ar, const unsigned int version) {
ar & event;
}
};

struct ServerDoEventPacket {
Event event;

DEF_SERIALIZE(Archive& ar, const unsigned int version) {
ar & event;
}
};


/**
* A class which wraps around a packet that has yet to be sent across the network.
* Note: this class can only be instantiated as a shared_ptr using the provided friend
Expand Down
4 changes: 1 addition & 3 deletions include/shared/network/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,9 @@ class Session : public std::enable_shared_from_this<Session> {
* Sends an event on the socket after packaging it into
* the packet format.
*
* @param type Type of the event, should either be ServerDoEvent or
* ClientRequestEvent
* @param evt The event object to send
*/
void sendEventAsync(PacketType type, Event evt);
void sendEventAsync(Event evt);

/**
* Get the information associated with this session.
Expand Down
2 changes: 1 addition & 1 deletion src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void Client::processClientInput() {

if (movement.has_value()) {
auto eid = 0;
this->session->sendEventAsync(PacketType::ClientRequestEvent, Event(eid, EventType::MoveRelative, MoveRelativeEvent(eid, movement.value())));
this->session->sendEventAsync(Event(eid, EventType::MoveRelative, MoveRelativeEvent(eid, movement.value())));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ std::chrono::milliseconds Server::doTick() {

// Tell each client the current lobby status
for (const auto& [eid, session]: this->sessions) {
session->sendEventAsync(PacketType::ServerDoEvent, Event(this->world_eid,
session->sendEventAsync(Event(this->world_eid,
EventType::LoadGameState, LoadGameStateEvent(this->state)));
};

break;
case GamePhase::GAME:
for(const auto& [eid, session]: this->sessions) {
session->sendEventAsync(PacketType::ServerDoEvent, Event(this->world_eid, EventType::LoadGameState, LoadGameStateEvent(this->state)));
session->sendEventAsync(Event(this->world_eid, EventType::LoadGameState, LoadGameStateEvent(this->state)));
std::vector<Event> events = session->getEvents();
for(const Event& event: events) {
switch (event.type) {
Expand Down
25 changes: 4 additions & 21 deletions src/shared/network/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ void Session::connectTo(basic_resolver_results<class boost::asio::ip::tcp> endpo

void Session::_handleReceivedPacket(PacketType type, std::string data) {
// First figure out if packet is event or non-event
if (type == PacketType::ClientRequestEvent) {
auto event = deserialize<ClientRequestEventPacket>(data).event;
this->received_events.push_back(event);
} else if (type == PacketType::ServerDoEvent) {
auto event = deserialize<ServerDoEventPacket>(data).event;
if (type == PacketType::Event) {
auto event = deserialize<EventPacket>(data).event;
this->received_events.push_back(event);
} else if (type == PacketType::ServerAssignEID) {
this->info.client_eid = deserialize<ServerAssignEIDPacket>(data).eid;
Expand Down Expand Up @@ -79,24 +76,10 @@ void Session::sendPacketAsync(std::shared_ptr<PackagedPacket> packet) {
});
}

void Session::sendEventAsync(PacketType type, Event evt) {
void Session::sendEventAsync(Event event) {
std::shared_ptr<PackagedPacket> packet = nullptr;

if (type == PacketType::ServerDoEvent) {
packet = PackagedPacket::make_shared(type, ServerDoEventPacket {
.event = evt
});
} else if (type == PacketType::ClientRequestEvent) {
packet = PackagedPacket::make_shared(type, ClientRequestEventPacket {
.event = evt
});
} else {
std::cerr << "Error: non event packet passed into Session::sendEventAsync(). Ignoring"
<< std::endl;
return;
}

this->sendPacketAsync(packet);
this->sendPacketAsync(PackagedPacket::make_shared(PacketType::Event, EventPacket(event)));
}

void Session::_receivePacketAsync() {
Expand Down
4 changes: 2 additions & 2 deletions src/shared/tests/serialize_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ TEST(SerializeTest, SerializePacketEvent) {
state.addPlayerToLobby(1, "Player Name");
Event evt(0, EventType::LoadGameState, LoadGameStateEvent(state));

ServerDoEventPacket packet {.event=evt};
ServerDoEventPacket packet2 = deserialize<ServerDoEventPacket>(serialize(packet));
EventPacket packet {.event=evt};
EventPacket packet2 = deserialize<EventPacket>(serialize(packet));

ASSERT_EQ(packet.event.type, EventType::LoadGameState);
ASSERT_EQ(packet.event.type, packet2.event.type);
Expand Down

0 comments on commit a10cce8

Please sign in to comment.