Network: Changed timeout for receiving packets to 100ms

This commit is contained in:
B3n30 2017-07-15 21:24:11 +02:00
parent 253d3dd3d8
commit 77df82f5d6
5 changed files with 50 additions and 43 deletions

View file

@ -115,6 +115,12 @@ private:
template <typename T> template <typename T>
Packet& Packet::operator>>(std::vector<T>& out_data) { 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) { for (std::size_t i = 0; i < out_data.size(); ++i) {
T character = 0; T character = 0;
*this >> character; *this >> character;
@ -135,6 +141,10 @@ Packet& Packet::operator>>(std::array<T, S>& out_data) {
template <typename T> template <typename T>
Packet& Packet::operator<<(const std::vector<T>& in_data) { 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) { for (std::size_t i = 0; i < in_data.size(); ++i) {
*this << in_data[i]; *this << in_data[i];
} }

View file

@ -74,7 +74,7 @@ public:
void SendMacCollision(ENetPeer* client); 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); void SendVersionMismatch(ENetPeer* client);
@ -139,7 +139,7 @@ public:
void Room::RoomImpl::ServerLoop() { void Room::RoomImpl::ServerLoop() {
while (state != State::Closed) { while (state != State::Closed) {
ENetEvent event; ENetEvent event;
if (enet_host_service(server, &event, 1000) > 0) { if (enet_host_service(server, &event, 100) > 0) {
switch (event.type) { switch (event.type) {
case ENET_EVENT_TYPE_RECEIVE: case ENET_EVENT_TYPE_RECEIVE:
switch (event.packet->data[0]) { switch (event.packet->data[0]) {
@ -175,7 +175,7 @@ void Room::RoomImpl::StartLoop() {
void Room::RoomImpl::HandleJoinRequest(const ENetEvent* event) { void Room::RoomImpl::HandleJoinRequest(const ENetEvent* event) {
Packet packet; Packet packet;
packet.Append(event->packet->data, event->packet->dataLength); packet.Append(event->packet->data, event->packet->dataLength);
packet.IgnoreBytes(sizeof(MessageID)); packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
std::string nickname; std::string nickname;
packet >> nickname; packet >> nickname;
@ -234,7 +234,7 @@ bool Room::RoomImpl::IsValidMacAddress(const MacAddress& address) const {
void Room::RoomImpl::SendNameCollision(ENetPeer* client) { void Room::RoomImpl::SendNameCollision(ENetPeer* client) {
Packet packet; Packet packet;
packet << static_cast<MessageID>(IdNameCollision); packet << static_cast<u8>(IdNameCollision);
ENetPacket* enet_packet = ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); 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) { void Room::RoomImpl::SendMacCollision(ENetPeer* client) {
Packet packet; Packet packet;
packet << static_cast<MessageID>(IdMacCollision); packet << static_cast<u8>(IdMacCollision);
ENetPacket* enet_packet = ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); 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) { void Room::RoomImpl::SendVersionMismatch(ENetPeer* client) {
Packet packet; Packet packet;
packet << static_cast<MessageID>(IdVersionMismatch); packet << static_cast<u8>(IdVersionMismatch);
packet << network_version; packet << network_version;
ENetPacket* enet_packet = ENetPacket* enet_packet =
@ -265,7 +265,7 @@ void Room::RoomImpl::SendVersionMismatch(ENetPeer* client) {
void Room::RoomImpl::SendJoinSuccess(ENetPeer* client, MacAddress mac_address) { void Room::RoomImpl::SendJoinSuccess(ENetPeer* client, MacAddress mac_address) {
Packet packet; Packet packet;
packet << static_cast<MessageID>(IdJoinSuccess); packet << static_cast<u8>(IdJoinSuccess);
packet << mac_address; packet << mac_address;
ENetPacket* enet_packet = ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); 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() { void Room::RoomImpl::SendCloseMessage() {
Packet packet; Packet packet;
packet << static_cast<MessageID>(IdCloseRoom); packet << static_cast<u8>(IdCloseRoom);
ENetPacket* enet_packet = ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
for (auto& member : members) { for (auto& member : members) {
@ -289,7 +289,7 @@ void Room::RoomImpl::SendCloseMessage() {
void Room::RoomImpl::BroadcastRoomInformation() { void Room::RoomImpl::BroadcastRoomInformation() {
Packet packet; Packet packet;
packet << static_cast<MessageID>(IdRoomInformation); packet << static_cast<u8>(IdRoomInformation);
packet << room_information.name; packet << room_information.name;
packet << room_information.member_slots; packet << room_information.member_slots;
@ -321,7 +321,7 @@ MacAddress Room::RoomImpl::GenerateMacAddress() {
void Room::RoomImpl::HandleWifiPacket(const ENetEvent* event) { void Room::RoomImpl::HandleWifiPacket(const ENetEvent* event) {
Packet in_packet; Packet in_packet;
in_packet.Append(event->packet->data, event->packet->dataLength); 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 Type
in_packet.IgnoreBytes(sizeof(u8)); // WifiPacket Channel in_packet.IgnoreBytes(sizeof(u8)); // WifiPacket Channel
in_packet.IgnoreBytes(sizeof(MacAddress)); // WifiPacket Transmitter Address in_packet.IgnoreBytes(sizeof(MacAddress)); // WifiPacket Transmitter Address
@ -354,7 +354,7 @@ void Room::RoomImpl::HandleChatPacket(const ENetEvent* event) {
Packet in_packet; Packet in_packet;
in_packet.Append(event->packet->data, event->packet->dataLength); 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; std::string message;
in_packet >> message; in_packet >> message;
auto CompareNetworkAddress = [event](const Member member) -> bool { auto CompareNetworkAddress = [event](const Member member) -> bool {
@ -366,7 +366,7 @@ void Room::RoomImpl::HandleChatPacket(const ENetEvent* event) {
} }
Packet out_packet; Packet out_packet;
out_packet << static_cast<MessageID>(IdChatMessage); out_packet << static_cast<u8>(IdChatMessage);
out_packet << sending_member->nickname; out_packet << sending_member->nickname;
out_packet << message; out_packet << message;
@ -383,7 +383,7 @@ void Room::RoomImpl::HandleGameNamePacket(const ENetEvent* event) {
Packet in_packet; Packet in_packet;
in_packet.Append(event->packet->data, event->packet->dataLength); 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; std::string game_name;
in_packet >> game_name; in_packet >> game_name;
auto member = auto member =

View file

@ -30,8 +30,7 @@ const MacAddress NoPreferredMac = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
constexpr MacAddress BroadcastMac = {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 // The different types of messages that can be sent. The first byte of each packet defines the type
using MessageID = u8; enum RoomMessageTypes : u8 {
enum RoomMessageTypes {
IdJoinRequest = 1, IdJoinRequest = 1,
IdJoinSuccess, IdJoinSuccess,
IdRoomInformation, IdRoomInformation,

View file

@ -38,13 +38,15 @@ public:
std::mutex send_list_mutex; ///< Mutex that controls access to the `send_list` variable. 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 std::list<Packet> send_list; ///< A list that stores all packets to send the async
void MemberLoop(); void MemberLoop();
void StartLoop(); void StartLoop();
/** /**
* Sends data to the room. It will be send on channel 0 with flag RELIABLE * Sends data to the room. It will be send on channel 0 with flag RELIABLE
* @param packet The data to send * @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 * Sends a request to the server, asking for permission to join a room with the specified
* nickname and preferred mac. * nickname and preferred mac.
@ -99,11 +101,13 @@ void RoomMember::RoomMemberImpl::MemberLoop() {
while (IsConnected()) { while (IsConnected()) {
std::lock_guard<std::mutex> lock(network_mutex); std::lock_guard<std::mutex> lock(network_mutex);
ENetEvent event; ENetEvent event;
if (enet_host_service(client, &event, 1000) > 0) { if (enet_host_service(client, &event, 100) > 0) {
switch (event.type) { switch (event.type) {
case ENET_EVENT_TYPE_RECEIVE: case ENET_EVENT_TYPE_RECEIVE:
switch (event.packet->data[0]) { switch (event.packet->data[0]) {
// TODO(B3N30): Handle the other message types case IdWifiPacket:
HandleWifiPackets(&event);
break;
case IdChatMessage: case IdChatMessage:
HandleChatPacket(&event); HandleChatPacket(&event);
break; break;
@ -130,8 +134,6 @@ void RoomMember::RoomMemberImpl::MemberLoop() {
case IdCloseRoom: case IdCloseRoom:
SetState(State::LostConnection); SetState(State::LostConnection);
break; break;
default:
break;
} }
enet_packet_destroy(event.packet); enet_packet_destroy(event.packet);
break; break;
@ -158,7 +160,7 @@ void RoomMember::RoomMemberImpl::StartLoop() {
loop_thread = std::make_unique<std::thread>(&RoomMember::RoomMemberImpl::MemberLoop, this); 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); std::lock_guard<std::mutex> lock(send_list_mutex);
send_list.push_back(std::move(packet)); 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, void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname,
const MacAddress& preferred_mac) { const MacAddress& preferred_mac) {
Packet packet; Packet packet;
packet << static_cast<MessageID>(IdJoinRequest); packet << static_cast<u8>(IdJoinRequest);
packet << nickname; packet << nickname;
packet << preferred_mac; packet << preferred_mac;
packet << network_version; packet << network_version;
Send(packet); Send(std::move(packet));
} }
void RoomMember::RoomMemberImpl::HandleRoomInformationPacket(const ENetEvent* event) { 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); packet.Append(event->packet->data, event->packet->dataLength);
// Ignore the first byte, which is the message id. // Ignore the first byte, which is the message id.
packet.IgnoreBytes(sizeof(MessageID)); packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
RoomInformation info{}; RoomInformation info{};
packet >> info.name; packet >> info.name;
@ -203,9 +205,9 @@ void RoomMember::RoomMemberImpl::HandleJoinPacket(const ENetEvent* event) {
packet.Append(event->packet->data, event->packet->dataLength); packet.Append(event->packet->data, event->packet->dataLength);
// Ignore the first byte, which is the message id. // 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; packet >> mac_address;
// TODO(B3N30): Invoke callbacks // TODO(B3N30): Invoke callbacks
} }
@ -216,9 +218,9 @@ void RoomMember::RoomMemberImpl::HandleWifiPackets(const ENetEvent* event) {
packet.Append(event->packet->data, event->packet->dataLength); packet.Append(event->packet->data, event->packet->dataLength);
// Ignore the first byte, which is the message id. // 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; u8 frame_type;
packet >> frame_type; packet >> frame_type;
WifiPacket::PacketType type = static_cast<WifiPacket::PacketType>(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; u32 data_length;
packet >> data_length; packet >> data_length;
std::vector<u8> data(data_length); packet >> wifi_packet.data;
packet >> data;
wifi_packet.data = std::move(data);
// TODO(B3N30): Invoke callbacks // TODO(B3N30): Invoke callbacks
} }
@ -243,7 +243,7 @@ void RoomMember::RoomMemberImpl::HandleChatPacket(const ENetEvent* event) {
packet.Append(event->packet->data, event->packet->dataLength); packet.Append(event->packet->data, event->packet->dataLength);
// Ignore the first byte, which is the message id. // Ignore the first byte, which is the message id.
packet.IgnoreBytes(sizeof(MessageID)); packet.IgnoreBytes(sizeof(u8));
ChatEntry chat_entry{}; ChatEntry chat_entry{};
packet >> chat_entry.nickname; packet >> chat_entry.nickname;
@ -300,9 +300,8 @@ const std::string& RoomMember::GetNickname() const {
} }
const MacAddress& RoomMember::GetMacAddress() const { const MacAddress& RoomMember::GetMacAddress() const {
if (GetState() == State::Joined) ASSERT_MSG(IsConnected(), "Tried to get MAC address while not connected");
return room_member_impl->mac_address; return room_member_impl->mac_address;
return MacAddress{};
} }
RoomInformation RoomMember::GetRoomInformation() const { RoomInformation RoomMember::GetRoomInformation() const {
@ -351,28 +350,27 @@ bool RoomMember::IsConnected() const {
void RoomMember::SendWifiPacket(const WifiPacket& wifi_packet) { void RoomMember::SendWifiPacket(const WifiPacket& wifi_packet) {
Packet packet; Packet packet;
packet << static_cast<MessageID>(IdWifiPacket); packet << static_cast<u8>(IdWifiPacket);
packet << static_cast<u8>(wifi_packet.type); packet << static_cast<u8>(wifi_packet.type);
packet << wifi_packet.channel; packet << wifi_packet.channel;
packet << wifi_packet.transmitter_address; packet << wifi_packet.transmitter_address;
packet << wifi_packet.destination_address; packet << wifi_packet.destination_address;
packet << static_cast<u32>(wifi_packet.data.size());
packet << wifi_packet.data; packet << wifi_packet.data;
room_member_impl->Send(packet); room_member_impl->Send(std::move(packet));
} }
void RoomMember::SendChatMessage(const std::string& message) { void RoomMember::SendChatMessage(const std::string& message) {
Packet packet; Packet packet;
packet << static_cast<MessageID>(IdChatMessage); packet << static_cast<u8>(IdChatMessage);
packet << message; packet << message;
room_member_impl->Send(packet); room_member_impl->Send(std::move(packet));
} }
void RoomMember::SendGameName(const std::string& game_name) { void RoomMember::SendGameName(const std::string& game_name) {
Packet packet; Packet packet;
packet << static_cast<MessageID>(IdSetGameName); packet << static_cast<u8>(IdSetGameName);
packet << game_name; packet << game_name;
room_member_impl->Send(packet); room_member_impl->Send(std::move(packet));
} }
void RoomMember::Leave() { void RoomMember::Leave() {

View file

@ -15,7 +15,7 @@ namespace Network {
/// Information about the received WiFi packets. /// Information about the received WiFi packets.
/// Acts as our own 802.11 header. /// Acts as our own 802.11 header.
struct WifiPacket { 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. PacketType type; ///< The type of 802.11 frame.
std::vector<u8> data; ///< Raw 802.11 frame data, starting at the management frame header std::vector<u8> data; ///< Raw 802.11 frame data, starting at the management frame header
/// for management frames. /// for management frames.