diff options
| author | 2017-07-14 09:20:39 +0200 | |
|---|---|---|
| committer | 2017-07-16 21:30:04 +0200 | |
| commit | a0626221a52056669c0b6d19b37d4189c1671fb7 (patch) | |
| tree | 13b05ebe3fe162c1d90f79dc38c9e7129958bc2e /src/network/room_member.cpp | |
| parent | Network: Send the game title (diff) | |
| download | yuzu-a0626221a52056669c0b6d19b37d4189c1671fb7.tar.gz yuzu-a0626221a52056669c0b6d19b37d4189c1671fb7.tar.xz yuzu-a0626221a52056669c0b6d19b37d4189c1671fb7.zip | |
Network: Made send async in RoomMember
Diffstat (limited to '')
| -rw-r--r-- | src/network/room_member.cpp | 53 |
1 files changed, 33 insertions, 20 deletions
diff --git a/src/network/room_member.cpp b/src/network/room_member.cpp index ec67aa5be..f6f8b0475 100644 --- a/src/network/room_member.cpp +++ b/src/network/room_member.cpp | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <atomic> | 5 | #include <atomic> |
| 6 | #include <list> | ||
| 6 | #include <mutex> | 7 | #include <mutex> |
| 7 | #include <thread> | 8 | #include <thread> |
| 8 | #include "common/assert.h" | 9 | #include "common/assert.h" |
| @@ -33,8 +34,10 @@ public: | |||
| 33 | 34 | ||
| 34 | std::mutex network_mutex; ///< Mutex that controls access to the `client` variable. | 35 | std::mutex network_mutex; ///< Mutex that controls access to the `client` variable. |
| 35 | /// Thread that receives and dispatches network packets | 36 | /// Thread that receives and dispatches network packets |
| 36 | std::unique_ptr<std::thread> receive_thread; | 37 | std::unique_ptr<std::thread> loop_thread; |
| 37 | void ReceiveLoop(); | 38 | std::mutex send_list_mutex; ///< Mutex that controls access to the `send_list` variable. |
| 39 | std::list<Packet> send_list; ///< A list that stores all packets to send the async | ||
| 40 | void MemberLoop(); | ||
| 38 | void StartLoop(); | 41 | void StartLoop(); |
| 39 | 42 | ||
| 40 | /** | 43 | /** |
| @@ -91,7 +94,7 @@ bool RoomMember::RoomMemberImpl::IsConnected() const { | |||
| 91 | return state == State::Joining || state == State::Joined; | 94 | return state == State::Joining || state == State::Joined; |
| 92 | } | 95 | } |
| 93 | 96 | ||
| 94 | void RoomMember::RoomMemberImpl::ReceiveLoop() { | 97 | void RoomMember::RoomMemberImpl::MemberLoop() { |
| 95 | // Receive packets while the connection is open | 98 | // Receive packets while the connection is open |
| 96 | while (IsConnected()) { | 99 | while (IsConnected()) { |
| 97 | std::lock_guard<std::mutex> lock(network_mutex); | 100 | std::lock_guard<std::mutex> lock(network_mutex); |
| @@ -121,6 +124,9 @@ void RoomMember::RoomMemberImpl::ReceiveLoop() { | |||
| 121 | case IdMacCollision: | 124 | case IdMacCollision: |
| 122 | SetState(State::MacCollision); | 125 | SetState(State::MacCollision); |
| 123 | break; | 126 | break; |
| 127 | case IdVersionMismatch: | ||
| 128 | SetState(State::WrongVersion); | ||
| 129 | break; | ||
| 124 | default: | 130 | default: |
| 125 | break; | 131 | break; |
| 126 | } | 132 | } |
| @@ -128,22 +134,30 @@ void RoomMember::RoomMemberImpl::ReceiveLoop() { | |||
| 128 | break; | 134 | break; |
| 129 | case ENET_EVENT_TYPE_DISCONNECT: | 135 | case ENET_EVENT_TYPE_DISCONNECT: |
| 130 | SetState(State::LostConnection); | 136 | SetState(State::LostConnection); |
| 137 | break; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | { | ||
| 141 | std::lock_guard<std::mutex> lock(send_list_mutex); | ||
| 142 | for (const auto& packet : send_list) { | ||
| 143 | ENetPacket* enetPacket = enet_packet_create(packet.GetData(), packet.GetDataSize(), | ||
| 144 | ENET_PACKET_FLAG_RELIABLE); | ||
| 145 | enet_peer_send(server, 0, enetPacket); | ||
| 131 | } | 146 | } |
| 147 | enet_host_flush(client); | ||
| 148 | send_list.clear(); | ||
| 132 | } | 149 | } |
| 133 | } | 150 | } |
| 134 | Disconnect(); | 151 | Disconnect(); |
| 135 | }; | 152 | }; |
| 136 | 153 | ||
| 137 | void RoomMember::RoomMemberImpl::StartLoop() { | 154 | void RoomMember::RoomMemberImpl::StartLoop() { |
| 138 | receive_thread = std::make_unique<std::thread>(&RoomMember::RoomMemberImpl::ReceiveLoop, this); | 155 | loop_thread = std::make_unique<std::thread>(&RoomMember::RoomMemberImpl::MemberLoop, this); |
| 139 | } | 156 | } |
| 140 | 157 | ||
| 141 | void RoomMember::RoomMemberImpl::Send(Packet& packet) { | 158 | void RoomMember::RoomMemberImpl::Send(Packet& packet) { |
| 142 | std::lock_guard<std::mutex> lock(network_mutex); | 159 | std::lock_guard<std::mutex> lock(send_list_mutex); |
| 143 | ENetPacket* enetPacket = | 160 | send_list.push_back(std::move(packet)); |
| 144 | enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); | ||
| 145 | enet_peer_send(server, 0, enetPacket); | ||
| 146 | enet_host_flush(client); | ||
| 147 | } | 161 | } |
| 148 | 162 | ||
| 149 | void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname, | 163 | void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname, |
| @@ -152,6 +166,7 @@ void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname, | |||
| 152 | packet << static_cast<MessageID>(IdJoinRequest); | 166 | packet << static_cast<MessageID>(IdJoinRequest); |
| 153 | packet << nickname; | 167 | packet << nickname; |
| 154 | packet << preferred_mac; | 168 | packet << preferred_mac; |
| 169 | packet << network_version; | ||
| 155 | Send(packet); | 170 | Send(packet); |
| 156 | } | 171 | } |
| 157 | 172 | ||
| @@ -238,11 +253,9 @@ void RoomMember::RoomMemberImpl::Disconnect() { | |||
| 238 | room_information.member_slots = 0; | 253 | room_information.member_slots = 0; |
| 239 | room_information.name.clear(); | 254 | room_information.name.clear(); |
| 240 | 255 | ||
| 241 | if (server) { | 256 | if (!server) |
| 242 | enet_peer_disconnect(server, 0); | ||
| 243 | } else { | ||
| 244 | return; | 257 | return; |
| 245 | } | 258 | enet_peer_disconnect(server, 0); |
| 246 | 259 | ||
| 247 | ENetEvent event; | 260 | ENetEvent event; |
| 248 | while (enet_host_service(client, &event, ConnectionTimeoutMs) > 0) { | 261 | while (enet_host_service(client, &event, ConnectionTimeoutMs) > 0) { |
| @@ -296,14 +309,14 @@ RoomInformation RoomMember::GetRoomInformation() const { | |||
| 296 | void RoomMember::Join(const std::string& nick, const char* server_addr, u16 server_port, | 309 | void RoomMember::Join(const std::string& nick, const char* server_addr, u16 server_port, |
| 297 | u16 client_port) { | 310 | u16 client_port) { |
| 298 | // If the member is connected, kill the connection first | 311 | // If the member is connected, kill the connection first |
| 299 | if (room_member_impl->receive_thread && room_member_impl->receive_thread->joinable()) { | 312 | if (room_member_impl->loop_thread && room_member_impl->loop_thread->joinable()) { |
| 300 | room_member_impl->SetState(State::Error); | 313 | room_member_impl->SetState(State::Error); |
| 301 | room_member_impl->receive_thread->join(); | 314 | room_member_impl->loop_thread->join(); |
| 302 | room_member_impl->receive_thread.reset(); | 315 | room_member_impl->loop_thread.reset(); |
| 303 | } | 316 | } |
| 304 | // If the thread isn't running but the ptr still exists, reset it | 317 | // If the thread isn't running but the ptr still exists, reset it |
| 305 | else if (room_member_impl->receive_thread) { | 318 | else if (room_member_impl->loop_thread) { |
| 306 | room_member_impl->receive_thread.reset(); | 319 | room_member_impl->loop_thread.reset(); |
| 307 | } | 320 | } |
| 308 | 321 | ||
| 309 | ENetAddress address{}; | 322 | ENetAddress address{}; |
| @@ -361,8 +374,8 @@ void RoomMember::SendGameName(const std::string& game_name) { | |||
| 361 | 374 | ||
| 362 | void RoomMember::Leave() { | 375 | void RoomMember::Leave() { |
| 363 | room_member_impl->SetState(State::Idle); | 376 | room_member_impl->SetState(State::Idle); |
| 364 | room_member_impl->receive_thread->join(); | 377 | room_member_impl->loop_thread->join(); |
| 365 | room_member_impl->receive_thread.reset(); | 378 | room_member_impl->loop_thread.reset(); |
| 366 | } | 379 | } |
| 367 | 380 | ||
| 368 | } // namespace Network | 381 | } // namespace Network |