Network: Changed timeout for receiving packets to 100ms
This commit is contained in:
parent
253d3dd3d8
commit
77df82f5d6
|
@ -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];
|
||||
}
|
||||
|
|
|
@ -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 =
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
ASSERT_MSG(IsConnected(), "Tried to get MAC address while not connected");
|
||||
return room_member_impl->mac_address;
|
||||
return MacAddress{};
|
||||
}
|
||||
|
||||
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() {
|
||||
|
|
|
@ -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.
|
||||
|
|
Reference in New Issue