summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorB3n30 <benediktthomas@gmail.com>2017-07-15 21:24:11 +0200
committerB3n30 <benediktthomas@gmail.com>2017-07-16 21:30:17 +0200
commit77df82f5d66683f4928c4ad37f1deb77b79bb7df (patch)
tree036bb0c3ae09eccfa1e4de78c38083daf4835c85
parentNetwork: Propagate Room closing to connected members (diff)
downloadyuzu-77df82f5d66683f4928c4ad37f1deb77b79bb7df.tar
yuzu-77df82f5d66683f4928c4ad37f1deb77b79bb7df.tar.gz
yuzu-77df82f5d66683f4928c4ad37f1deb77b79bb7df.tar.bz2
yuzu-77df82f5d66683f4928c4ad37f1deb77b79bb7df.tar.lz
yuzu-77df82f5d66683f4928c4ad37f1deb77b79bb7df.tar.xz
yuzu-77df82f5d66683f4928c4ad37f1deb77b79bb7df.tar.zst
yuzu-77df82f5d66683f4928c4ad37f1deb77b79bb7df.zip
-rw-r--r--src/network/packet.h10
-rw-r--r--src/network/room.cpp26
-rw-r--r--src/network/room.h3
-rw-r--r--src/network/room_member.cpp52
-rw-r--r--src/network/room_member.h2
5 files changed, 50 insertions, 43 deletions
diff --git a/src/network/packet.h b/src/network/packet.h
index 026271701..94b351ab1 100644
--- a/src/network/packet.h
+++ b/src/network/packet.h
@@ -115,6 +115,12 @@ private:
template <typename T>
Packet& Packet::operator>>(std::vector<T>& out_data) {
+ // First extract the size
+ u32 size = 0;
+ *this >> size;
+ out_data.resize(size);
+
+ // Then extract the data
for (std::size_t i = 0; i < out_data.size(); ++i) {
T character = 0;
*this >> character;
@@ -135,6 +141,10 @@ Packet& Packet::operator>>(std::array<T, S>& out_data) {
template <typename T>
Packet& Packet::operator<<(const std::vector<T>& in_data) {
+ // First insert the size
+ *this << static_cast<u32>(in_data.size());
+
+ // Then insert the data
for (std::size_t i = 0; i < in_data.size(); ++i) {
*this << in_data[i];
}
diff --git a/src/network/room.cpp b/src/network/room.cpp
index 3f72d7cbe..8b7915bb7 100644
--- a/src/network/room.cpp
+++ b/src/network/room.cpp
@@ -74,7 +74,7 @@ public:
void SendMacCollision(ENetPeer* client);
/**
- * Sends a ID_ROOM_VERSION_MISMATCH message telling the client that the MAC is invalid.
+ * Sends a ID_ROOM_VERSION_MISMATCH message telling the client that the version is invalid.
*/
void SendVersionMismatch(ENetPeer* client);
@@ -139,7 +139,7 @@ public:
void Room::RoomImpl::ServerLoop() {
while (state != State::Closed) {
ENetEvent event;
- if (enet_host_service(server, &event, 1000) > 0) {
+ if (enet_host_service(server, &event, 100) > 0) {
switch (event.type) {
case ENET_EVENT_TYPE_RECEIVE:
switch (event.packet->data[0]) {
@@ -175,7 +175,7 @@ void Room::RoomImpl::StartLoop() {
void Room::RoomImpl::HandleJoinRequest(const ENetEvent* event) {
Packet packet;
packet.Append(event->packet->data, event->packet->dataLength);
- packet.IgnoreBytes(sizeof(MessageID));
+ packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
std::string nickname;
packet >> nickname;
@@ -234,7 +234,7 @@ bool Room::RoomImpl::IsValidMacAddress(const MacAddress& address) const {
void Room::RoomImpl::SendNameCollision(ENetPeer* client) {
Packet packet;
- packet << static_cast<MessageID>(IdNameCollision);
+ packet << static_cast<u8>(IdNameCollision);
ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
@@ -244,7 +244,7 @@ void Room::RoomImpl::SendNameCollision(ENetPeer* client) {
void Room::RoomImpl::SendMacCollision(ENetPeer* client) {
Packet packet;
- packet << static_cast<MessageID>(IdMacCollision);
+ packet << static_cast<u8>(IdMacCollision);
ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
@@ -254,7 +254,7 @@ void Room::RoomImpl::SendMacCollision(ENetPeer* client) {
void Room::RoomImpl::SendVersionMismatch(ENetPeer* client) {
Packet packet;
- packet << static_cast<MessageID>(IdVersionMismatch);
+ packet << static_cast<u8>(IdVersionMismatch);
packet << network_version;
ENetPacket* enet_packet =
@@ -265,7 +265,7 @@ void Room::RoomImpl::SendVersionMismatch(ENetPeer* client) {
void Room::RoomImpl::SendJoinSuccess(ENetPeer* client, MacAddress mac_address) {
Packet packet;
- packet << static_cast<MessageID>(IdJoinSuccess);
+ packet << static_cast<u8>(IdJoinSuccess);
packet << mac_address;
ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
@@ -275,7 +275,7 @@ void Room::RoomImpl::SendJoinSuccess(ENetPeer* client, MacAddress mac_address) {
void Room::RoomImpl::SendCloseMessage() {
Packet packet;
- packet << static_cast<MessageID>(IdCloseRoom);
+ packet << static_cast<u8>(IdCloseRoom);
ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
for (auto& member : members) {
@@ -289,7 +289,7 @@ void Room::RoomImpl::SendCloseMessage() {
void Room::RoomImpl::BroadcastRoomInformation() {
Packet packet;
- packet << static_cast<MessageID>(IdRoomInformation);
+ packet << static_cast<u8>(IdRoomInformation);
packet << room_information.name;
packet << room_information.member_slots;
@@ -321,7 +321,7 @@ MacAddress Room::RoomImpl::GenerateMacAddress() {
void Room::RoomImpl::HandleWifiPacket(const ENetEvent* event) {
Packet in_packet;
in_packet.Append(event->packet->data, event->packet->dataLength);
- in_packet.IgnoreBytes(sizeof(MessageID));
+ in_packet.IgnoreBytes(sizeof(u8)); // Message type
in_packet.IgnoreBytes(sizeof(u8)); // WifiPacket Type
in_packet.IgnoreBytes(sizeof(u8)); // WifiPacket Channel
in_packet.IgnoreBytes(sizeof(MacAddress)); // WifiPacket Transmitter Address
@@ -354,7 +354,7 @@ void Room::RoomImpl::HandleChatPacket(const ENetEvent* event) {
Packet in_packet;
in_packet.Append(event->packet->data, event->packet->dataLength);
- in_packet.IgnoreBytes(sizeof(MessageID));
+ in_packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
std::string message;
in_packet >> message;
auto CompareNetworkAddress = [event](const Member member) -> bool {
@@ -366,7 +366,7 @@ void Room::RoomImpl::HandleChatPacket(const ENetEvent* event) {
}
Packet out_packet;
- out_packet << static_cast<MessageID>(IdChatMessage);
+ out_packet << static_cast<u8>(IdChatMessage);
out_packet << sending_member->nickname;
out_packet << message;
@@ -383,7 +383,7 @@ void Room::RoomImpl::HandleGameNamePacket(const ENetEvent* event) {
Packet in_packet;
in_packet.Append(event->packet->data, event->packet->dataLength);
- in_packet.IgnoreBytes(sizeof(MessageID));
+ in_packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
std::string game_name;
in_packet >> game_name;
auto member =
diff --git a/src/network/room.h b/src/network/room.h
index ffa17599d..54cccf0ae 100644
--- a/src/network/room.h
+++ b/src/network/room.h
@@ -30,8 +30,7 @@ const MacAddress NoPreferredMac = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
constexpr MacAddress BroadcastMac = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// The different types of messages that can be sent. The first byte of each packet defines the type
-using MessageID = u8;
-enum RoomMessageTypes {
+enum RoomMessageTypes : u8 {
IdJoinRequest = 1,
IdJoinSuccess,
IdRoomInformation,
diff --git a/src/network/room_member.cpp b/src/network/room_member.cpp
index 8fd226ba5..dac9bacae 100644
--- a/src/network/room_member.cpp
+++ b/src/network/room_member.cpp
@@ -38,13 +38,15 @@ public:
std::mutex send_list_mutex; ///< Mutex that controls access to the `send_list` variable.
std::list<Packet> send_list; ///< A list that stores all packets to send the async
void MemberLoop();
+
void StartLoop();
/**
* Sends data to the room. It will be send on channel 0 with flag RELIABLE
* @param packet The data to send
*/
- void Send(Packet& packet);
+ void Send(Packet&& packet);
+
/**
* Sends a request to the server, asking for permission to join a room with the specified
* nickname and preferred mac.
@@ -99,11 +101,13 @@ void RoomMember::RoomMemberImpl::MemberLoop() {
while (IsConnected()) {
std::lock_guard<std::mutex> lock(network_mutex);
ENetEvent event;
- if (enet_host_service(client, &event, 1000) > 0) {
+ if (enet_host_service(client, &event, 100) > 0) {
switch (event.type) {
case ENET_EVENT_TYPE_RECEIVE:
switch (event.packet->data[0]) {
- // TODO(B3N30): Handle the other message types
+ case IdWifiPacket:
+ HandleWifiPackets(&event);
+ break;
case IdChatMessage:
HandleChatPacket(&event);
break;
@@ -130,8 +134,6 @@ void RoomMember::RoomMemberImpl::MemberLoop() {
case IdCloseRoom:
SetState(State::LostConnection);
break;
- default:
- break;
}
enet_packet_destroy(event.packet);
break;
@@ -158,7 +160,7 @@ void RoomMember::RoomMemberImpl::StartLoop() {
loop_thread = std::make_unique<std::thread>(&RoomMember::RoomMemberImpl::MemberLoop, this);
}
-void RoomMember::RoomMemberImpl::Send(Packet& packet) {
+void RoomMember::RoomMemberImpl::Send(Packet&& packet) {
std::lock_guard<std::mutex> lock(send_list_mutex);
send_list.push_back(std::move(packet));
}
@@ -166,11 +168,11 @@ void RoomMember::RoomMemberImpl::Send(Packet& packet) {
void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname,
const MacAddress& preferred_mac) {
Packet packet;
- packet << static_cast<MessageID>(IdJoinRequest);
+ packet << static_cast<u8>(IdJoinRequest);
packet << nickname;
packet << preferred_mac;
packet << network_version;
- Send(packet);
+ Send(std::move(packet));
}
void RoomMember::RoomMemberImpl::HandleRoomInformationPacket(const ENetEvent* event) {
@@ -178,7 +180,7 @@ void RoomMember::RoomMemberImpl::HandleRoomInformationPacket(const ENetEvent* ev
packet.Append(event->packet->data, event->packet->dataLength);
// Ignore the first byte, which is the message id.
- packet.IgnoreBytes(sizeof(MessageID));
+ packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
RoomInformation info{};
packet >> info.name;
@@ -203,9 +205,9 @@ void RoomMember::RoomMemberImpl::HandleJoinPacket(const ENetEvent* event) {
packet.Append(event->packet->data, event->packet->dataLength);
// Ignore the first byte, which is the message id.
- packet.IgnoreBytes(sizeof(MessageID));
+ packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
- // Parse the MAC Address from the BitStream
+ // Parse the MAC Address from the packet
packet >> mac_address;
// TODO(B3N30): Invoke callbacks
}
@@ -216,9 +218,9 @@ void RoomMember::RoomMemberImpl::HandleWifiPackets(const ENetEvent* event) {
packet.Append(event->packet->data, event->packet->dataLength);
// Ignore the first byte, which is the message id.
- packet.IgnoreBytes(sizeof(MessageID));
+ packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
- // Parse the WifiPacket from the BitStream
+ // Parse the WifiPacket from the packet
u8 frame_type;
packet >> frame_type;
WifiPacket::PacketType type = static_cast<WifiPacket::PacketType>(frame_type);
@@ -231,10 +233,8 @@ void RoomMember::RoomMemberImpl::HandleWifiPackets(const ENetEvent* event) {
u32 data_length;
packet >> data_length;
- std::vector<u8> data(data_length);
- packet >> data;
+ packet >> wifi_packet.data;
- wifi_packet.data = std::move(data);
// TODO(B3N30): Invoke callbacks
}
@@ -243,7 +243,7 @@ void RoomMember::RoomMemberImpl::HandleChatPacket(const ENetEvent* event) {
packet.Append(event->packet->data, event->packet->dataLength);
// Ignore the first byte, which is the message id.
- packet.IgnoreBytes(sizeof(MessageID));
+ packet.IgnoreBytes(sizeof(u8));
ChatEntry chat_entry{};
packet >> chat_entry.nickname;
@@ -300,9 +300,8 @@ const std::string& RoomMember::GetNickname() const {
}
const MacAddress& RoomMember::GetMacAddress() const {
- if (GetState() == State::Joined)
- return room_member_impl->mac_address;
- return MacAddress{};
+ ASSERT_MSG(IsConnected(), "Tried to get MAC address while not connected");
+ return room_member_impl->mac_address;
}
RoomInformation RoomMember::GetRoomInformation() const {
@@ -351,28 +350,27 @@ bool RoomMember::IsConnected() const {
void RoomMember::SendWifiPacket(const WifiPacket& wifi_packet) {
Packet packet;
- packet << static_cast<MessageID>(IdWifiPacket);
+ packet << static_cast<u8>(IdWifiPacket);
packet << static_cast<u8>(wifi_packet.type);
packet << wifi_packet.channel;
packet << wifi_packet.transmitter_address;
packet << wifi_packet.destination_address;
- packet << static_cast<u32>(wifi_packet.data.size());
packet << wifi_packet.data;
- room_member_impl->Send(packet);
+ room_member_impl->Send(std::move(packet));
}
void RoomMember::SendChatMessage(const std::string& message) {
Packet packet;
- packet << static_cast<MessageID>(IdChatMessage);
+ packet << static_cast<u8>(IdChatMessage);
packet << message;
- room_member_impl->Send(packet);
+ room_member_impl->Send(std::move(packet));
}
void RoomMember::SendGameName(const std::string& game_name) {
Packet packet;
- packet << static_cast<MessageID>(IdSetGameName);
+ packet << static_cast<u8>(IdSetGameName);
packet << game_name;
- room_member_impl->Send(packet);
+ room_member_impl->Send(std::move(packet));
}
void RoomMember::Leave() {
diff --git a/src/network/room_member.h b/src/network/room_member.h
index fce608c82..bc1af3a7e 100644
--- a/src/network/room_member.h
+++ b/src/network/room_member.h
@@ -15,7 +15,7 @@ namespace Network {
/// Information about the received WiFi packets.
/// Acts as our own 802.11 header.
struct WifiPacket {
- enum class PacketType { Beacon, Data, Authentication, AssociationResponse };
+ enum class PacketType : u8 { Beacon, Data, Authentication, AssociationResponse };
PacketType type; ///< The type of 802.11 frame.
std::vector<u8> data; ///< Raw 802.11 frame data, starting at the management frame header
/// for management frames.