Skip to content
Snippets Groups Projects
Commit dcfe0a5f authored by FearlessTobi's avatar FearlessTobi
Browse files

network: Add initial files and enet dependency

parent 1bcde9dd
No related branches found
No related tags found
No related merge requests found
[submodule "enet"]
path = externals/enet
url = https://github.com/lsalzman/enet.git
[submodule "inih"] [submodule "inih"]
path = externals/inih/inih path = externals/inih/inih
url = https://github.com/benhoyt/inih.git url = https://github.com/benhoyt/inih.git
......
...@@ -73,6 +73,10 @@ if (YUZU_USE_EXTERNAL_SDL2) ...@@ -73,6 +73,10 @@ if (YUZU_USE_EXTERNAL_SDL2)
add_library(SDL2 ALIAS SDL2-static) add_library(SDL2 ALIAS SDL2-static)
endif() endif()
# ENet
add_subdirectory(enet)
target_include_directories(enet INTERFACE ./enet/include)
# Cubeb # Cubeb
if(ENABLE_CUBEB) if(ENABLE_CUBEB)
set(BUILD_TESTS OFF CACHE BOOL "") set(BUILD_TESTS OFF CACHE BOOL "")
......
Subproject commit 39a72ab1990014eb399cee9d538fd529df99c6a0
...@@ -156,6 +156,7 @@ add_subdirectory(common) ...@@ -156,6 +156,7 @@ add_subdirectory(common)
add_subdirectory(core) add_subdirectory(core)
add_subdirectory(audio_core) add_subdirectory(audio_core)
add_subdirectory(video_core) add_subdirectory(video_core)
add_subdirectory(network)
add_subdirectory(input_common) add_subdirectory(input_common)
add_subdirectory(shader_recompiler) add_subdirectory(shader_recompiler)
......
add_library(network STATIC
network.cpp
network.h
packet.cpp
packet.h
room.cpp
room.h
room_member.cpp
room_member.h
verify_user.cpp
verify_user.h
)
create_target_directory_groups(network)
target_link_libraries(network PRIVATE common enet Boost::boost)
// 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
// 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
// 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& 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
// 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;
*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;
*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
This diff is collapsed.
// 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"
#include "network/verify_user.h"
namespace Network {
constexpr u32 network_version = 4; ///< The version of this Room and RoomMember
constexpr u16 DefaultRoomPort = 24872;
constexpr u32 MaxMessageSize = 500;
/// Maximum number of concurrent connections allowed to this room.
static constexpr u32 MaxConcurrentConnections = 254;
constexpr std::size_t NumChannels = 1; // Number of channels used for the connection
struct RoomInformation {
std::string name; ///< Name of the server
std::string description; ///< Server description
u32 member_slots; ///< Maximum number of members in this room
u16 port; ///< The port of this room
std::string preferred_game; ///< Game to advertise that you want to play
u64 preferred_game_id; ///< Title ID for the advertised game
std::string host_username; ///< Forum username of the host
bool enable_citra_mods; ///< Allow Citra Moderators to moderate on 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,
IdWrongPassword,
IdCloseRoom,
IdRoomIsFull,
IdConsoleIdCollision,
IdStatusMessage,
IdHostKicked,
IdHostBanned,
/// Moderation requests
IdModKick,
IdModBan,
IdModUnban,
IdModGetBanList,
// Moderation responses
IdModBanListResponse,
IdModPermissionDenied,
IdModNoSuchUser,
IdJoinSuccessAsMod,
};
/// Types of system status messages
enum StatusMessageTypes : u8 {
IdMemberJoin = 1, ///< Member joining
IdMemberLeave, ///< Member leaving
IdMemberKicked, ///< A member is kicked from the room
IdMemberBanned, ///< A member is banned from the room
IdAddressUnbanned, ///< A username / ip address is unbanned from the room
};
/// 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.
std::string username; ///< The web services username of the member. Can be empty.
std::string display_name; ///< The web services display name of the member. Can be empty.
std::string avatar_url; ///< Url to the member's avatar. Can be empty.
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 the verify UID of this room.
*/
std::string GetVerifyUID() const;
/**
* Gets a list of the mbmers connected to the room.
*/
std::vector<Member> GetRoomMemberList() const;
/**
* Checks if the room is password protected
*/
bool HasPassword() const;
using UsernameBanList = std::vector<std::string>;
using IPBanList = std::vector<std::string>;
using BanList = std::pair<UsernameBanList, IPBanList>;
/**
* Creates the socket for this room. Will bind to default address if
* server is empty string.
*/
bool Create(const std::string& name, const std::string& description = "",
const std::string& server = "", u16 server_port = DefaultRoomPort,
const std::string& password = "",
const u32 max_connections = MaxConcurrentConnections,
const std::string& host_username = "", const std::string& preferred_game = "",
u64 preferred_game_id = 0,
std::unique_ptr<VerifyUser::Backend> verify_backend = nullptr,
const BanList& ban_list = {}, bool enable_citra_mods = false);
/**
* Sets the verification GUID of the room.
*/
void SetVerifyUID(const std::string& uid);
/**
* Gets the ban list (including banned forum usernames and IPs) of the room.
*/
BanList GetBanList() const;
/**
* Destroys the socket
*/
void Destroy();
private:
class RoomImpl;
std::unique_ptr<RoomImpl> room_impl;
};
} // namespace Network
This diff is collapsed.
// 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 <boost/serialization/vector.hpp>
#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,
Deauthentication,
NodeMap
};
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.
private:
template <class Archive>
void serialize(Archive& ar, const unsigned int) {
ar& type;
ar& data;
ar& transmitter_address;
ar& destination_address;
ar& channel;
}
friend class boost::serialization::access;
};
/// Represents a chat message.
struct ChatEntry {
std::string nickname; ///< Nickname of the client who sent this message.
/// Web services username of the client who sent this message, can be empty.
std::string username;
std::string message; ///< Body of the message.
};
/// Represents a system status message.
struct StatusMessageEntry {
StatusMessageTypes type; ///< Type of the message
/// Subject of the message. i.e. the user who is joining/leaving/being banned, etc.
std::string nickname;
std::string username;
};
/**
* 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 {
Uninitialized, ///< Not initialized
Idle, ///< Default state (i.e. not connected)
Joining, ///< The client is attempting to join a room.
Joined, ///< The client is connected to the room and is ready to send/receive packets.
Moderator, ///< The client is connnected to the room and is granted mod permissions.
};
enum class Error : u8 {
// Reasons why connection was closed
LostConnection, ///< Connection closed
HostKicked, ///< Kicked by the host
// Reasons why connection was rejected
UnknownError, ///< Some error [permissions to network device missing or something]
NameCollision, ///< Somebody is already using this name
MacCollision, ///< Somebody is already using that mac-address
ConsoleIdCollision, ///< Somebody in the room has the same Console ID
WrongVersion, ///< The room version is not the same as for this RoomMember
WrongPassword, ///< The password doesn't match the one from the Room
CouldNotConnect, ///< The room is not responding to a connection attempt
RoomIsFull, ///< Room is already at the maximum number of players
HostBanned, ///< The user is banned by the host
// Reasons why moderation request failed
PermissionDenied, ///< The user does not have mod permissions
NoSuchUser, ///< The nickname the user attempts to kick/ban does not exist
};
struct MemberInformation {
std::string nickname; ///< Nickname of the member.
std::string username; ///< The web services username of the member. Can be empty.
std::string display_name; ///< The web services display name of the member. Can be empty.
std::string avatar_url; ///< Url to the member's avatar. Can be empty.
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 username of the RoomMember.
*/
const std::string& GetUsername() 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.
* A console ID hash is passed in to check console ID conflicts.
* This may fail if the username or console ID is already taken.
*/
void Join(const std::string& nickname, const std::string& console_id_hash,
const char* server_addr = "127.0.0.1", u16 server_port = DefaultRoomPort,
u16 client_port = 0, const MacAddress& preferred_mac = NoPreferredMac,
const std::string& password = "", const std::string& token = "");
/**
* 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);
/**
* Sends a moderation request to the room.
* @param type Moderation request type.
* @param nickname The subject of the request. (i.e. the user you want to kick/ban)
*/
void SendModerationRequest(RoomMessageTypes type, const std::string& nickname);
/**
* Attempts to retrieve ban list from the room.
* If success, the ban list callback would be called. Otherwise an error would be emitted.
*/
void RequestBanList();
/**
* 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 an error happened. 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<Error> BindOnError(std::function<void(const Error&)> 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);
/**
* Binds a function to an event that will be triggered every time a StatusMessage is
* received. The function will 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<StatusMessageEntry> BindOnStatusMessageReceived(
std::function<void(const StatusMessageEntry&)> callback);
/**
* Binds a function to an event that will be triggered every time a requested ban list
* received. The function will 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<Room::BanList> BindOnBanListReceived(
std::function<void(const Room::BanList&)> callback);
/**
* Leaves the current room.
*/
void Leave();
private:
class RoomMemberImpl;
std::unique_ptr<RoomMemberImpl> room_member_impl;
};
inline const char* GetStateStr(const RoomMember::State& s) {
switch (s) {
case RoomMember::State::Uninitialized:
return "Uninitialized";
case RoomMember::State::Idle:
return "Idle";
case RoomMember::State::Joining:
return "Joining";
case RoomMember::State::Joined:
return "Joined";
case RoomMember::State::Moderator:
return "Moderator";
}
return "Unknown";
}
inline const char* GetErrorStr(const RoomMember::Error& e) {
switch (e) {
case RoomMember::Error::LostConnection:
return "LostConnection";
case RoomMember::Error::HostKicked:
return "HostKicked";
case RoomMember::Error::UnknownError:
return "UnknownError";
case RoomMember::Error::NameCollision:
return "NameCollision";
case RoomMember::Error::MacCollision:
return "MaxCollision";
case RoomMember::Error::ConsoleIdCollision:
return "ConsoleIdCollision";
case RoomMember::Error::WrongVersion:
return "WrongVersion";
case RoomMember::Error::WrongPassword:
return "WrongPassword";
case RoomMember::Error::CouldNotConnect:
return "CouldNotConnect";
case RoomMember::Error::RoomIsFull:
return "RoomIsFull";
case RoomMember::Error::HostBanned:
return "HostBanned";
case RoomMember::Error::PermissionDenied:
return "PermissionDenied";
case RoomMember::Error::NoSuchUser:
return "NoSuchUser";
default:
return "Unknown";
}
}
} // namespace Network
// Copyright 2018 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include "network/verify_user.h"
namespace Network::VerifyUser {
Backend::~Backend() = default;
NullBackend::~NullBackend() = default;
UserData NullBackend::LoadUserData([[maybe_unused]] const std::string& verify_UID,
[[maybe_unused]] const std::string& token) {
return {};
}
} // namespace Network::VerifyUser
// Copyright 2018 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <string>
#include "common/logging/log.h"
namespace Network::VerifyUser {
struct UserData {
std::string username;
std::string display_name;
std::string avatar_url;
bool moderator = false; ///< Whether the user is a Citra Moderator.
};
/**
* A backend used for verifying users and loading user data.
*/
class Backend {
public:
virtual ~Backend();
/**
* Verifies the given token and loads the information into a UserData struct.
* @param verify_UID A GUID that may be used for verification.
* @param token A token that contains user data and verification data. The format and content is
* decided by backends.
*/
virtual UserData LoadUserData(const std::string& verify_UID, const std::string& token) = 0;
};
/**
* A null backend where the token is ignored.
* No verification is performed here and the function returns an empty UserData.
*/
class NullBackend final : public Backend {
public:
~NullBackend();
UserData LoadUserData(const std::string& verify_UID, const std::string& token) override;
};
} // namespace Network::VerifyUser
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment