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