summaryrefslogtreecommitdiffstats
path: root/src/network
diff options
context:
space:
mode:
authorJames Rowe <jroweboy@gmail.com>2018-01-12 03:21:20 +0100
committerJames Rowe <jroweboy@gmail.com>2018-01-13 03:11:03 +0100
commitebf9a784a9f7f4148a669dbb39e7cd50df779a14 (patch)
treed585685a1c0a34b903af1d086d62560bf56bb29f /src/network
parentconfig: Default CPU core to Unicorn. (diff)
downloadyuzu-ebf9a784a9f7f4148a669dbb39e7cd50df779a14.tar
yuzu-ebf9a784a9f7f4148a669dbb39e7cd50df779a14.tar.gz
yuzu-ebf9a784a9f7f4148a669dbb39e7cd50df779a14.tar.bz2
yuzu-ebf9a784a9f7f4148a669dbb39e7cd50df779a14.tar.lz
yuzu-ebf9a784a9f7f4148a669dbb39e7cd50df779a14.tar.xz
yuzu-ebf9a784a9f7f4148a669dbb39e7cd50df779a14.tar.zst
yuzu-ebf9a784a9f7f4148a669dbb39e7cd50df779a14.zip
Diffstat (limited to 'src/network')
-rw-r--r--src/network/CMakeLists.txt18
-rw-r--r--src/network/network.cpp50
-rw-r--r--src/network/network.h25
-rw-r--r--src/network/packet.cpp263
-rw-r--r--src/network/packet.h166
-rw-r--r--src/network/room.cpp497
-rw-r--r--src/network/room.h101
-rw-r--r--src/network/room_member.cpp490
-rw-r--r--src/network/room_member.h182
9 files changed, 0 insertions, 1792 deletions
diff --git a/src/network/CMakeLists.txt b/src/network/CMakeLists.txt
deleted file mode 100644
index ac9d028da..000000000
--- a/src/network/CMakeLists.txt
+++ /dev/null
@@ -1,18 +0,0 @@
-set(SRCS
- network.cpp
- packet.cpp
- room.cpp
- room_member.cpp
- )
-
-set(HEADERS
- network.h
- packet.h
- room.h
- room_member.h
- )
-
-create_directory_groups(${SRCS} ${HEADERS})
-
-add_library(network STATIC ${SRCS} ${HEADERS})
-target_link_libraries(network PRIVATE common enet)
diff --git a/src/network/network.cpp b/src/network/network.cpp
deleted file mode 100644
index 51b5d6a9f..000000000
--- a/src/network/network.cpp
+++ /dev/null
@@ -1,50 +0,0 @@
-// Copyright 2017 Citra Emulator Project
-// Licensed under GPLv2 or any later version
-// Refer to the license.txt file included.
-
-#include "common/assert.h"
-#include "common/logging/log.h"
-#include "enet/enet.h"
-#include "network/network.h"
-
-namespace Network {
-
-static std::shared_ptr<RoomMember> g_room_member; ///< RoomMember (Client) for network games
-static std::shared_ptr<Room> g_room; ///< Room (Server) for network games
-// TODO(B3N30): Put these globals into a networking class
-
-bool Init() {
- if (enet_initialize() != 0) {
- LOG_ERROR(Network, "Error initalizing ENet");
- return false;
- }
- g_room = std::make_shared<Room>();
- g_room_member = std::make_shared<RoomMember>();
- LOG_DEBUG(Network, "initialized OK");
- return true;
-}
-
-std::weak_ptr<Room> GetRoom() {
- return g_room;
-}
-
-std::weak_ptr<RoomMember> GetRoomMember() {
- return g_room_member;
-}
-
-void Shutdown() {
- if (g_room_member) {
- if (g_room_member->IsConnected())
- g_room_member->Leave();
- g_room_member.reset();
- }
- if (g_room) {
- if (g_room->GetState() == Room::State::Open)
- g_room->Destroy();
- g_room.reset();
- }
- enet_deinitialize();
- LOG_DEBUG(Network, "shutdown OK");
-}
-
-} // namespace Network
diff --git a/src/network/network.h b/src/network/network.h
deleted file mode 100644
index 6d002d693..000000000
--- a/src/network/network.h
+++ /dev/null
@@ -1,25 +0,0 @@
-// Copyright 2017 Citra Emulator Project
-// Licensed under GPLv2 or any later version
-// Refer to the license.txt file included.
-
-#pragma once
-
-#include <memory>
-#include "network/room.h"
-#include "network/room_member.h"
-
-namespace Network {
-
-/// Initializes and registers the network device, the room, and the room member.
-bool Init();
-
-/// Returns a pointer to the room handle
-std::weak_ptr<Room> GetRoom();
-
-/// Returns a pointer to the room member handle
-std::weak_ptr<RoomMember> GetRoomMember();
-
-/// Unregisters the network device, the room, and the room member and shut them down.
-void Shutdown();
-
-} // namespace Network
diff --git a/src/network/packet.cpp b/src/network/packet.cpp
deleted file mode 100644
index 7e1a812f3..000000000
--- a/src/network/packet.cpp
+++ /dev/null
@@ -1,263 +0,0 @@
-// Copyright 2017 Citra Emulator Project
-// Licensed under GPLv2 or any later version
-// Refer to the license.txt file included.
-
-#ifdef _WIN32
-#include <winsock2.h>
-#else
-#include <arpa/inet.h>
-#endif
-#include <cstring>
-#include <string>
-#include "network/packet.h"
-
-namespace Network {
-
-#ifndef htonll
-u64 htonll(u64 x) {
- return ((1 == htonl(1)) ? (x) : ((uint64_t)htonl((x)&0xFFFFFFFF) << 32) | htonl((x) >> 32));
-}
-#endif
-
-#ifndef ntohll
-u64 ntohll(u64 x) {
- return ((1 == ntohl(1)) ? (x) : ((uint64_t)ntohl((x)&0xFFFFFFFF) << 32) | ntohl((x) >> 32));
-}
-#endif
-
-void Packet::Append(const void* in_data, std::size_t size_in_bytes) {
- if (in_data && (size_in_bytes > 0)) {
- std::size_t start = data.size();
- data.resize(start + size_in_bytes);
- std::memcpy(&data[start], in_data, size_in_bytes);
- }
-}
-
-void Packet::Read(void* out_data, std::size_t size_in_bytes) {
- if (out_data && CheckSize(size_in_bytes)) {
- std::memcpy(out_data, &data[read_pos], size_in_bytes);
- read_pos += size_in_bytes;
- }
-}
-
-void Packet::Clear() {
- data.clear();
- read_pos = 0;
- is_valid = true;
-}
-
-const void* Packet::GetData() const {
- return !data.empty() ? &data[0] : nullptr;
-}
-
-void Packet::IgnoreBytes(u32 length) {
- read_pos += length;
-}
-
-std::size_t Packet::GetDataSize() const {
- return data.size();
-}
-
-bool Packet::EndOfPacket() const {
- return read_pos >= data.size();
-}
-
-Packet::operator bool() const {
- return is_valid ? &Packet::CheckSize : nullptr;
-}
-
-Packet& Packet::operator>>(bool& out_data) {
- u8 value;
- if (*this >> value) {
- out_data = (value != 0);
- }
- return *this;
-}
-
-Packet& Packet::operator>>(s8& out_data) {
- Read(&out_data, sizeof(out_data));
- return *this;
-}
-
-Packet& Packet::operator>>(u8& out_data) {
- Read(&out_data, sizeof(out_data));
- return *this;
-}
-
-Packet& Packet::operator>>(s16& out_data) {
- s16 value;
- Read(&value, sizeof(value));
- out_data = ntohs(value);
- return *this;
-}
-
-Packet& Packet::operator>>(u16& out_data) {
- u16 value;
- Read(&value, sizeof(value));
- out_data = ntohs(value);
- return *this;
-}
-
-Packet& Packet::operator>>(s32& out_data) {
- s32 value;
- Read(&value, sizeof(value));
- out_data = ntohl(value);
- return *this;
-}
-
-Packet& Packet::operator>>(u32& out_data) {
- u32 value;
- Read(&value, sizeof(value));
- out_data = ntohl(value);
- return *this;
-}
-
-Packet& Packet::operator>>(s64& out_data) {
- s64 value;
- Read(&value, sizeof(value));
- out_data = ntohll(value);
- return *this;
-}
-
-Packet& Packet::operator>>(u64& out_data) {
- u64 value;
- Read(&value, sizeof(value));
- out_data = ntohll(value);
- return *this;
-}
-
-Packet& Packet::operator>>(float& out_data) {
- Read(&out_data, sizeof(out_data));
- return *this;
-}
-
-Packet& Packet::operator>>(double& out_data) {
- Read(&out_data, sizeof(out_data));
- return *this;
-}
-
-Packet& Packet::operator>>(char* out_data) {
- // First extract string length
- u32 length = 0;
- *this >> length;
-
- if ((length > 0) && CheckSize(length)) {
- // Then extract characters
- std::memcpy(out_data, &data[read_pos], length);
- out_data[length] = '\0';
-
- // Update reading position
- read_pos += length;
- }
-
- return *this;
-}
-
-Packet& Packet::operator>>(std::string& out_data) {
- // First extract string length
- u32 length = 0;
- *this >> length;
-
- out_data.clear();
- if ((length > 0) && CheckSize(length)) {
- // Then extract characters
- out_data.assign(&data[read_pos], length);
-
- // Update reading position
- read_pos += length;
- }
-
- return *this;
-}
-
-Packet& Packet::operator<<(bool in_data) {
- *this << static_cast<u8>(in_data);
- return *this;
-}
-
-Packet& Packet::operator<<(s8 in_data) {
- Append(&in_data, sizeof(in_data));
- return *this;
-}
-
-Packet& Packet::operator<<(u8 in_data) {
- Append(&in_data, sizeof(in_data));
- return *this;
-}
-
-Packet& Packet::operator<<(s16 in_data) {
- s16 toWrite = htons(in_data);
- Append(&toWrite, sizeof(toWrite));
- return *this;
-}
-
-Packet& Packet::operator<<(u16 in_data) {
- u16 toWrite = htons(in_data);
- Append(&toWrite, sizeof(toWrite));
- return *this;
-}
-
-Packet& Packet::operator<<(s32 in_data) {
- s32 toWrite = htonl(in_data);
- Append(&toWrite, sizeof(toWrite));
- return *this;
-}
-
-Packet& Packet::operator<<(u32 in_data) {
- u32 toWrite = htonl(in_data);
- Append(&toWrite, sizeof(toWrite));
- return *this;
-}
-
-Packet& Packet::operator<<(s64 in_data) {
- s64 toWrite = htonll(in_data);
- Append(&toWrite, sizeof(toWrite));
- return *this;
-}
-
-Packet& Packet::operator<<(u64 in_data) {
- u64 toWrite = htonll(in_data);
- Append(&toWrite, sizeof(toWrite));
- return *this;
-}
-
-Packet& Packet::operator<<(float in_data) {
- Append(&in_data, sizeof(in_data));
- return *this;
-}
-
-Packet& Packet::operator<<(double in_data) {
- Append(&in_data, sizeof(in_data));
- return *this;
-}
-
-Packet& Packet::operator<<(const char* in_data) {
- // First insert string length
- u32 length = static_cast<u32>(std::strlen(in_data));
- *this << length;
-
- // Then insert characters
- Append(in_data, length * sizeof(char));
-
- return *this;
-}
-
-Packet& Packet::operator<<(const std::string& in_data) {
- // First insert string length
- u32 length = static_cast<u32>(in_data.size());
- *this << length;
-
- // Then insert characters
- if (length > 0)
- Append(in_data.c_str(), length * sizeof(std::string::value_type));
-
- return *this;
-}
-
-bool Packet::CheckSize(std::size_t size) {
- is_valid = is_valid && (read_pos + size <= data.size());
-
- return is_valid;
-}
-
-} // namespace Network
diff --git a/src/network/packet.h b/src/network/packet.h
deleted file mode 100644
index 5a2e58dc2..000000000
--- a/src/network/packet.h
+++ /dev/null
@@ -1,166 +0,0 @@
-// Copyright 2017 Citra Emulator Project
-// Licensed under GPLv2 or any later version
-// Refer to the license.txt file included.
-
-#pragma once
-
-#include <array>
-#include <vector>
-#include "common/common_types.h"
-
-namespace Network {
-
-/// A class that serializes data for network transfer. It also handles endianess
-class Packet {
-public:
- Packet() = default;
- ~Packet() = default;
-
- /**
- * Append data to the end of the packet
- * @param data Pointer to the sequence of bytes to append
- * @param size_in_bytes Number of bytes to append
- */
- void Append(const void* data, std::size_t size_in_bytes);
-
- /**
- * Reads data from the current read position of the packet
- * @param out_data Pointer where the data should get written to
- * @param size_in_bytes Number of bytes to read
- */
- void Read(void* out_data, std::size_t size_in_bytes);
-
- /**
- * Clear the packet
- * After calling Clear, the packet is empty.
- */
- void Clear();
-
- /**
- * Ignores bytes while reading
- * @param length THe number of bytes to ignore
- */
- void IgnoreBytes(u32 length);
-
- /**
- * Get a pointer to the data contained in the packet
- * @return Pointer to the data
- */
- const void* GetData() const;
-
- /**
- * This function returns the number of bytes pointed to by
- * what getData returns.
- * @return Data size, in bytes
- */
- std::size_t GetDataSize() const;
-
- /**
- * This function is useful to know if there is some data
- * left to be read, without actually reading it.
- * @return True if all data was read, false otherwise
- */
- bool EndOfPacket() const;
-
- explicit operator bool() const;
-
- /// Overloads of operator >> to read data from the packet
- Packet& operator>>(bool& out_data);
- Packet& operator>>(s8& out_data);
- Packet& operator>>(u8& out_data);
- Packet& operator>>(s16& out_data);
- Packet& operator>>(u16& out_data);
- Packet& operator>>(s32& out_data);
- Packet& operator>>(u32& out_data);
- Packet& operator>>(s64& out_data);
- Packet& operator>>(u64& out_data);
- Packet& operator>>(float& out_data);
- Packet& operator>>(double& out_data);
- Packet& operator>>(char* out_data);
- Packet& operator>>(std::string& out_data);
- template <typename T>
- Packet& operator>>(std::vector<T>& out_data);
- template <typename T, std::size_t S>
- Packet& operator>>(std::array<T, S>& out_data);
-
- /// Overloads of operator << to write data into the packet
- Packet& operator<<(bool in_data);
- Packet& operator<<(s8 in_data);
- Packet& operator<<(u8 in_data);
- Packet& operator<<(s16 in_data);
- Packet& operator<<(u16 in_data);
- Packet& operator<<(s32 in_data);
- Packet& operator<<(u32 in_data);
- Packet& operator<<(s64 in_data);
- Packet& operator<<(u64 in_data);
- Packet& operator<<(float in_data);
- Packet& operator<<(double in_data);
- Packet& operator<<(const char* in_data);
- Packet& operator<<(const std::string& in_data);
- template <typename T>
- Packet& operator<<(const std::vector<T>& in_data);
- template <typename T, std::size_t S>
- Packet& operator<<(const std::array<T, S>& data);
-
-private:
- /**
- * Check if the packet can extract a given number of bytes
- * This function updates accordingly the state of the packet.
- * @param size Size to check
- * @return True if size bytes can be read from the packet
- */
- bool CheckSize(std::size_t size);
-
- // Member data
- std::vector<char> data; ///< Data stored in the packet
- std::size_t read_pos = 0; ///< Current reading position in the packet
- bool is_valid = true; ///< Reading state of the packet
-};
-
-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;
- out_data[i] = character;
- }
- return *this;
-}
-
-template <typename T, std::size_t S>
-Packet& Packet::operator>>(std::array<T, S>& out_data) {
- for (std::size_t i = 0; i < out_data.size(); ++i) {
- T character = 0;
- *this >> character;
- out_data[i] = character;
- }
- return *this;
-}
-
-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];
- }
- return *this;
-}
-
-template <typename T, std::size_t S>
-Packet& Packet::operator<<(const std::array<T, S>& in_data) {
- for (std::size_t i = 0; i < in_data.size(); ++i) {
- *this << in_data[i];
- }
- return *this;
-}
-
-} // namespace Network
diff --git a/src/network/room.cpp b/src/network/room.cpp
deleted file mode 100644
index 261049ab0..000000000
--- a/src/network/room.cpp
+++ /dev/null
@@ -1,497 +0,0 @@
-// Copyright 2017 Citra Emulator Project
-// Licensed under GPLv2 or any later version
-// Refer to the license.txt file included.
-
-#include <algorithm>
-#include <atomic>
-#include <mutex>
-#include <random>
-#include <thread>
-#include "enet/enet.h"
-#include "network/packet.h"
-#include "network/room.h"
-
-namespace Network {
-
-/// Maximum number of concurrent connections allowed to this room.
-static constexpr u32 MaxConcurrentConnections = 10;
-
-class Room::RoomImpl {
-public:
- // This MAC address is used to generate a 'Nintendo' like Mac address.
- const MacAddress NintendoOUI;
- std::mt19937 random_gen; ///< Random number generator. Used for GenerateMacAddress
-
- ENetHost* server = nullptr; ///< Network interface.
-
- std::atomic<State> state{State::Closed}; ///< Current state of the room.
- RoomInformation room_information; ///< Information about this room.
-
- struct Member {
- std::string nickname; ///< The nickname of the member.
- GameInfo game_info; ///< The current game of the member
- MacAddress mac_address; ///< The assigned mac address of the member.
- ENetPeer* peer; ///< The remote peer.
- };
- using MemberList = std::vector<Member>;
- MemberList members; ///< Information about the members of this room
- mutable std::mutex member_mutex; ///< Mutex for locking the members list
- /// This should be a std::shared_mutex as soon as C++17 is supported
-
- RoomImpl()
- : random_gen(std::random_device()()), NintendoOUI{0x00, 0x1F, 0x32, 0x00, 0x00, 0x00} {}
-
- /// Thread that receives and dispatches network packets
- std::unique_ptr<std::thread> room_thread;
-
- /// Thread function that will receive and dispatch messages until the room is destroyed.
- void ServerLoop();
- void StartLoop();
-
- /**
- * Parses and answers a room join request from a client.
- * Validates the uniqueness of the username and assigns the MAC address
- * that the client will use for the remainder of the connection.
- */
- void HandleJoinRequest(const ENetEvent* event);
-
- /**
- * Returns whether the nickname is valid, ie. isn't already taken by someone else in the room.
- */
- bool IsValidNickname(const std::string& nickname) const;
-
- /**
- * Returns whether the MAC address is valid, ie. isn't already taken by someone else in the
- * room.
- */
- bool IsValidMacAddress(const MacAddress& address) const;
-
- /**
- * Sends a ID_ROOM_NAME_COLLISION message telling the client that the name is invalid.
- */
- void SendNameCollision(ENetPeer* client);
-
- /**
- * Sends a ID_ROOM_MAC_COLLISION message telling the client that the MAC is invalid.
- */
- void SendMacCollision(ENetPeer* client);
-
- /**
- * Sends a ID_ROOM_VERSION_MISMATCH message telling the client that the version is invalid.
- */
- void SendVersionMismatch(ENetPeer* client);
-
- /**
- * Notifies the member that its connection attempt was successful,
- * and it is now part of the room.
- */
- void SendJoinSuccess(ENetPeer* client, MacAddress mac_address);
-
- /**
- * Notifies the members that the room is closed,
- */
- void SendCloseMessage();
-
- /**
- * Sends the information about the room, along with the list of members
- * to every connected client in the room.
- * The packet has the structure:
- * <MessageID>ID_ROOM_INFORMATION
- * <String> room_name
- * <u32> member_slots: The max number of clients allowed in this room
- * <u32> num_members: the number of currently joined clients
- * This is followed by the following three values for each member:
- * <String> nickname of that member
- * <MacAddress> mac_address of that member
- * <String> game_name of that member
- */
- void BroadcastRoomInformation();
-
- /**
- * Generates a free MAC address to assign to a new client.
- * The first 3 bytes are the NintendoOUI 0x00, 0x1F, 0x32
- */
- MacAddress GenerateMacAddress();
-
- /**
- * Broadcasts this packet to all members except the sender.
- * @param event The ENet event containing the data
- */
- void HandleWifiPacket(const ENetEvent* event);
-
- /**
- * Extracts a chat entry from a received ENet packet and adds it to the chat queue.
- * @param event The ENet event that was received.
- */
- void HandleChatPacket(const ENetEvent* event);
-
- /**
- * Extracts the game name from a received ENet packet and broadcasts it.
- * @param event The ENet event that was received.
- */
- void HandleGameNamePacket(const ENetEvent* event);
-
- /**
- * Removes the client from the members list if it was in it and announces the change
- * to all other clients.
- */
- void HandleClientDisconnection(ENetPeer* client);
-};
-
-// RoomImpl
-void Room::RoomImpl::ServerLoop() {
- while (state != State::Closed) {
- ENetEvent event;
- if (enet_host_service(server, &event, 100) > 0) {
- switch (event.type) {
- case ENET_EVENT_TYPE_RECEIVE:
- switch (event.packet->data[0]) {
- case IdJoinRequest:
- HandleJoinRequest(&event);
- break;
- case IdSetGameInfo:
- HandleGameNamePacket(&event);
- break;
- case IdWifiPacket:
- HandleWifiPacket(&event);
- break;
- case IdChatMessage:
- HandleChatPacket(&event);
- break;
- }
- enet_packet_destroy(event.packet);
- break;
- case ENET_EVENT_TYPE_DISCONNECT:
- HandleClientDisconnection(event.peer);
- break;
- }
- }
- }
- // Close the connection to all members:
- SendCloseMessage();
-}
-
-void Room::RoomImpl::StartLoop() {
- room_thread = std::make_unique<std::thread>(&Room::RoomImpl::ServerLoop, this);
-}
-
-void Room::RoomImpl::HandleJoinRequest(const ENetEvent* event) {
- Packet packet;
- packet.Append(event->packet->data, event->packet->dataLength);
- packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
- std::string nickname;
- packet >> nickname;
-
- MacAddress preferred_mac;
- packet >> preferred_mac;
-
- u32 client_version;
- packet >> client_version;
-
- if (!IsValidNickname(nickname)) {
- SendNameCollision(event->peer);
- return;
- }
-
- if (preferred_mac != NoPreferredMac) {
- // Verify if the preferred mac is available
- if (!IsValidMacAddress(preferred_mac)) {
- SendMacCollision(event->peer);
- return;
- }
- } else {
- // Assign a MAC address of this client automatically
- preferred_mac = GenerateMacAddress();
- }
-
- if (client_version != network_version) {
- SendVersionMismatch(event->peer);
- return;
- }
-
- // At this point the client is ready to be added to the room.
- Member member{};
- member.mac_address = preferred_mac;
- member.nickname = nickname;
- member.peer = event->peer;
-
- {
- std::lock_guard<std::mutex> lock(member_mutex);
- members.push_back(std::move(member));
- }
-
- // Notify everyone that the room information has changed.
- BroadcastRoomInformation();
- SendJoinSuccess(event->peer, preferred_mac);
-}
-
-bool Room::RoomImpl::IsValidNickname(const std::string& nickname) const {
- // A nickname is valid if it is not already taken by anybody else in the room.
- // TODO(B3N30): Check for empty names, spaces, etc.
- std::lock_guard<std::mutex> lock(member_mutex);
- return std::all_of(members.begin(), members.end(),
- [&nickname](const auto& member) { return member.nickname != nickname; });
-}
-
-bool Room::RoomImpl::IsValidMacAddress(const MacAddress& address) const {
- // A MAC address is valid if it is not already taken by anybody else in the room.
- std::lock_guard<std::mutex> lock(member_mutex);
- return std::all_of(members.begin(), members.end(),
- [&address](const auto& member) { return member.mac_address != address; });
-}
-
-void Room::RoomImpl::SendNameCollision(ENetPeer* client) {
- Packet packet;
- packet << static_cast<u8>(IdNameCollision);
-
- ENetPacket* enet_packet =
- enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
- enet_peer_send(client, 0, enet_packet);
- enet_host_flush(server);
-}
-
-void Room::RoomImpl::SendMacCollision(ENetPeer* client) {
- Packet packet;
- packet << static_cast<u8>(IdMacCollision);
-
- ENetPacket* enet_packet =
- enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
- enet_peer_send(client, 0, enet_packet);
- enet_host_flush(server);
-}
-
-void Room::RoomImpl::SendVersionMismatch(ENetPeer* client) {
- Packet packet;
- packet << static_cast<u8>(IdVersionMismatch);
- packet << network_version;
-
- ENetPacket* enet_packet =
- enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
- enet_peer_send(client, 0, enet_packet);
- enet_host_flush(server);
-}
-
-void Room::RoomImpl::SendJoinSuccess(ENetPeer* client, MacAddress mac_address) {
- Packet packet;
- packet << static_cast<u8>(IdJoinSuccess);
- packet << mac_address;
- ENetPacket* enet_packet =
- enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
- enet_peer_send(client, 0, enet_packet);
- enet_host_flush(server);
-}
-
-void Room::RoomImpl::SendCloseMessage() {
- Packet packet;
- packet << static_cast<u8>(IdCloseRoom);
- ENetPacket* enet_packet =
- enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
- std::lock_guard<std::mutex> lock(member_mutex);
- for (auto& member : members) {
- enet_peer_send(member.peer, 0, enet_packet);
- }
- enet_host_flush(server);
- for (auto& member : members) {
- enet_peer_disconnect(member.peer, 0);
- }
-}
-
-void Room::RoomImpl::BroadcastRoomInformation() {
- Packet packet;
- packet << static_cast<u8>(IdRoomInformation);
- packet << room_information.name;
- packet << room_information.member_slots;
-
- packet << static_cast<u32>(members.size());
- {
- std::lock_guard<std::mutex> lock(member_mutex);
- for (const auto& member : members) {
- packet << member.nickname;
- packet << member.mac_address;
- packet << member.game_info.name;
- packet << member.game_info.id;
- }
- }
-
- ENetPacket* enet_packet =
- enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
- enet_host_broadcast(server, 0, enet_packet);
- enet_host_flush(server);
-}
-
-MacAddress Room::RoomImpl::GenerateMacAddress() {
- MacAddress result_mac =
- NintendoOUI; // The first three bytes of each MAC address will be the NintendoOUI
- std::uniform_int_distribution<> dis(0x00, 0xFF); // Random byte between 0 and 0xFF
- do {
- for (size_t i = 3; i < result_mac.size(); ++i) {
- result_mac[i] = dis(random_gen);
- }
- } while (!IsValidMacAddress(result_mac));
- return result_mac;
-}
-
-void Room::RoomImpl::HandleWifiPacket(const ENetEvent* event) {
- Packet in_packet;
- in_packet.Append(event->packet->data, event->packet->dataLength);
- 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
- MacAddress destination_address;
- in_packet >> destination_address;
-
- Packet out_packet;
- out_packet.Append(event->packet->data, event->packet->dataLength);
- ENetPacket* enet_packet = enet_packet_create(out_packet.GetData(), out_packet.GetDataSize(),
- ENET_PACKET_FLAG_RELIABLE);
-
- if (destination_address == BroadcastMac) { // Send the data to everyone except the sender
- std::lock_guard<std::mutex> lock(member_mutex);
- for (const auto& member : members) {
- if (member.peer != event->peer)
- enet_peer_send(member.peer, 0, enet_packet);
- }
- } else { // Send the data only to the destination client
- std::lock_guard<std::mutex> lock(member_mutex);
- auto member = std::find_if(members.begin(), members.end(),
- [destination_address](const Member& member) -> bool {
- return member.mac_address == destination_address;
- });
- if (member != members.end()) {
- enet_peer_send(member->peer, 0, enet_packet);
- }
- }
- enet_host_flush(server);
-}
-
-void Room::RoomImpl::HandleChatPacket(const ENetEvent* event) {
- Packet in_packet;
- in_packet.Append(event->packet->data, event->packet->dataLength);
-
- in_packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
- std::string message;
- in_packet >> message;
- auto CompareNetworkAddress = [event](const Member member) -> bool {
- return member.peer == event->peer;
- };
-
- std::lock_guard<std::mutex> lock(member_mutex);
- const auto sending_member = std::find_if(members.begin(), members.end(), CompareNetworkAddress);
- if (sending_member == members.end()) {
- return; // Received a chat message from a unknown sender
- }
-
- Packet out_packet;
- out_packet << static_cast<u8>(IdChatMessage);
- out_packet << sending_member->nickname;
- out_packet << message;
-
- ENetPacket* enet_packet = enet_packet_create(out_packet.GetData(), out_packet.GetDataSize(),
- ENET_PACKET_FLAG_RELIABLE);
- for (const auto& member : members) {
- if (member.peer != event->peer)
- enet_peer_send(member.peer, 0, enet_packet);
- }
- enet_host_flush(server);
-}
-
-void Room::RoomImpl::HandleGameNamePacket(const ENetEvent* event) {
- Packet in_packet;
- in_packet.Append(event->packet->data, event->packet->dataLength);
-
- in_packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
- GameInfo game_info;
- in_packet >> game_info.name;
- in_packet >> game_info.id;
-
- {
- std::lock_guard<std::mutex> lock(member_mutex);
- auto member =
- std::find_if(members.begin(), members.end(), [event](const Member& member) -> bool {
- return member.peer == event->peer;
- });
- if (member != members.end()) {
- member->game_info = game_info;
- }
- }
- BroadcastRoomInformation();
-}
-
-void Room::RoomImpl::HandleClientDisconnection(ENetPeer* client) {
- // Remove the client from the members list.
- {
- std::lock_guard<std::mutex> lock(member_mutex);
- members.erase(
- std::remove_if(members.begin(), members.end(),
- [client](const Member& member) { return member.peer == client; }),
- members.end());
- }
-
- // Announce the change to all clients.
- enet_peer_disconnect(client, 0);
- BroadcastRoomInformation();
-}
-
-// Room
-Room::Room() : room_impl{std::make_unique<RoomImpl>()} {}
-
-Room::~Room() = default;
-
-void Room::Create(const std::string& name, const std::string& server_address, u16 server_port) {
- ENetAddress address;
- address.host = ENET_HOST_ANY;
- if (!server_address.empty()) {
- enet_address_set_host(&address, server_address.c_str());
- }
- address.port = server_port;
-
- room_impl->server = enet_host_create(&address, MaxConcurrentConnections, NumChannels, 0, 0);
- // TODO(B3N30): Allow specifying the maximum number of concurrent connections.
- room_impl->state = State::Open;
-
- room_impl->room_information.name = name;
- room_impl->room_information.member_slots = MaxConcurrentConnections;
- room_impl->StartLoop();
-}
-
-Room::State Room::GetState() const {
- return room_impl->state;
-}
-
-const RoomInformation& Room::GetRoomInformation() const {
- return room_impl->room_information;
-}
-
-std::vector<Room::Member> Room::GetRoomMemberList() const {
- std::vector<Room::Member> member_list;
- std::lock_guard<std::mutex> lock(room_impl->member_mutex);
- for (const auto& member_impl : room_impl->members) {
- Member member;
- member.nickname = member_impl.nickname;
- member.mac_address = member_impl.mac_address;
- member.game_info = member_impl.game_info;
- member_list.push_back(member);
- }
- return member_list;
-};
-
-void Room::Destroy() {
- room_impl->state = State::Closed;
- room_impl->room_thread->join();
- room_impl->room_thread.reset();
-
- if (room_impl->server) {
- enet_host_destroy(room_impl->server);
- }
- room_impl->room_information = {};
- room_impl->server = nullptr;
- {
- std::lock_guard<std::mutex> lock(room_impl->member_mutex);
- room_impl->members.clear();
- }
- room_impl->room_information.member_slots = 0;
- room_impl->room_information.name.clear();
-}
-
-} // namespace Network
diff --git a/src/network/room.h b/src/network/room.h
deleted file mode 100644
index 8285a4d0c..000000000
--- a/src/network/room.h
+++ /dev/null
@@ -1,101 +0,0 @@
-// Copyright 2017 Citra Emulator Project
-// Licensed under GPLv2 or any later version
-// Refer to the license.txt file included.
-
-#pragma once
-
-#include <array>
-#include <memory>
-#include <string>
-#include <vector>
-#include "common/common_types.h"
-
-namespace Network {
-
-constexpr u32 network_version = 1; ///< The version of this Room and RoomMember
-
-constexpr u16 DefaultRoomPort = 1234;
-constexpr size_t NumChannels = 1; // Number of channels used for the connection
-
-struct RoomInformation {
- std::string name; ///< Name of the server
- u32 member_slots; ///< Maximum number of members in this room
-};
-
-struct GameInfo {
- std::string name{""};
- u64 id{0};
-};
-
-using MacAddress = std::array<u8, 6>;
-/// A special MAC address that tells the room we're joining to assign us a MAC address
-/// automatically.
-constexpr MacAddress NoPreferredMac = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
-
-// 802.11 broadcast MAC address
-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
-enum RoomMessageTypes : u8 {
- IdJoinRequest = 1,
- IdJoinSuccess,
- IdRoomInformation,
- IdSetGameInfo,
- IdWifiPacket,
- IdChatMessage,
- IdNameCollision,
- IdMacCollision,
- IdVersionMismatch,
- IdCloseRoom
-};
-
-/// This is what a server [person creating a server] would use.
-class Room final {
-public:
- enum class State : u8 {
- Open, ///< The room is open and ready to accept connections.
- Closed, ///< The room is not opened and can not accept connections.
- };
-
- struct Member {
- std::string nickname; ///< The nickname of the member.
- GameInfo game_info; ///< The current game of the member
- MacAddress mac_address; ///< The assigned mac address of the member.
- };
-
- Room();
- ~Room();
-
- /**
- * Gets the current state of the room.
- */
- State GetState() const;
-
- /**
- * Gets the room information of the room.
- */
- const RoomInformation& GetRoomInformation() const;
-
- /**
- * Gets a list of the mbmers connected to the room.
- */
- std::vector<Member> GetRoomMemberList() const;
-
- /**
- * Creates the socket for this room. Will bind to default address if
- * server is empty string.
- */
- void Create(const std::string& name, const std::string& server = "",
- u16 server_port = DefaultRoomPort);
-
- /**
- * Destroys the socket
- */
- void Destroy();
-
-private:
- class RoomImpl;
- std::unique_ptr<RoomImpl> room_impl;
-};
-
-} // namespace Network
diff --git a/src/network/room_member.cpp b/src/network/room_member.cpp
deleted file mode 100644
index f229ec6fd..000000000
--- a/src/network/room_member.cpp
+++ /dev/null
@@ -1,490 +0,0 @@
-// Copyright 2017 Citra Emulator Project
-// Licensed under GPLv2 or any later version
-// Refer to the license.txt file included.
-
-#include <atomic>
-#include <list>
-#include <mutex>
-#include <set>
-#include <thread>
-#include "common/assert.h"
-#include "enet/enet.h"
-#include "network/packet.h"
-#include "network/room_member.h"
-
-namespace Network {
-
-constexpr u32 ConnectionTimeoutMs = 5000;
-
-class RoomMember::RoomMemberImpl {
-public:
- ENetHost* client = nullptr; ///< ENet network interface.
- ENetPeer* server = nullptr; ///< The server peer the client is connected to
-
- /// Information about the clients connected to the same room as us.
- MemberList member_information;
- /// Information about the room we're connected to.
- RoomInformation room_information;
-
- /// The current game name, id and version
- GameInfo current_game_info;
-
- std::atomic<State> state{State::Idle}; ///< Current state of the RoomMember.
- void SetState(const State new_state);
- bool IsConnected() const;
-
- std::string nickname; ///< The nickname of this member.
- MacAddress mac_address; ///< The mac_address of this member.
-
- std::mutex network_mutex; ///< Mutex that controls access to the `client` variable.
- /// Thread that receives and dispatches network packets
- std::unique_ptr<std::thread> loop_thread;
- 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
-
- template <typename T>
- using CallbackSet = std::set<CallbackHandle<T>>;
- std::mutex callback_mutex; ///< The mutex used for handling callbacks
-
- class Callbacks {
- public:
- template <typename T>
- CallbackSet<T>& Get();
-
- private:
- CallbackSet<WifiPacket> callback_set_wifi_packet;
- CallbackSet<ChatEntry> callback_set_chat_messages;
- CallbackSet<RoomInformation> callback_set_room_information;
- CallbackSet<State> callback_set_state;
- };
- Callbacks callbacks; ///< All CallbackSets to all events
-
- 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);
-
- /**
- * Sends a request to the server, asking for permission to join a room with the specified
- * nickname and preferred mac.
- * @params nickname The desired nickname.
- * @params preferred_mac The preferred MAC address to use in the room, the NoPreferredMac tells
- * the server to assign one for us.
- */
- void SendJoinRequest(const std::string& nickname,
- const MacAddress& preferred_mac = NoPreferredMac);
-
- /**
- * Extracts a MAC Address from a received ENet packet.
- * @param event The ENet event that was received.
- */
- void HandleJoinPacket(const ENetEvent* event);
- /**
- * Extracts RoomInformation and MemberInformation from a received RakNet packet.
- * @param event The ENet event that was received.
- */
- void HandleRoomInformationPacket(const ENetEvent* event);
-
- /**
- * Extracts a WifiPacket from a received ENet packet.
- * @param event The ENet event that was received.
- */
- void HandleWifiPackets(const ENetEvent* event);
-
- /**
- * Extracts a chat entry from a received ENet packet and adds it to the chat queue.
- * @param event The ENet event that was received.
- */
- void HandleChatPacket(const ENetEvent* event);
-
- /**
- * Disconnects the RoomMember from the Room
- */
- void Disconnect();
-
- template <typename T>
- void Invoke(const T& data);
-
- template <typename T>
- CallbackHandle<T> Bind(std::function<void(const T&)> callback);
-};
-
-// RoomMemberImpl
-void RoomMember::RoomMemberImpl::SetState(const State new_state) {
- if (state != new_state) {
- state = new_state;
- Invoke<State>(state);
- }
-}
-
-bool RoomMember::RoomMemberImpl::IsConnected() const {
- return state == State::Joining || state == State::Joined;
-}
-
-void RoomMember::RoomMemberImpl::MemberLoop() {
- // Receive packets while the connection is open
- while (IsConnected()) {
- std::lock_guard<std::mutex> lock(network_mutex);
- ENetEvent event;
- if (enet_host_service(client, &event, 100) > 0) {
- switch (event.type) {
- case ENET_EVENT_TYPE_RECEIVE:
- switch (event.packet->data[0]) {
- case IdWifiPacket:
- HandleWifiPackets(&event);
- break;
- case IdChatMessage:
- HandleChatPacket(&event);
- break;
- case IdRoomInformation:
- HandleRoomInformationPacket(&event);
- break;
- case IdJoinSuccess:
- // The join request was successful, we are now in the room.
- // If we joined successfully, there must be at least one client in the room: us.
- ASSERT_MSG(member_information.size() > 0,
- "We have not yet received member information.");
- HandleJoinPacket(&event); // Get the MAC Address for the client
- SetState(State::Joined);
- break;
- case IdNameCollision:
- SetState(State::NameCollision);
- break;
- case IdMacCollision:
- SetState(State::MacCollision);
- break;
- case IdVersionMismatch:
- SetState(State::WrongVersion);
- break;
- case IdCloseRoom:
- SetState(State::LostConnection);
- break;
- }
- enet_packet_destroy(event.packet);
- break;
- case ENET_EVENT_TYPE_DISCONNECT:
- SetState(State::LostConnection);
- break;
- }
- }
- {
- std::lock_guard<std::mutex> lock(send_list_mutex);
- for (const auto& packet : send_list) {
- ENetPacket* enetPacket = enet_packet_create(packet.GetData(), packet.GetDataSize(),
- ENET_PACKET_FLAG_RELIABLE);
- enet_peer_send(server, 0, enetPacket);
- }
- enet_host_flush(client);
- send_list.clear();
- }
- }
- Disconnect();
-};
-
-void RoomMember::RoomMemberImpl::StartLoop() {
- loop_thread = std::make_unique<std::thread>(&RoomMember::RoomMemberImpl::MemberLoop, this);
-}
-
-void RoomMember::RoomMemberImpl::Send(Packet&& packet) {
- std::lock_guard<std::mutex> lock(send_list_mutex);
- send_list.push_back(std::move(packet));
-}
-
-void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname,
- const MacAddress& preferred_mac) {
- Packet packet;
- packet << static_cast<u8>(IdJoinRequest);
- packet << nickname;
- packet << preferred_mac;
- packet << network_version;
- Send(std::move(packet));
-}
-
-void RoomMember::RoomMemberImpl::HandleRoomInformationPacket(const ENetEvent* event) {
- Packet packet;
- packet.Append(event->packet->data, event->packet->dataLength);
-
- // Ignore the first byte, which is the message id.
- packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
-
- RoomInformation info{};
- packet >> info.name;
- packet >> info.member_slots;
- room_information.name = info.name;
- room_information.member_slots = info.member_slots;
-
- u32 num_members;
- packet >> num_members;
- member_information.resize(num_members);
-
- for (auto& member : member_information) {
- packet >> member.nickname;
- packet >> member.mac_address;
- packet >> member.game_info.name;
- packet >> member.game_info.id;
- }
- Invoke(room_information);
-}
-
-void RoomMember::RoomMemberImpl::HandleJoinPacket(const ENetEvent* event) {
- Packet packet;
- packet.Append(event->packet->data, event->packet->dataLength);
-
- // Ignore the first byte, which is the message id.
- packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
-
- // Parse the MAC Address from the packet
- packet >> mac_address;
- SetState(State::Joined);
-}
-
-void RoomMember::RoomMemberImpl::HandleWifiPackets(const ENetEvent* event) {
- WifiPacket wifi_packet{};
- Packet packet;
- packet.Append(event->packet->data, event->packet->dataLength);
-
- // Ignore the first byte, which is the message id.
- packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
-
- // Parse the WifiPacket from the packet
- u8 frame_type;
- packet >> frame_type;
- WifiPacket::PacketType type = static_cast<WifiPacket::PacketType>(frame_type);
-
- wifi_packet.type = type;
- packet >> wifi_packet.channel;
- packet >> wifi_packet.transmitter_address;
- packet >> wifi_packet.destination_address;
-
- u32 data_length;
- packet >> data_length;
-
- packet >> wifi_packet.data;
-
- Invoke<WifiPacket>(wifi_packet);
-}
-
-void RoomMember::RoomMemberImpl::HandleChatPacket(const ENetEvent* event) {
- Packet packet;
- packet.Append(event->packet->data, event->packet->dataLength);
-
- // Ignore the first byte, which is the message id.
- packet.IgnoreBytes(sizeof(u8));
-
- ChatEntry chat_entry{};
- packet >> chat_entry.nickname;
- packet >> chat_entry.message;
- Invoke<ChatEntry>(chat_entry);
-}
-
-void RoomMember::RoomMemberImpl::Disconnect() {
- member_information.clear();
- room_information.member_slots = 0;
- room_information.name.clear();
-
- if (!server)
- return;
- enet_peer_disconnect(server, 0);
-
- ENetEvent event;
- while (enet_host_service(client, &event, ConnectionTimeoutMs) > 0) {
- switch (event.type) {
- case ENET_EVENT_TYPE_RECEIVE:
- enet_packet_destroy(event.packet); // Ignore all incoming data
- break;
- case ENET_EVENT_TYPE_DISCONNECT:
- server = nullptr;
- return;
- }
- }
- // didn't disconnect gracefully force disconnect
- enet_peer_reset(server);
- server = nullptr;
-}
-
-template <>
-RoomMember::RoomMemberImpl::CallbackSet<WifiPacket>& RoomMember::RoomMemberImpl::Callbacks::Get() {
- return callback_set_wifi_packet;
-}
-
-template <>
-RoomMember::RoomMemberImpl::CallbackSet<RoomMember::State>&
-RoomMember::RoomMemberImpl::Callbacks::Get() {
- return callback_set_state;
-}
-
-template <>
-RoomMember::RoomMemberImpl::CallbackSet<RoomInformation>&
-RoomMember::RoomMemberImpl::Callbacks::Get() {
- return callback_set_room_information;
-}
-
-template <>
-RoomMember::RoomMemberImpl::CallbackSet<ChatEntry>& RoomMember::RoomMemberImpl::Callbacks::Get() {
- return callback_set_chat_messages;
-}
-
-template <typename T>
-void RoomMember::RoomMemberImpl::Invoke(const T& data) {
- std::lock_guard<std::mutex> lock(callback_mutex);
- CallbackSet<T> callback_set = callbacks.Get<T>();
- for (auto const& callback : callback_set)
- (*callback)(data);
-}
-
-template <typename T>
-RoomMember::CallbackHandle<T> RoomMember::RoomMemberImpl::Bind(
- std::function<void(const T&)> callback) {
- std::lock_guard<std::mutex> lock(callback_mutex);
- CallbackHandle<T> handle;
- handle = std::make_shared<std::function<void(const T&)>>(callback);
- callbacks.Get<T>().insert(handle);
- return handle;
-}
-
-// RoomMember
-RoomMember::RoomMember() : room_member_impl{std::make_unique<RoomMemberImpl>()} {
- room_member_impl->client = enet_host_create(nullptr, 1, NumChannels, 0, 0);
- ASSERT_MSG(room_member_impl->client != nullptr, "Could not create client");
-}
-
-RoomMember::~RoomMember() {
- ASSERT_MSG(!IsConnected(), "RoomMember is being destroyed while connected");
- enet_host_destroy(room_member_impl->client);
-}
-
-RoomMember::State RoomMember::GetState() const {
- return room_member_impl->state;
-}
-
-const RoomMember::MemberList& RoomMember::GetMemberInformation() const {
- return room_member_impl->member_information;
-}
-
-const std::string& RoomMember::GetNickname() const {
- return room_member_impl->nickname;
-}
-
-const MacAddress& RoomMember::GetMacAddress() const {
- ASSERT_MSG(IsConnected(), "Tried to get MAC address while not connected");
- return room_member_impl->mac_address;
-}
-
-RoomInformation RoomMember::GetRoomInformation() const {
- return room_member_impl->room_information;
-}
-
-void RoomMember::Join(const std::string& nick, const char* server_addr, u16 server_port,
- u16 client_port, const MacAddress& preferred_mac) {
- // If the member is connected, kill the connection first
- if (room_member_impl->loop_thread && room_member_impl->loop_thread->joinable()) {
- room_member_impl->SetState(State::Error);
- room_member_impl->loop_thread->join();
- room_member_impl->loop_thread.reset();
- }
- // If the thread isn't running but the ptr still exists, reset it
- else if (room_member_impl->loop_thread) {
- room_member_impl->loop_thread.reset();
- }
-
- ENetAddress address{};
- enet_address_set_host(&address, server_addr);
- address.port = server_port;
- room_member_impl->server =
- enet_host_connect(room_member_impl->client, &address, NumChannels, 0);
-
- if (!room_member_impl->server) {
- room_member_impl->SetState(State::Error);
- return;
- }
-
- ENetEvent event{};
- int net = enet_host_service(room_member_impl->client, &event, ConnectionTimeoutMs);
- if (net > 0 && event.type == ENET_EVENT_TYPE_CONNECT) {
- room_member_impl->nickname = nick;
- room_member_impl->SetState(State::Joining);
- room_member_impl->StartLoop();
- room_member_impl->SendJoinRequest(nick, preferred_mac);
- SendGameInfo(room_member_impl->current_game_info);
- } else {
- room_member_impl->SetState(State::CouldNotConnect);
- }
-}
-
-bool RoomMember::IsConnected() const {
- return room_member_impl->IsConnected();
-}
-
-void RoomMember::SendWifiPacket(const WifiPacket& wifi_packet) {
- Packet packet;
- 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 << wifi_packet.data;
- room_member_impl->Send(std::move(packet));
-}
-
-void RoomMember::SendChatMessage(const std::string& message) {
- Packet packet;
- packet << static_cast<u8>(IdChatMessage);
- packet << message;
- room_member_impl->Send(std::move(packet));
-}
-
-void RoomMember::SendGameInfo(const GameInfo& game_info) {
- room_member_impl->current_game_info = game_info;
- if (!IsConnected())
- return;
-
- Packet packet;
- packet << static_cast<u8>(IdSetGameInfo);
- packet << game_info.name;
- packet << game_info.id;
- room_member_impl->Send(std::move(packet));
-}
-
-RoomMember::CallbackHandle<RoomMember::State> RoomMember::BindOnStateChanged(
- std::function<void(const RoomMember::State&)> callback) {
- return room_member_impl->Bind(callback);
-}
-
-RoomMember::CallbackHandle<WifiPacket> RoomMember::BindOnWifiPacketReceived(
- std::function<void(const WifiPacket&)> callback) {
- return room_member_impl->Bind(callback);
-}
-
-RoomMember::CallbackHandle<RoomInformation> RoomMember::BindOnRoomInformationChanged(
- std::function<void(const RoomInformation&)> callback) {
- return room_member_impl->Bind(callback);
-}
-
-RoomMember::CallbackHandle<ChatEntry> RoomMember::BindOnChatMessageRecieved(
- std::function<void(const ChatEntry&)> callback) {
- return room_member_impl->Bind(callback);
-}
-
-template <typename T>
-void RoomMember::Unbind(CallbackHandle<T> handle) {
- std::lock_guard<std::mutex> lock(room_member_impl->callback_mutex);
- room_member_impl->callbacks.Get<T>().erase(handle);
-}
-
-void RoomMember::Leave() {
- room_member_impl->SetState(State::Idle);
- room_member_impl->loop_thread->join();
- room_member_impl->loop_thread.reset();
-}
-
-template void RoomMember::Unbind(CallbackHandle<WifiPacket>);
-template void RoomMember::Unbind(CallbackHandle<RoomMember::State>);
-template void RoomMember::Unbind(CallbackHandle<RoomInformation>);
-template void RoomMember::Unbind(CallbackHandle<ChatEntry>);
-
-} // namespace Network
diff --git a/src/network/room_member.h b/src/network/room_member.h
deleted file mode 100644
index 98770a234..000000000
--- a/src/network/room_member.h
+++ /dev/null
@@ -1,182 +0,0 @@
-// Copyright 2017 Citra Emulator Project
-// Licensed under GPLv2 or any later version
-// Refer to the license.txt file included.
-
-#pragma once
-
-#include <functional>
-#include <memory>
-#include <string>
-#include <vector>
-#include "common/common_types.h"
-#include "network/room.h"
-
-namespace Network {
-
-/// Information about the received WiFi packets.
-/// Acts as our own 802.11 header.
-struct WifiPacket {
- 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.
- MacAddress transmitter_address; ///< Mac address of the transmitter.
- MacAddress destination_address; ///< Mac address of the receiver.
- u8 channel; ///< WiFi channel where this frame was transmitted.
-};
-
-/// Represents a chat message.
-struct ChatEntry {
- std::string nickname; ///< Nickname of the client who sent this message.
- std::string message; ///< Body of the message.
-};
-
-/**
- * This is what a client [person joining a server] would use.
- * It also has to be used if you host a game yourself (You'd create both, a Room and a
- * RoomMembership for yourself)
- */
-class RoomMember final {
-public:
- enum class State : u8 {
- Idle, ///< Default state
- Error, ///< Some error [permissions to network device missing or something]
- Joining, ///< The client is attempting to join a room.
- Joined, ///< The client is connected to the room and is ready to send/receive packets.
- LostConnection, ///< Connection closed
-
- // Reasons why connection was rejected
- NameCollision, ///< Somebody is already using this name
- MacCollision, ///< Somebody is already using that mac-address
- WrongVersion, ///< The room version is not the same as for this RoomMember
- CouldNotConnect ///< The room is not responding to a connection attempt
- };
-
- struct MemberInformation {
- std::string nickname; ///< Nickname of the member.
- GameInfo game_info; ///< Name of the game they're currently playing, or empty if they're
- /// not playing anything.
- MacAddress mac_address; ///< MAC address associated with this member.
- };
- using MemberList = std::vector<MemberInformation>;
-
- // The handle for the callback functions
- template <typename T>
- using CallbackHandle = std::shared_ptr<std::function<void(const T&)>>;
-
- /**
- * Unbinds a callback function from the events.
- * @param handle The connection handle to disconnect
- */
- template <typename T>
- void Unbind(CallbackHandle<T> handle);
-
- RoomMember();
- ~RoomMember();
-
- /**
- * Returns the status of our connection to the room.
- */
- State GetState() const;
-
- /**
- * Returns information about the members in the room we're currently connected to.
- */
- const MemberList& GetMemberInformation() const;
-
- /**
- * Returns the nickname of the RoomMember.
- */
- const std::string& GetNickname() const;
-
- /**
- * Returns the MAC address of the RoomMember.
- */
- const MacAddress& GetMacAddress() const;
-
- /**
- * Returns information about the room we're currently connected to.
- */
- RoomInformation GetRoomInformation() const;
-
- /**
- * Returns whether we're connected to a server or not.
- */
- bool IsConnected() const;
-
- /**
- * Attempts to join a room at the specified address and port, using the specified nickname.
- * This may fail if the username is already taken.
- */
- void Join(const std::string& nickname, const char* server_addr = "127.0.0.1",
- const u16 serverPort = DefaultRoomPort, const u16 clientPort = 0,
- const MacAddress& preferred_mac = NoPreferredMac);
-
- /**
- * Sends a WiFi packet to the room.
- * @param packet The WiFi packet to send.
- */
- void SendWifiPacket(const WifiPacket& packet);
-
- /**
- * Sends a chat message to the room.
- * @param message The contents of the message.
- */
- void SendChatMessage(const std::string& message);
-
- /**
- * Sends the current game info to the room.
- * @param game_info The game information.
- */
- void SendGameInfo(const GameInfo& game_info);
-
- /**
- * Binds a function to an event that will be triggered every time the State of the member
- * changed. The function wil be called every time the event is triggered. The callback function
- * must not bind or unbind a function. Doing so will cause a deadlock
- * @param callback The function to call
- * @return A handle used for removing the function from the registered list
- */
- CallbackHandle<State> BindOnStateChanged(std::function<void(const State&)> callback);
-
- /**
- * Binds a function to an event that will be triggered every time a WifiPacket is received.
- * The function wil be called everytime the event is triggered.
- * The callback function must not bind or unbind a function. Doing so will cause a deadlock
- * @param callback The function to call
- * @return A handle used for removing the function from the registered list
- */
- CallbackHandle<WifiPacket> BindOnWifiPacketReceived(
- std::function<void(const WifiPacket&)> callback);
-
- /**
- * Binds a function to an event that will be triggered every time the RoomInformation changes.
- * The function wil be called every time the event is triggered.
- * The callback function must not bind or unbind a function. Doing so will cause a deadlock
- * @param callback The function to call
- * @return A handle used for removing the function from the registered list
- */
- CallbackHandle<RoomInformation> BindOnRoomInformationChanged(
- std::function<void(const RoomInformation&)> callback);
-
- /**
- * Binds a function to an event that will be triggered every time a ChatMessage is received.
- * The function wil be called every time the event is triggered.
- * The callback function must not bind or unbind a function. Doing so will cause a deadlock
- * @param callback The function to call
- * @return A handle used for removing the function from the registered list
- */
- CallbackHandle<ChatEntry> BindOnChatMessageRecieved(
- std::function<void(const ChatEntry&)> callback);
-
- /**
- * Leaves the current room.
- */
- void Leave();
-
-private:
- class RoomMemberImpl;
- std::unique_ptr<RoomMemberImpl> room_member_impl;
-};
-
-} // namespace Network