diff options
| author | 2017-07-07 21:34:15 +0200 | |
|---|---|---|
| committer | 2017-07-07 15:34:15 -0400 | |
| commit | 2e37ce01c9dcdc5932cf5fe47a1ade0e0d2b7cf8 (patch) | |
| tree | b5298b3c528d9acc1f3fa48f19c9f9c19c97d036 /src/network | |
| parent | Merge pull request #2814 from Kloen/macro-remove (diff) | |
| download | yuzu-2e37ce01c9dcdc5932cf5fe47a1ade0e0d2b7cf8.tar.gz yuzu-2e37ce01c9dcdc5932cf5fe47a1ade0e0d2b7cf8.tar.xz yuzu-2e37ce01c9dcdc5932cf5fe47a1ade0e0d2b7cf8.zip | |
Implement basic virtual Room support based on enet (#2803)
* Added support for network with ENet lib,
connecting is possible, but data can't be sent, yet.
* fixup! Added support for network with ENet lib,
* fixup! CLang
* fixup! Added support for network with ENet lib,
* fixup! Added support for network with ENet lib,
* fixup! Clang format
* More fixups!
* Moved ENetHost* and ENetPeer* into pimpl classes
* fixup! Moved ENetHost* and ENetPeer* into pimpl classes
* fixup! Clang again
* fixup! Moved ENetHost* and ENetPeer* into pimpl classes
* fixup! Moved ENetHost* and ENetPeer* into pimpl classes
* fixup! Moved ENetHost* and ENetPeer* into pimpl classes
Diffstat (limited to 'src/network')
| -rw-r--r-- | src/network/CMakeLists.txt | 16 | ||||
| -rw-r--r-- | src/network/network.cpp | 50 | ||||
| -rw-r--r-- | src/network/network.h | 25 | ||||
| -rw-r--r-- | src/network/room.cpp | 60 | ||||
| -rw-r--r-- | src/network/room.h | 60 | ||||
| -rw-r--r-- | src/network/room_member.cpp | 74 | ||||
| -rw-r--r-- | src/network/room_member.h | 65 |
7 files changed, 350 insertions, 0 deletions
diff --git a/src/network/CMakeLists.txt b/src/network/CMakeLists.txt new file mode 100644 index 000000000..aeabe430e --- /dev/null +++ b/src/network/CMakeLists.txt | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | set(SRCS | ||
| 2 | network.cpp | ||
| 3 | room.cpp | ||
| 4 | room_member.cpp | ||
| 5 | ) | ||
| 6 | |||
| 7 | set(HEADERS | ||
| 8 | network.h | ||
| 9 | room.h | ||
| 10 | room_member.h | ||
| 11 | ) | ||
| 12 | |||
| 13 | create_directory_groups(${SRCS} ${HEADERS}) | ||
| 14 | |||
| 15 | add_library(network STATIC ${SRCS} ${HEADERS}) | ||
| 16 | target_link_libraries(network PRIVATE common enet) | ||
diff --git a/src/network/network.cpp b/src/network/network.cpp new file mode 100644 index 000000000..51b5d6a9f --- /dev/null +++ b/src/network/network.cpp | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "common/assert.h" | ||
| 6 | #include "common/logging/log.h" | ||
| 7 | #include "enet/enet.h" | ||
| 8 | #include "network/network.h" | ||
| 9 | |||
| 10 | namespace Network { | ||
| 11 | |||
| 12 | static std::shared_ptr<RoomMember> g_room_member; ///< RoomMember (Client) for network games | ||
| 13 | static std::shared_ptr<Room> g_room; ///< Room (Server) for network games | ||
| 14 | // TODO(B3N30): Put these globals into a networking class | ||
| 15 | |||
| 16 | bool Init() { | ||
| 17 | if (enet_initialize() != 0) { | ||
| 18 | LOG_ERROR(Network, "Error initalizing ENet"); | ||
| 19 | return false; | ||
| 20 | } | ||
| 21 | g_room = std::make_shared<Room>(); | ||
| 22 | g_room_member = std::make_shared<RoomMember>(); | ||
| 23 | LOG_DEBUG(Network, "initialized OK"); | ||
| 24 | return true; | ||
| 25 | } | ||
| 26 | |||
| 27 | std::weak_ptr<Room> GetRoom() { | ||
| 28 | return g_room; | ||
| 29 | } | ||
| 30 | |||
| 31 | std::weak_ptr<RoomMember> GetRoomMember() { | ||
| 32 | return g_room_member; | ||
| 33 | } | ||
| 34 | |||
| 35 | void Shutdown() { | ||
| 36 | if (g_room_member) { | ||
| 37 | if (g_room_member->IsConnected()) | ||
| 38 | g_room_member->Leave(); | ||
| 39 | g_room_member.reset(); | ||
| 40 | } | ||
| 41 | if (g_room) { | ||
| 42 | if (g_room->GetState() == Room::State::Open) | ||
| 43 | g_room->Destroy(); | ||
| 44 | g_room.reset(); | ||
| 45 | } | ||
| 46 | enet_deinitialize(); | ||
| 47 | LOG_DEBUG(Network, "shutdown OK"); | ||
| 48 | } | ||
| 49 | |||
| 50 | } // namespace Network | ||
diff --git a/src/network/network.h b/src/network/network.h new file mode 100644 index 000000000..6d002d693 --- /dev/null +++ b/src/network/network.h | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <memory> | ||
| 8 | #include "network/room.h" | ||
| 9 | #include "network/room_member.h" | ||
| 10 | |||
| 11 | namespace Network { | ||
| 12 | |||
| 13 | /// Initializes and registers the network device, the room, and the room member. | ||
| 14 | bool Init(); | ||
| 15 | |||
| 16 | /// Returns a pointer to the room handle | ||
| 17 | std::weak_ptr<Room> GetRoom(); | ||
| 18 | |||
| 19 | /// Returns a pointer to the room member handle | ||
| 20 | std::weak_ptr<RoomMember> GetRoomMember(); | ||
| 21 | |||
| 22 | /// Unregisters the network device, the room, and the room member and shut them down. | ||
| 23 | void Shutdown(); | ||
| 24 | |||
| 25 | } // namespace Network | ||
diff --git a/src/network/room.cpp b/src/network/room.cpp new file mode 100644 index 000000000..48de2f5cb --- /dev/null +++ b/src/network/room.cpp | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "enet/enet.h" | ||
| 6 | #include "network/room.h" | ||
| 7 | |||
| 8 | namespace Network { | ||
| 9 | |||
| 10 | /// Maximum number of concurrent connections allowed to this room. | ||
| 11 | static constexpr u32 MaxConcurrentConnections = 10; | ||
| 12 | |||
| 13 | class Room::RoomImpl { | ||
| 14 | public: | ||
| 15 | ENetHost* server = nullptr; ///< Network interface. | ||
| 16 | |||
| 17 | std::atomic<State> state{State::Closed}; ///< Current state of the room. | ||
| 18 | RoomInformation room_information; ///< Information about this room. | ||
| 19 | }; | ||
| 20 | |||
| 21 | Room::Room() : room_impl{std::make_unique<RoomImpl>()} {} | ||
| 22 | |||
| 23 | Room::~Room() = default; | ||
| 24 | |||
| 25 | void Room::Create(const std::string& name, const std::string& server_address, u16 server_port) { | ||
| 26 | ENetAddress address; | ||
| 27 | address.host = ENET_HOST_ANY; | ||
| 28 | enet_address_set_host(&address, server_address.c_str()); | ||
| 29 | address.port = server_port; | ||
| 30 | |||
| 31 | room_impl->server = enet_host_create(&address, MaxConcurrentConnections, NumChannels, 0, 0); | ||
| 32 | // TODO(B3N30): Allow specifying the maximum number of concurrent connections. | ||
| 33 | room_impl->state = State::Open; | ||
| 34 | |||
| 35 | room_impl->room_information.name = name; | ||
| 36 | room_impl->room_information.member_slots = MaxConcurrentConnections; | ||
| 37 | |||
| 38 | // TODO(B3N30): Start the receiving thread | ||
| 39 | } | ||
| 40 | |||
| 41 | Room::State Room::GetState() const { | ||
| 42 | return room_impl->state; | ||
| 43 | } | ||
| 44 | |||
| 45 | const RoomInformation& Room::GetRoomInformation() const { | ||
| 46 | return room_impl->room_information; | ||
| 47 | } | ||
| 48 | |||
| 49 | void Room::Destroy() { | ||
| 50 | room_impl->state = State::Closed; | ||
| 51 | // TODO(B3n30): Join the receiving thread | ||
| 52 | |||
| 53 | if (room_impl->server) { | ||
| 54 | enet_host_destroy(room_impl->server); | ||
| 55 | } | ||
| 56 | room_impl->room_information = {}; | ||
| 57 | room_impl->server = nullptr; | ||
| 58 | } | ||
| 59 | |||
| 60 | } // namespace Network | ||
diff --git a/src/network/room.h b/src/network/room.h new file mode 100644 index 000000000..70c64d5f1 --- /dev/null +++ b/src/network/room.h | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <atomic> | ||
| 8 | #include <memory> | ||
| 9 | #include <string> | ||
| 10 | #include "common/common_types.h" | ||
| 11 | |||
| 12 | namespace Network { | ||
| 13 | |||
| 14 | constexpr u16 DefaultRoomPort = 1234; | ||
| 15 | constexpr size_t NumChannels = 1; // Number of channels used for the connection | ||
| 16 | |||
| 17 | struct RoomInformation { | ||
| 18 | std::string name; ///< Name of the server | ||
| 19 | u32 member_slots; ///< Maximum number of members in this room | ||
| 20 | }; | ||
| 21 | |||
| 22 | /// This is what a server [person creating a server] would use. | ||
| 23 | class Room final { | ||
| 24 | public: | ||
| 25 | enum class State : u8 { | ||
| 26 | Open, ///< The room is open and ready to accept connections. | ||
| 27 | Closed, ///< The room is not opened and can not accept connections. | ||
| 28 | }; | ||
| 29 | |||
| 30 | Room(); | ||
| 31 | ~Room(); | ||
| 32 | |||
| 33 | /** | ||
| 34 | * Gets the current state of the room. | ||
| 35 | */ | ||
| 36 | State GetState() const; | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Gets the room information of the room. | ||
| 40 | */ | ||
| 41 | const RoomInformation& GetRoomInformation() const; | ||
| 42 | |||
| 43 | /** | ||
| 44 | * Creates the socket for this room. Will bind to default address if | ||
| 45 | * server is empty string. | ||
| 46 | */ | ||
| 47 | void Create(const std::string& name, const std::string& server = "", | ||
| 48 | u16 server_port = DefaultRoomPort); | ||
| 49 | |||
| 50 | /** | ||
| 51 | * Destroys the socket | ||
| 52 | */ | ||
| 53 | void Destroy(); | ||
| 54 | |||
| 55 | private: | ||
| 56 | class RoomImpl; | ||
| 57 | std::unique_ptr<RoomImpl> room_impl; | ||
| 58 | }; | ||
| 59 | |||
| 60 | } // namespace Network | ||
diff --git a/src/network/room_member.cpp b/src/network/room_member.cpp new file mode 100644 index 000000000..c87f009f4 --- /dev/null +++ b/src/network/room_member.cpp | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "common/assert.h" | ||
| 6 | #include "enet/enet.h" | ||
| 7 | #include "network/room_member.h" | ||
| 8 | |||
| 9 | namespace Network { | ||
| 10 | |||
| 11 | constexpr u32 ConnectionTimeoutMs = 5000; | ||
| 12 | |||
| 13 | class RoomMember::RoomMemberImpl { | ||
| 14 | public: | ||
| 15 | ENetHost* client = nullptr; ///< ENet network interface. | ||
| 16 | ENetPeer* server = nullptr; ///< The server peer the client is connected to | ||
| 17 | |||
| 18 | std::atomic<State> state{State::Idle}; ///< Current state of the RoomMember. | ||
| 19 | |||
| 20 | std::string nickname; ///< The nickname of this member. | ||
| 21 | }; | ||
| 22 | |||
| 23 | RoomMember::RoomMember() : room_member_impl{std::make_unique<RoomMemberImpl>()} { | ||
| 24 | room_member_impl->client = enet_host_create(nullptr, 1, NumChannels, 0, 0); | ||
| 25 | ASSERT_MSG(room_member_impl->client != nullptr, "Could not create client"); | ||
| 26 | } | ||
| 27 | |||
| 28 | RoomMember::~RoomMember() { | ||
| 29 | ASSERT_MSG(!IsConnected(), "RoomMember is being destroyed while connected"); | ||
| 30 | enet_host_destroy(room_member_impl->client); | ||
| 31 | } | ||
| 32 | |||
| 33 | RoomMember::State RoomMember::GetState() const { | ||
| 34 | return room_member_impl->state; | ||
| 35 | } | ||
| 36 | |||
| 37 | void RoomMember::Join(const std::string& nick, const char* server_addr, u16 server_port, | ||
| 38 | u16 client_port) { | ||
| 39 | ENetAddress address{}; | ||
| 40 | enet_address_set_host(&address, server_addr); | ||
| 41 | address.port = server_port; | ||
| 42 | |||
| 43 | room_member_impl->server = | ||
| 44 | enet_host_connect(room_member_impl->client, &address, NumChannels, 0); | ||
| 45 | |||
| 46 | if (!room_member_impl->server) { | ||
| 47 | room_member_impl->state = State::Error; | ||
| 48 | return; | ||
| 49 | } | ||
| 50 | |||
| 51 | ENetEvent event{}; | ||
| 52 | int net = enet_host_service(room_member_impl->client, &event, ConnectionTimeoutMs); | ||
| 53 | if (net > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { | ||
| 54 | room_member_impl->nickname = nick; | ||
| 55 | room_member_impl->state = State::Joining; | ||
| 56 | // TODO(B3N30): Send a join request with the nickname to the server | ||
| 57 | // TODO(B3N30): Start the receive thread | ||
| 58 | } else { | ||
| 59 | room_member_impl->state = State::CouldNotConnect; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | bool RoomMember::IsConnected() const { | ||
| 64 | return room_member_impl->state == State::Joining || room_member_impl->state == State::Joined; | ||
| 65 | } | ||
| 66 | |||
| 67 | void RoomMember::Leave() { | ||
| 68 | enet_peer_disconnect(room_member_impl->server, 0); | ||
| 69 | room_member_impl->state = State::Idle; | ||
| 70 | // TODO(B3N30): Close the receive thread | ||
| 71 | enet_peer_reset(room_member_impl->server); | ||
| 72 | } | ||
| 73 | |||
| 74 | } // namespace Network | ||
diff --git a/src/network/room_member.h b/src/network/room_member.h new file mode 100644 index 000000000..177622b69 --- /dev/null +++ b/src/network/room_member.h | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <atomic> | ||
| 8 | #include <memory> | ||
| 9 | #include <string> | ||
| 10 | #include "common/common_types.h" | ||
| 11 | #include "network/room.h" | ||
| 12 | |||
| 13 | namespace Network { | ||
| 14 | |||
| 15 | /** | ||
| 16 | * This is what a client [person joining a server] would use. | ||
| 17 | * It also has to be used if you host a game yourself (You'd create both, a Room and a | ||
| 18 | * RoomMembership for yourself) | ||
| 19 | */ | ||
| 20 | class RoomMember final { | ||
| 21 | public: | ||
| 22 | enum class State : u8 { | ||
| 23 | Idle, ///< Default state | ||
| 24 | Error, ///< Some error [permissions to network device missing or something] | ||
| 25 | Joining, ///< The client is attempting to join a room. | ||
| 26 | Joined, ///< The client is connected to the room and is ready to send/receive packets. | ||
| 27 | LostConnection, ///< Connection closed | ||
| 28 | |||
| 29 | // Reasons why connection was rejected | ||
| 30 | NameCollision, ///< Somebody is already using this name | ||
| 31 | MacCollision, ///< Somebody is already using that mac-address | ||
| 32 | CouldNotConnect ///< The room is not responding to a connection attempt | ||
| 33 | }; | ||
| 34 | |||
| 35 | RoomMember(); | ||
| 36 | ~RoomMember(); | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Returns the status of our connection to the room. | ||
| 40 | */ | ||
| 41 | State GetState() const; | ||
| 42 | |||
| 43 | /** | ||
| 44 | * Returns whether we're connected to a server or not. | ||
| 45 | */ | ||
| 46 | bool IsConnected() const; | ||
| 47 | |||
| 48 | /** | ||
| 49 | * Attempts to join a room at the specified address and port, using the specified nickname. | ||
| 50 | * This may fail if the username is already taken. | ||
| 51 | */ | ||
| 52 | void Join(const std::string& nickname, const char* server_addr = "127.0.0.1", | ||
| 53 | const u16 serverPort = DefaultRoomPort, const u16 clientPort = 0); | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Leaves the current room. | ||
| 57 | */ | ||
| 58 | void Leave(); | ||
| 59 | |||
| 60 | private: | ||
| 61 | class RoomMemberImpl; | ||
| 62 | std::unique_ptr<RoomMemberImpl> room_member_impl; | ||
| 63 | }; | ||
| 64 | |||
| 65 | } // namespace Network | ||