diff options
| author | 2017-07-08 15:24:47 +0200 | |
|---|---|---|
| committer | 2017-07-16 21:28:47 +0200 | |
| commit | 589dc083a58425cadd8390ddd81854dcf054dd27 (patch) | |
| tree | b665a2ff519ddbfb7d3f6a035f076342d69c2148 /src/network/room.cpp | |
| parent | Merge pull request #2824 from jroweboy/mingw_compile_test (diff) | |
| download | yuzu-589dc083a58425cadd8390ddd81854dcf054dd27.tar.gz yuzu-589dc083a58425cadd8390ddd81854dcf054dd27.tar.xz yuzu-589dc083a58425cadd8390ddd81854dcf054dd27.zip | |
Network: Threads for Room and RoomMember
Diffstat (limited to 'src/network/room.cpp')
| -rw-r--r-- | src/network/room.cpp | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/src/network/room.cpp b/src/network/room.cpp index 48de2f5cb..274cb4159 100644 --- a/src/network/room.cpp +++ b/src/network/room.cpp | |||
| @@ -2,6 +2,8 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <atomic> | ||
| 6 | #include <thread> | ||
| 5 | #include "enet/enet.h" | 7 | #include "enet/enet.h" |
| 6 | #include "network/room.h" | 8 | #include "network/room.h" |
| 7 | 9 | ||
| @@ -16,8 +18,38 @@ public: | |||
| 16 | 18 | ||
| 17 | std::atomic<State> state{State::Closed}; ///< Current state of the room. | 19 | std::atomic<State> state{State::Closed}; ///< Current state of the room. |
| 18 | RoomInformation room_information; ///< Information about this room. | 20 | RoomInformation room_information; ///< Information about this room. |
| 21 | |||
| 22 | /// Thread that receives and dispatches network packets | ||
| 23 | std::unique_ptr<std::thread> room_thread; | ||
| 24 | |||
| 25 | /// Thread function that will receive and dispatch messages until the room is destroyed. | ||
| 26 | void ServerLoop(); | ||
| 27 | void StartLoop(); | ||
| 19 | }; | 28 | }; |
| 20 | 29 | ||
| 30 | // RoomImpl | ||
| 31 | void Room::RoomImpl::ServerLoop() { | ||
| 32 | while (state != State::Closed) { | ||
| 33 | ENetEvent event; | ||
| 34 | if (enet_host_service(server, &event, 1000) > 0) { | ||
| 35 | switch (event.type) { | ||
| 36 | case ENET_EVENT_TYPE_RECEIVE: | ||
| 37 | // TODO(B3N30): Select the type of message and handle it | ||
| 38 | enet_packet_destroy(event.packet); | ||
| 39 | break; | ||
| 40 | case ENET_EVENT_TYPE_DISCONNECT: | ||
| 41 | // TODO(B3N30): Handle the disconnect from a client | ||
| 42 | break; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | void Room::RoomImpl::StartLoop() { | ||
| 49 | room_thread = std::make_unique<std::thread>(&Room::RoomImpl::ServerLoop, this); | ||
| 50 | } | ||
| 51 | |||
| 52 | // Room | ||
| 21 | Room::Room() : room_impl{std::make_unique<RoomImpl>()} {} | 53 | Room::Room() : room_impl{std::make_unique<RoomImpl>()} {} |
| 22 | 54 | ||
| 23 | Room::~Room() = default; | 55 | Room::~Room() = default; |
| @@ -34,8 +66,7 @@ void Room::Create(const std::string& name, const std::string& server_address, u1 | |||
| 34 | 66 | ||
| 35 | room_impl->room_information.name = name; | 67 | room_impl->room_information.name = name; |
| 36 | room_impl->room_information.member_slots = MaxConcurrentConnections; | 68 | room_impl->room_information.member_slots = MaxConcurrentConnections; |
| 37 | 69 | room_impl->StartLoop(); | |
| 38 | // TODO(B3N30): Start the receiving thread | ||
| 39 | } | 70 | } |
| 40 | 71 | ||
| 41 | Room::State Room::GetState() const { | 72 | Room::State Room::GetState() const { |
| @@ -48,7 +79,8 @@ const RoomInformation& Room::GetRoomInformation() const { | |||
| 48 | 79 | ||
| 49 | void Room::Destroy() { | 80 | void Room::Destroy() { |
| 50 | room_impl->state = State::Closed; | 81 | room_impl->state = State::Closed; |
| 51 | // TODO(B3n30): Join the receiving thread | 82 | room_impl->room_thread->join(); |
| 83 | room_impl->room_thread.reset(); | ||
| 52 | 84 | ||
| 53 | if (room_impl->server) { | 85 | if (room_impl->server) { |
| 54 | enet_host_destroy(room_impl->server); | 86 | enet_host_destroy(room_impl->server); |