diff options
Diffstat (limited to 'src/network')
| -rw-r--r-- | src/network/room.cpp | 63 | ||||
| -rw-r--r-- | src/network/room.h | 1 | ||||
| -rw-r--r-- | src/network/room_member.cpp | 57 | ||||
| -rw-r--r-- | src/network/room_member.h | 35 |
4 files changed, 155 insertions, 1 deletions
diff --git a/src/network/room.cpp b/src/network/room.cpp index 8c63b255b..dc5dbce7f 100644 --- a/src/network/room.cpp +++ b/src/network/room.cpp | |||
| @@ -212,6 +212,12 @@ public: | |||
| 212 | void HandleProxyPacket(const ENetEvent* event); | 212 | void HandleProxyPacket(const ENetEvent* event); |
| 213 | 213 | ||
| 214 | /** | 214 | /** |
| 215 | * Broadcasts this packet to all members except the sender. | ||
| 216 | * @param event The ENet event containing the data | ||
| 217 | */ | ||
| 218 | void HandleLdnPacket(const ENetEvent* event); | ||
| 219 | |||
| 220 | /** | ||
| 215 | * Extracts a chat entry from a received ENet packet and adds it to the chat queue. | 221 | * Extracts a chat entry from a received ENet packet and adds it to the chat queue. |
| 216 | * @param event The ENet event that was received. | 222 | * @param event The ENet event that was received. |
| 217 | */ | 223 | */ |
| @@ -247,6 +253,9 @@ void Room::RoomImpl::ServerLoop() { | |||
| 247 | case IdProxyPacket: | 253 | case IdProxyPacket: |
| 248 | HandleProxyPacket(&event); | 254 | HandleProxyPacket(&event); |
| 249 | break; | 255 | break; |
| 256 | case IdLdnPacket: | ||
| 257 | HandleLdnPacket(&event); | ||
| 258 | break; | ||
| 250 | case IdChatMessage: | 259 | case IdChatMessage: |
| 251 | HandleChatPacket(&event); | 260 | HandleChatPacket(&event); |
| 252 | break; | 261 | break; |
| @@ -861,6 +870,60 @@ void Room::RoomImpl::HandleProxyPacket(const ENetEvent* event) { | |||
| 861 | enet_host_flush(server); | 870 | enet_host_flush(server); |
| 862 | } | 871 | } |
| 863 | 872 | ||
| 873 | void Room::RoomImpl::HandleLdnPacket(const ENetEvent* event) { | ||
| 874 | Packet in_packet; | ||
| 875 | in_packet.Append(event->packet->data, event->packet->dataLength); | ||
| 876 | |||
| 877 | in_packet.IgnoreBytes(sizeof(u8)); // Message type | ||
| 878 | |||
| 879 | in_packet.IgnoreBytes(sizeof(u8)); // LAN packet type | ||
| 880 | in_packet.IgnoreBytes(sizeof(IPv4Address)); // Local IP | ||
| 881 | |||
| 882 | IPv4Address remote_ip; | ||
| 883 | in_packet.Read(remote_ip); // Remote IP | ||
| 884 | |||
| 885 | bool broadcast; | ||
| 886 | in_packet.Read(broadcast); // Broadcast | ||
| 887 | |||
| 888 | Packet out_packet; | ||
| 889 | out_packet.Append(event->packet->data, event->packet->dataLength); | ||
| 890 | ENetPacket* enet_packet = enet_packet_create(out_packet.GetData(), out_packet.GetDataSize(), | ||
| 891 | ENET_PACKET_FLAG_RELIABLE); | ||
| 892 | |||
| 893 | const auto& destination_address = remote_ip; | ||
| 894 | if (broadcast) { // Send the data to everyone except the sender | ||
| 895 | std::lock_guard lock(member_mutex); | ||
| 896 | bool sent_packet = false; | ||
| 897 | for (const auto& member : members) { | ||
| 898 | if (member.peer != event->peer) { | ||
| 899 | sent_packet = true; | ||
| 900 | enet_peer_send(member.peer, 0, enet_packet); | ||
| 901 | } | ||
| 902 | } | ||
| 903 | |||
| 904 | if (!sent_packet) { | ||
| 905 | enet_packet_destroy(enet_packet); | ||
| 906 | } | ||
| 907 | } else { | ||
| 908 | std::lock_guard lock(member_mutex); | ||
| 909 | auto member = std::find_if(members.begin(), members.end(), | ||
| 910 | [destination_address](const Member& member_entry) -> bool { | ||
| 911 | return member_entry.fake_ip == destination_address; | ||
| 912 | }); | ||
| 913 | if (member != members.end()) { | ||
| 914 | enet_peer_send(member->peer, 0, enet_packet); | ||
| 915 | } else { | ||
| 916 | LOG_ERROR(Network, | ||
| 917 | "Attempting to send to unknown IP address: " | ||
| 918 | "{}.{}.{}.{}", | ||
| 919 | destination_address[0], destination_address[1], destination_address[2], | ||
| 920 | destination_address[3]); | ||
| 921 | enet_packet_destroy(enet_packet); | ||
| 922 | } | ||
| 923 | } | ||
| 924 | enet_host_flush(server); | ||
| 925 | } | ||
| 926 | |||
| 864 | void Room::RoomImpl::HandleChatPacket(const ENetEvent* event) { | 927 | void Room::RoomImpl::HandleChatPacket(const ENetEvent* event) { |
| 865 | Packet in_packet; | 928 | Packet in_packet; |
| 866 | in_packet.Append(event->packet->data, event->packet->dataLength); | 929 | in_packet.Append(event->packet->data, event->packet->dataLength); |
diff --git a/src/network/room.h b/src/network/room.h index c2a4b1a70..edbd3ecfb 100644 --- a/src/network/room.h +++ b/src/network/room.h | |||
| @@ -40,6 +40,7 @@ enum RoomMessageTypes : u8 { | |||
| 40 | IdRoomInformation, | 40 | IdRoomInformation, |
| 41 | IdSetGameInfo, | 41 | IdSetGameInfo, |
| 42 | IdProxyPacket, | 42 | IdProxyPacket, |
| 43 | IdLdnPacket, | ||
| 43 | IdChatMessage, | 44 | IdChatMessage, |
| 44 | IdNameCollision, | 45 | IdNameCollision, |
| 45 | IdIpCollision, | 46 | IdIpCollision, |
diff --git a/src/network/room_member.cpp b/src/network/room_member.cpp index 06818af78..b94cb24ad 100644 --- a/src/network/room_member.cpp +++ b/src/network/room_member.cpp | |||
| @@ -58,6 +58,7 @@ public: | |||
| 58 | 58 | ||
| 59 | private: | 59 | private: |
| 60 | CallbackSet<ProxyPacket> callback_set_proxy_packet; | 60 | CallbackSet<ProxyPacket> callback_set_proxy_packet; |
| 61 | CallbackSet<LDNPacket> callback_set_ldn_packet; | ||
| 61 | CallbackSet<ChatEntry> callback_set_chat_messages; | 62 | CallbackSet<ChatEntry> callback_set_chat_messages; |
| 62 | CallbackSet<StatusMessageEntry> callback_set_status_messages; | 63 | CallbackSet<StatusMessageEntry> callback_set_status_messages; |
| 63 | CallbackSet<RoomInformation> callback_set_room_information; | 64 | CallbackSet<RoomInformation> callback_set_room_information; |
| @@ -108,6 +109,12 @@ public: | |||
| 108 | void HandleProxyPackets(const ENetEvent* event); | 109 | void HandleProxyPackets(const ENetEvent* event); |
| 109 | 110 | ||
| 110 | /** | 111 | /** |
| 112 | * Extracts an LdnPacket from a received ENet packet. | ||
| 113 | * @param event The ENet event that was received. | ||
| 114 | */ | ||
| 115 | void HandleLdnPackets(const ENetEvent* event); | ||
| 116 | |||
| 117 | /** | ||
| 111 | * Extracts a chat entry from a received ENet packet and adds it to the chat queue. | 118 | * Extracts a chat entry from a received ENet packet and adds it to the chat queue. |
| 112 | * @param event The ENet event that was received. | 119 | * @param event The ENet event that was received. |
| 113 | */ | 120 | */ |
| @@ -166,6 +173,9 @@ void RoomMember::RoomMemberImpl::MemberLoop() { | |||
| 166 | case IdProxyPacket: | 173 | case IdProxyPacket: |
| 167 | HandleProxyPackets(&event); | 174 | HandleProxyPackets(&event); |
| 168 | break; | 175 | break; |
| 176 | case IdLdnPacket: | ||
| 177 | HandleLdnPackets(&event); | ||
| 178 | break; | ||
| 169 | case IdChatMessage: | 179 | case IdChatMessage: |
| 170 | HandleChatPacket(&event); | 180 | HandleChatPacket(&event); |
| 171 | break; | 181 | break; |
| @@ -372,6 +382,27 @@ void RoomMember::RoomMemberImpl::HandleProxyPackets(const ENetEvent* event) { | |||
| 372 | Invoke<ProxyPacket>(proxy_packet); | 382 | Invoke<ProxyPacket>(proxy_packet); |
| 373 | } | 383 | } |
| 374 | 384 | ||
| 385 | void RoomMember::RoomMemberImpl::HandleLdnPackets(const ENetEvent* event) { | ||
| 386 | LDNPacket ldn_packet{}; | ||
| 387 | Packet packet; | ||
| 388 | packet.Append(event->packet->data, event->packet->dataLength); | ||
| 389 | |||
| 390 | // Ignore the first byte, which is the message id. | ||
| 391 | packet.IgnoreBytes(sizeof(u8)); // Ignore the message type | ||
| 392 | |||
| 393 | u8 packet_type; | ||
| 394 | packet.Read(packet_type); | ||
| 395 | ldn_packet.type = static_cast<LDNPacketType>(packet_type); | ||
| 396 | |||
| 397 | packet.Read(ldn_packet.local_ip); | ||
| 398 | packet.Read(ldn_packet.remote_ip); | ||
| 399 | packet.Read(ldn_packet.broadcast); | ||
| 400 | |||
| 401 | packet.Read(ldn_packet.data); | ||
| 402 | |||
| 403 | Invoke<LDNPacket>(ldn_packet); | ||
| 404 | } | ||
| 405 | |||
| 375 | void RoomMember::RoomMemberImpl::HandleChatPacket(const ENetEvent* event) { | 406 | void RoomMember::RoomMemberImpl::HandleChatPacket(const ENetEvent* event) { |
| 376 | Packet packet; | 407 | Packet packet; |
| 377 | packet.Append(event->packet->data, event->packet->dataLength); | 408 | packet.Append(event->packet->data, event->packet->dataLength); |
| @@ -450,6 +481,11 @@ RoomMember::RoomMemberImpl::CallbackSet<ProxyPacket>& RoomMember::RoomMemberImpl | |||
| 450 | } | 481 | } |
| 451 | 482 | ||
| 452 | template <> | 483 | template <> |
| 484 | RoomMember::RoomMemberImpl::CallbackSet<LDNPacket>& RoomMember::RoomMemberImpl::Callbacks::Get() { | ||
| 485 | return callback_set_ldn_packet; | ||
| 486 | } | ||
| 487 | |||
| 488 | template <> | ||
| 453 | RoomMember::RoomMemberImpl::CallbackSet<RoomMember::State>& | 489 | RoomMember::RoomMemberImpl::CallbackSet<RoomMember::State>& |
| 454 | RoomMember::RoomMemberImpl::Callbacks::Get() { | 490 | RoomMember::RoomMemberImpl::Callbacks::Get() { |
| 455 | return callback_set_state; | 491 | return callback_set_state; |
| @@ -607,6 +643,21 @@ void RoomMember::SendProxyPacket(const ProxyPacket& proxy_packet) { | |||
| 607 | room_member_impl->Send(std::move(packet)); | 643 | room_member_impl->Send(std::move(packet)); |
| 608 | } | 644 | } |
| 609 | 645 | ||
| 646 | void RoomMember::SendLdnPacket(const LDNPacket& ldn_packet) { | ||
| 647 | Packet packet; | ||
| 648 | packet.Write(static_cast<u8>(IdLdnPacket)); | ||
| 649 | |||
| 650 | packet.Write(static_cast<u8>(ldn_packet.type)); | ||
| 651 | |||
| 652 | packet.Write(ldn_packet.local_ip); | ||
| 653 | packet.Write(ldn_packet.remote_ip); | ||
| 654 | packet.Write(ldn_packet.broadcast); | ||
| 655 | |||
| 656 | packet.Write(ldn_packet.data); | ||
| 657 | |||
| 658 | room_member_impl->Send(std::move(packet)); | ||
| 659 | } | ||
| 660 | |||
| 610 | void RoomMember::SendChatMessage(const std::string& message) { | 661 | void RoomMember::SendChatMessage(const std::string& message) { |
| 611 | Packet packet; | 662 | Packet packet; |
| 612 | packet.Write(static_cast<u8>(IdChatMessage)); | 663 | packet.Write(static_cast<u8>(IdChatMessage)); |
| @@ -663,6 +714,11 @@ RoomMember::CallbackHandle<ProxyPacket> RoomMember::BindOnProxyPacketReceived( | |||
| 663 | return room_member_impl->Bind(callback); | 714 | return room_member_impl->Bind(callback); |
| 664 | } | 715 | } |
| 665 | 716 | ||
| 717 | RoomMember::CallbackHandle<LDNPacket> RoomMember::BindOnLdnPacketReceived( | ||
| 718 | std::function<void(const LDNPacket&)> callback) { | ||
| 719 | return room_member_impl->Bind(std::move(callback)); | ||
| 720 | } | ||
| 721 | |||
| 666 | RoomMember::CallbackHandle<RoomInformation> RoomMember::BindOnRoomInformationChanged( | 722 | RoomMember::CallbackHandle<RoomInformation> RoomMember::BindOnRoomInformationChanged( |
| 667 | std::function<void(const RoomInformation&)> callback) { | 723 | std::function<void(const RoomInformation&)> callback) { |
| 668 | return room_member_impl->Bind(callback); | 724 | return room_member_impl->Bind(callback); |
| @@ -699,6 +755,7 @@ void RoomMember::Leave() { | |||
| 699 | } | 755 | } |
| 700 | 756 | ||
| 701 | template void RoomMember::Unbind(CallbackHandle<ProxyPacket>); | 757 | template void RoomMember::Unbind(CallbackHandle<ProxyPacket>); |
| 758 | template void RoomMember::Unbind(CallbackHandle<LDNPacket>); | ||
| 702 | template void RoomMember::Unbind(CallbackHandle<RoomMember::State>); | 759 | template void RoomMember::Unbind(CallbackHandle<RoomMember::State>); |
| 703 | template void RoomMember::Unbind(CallbackHandle<RoomMember::Error>); | 760 | template void RoomMember::Unbind(CallbackHandle<RoomMember::Error>); |
| 704 | template void RoomMember::Unbind(CallbackHandle<RoomInformation>); | 761 | template void RoomMember::Unbind(CallbackHandle<RoomInformation>); |
diff --git a/src/network/room_member.h b/src/network/room_member.h index f578f7f6a..0d6417294 100644 --- a/src/network/room_member.h +++ b/src/network/room_member.h | |||
| @@ -17,7 +17,24 @@ namespace Network { | |||
| 17 | using AnnounceMultiplayerRoom::GameInfo; | 17 | using AnnounceMultiplayerRoom::GameInfo; |
| 18 | using AnnounceMultiplayerRoom::RoomInformation; | 18 | using AnnounceMultiplayerRoom::RoomInformation; |
| 19 | 19 | ||
| 20 | /// Information about the received WiFi packets. | 20 | enum class LDNPacketType : u8 { |
| 21 | Scan, | ||
| 22 | ScanResp, | ||
| 23 | Connect, | ||
| 24 | SyncNetwork, | ||
| 25 | Disconnect, | ||
| 26 | DestroyNetwork, | ||
| 27 | }; | ||
| 28 | |||
| 29 | struct LDNPacket { | ||
| 30 | LDNPacketType type; | ||
| 31 | IPv4Address local_ip; | ||
| 32 | IPv4Address remote_ip; | ||
| 33 | bool broadcast; | ||
| 34 | std::vector<u8> data; | ||
| 35 | }; | ||
| 36 | |||
| 37 | /// Information about the received proxy packets. | ||
| 21 | struct ProxyPacket { | 38 | struct ProxyPacket { |
| 22 | SockAddrIn local_endpoint; | 39 | SockAddrIn local_endpoint; |
| 23 | SockAddrIn remote_endpoint; | 40 | SockAddrIn remote_endpoint; |
| @@ -152,6 +169,12 @@ public: | |||
| 152 | void SendProxyPacket(const ProxyPacket& packet); | 169 | void SendProxyPacket(const ProxyPacket& packet); |
| 153 | 170 | ||
| 154 | /** | 171 | /** |
| 172 | * Sends an LDN packet to the room. | ||
| 173 | * @param packet The WiFi packet to send. | ||
| 174 | */ | ||
| 175 | void SendLdnPacket(const LDNPacket& packet); | ||
| 176 | |||
| 177 | /** | ||
| 155 | * Sends a chat message to the room. | 178 | * Sends a chat message to the room. |
| 156 | * @param message The contents of the message. | 179 | * @param message The contents of the message. |
| 157 | */ | 180 | */ |
| @@ -205,6 +228,16 @@ public: | |||
| 205 | std::function<void(const ProxyPacket&)> callback); | 228 | std::function<void(const ProxyPacket&)> callback); |
| 206 | 229 | ||
| 207 | /** | 230 | /** |
| 231 | * Binds a function to an event that will be triggered every time an LDNPacket is received. | ||
| 232 | * The function wil be called everytime the event is triggered. | ||
| 233 | * The callback function must not bind or unbind a function. Doing so will cause a deadlock | ||
| 234 | * @param callback The function to call | ||
| 235 | * @return A handle used for removing the function from the registered list | ||
| 236 | */ | ||
| 237 | CallbackHandle<LDNPacket> BindOnLdnPacketReceived( | ||
| 238 | std::function<void(const LDNPacket&)> callback); | ||
| 239 | |||
| 240 | /** | ||
| 208 | * Binds a function to an event that will be triggered every time the RoomInformation changes. | 241 | * Binds a function to an event that will be triggered every time the RoomInformation changes. |
| 209 | * The function wil be called every time the event is triggered. | 242 | * The function wil be called every time the event is triggered. |
| 210 | * The callback function must not bind or unbind a function. Doing so will cause a deadlock | 243 | * The callback function must not bind or unbind a function. Doing so will cause a deadlock |