2017-07-07 20:34:15 +01:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-08-19 18:14:33 +01:00
|
|
|
#include <functional>
|
2017-07-07 20:34:15 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2017-07-08 17:31:35 +01:00
|
|
|
#include <vector>
|
2017-07-07 20:34:15 +01:00
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "network/room.h"
|
|
|
|
|
|
|
|
namespace Network {
|
|
|
|
|
2017-07-09 09:40:11 +01:00
|
|
|
/// Information about the received WiFi packets.
|
|
|
|
/// Acts as our own 802.11 header.
|
|
|
|
struct WifiPacket {
|
2017-11-19 18:52:37 +00:00
|
|
|
enum class PacketType : u8 {
|
|
|
|
Beacon,
|
|
|
|
Data,
|
|
|
|
Authentication,
|
|
|
|
AssociationResponse,
|
2018-03-27 13:01:29 +01:00
|
|
|
Deauthentication,
|
|
|
|
NodeMap
|
2017-11-19 18:52:37 +00:00
|
|
|
};
|
2017-07-09 14:06:02 +01:00
|
|
|
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.
|
2017-07-09 09:40:11 +01:00
|
|
|
MacAddress transmitter_address; ///< Mac address of the transmitter.
|
|
|
|
MacAddress destination_address; ///< Mac address of the receiver.
|
2017-07-09 14:06:02 +01:00
|
|
|
u8 channel; ///< WiFi channel where this frame was transmitted.
|
2017-07-09 09:40:11 +01:00
|
|
|
};
|
|
|
|
|
2017-07-09 11:26:03 +01:00
|
|
|
/// Represents a chat message.
|
|
|
|
struct ChatEntry {
|
|
|
|
std::string nickname; ///< Nickname of the client who sent this message.
|
2018-10-27 08:35:01 +01:00
|
|
|
/// Web services username of the client who sent this message, can be empty.
|
|
|
|
std::string username;
|
|
|
|
std::string message; ///< Body of the message.
|
2017-07-09 11:26:03 +01:00
|
|
|
};
|
|
|
|
|
2018-11-09 13:55:57 +00:00
|
|
|
/// 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;
|
|
|
|
};
|
|
|
|
|
2017-07-07 20:34:15 +01:00
|
|
|
/**
|
|
|
|
* 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 {
|
2018-07-24 11:57:45 +01:00
|
|
|
Uninitialized, ///< Not initialized
|
2018-11-24 08:13:46 +00:00
|
|
|
Idle, ///< Default state (i.e. not connected)
|
2018-07-24 11:57:45 +01:00
|
|
|
Joining, ///< The client is attempting to join a room.
|
2018-12-15 09:13:46 +00:00
|
|
|
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.
|
2018-11-24 08:13:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum class Error : u8 {
|
|
|
|
// Reasons why connection was closed
|
2017-07-07 20:34:15 +01:00
|
|
|
LostConnection, ///< Connection closed
|
2018-11-24 08:13:46 +00:00
|
|
|
HostKicked, ///< Kicked by the host
|
2017-07-07 20:34:15 +01:00
|
|
|
|
|
|
|
// Reasons why connection was rejected
|
2018-11-24 08:13:46 +00:00
|
|
|
UnknownError, ///< Some error [permissions to network device missing or something]
|
2018-10-31 15:07:03 +00:00
|
|
|
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
|
2018-11-24 08:13:46 +00:00
|
|
|
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
|
2017-07-07 20:34:15 +01:00
|
|
|
};
|
|
|
|
|
2017-07-08 17:31:35 +01:00
|
|
|
struct MemberInformation {
|
2018-10-27 08:35:01 +01:00
|
|
|
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.
|
2017-08-19 18:14:33 +01:00
|
|
|
GameInfo game_info; ///< Name of the game they're currently playing, or empty if they're
|
2017-07-08 17:31:35 +01:00
|
|
|
/// not playing anything.
|
|
|
|
MacAddress mac_address; ///< MAC address associated with this member.
|
|
|
|
};
|
|
|
|
using MemberList = std::vector<MemberInformation>;
|
|
|
|
|
2017-08-19 18:14:33 +01:00
|
|
|
// 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);
|
|
|
|
|
2017-07-07 20:34:15 +01:00
|
|
|
RoomMember();
|
|
|
|
~RoomMember();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the status of our connection to the room.
|
|
|
|
*/
|
|
|
|
State GetState() const;
|
|
|
|
|
2017-07-08 17:31:35 +01:00
|
|
|
/**
|
|
|
|
* Returns information about the members in the room we're currently connected to.
|
|
|
|
*/
|
|
|
|
const MemberList& GetMemberInformation() const;
|
2017-07-09 14:06:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the nickname of the RoomMember.
|
|
|
|
*/
|
|
|
|
const std::string& GetNickname() const;
|
|
|
|
|
2018-10-27 08:35:01 +01:00
|
|
|
/**
|
|
|
|
* Returns the username of the RoomMember.
|
|
|
|
*/
|
|
|
|
const std::string& GetUsername() const;
|
|
|
|
|
2017-07-09 14:06:02 +01:00
|
|
|
/**
|
|
|
|
* Returns the MAC address of the RoomMember.
|
|
|
|
*/
|
|
|
|
const MacAddress& GetMacAddress() const;
|
|
|
|
|
2017-07-08 17:31:35 +01:00
|
|
|
/**
|
|
|
|
* Returns information about the room we're currently connected to.
|
|
|
|
*/
|
|
|
|
RoomInformation GetRoomInformation() const;
|
|
|
|
|
2017-07-07 20:34:15 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
2018-10-31 15:07:03 +00:00
|
|
|
* A console ID hash is passed in to check console ID conflicts.
|
|
|
|
* This may fail if the username or console ID is already taken.
|
2017-07-07 20:34:15 +01:00
|
|
|
*/
|
2018-10-31 15:07:03 +00:00
|
|
|
void Join(const std::string& nickname, const std::string& console_id_hash,
|
|
|
|
const char* server_addr = "127.0.0.1", const u16 server_port = DefaultRoomPort,
|
|
|
|
const u16 client_port = 0, const MacAddress& preferred_mac = NoPreferredMac,
|
2018-10-27 08:35:01 +01:00
|
|
|
const std::string& password = "", const std::string& token = "");
|
2017-07-07 20:34:15 +01:00
|
|
|
|
2017-07-09 09:40:11 +01:00
|
|
|
/**
|
|
|
|
* Sends a WiFi packet to the room.
|
|
|
|
* @param packet The WiFi packet to send.
|
|
|
|
*/
|
|
|
|
void SendWifiPacket(const WifiPacket& packet);
|
|
|
|
|
2017-07-09 11:26:03 +01:00
|
|
|
/**
|
|
|
|
* Sends a chat message to the room.
|
|
|
|
* @param message The contents of the message.
|
|
|
|
*/
|
|
|
|
void SendChatMessage(const std::string& message);
|
|
|
|
|
2017-07-09 14:06:02 +01:00
|
|
|
/**
|
2017-08-19 18:14:33 +01:00
|
|
|
* Sends the current game info to the room.
|
|
|
|
* @param game_info The game information.
|
|
|
|
*/
|
|
|
|
void SendGameInfo(const GameInfo& game_info);
|
|
|
|
|
2018-11-24 08:13:46 +00:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
|
2017-08-19 18:14:33 +01:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2018-11-24 08:13:46 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2017-08-19 18:14:33 +01:00
|
|
|
/**
|
|
|
|
* 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
|
2017-07-09 14:06:02 +01:00
|
|
|
*/
|
2017-08-19 18:14:33 +01:00
|
|
|
CallbackHandle<ChatEntry> BindOnChatMessageRecieved(
|
|
|
|
std::function<void(const ChatEntry&)> callback);
|
2017-07-09 14:06:02 +01:00
|
|
|
|
2018-11-09 13:55:57 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2018-11-24 08:13:46 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2017-07-07 20:34:15 +01:00
|
|
|
/**
|
|
|
|
* Leaves the current room.
|
|
|
|
*/
|
|
|
|
void Leave();
|
|
|
|
|
|
|
|
private:
|
|
|
|
class RoomMemberImpl;
|
|
|
|
std::unique_ptr<RoomMemberImpl> room_member_impl;
|
|
|
|
};
|
|
|
|
|
2019-07-14 19:01:13 +01:00
|
|
|
inline const char* GetStateStr(const RoomMember::State& s) {
|
2018-04-19 17:23:39 +01:00
|
|
|
switch (s) {
|
|
|
|
case RoomMember::State::Idle:
|
|
|
|
return "Idle";
|
|
|
|
case RoomMember::State::Joining:
|
|
|
|
return "Joining";
|
|
|
|
case RoomMember::State::Joined:
|
|
|
|
return "Joined";
|
2018-12-15 09:13:46 +00:00
|
|
|
case RoomMember::State::Moderator:
|
|
|
|
return "Moderator";
|
2018-11-24 08:13:46 +00:00
|
|
|
}
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
|
2019-07-14 19:01:13 +01:00
|
|
|
inline const char* GetErrorStr(const RoomMember::Error& e) {
|
2018-11-24 08:13:46 +00:00
|
|
|
switch (e) {
|
|
|
|
case RoomMember::Error::LostConnection:
|
2018-04-19 17:23:39 +01:00
|
|
|
return "LostConnection";
|
2018-11-24 08:13:46 +00:00
|
|
|
case RoomMember::Error::HostKicked:
|
|
|
|
return "HostKicked";
|
|
|
|
case RoomMember::Error::UnknownError:
|
|
|
|
return "UnknownError";
|
|
|
|
case RoomMember::Error::NameCollision:
|
2018-04-19 17:23:39 +01:00
|
|
|
return "NameCollision";
|
2018-11-24 08:13:46 +00:00
|
|
|
case RoomMember::Error::MacCollision:
|
|
|
|
return "MaxCollision";
|
|
|
|
case RoomMember::Error::ConsoleIdCollision:
|
|
|
|
return "ConsoleIdCollision";
|
|
|
|
case RoomMember::Error::WrongVersion:
|
2018-04-19 17:23:39 +01:00
|
|
|
return "WrongVersion";
|
2018-11-24 08:13:46 +00:00
|
|
|
case RoomMember::Error::WrongPassword:
|
2018-04-19 17:23:39 +01:00
|
|
|
return "WrongPassword";
|
2018-11-24 08:13:46 +00:00
|
|
|
case RoomMember::Error::CouldNotConnect:
|
2018-04-19 17:23:39 +01:00
|
|
|
return "CouldNotConnect";
|
2018-11-24 08:13:46 +00:00
|
|
|
case RoomMember::Error::RoomIsFull:
|
|
|
|
return "RoomIsFull";
|
|
|
|
case RoomMember::Error::HostBanned:
|
|
|
|
return "HostBanned";
|
|
|
|
case RoomMember::Error::PermissionDenied:
|
|
|
|
return "PermissionDenied";
|
|
|
|
case RoomMember::Error::NoSuchUser:
|
|
|
|
return "NoSuchUser";
|
2018-04-19 17:23:39 +01:00
|
|
|
}
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
|
2017-07-07 20:34:15 +01:00
|
|
|
} // namespace Network
|