diff options
Diffstat (limited to 'src/network/room_member.h')
| -rw-r--r-- | src/network/room_member.h | 318 |
1 files changed, 318 insertions, 0 deletions
diff --git a/src/network/room_member.h b/src/network/room_member.h new file mode 100644 index 000000000..bbb7d13d4 --- /dev/null +++ b/src/network/room_member.h | |||
| @@ -0,0 +1,318 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include <functional> | ||
| 7 | #include <memory> | ||
| 8 | #include <string> | ||
| 9 | #include <vector> | ||
| 10 | #include "common/announce_multiplayer_room.h" | ||
| 11 | #include "common/common_types.h" | ||
| 12 | #include "network/room.h" | ||
| 13 | |||
| 14 | namespace Network { | ||
| 15 | |||
| 16 | using AnnounceMultiplayerRoom::GameInfo; | ||
| 17 | using AnnounceMultiplayerRoom::RoomInformation; | ||
| 18 | |||
| 19 | /// Information about the received WiFi packets. | ||
| 20 | /// Acts as our own 802.11 header. | ||
| 21 | struct WifiPacket { | ||
| 22 | enum class PacketType : u8 { | ||
| 23 | Beacon, | ||
| 24 | Data, | ||
| 25 | Authentication, | ||
| 26 | AssociationResponse, | ||
| 27 | Deauthentication, | ||
| 28 | NodeMap | ||
| 29 | }; | ||
| 30 | PacketType type; ///< The type of 802.11 frame. | ||
| 31 | std::vector<u8> data; ///< Raw 802.11 frame data, starting at the management frame header | ||
| 32 | /// for management frames. | ||
| 33 | MacAddress transmitter_address; ///< Mac address of the transmitter. | ||
| 34 | MacAddress destination_address; ///< Mac address of the receiver. | ||
| 35 | u8 channel; ///< WiFi channel where this frame was transmitted. | ||
| 36 | }; | ||
| 37 | |||
| 38 | /// Represents a chat message. | ||
| 39 | struct ChatEntry { | ||
| 40 | std::string nickname; ///< Nickname of the client who sent this message. | ||
| 41 | /// Web services username of the client who sent this message, can be empty. | ||
| 42 | std::string username; | ||
| 43 | std::string message; ///< Body of the message. | ||
| 44 | }; | ||
| 45 | |||
| 46 | /// Represents a system status message. | ||
| 47 | struct StatusMessageEntry { | ||
| 48 | StatusMessageTypes type; ///< Type of the message | ||
| 49 | /// Subject of the message. i.e. the user who is joining/leaving/being banned, etc. | ||
| 50 | std::string nickname; | ||
| 51 | std::string username; | ||
| 52 | }; | ||
| 53 | |||
| 54 | /** | ||
| 55 | * This is what a client [person joining a server] would use. | ||
| 56 | * It also has to be used if you host a game yourself (You'd create both, a Room and a | ||
| 57 | * RoomMembership for yourself) | ||
| 58 | */ | ||
| 59 | class RoomMember final { | ||
| 60 | public: | ||
| 61 | enum class State : u8 { | ||
| 62 | Uninitialized, ///< Not initialized | ||
| 63 | Idle, ///< Default state (i.e. not connected) | ||
| 64 | Joining, ///< The client is attempting to join a room. | ||
| 65 | Joined, ///< The client is connected to the room and is ready to send/receive packets. | ||
| 66 | Moderator, ///< The client is connnected to the room and is granted mod permissions. | ||
| 67 | }; | ||
| 68 | |||
| 69 | enum class Error : u8 { | ||
| 70 | // Reasons why connection was closed | ||
| 71 | LostConnection, ///< Connection closed | ||
| 72 | HostKicked, ///< Kicked by the host | ||
| 73 | |||
| 74 | // Reasons why connection was rejected | ||
| 75 | UnknownError, ///< Some error [permissions to network device missing or something] | ||
| 76 | NameCollision, ///< Somebody is already using this name | ||
| 77 | MacCollision, ///< Somebody is already using that mac-address | ||
| 78 | ConsoleIdCollision, ///< Somebody in the room has the same Console ID | ||
| 79 | WrongVersion, ///< The room version is not the same as for this RoomMember | ||
| 80 | WrongPassword, ///< The password doesn't match the one from the Room | ||
| 81 | CouldNotConnect, ///< The room is not responding to a connection attempt | ||
| 82 | RoomIsFull, ///< Room is already at the maximum number of players | ||
| 83 | HostBanned, ///< The user is banned by the host | ||
| 84 | |||
| 85 | // Reasons why moderation request failed | ||
| 86 | PermissionDenied, ///< The user does not have mod permissions | ||
| 87 | NoSuchUser, ///< The nickname the user attempts to kick/ban does not exist | ||
| 88 | }; | ||
| 89 | |||
| 90 | struct MemberInformation { | ||
| 91 | std::string nickname; ///< Nickname of the member. | ||
| 92 | std::string username; ///< The web services username of the member. Can be empty. | ||
| 93 | std::string display_name; ///< The web services display name of the member. Can be empty. | ||
| 94 | std::string avatar_url; ///< Url to the member's avatar. Can be empty. | ||
| 95 | GameInfo game_info; ///< Name of the game they're currently playing, or empty if they're | ||
| 96 | /// not playing anything. | ||
| 97 | MacAddress mac_address; ///< MAC address associated with this member. | ||
| 98 | }; | ||
| 99 | using MemberList = std::vector<MemberInformation>; | ||
| 100 | |||
| 101 | // The handle for the callback functions | ||
| 102 | template <typename T> | ||
| 103 | using CallbackHandle = std::shared_ptr<std::function<void(const T&)>>; | ||
| 104 | |||
| 105 | /** | ||
| 106 | * Unbinds a callback function from the events. | ||
| 107 | * @param handle The connection handle to disconnect | ||
| 108 | */ | ||
| 109 | template <typename T> | ||
| 110 | void Unbind(CallbackHandle<T> handle); | ||
| 111 | |||
| 112 | RoomMember(); | ||
| 113 | ~RoomMember(); | ||
| 114 | |||
| 115 | /** | ||
| 116 | * Returns the status of our connection to the room. | ||
| 117 | */ | ||
| 118 | State GetState() const; | ||
| 119 | |||
| 120 | /** | ||
| 121 | * Returns information about the members in the room we're currently connected to. | ||
| 122 | */ | ||
| 123 | const MemberList& GetMemberInformation() const; | ||
| 124 | |||
| 125 | /** | ||
| 126 | * Returns the nickname of the RoomMember. | ||
| 127 | */ | ||
| 128 | const std::string& GetNickname() const; | ||
| 129 | |||
| 130 | /** | ||
| 131 | * Returns the username of the RoomMember. | ||
| 132 | */ | ||
| 133 | const std::string& GetUsername() const; | ||
| 134 | |||
| 135 | /** | ||
| 136 | * Returns the MAC address of the RoomMember. | ||
| 137 | */ | ||
| 138 | const MacAddress& GetMacAddress() const; | ||
| 139 | |||
| 140 | /** | ||
| 141 | * Returns information about the room we're currently connected to. | ||
| 142 | */ | ||
| 143 | RoomInformation GetRoomInformation() const; | ||
| 144 | |||
| 145 | /** | ||
| 146 | * Returns whether we're connected to a server or not. | ||
| 147 | */ | ||
| 148 | bool IsConnected() const; | ||
| 149 | |||
| 150 | /** | ||
| 151 | * Attempts to join a room at the specified address and port, using the specified nickname. | ||
| 152 | * A console ID hash is passed in to check console ID conflicts. | ||
| 153 | * This may fail if the username or console ID is already taken. | ||
| 154 | */ | ||
| 155 | void Join(const std::string& nickname, const std::string& console_id_hash, | ||
| 156 | const char* server_addr = "127.0.0.1", u16 server_port = DefaultRoomPort, | ||
| 157 | u16 client_port = 0, const MacAddress& preferred_mac = NoPreferredMac, | ||
| 158 | const std::string& password = "", const std::string& token = ""); | ||
| 159 | |||
| 160 | /** | ||
| 161 | * Sends a WiFi packet to the room. | ||
| 162 | * @param packet The WiFi packet to send. | ||
| 163 | */ | ||
| 164 | void SendWifiPacket(const WifiPacket& packet); | ||
| 165 | |||
| 166 | /** | ||
| 167 | * Sends a chat message to the room. | ||
| 168 | * @param message The contents of the message. | ||
| 169 | */ | ||
| 170 | void SendChatMessage(const std::string& message); | ||
| 171 | |||
| 172 | /** | ||
| 173 | * Sends the current game info to the room. | ||
| 174 | * @param game_info The game information. | ||
| 175 | */ | ||
| 176 | void SendGameInfo(const GameInfo& game_info); | ||
| 177 | |||
| 178 | /** | ||
| 179 | * Sends a moderation request to the room. | ||
| 180 | * @param type Moderation request type. | ||
| 181 | * @param nickname The subject of the request. (i.e. the user you want to kick/ban) | ||
| 182 | */ | ||
| 183 | void SendModerationRequest(RoomMessageTypes type, const std::string& nickname); | ||
| 184 | |||
| 185 | /** | ||
| 186 | * Attempts to retrieve ban list from the room. | ||
| 187 | * If success, the ban list callback would be called. Otherwise an error would be emitted. | ||
| 188 | */ | ||
| 189 | void RequestBanList(); | ||
| 190 | |||
| 191 | /** | ||
| 192 | * Binds a function to an event that will be triggered every time the State of the member | ||
| 193 | * changed. The function wil be called every time the event is triggered. The callback function | ||
| 194 | * must not bind or unbind a function. Doing so will cause a deadlock | ||
| 195 | * @param callback The function to call | ||
| 196 | * @return A handle used for removing the function from the registered list | ||
| 197 | */ | ||
| 198 | CallbackHandle<State> BindOnStateChanged(std::function<void(const State&)> callback); | ||
| 199 | |||
| 200 | /** | ||
| 201 | * Binds a function to an event that will be triggered every time an error happened. The | ||
| 202 | * function wil be called every time the event is triggered. The callback function must not bind | ||
| 203 | * or unbind a function. Doing so will cause a deadlock | ||
| 204 | * @param callback The function to call | ||
| 205 | * @return A handle used for removing the function from the registered list | ||
| 206 | */ | ||
| 207 | CallbackHandle<Error> BindOnError(std::function<void(const Error&)> callback); | ||
| 208 | |||
| 209 | /** | ||
| 210 | * Binds a function to an event that will be triggered every time a WifiPacket is received. | ||
| 211 | * The function wil be called everytime the event is triggered. | ||
| 212 | * The callback function must not bind or unbind a function. Doing so will cause a deadlock | ||
| 213 | * @param callback The function to call | ||
| 214 | * @return A handle used for removing the function from the registered list | ||
| 215 | */ | ||
| 216 | CallbackHandle<WifiPacket> BindOnWifiPacketReceived( | ||
| 217 | std::function<void(const WifiPacket&)> callback); | ||
| 218 | |||
| 219 | /** | ||
| 220 | * Binds a function to an event that will be triggered every time the RoomInformation changes. | ||
| 221 | * The function wil be called every time the event is triggered. | ||
| 222 | * The callback function must not bind or unbind a function. Doing so will cause a deadlock | ||
| 223 | * @param callback The function to call | ||
| 224 | * @return A handle used for removing the function from the registered list | ||
| 225 | */ | ||
| 226 | CallbackHandle<RoomInformation> BindOnRoomInformationChanged( | ||
| 227 | std::function<void(const RoomInformation&)> callback); | ||
| 228 | |||
| 229 | /** | ||
| 230 | * Binds a function to an event that will be triggered every time a ChatMessage is received. | ||
| 231 | * The function wil be called every time the event is triggered. | ||
| 232 | * The callback function must not bind or unbind a function. Doing so will cause a deadlock | ||
| 233 | * @param callback The function to call | ||
| 234 | * @return A handle used for removing the function from the registered list | ||
| 235 | */ | ||
| 236 | CallbackHandle<ChatEntry> BindOnChatMessageRecieved( | ||
| 237 | std::function<void(const ChatEntry&)> callback); | ||
| 238 | |||
| 239 | /** | ||
| 240 | * Binds a function to an event that will be triggered every time a StatusMessage is | ||
| 241 | * received. The function will be called every time the event is triggered. The callback | ||
| 242 | * function must not bind or unbind a function. Doing so will cause a deadlock | ||
| 243 | * @param callback The function to call | ||
| 244 | * @return A handle used for removing the function from the registered list | ||
| 245 | */ | ||
| 246 | CallbackHandle<StatusMessageEntry> BindOnStatusMessageReceived( | ||
| 247 | std::function<void(const StatusMessageEntry&)> callback); | ||
| 248 | |||
| 249 | /** | ||
| 250 | * Binds a function to an event that will be triggered every time a requested ban list | ||
| 251 | * received. The function will be called every time the event is triggered. The callback | ||
| 252 | * function must not bind or unbind a function. Doing so will cause a deadlock | ||
| 253 | * @param callback The function to call | ||
| 254 | * @return A handle used for removing the function from the registered list | ||
| 255 | */ | ||
| 256 | CallbackHandle<Room::BanList> BindOnBanListReceived( | ||
| 257 | std::function<void(const Room::BanList&)> callback); | ||
| 258 | |||
| 259 | /** | ||
| 260 | * Leaves the current room. | ||
| 261 | */ | ||
| 262 | void Leave(); | ||
| 263 | |||
| 264 | private: | ||
| 265 | class RoomMemberImpl; | ||
| 266 | std::unique_ptr<RoomMemberImpl> room_member_impl; | ||
| 267 | }; | ||
| 268 | |||
| 269 | inline const char* GetStateStr(const RoomMember::State& s) { | ||
| 270 | switch (s) { | ||
| 271 | case RoomMember::State::Uninitialized: | ||
| 272 | return "Uninitialized"; | ||
| 273 | case RoomMember::State::Idle: | ||
| 274 | return "Idle"; | ||
| 275 | case RoomMember::State::Joining: | ||
| 276 | return "Joining"; | ||
| 277 | case RoomMember::State::Joined: | ||
| 278 | return "Joined"; | ||
| 279 | case RoomMember::State::Moderator: | ||
| 280 | return "Moderator"; | ||
| 281 | } | ||
| 282 | return "Unknown"; | ||
| 283 | } | ||
| 284 | |||
| 285 | inline const char* GetErrorStr(const RoomMember::Error& e) { | ||
| 286 | switch (e) { | ||
| 287 | case RoomMember::Error::LostConnection: | ||
| 288 | return "LostConnection"; | ||
| 289 | case RoomMember::Error::HostKicked: | ||
| 290 | return "HostKicked"; | ||
| 291 | case RoomMember::Error::UnknownError: | ||
| 292 | return "UnknownError"; | ||
| 293 | case RoomMember::Error::NameCollision: | ||
| 294 | return "NameCollision"; | ||
| 295 | case RoomMember::Error::MacCollision: | ||
| 296 | return "MaxCollision"; | ||
| 297 | case RoomMember::Error::ConsoleIdCollision: | ||
| 298 | return "ConsoleIdCollision"; | ||
| 299 | case RoomMember::Error::WrongVersion: | ||
| 300 | return "WrongVersion"; | ||
| 301 | case RoomMember::Error::WrongPassword: | ||
| 302 | return "WrongPassword"; | ||
| 303 | case RoomMember::Error::CouldNotConnect: | ||
| 304 | return "CouldNotConnect"; | ||
| 305 | case RoomMember::Error::RoomIsFull: | ||
| 306 | return "RoomIsFull"; | ||
| 307 | case RoomMember::Error::HostBanned: | ||
| 308 | return "HostBanned"; | ||
| 309 | case RoomMember::Error::PermissionDenied: | ||
| 310 | return "PermissionDenied"; | ||
| 311 | case RoomMember::Error::NoSuchUser: | ||
| 312 | return "NoSuchUser"; | ||
| 313 | default: | ||
| 314 | return "Unknown"; | ||
| 315 | } | ||
| 316 | } | ||
| 317 | |||
| 318 | } // namespace Network | ||