room.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <memory>
  6. #include <string>
  7. #include <vector>
  8. #include "common/announce_multiplayer_room.h"
  9. #include "common/common_types.h"
  10. #include "common/socket_types.h"
  11. #include "network/verify_user.h"
  12. namespace Network {
  13. using AnnounceMultiplayerRoom::GameInfo;
  14. using AnnounceMultiplayerRoom::Member;
  15. using AnnounceMultiplayerRoom::RoomInformation;
  16. constexpr u32 network_version = 1; ///< The version of this Room and RoomMember
  17. constexpr u16 DefaultRoomPort = 24872;
  18. constexpr u32 MaxMessageSize = 500;
  19. /// Maximum number of concurrent connections allowed to this room.
  20. static constexpr u32 MaxConcurrentConnections = 254;
  21. constexpr std::size_t NumChannels = 1; // Number of channels used for the connection
  22. /// A special IP address that tells the room we're joining to assign us a IP address
  23. /// automatically.
  24. constexpr IPv4Address NoPreferredIP = {0xFF, 0xFF, 0xFF, 0xFF};
  25. // The different types of messages that can be sent. The first byte of each packet defines the type
  26. enum RoomMessageTypes : u8 {
  27. IdJoinRequest = 1,
  28. IdJoinSuccess,
  29. IdRoomInformation,
  30. IdSetGameInfo,
  31. IdProxyPacket,
  32. IdLdnPacket,
  33. IdChatMessage,
  34. IdNameCollision,
  35. IdIpCollision,
  36. IdVersionMismatch,
  37. IdWrongPassword,
  38. IdCloseRoom,
  39. IdRoomIsFull,
  40. IdStatusMessage,
  41. IdHostKicked,
  42. IdHostBanned,
  43. /// Moderation requests
  44. IdModKick,
  45. IdModBan,
  46. IdModUnban,
  47. IdModGetBanList,
  48. // Moderation responses
  49. IdModBanListResponse,
  50. IdModPermissionDenied,
  51. IdModNoSuchUser,
  52. IdJoinSuccessAsMod,
  53. };
  54. /// Types of system status messages
  55. enum StatusMessageTypes : u8 {
  56. IdMemberJoin = 1, ///< Member joining
  57. IdMemberLeave, ///< Member leaving
  58. IdMemberKicked, ///< A member is kicked from the room
  59. IdMemberBanned, ///< A member is banned from the room
  60. IdAddressUnbanned, ///< A username / ip address is unbanned from the room
  61. };
  62. /// This is what a server [person creating a server] would use.
  63. class Room final {
  64. public:
  65. enum class State : u8 {
  66. Open, ///< The room is open and ready to accept connections.
  67. Closed, ///< The room is not opened and can not accept connections.
  68. };
  69. Room();
  70. ~Room();
  71. /**
  72. * Gets the current state of the room.
  73. */
  74. State GetState() const;
  75. /**
  76. * Gets the room information of the room.
  77. */
  78. const RoomInformation& GetRoomInformation() const;
  79. /**
  80. * Gets the verify UID of this room.
  81. */
  82. std::string GetVerifyUID() const;
  83. /**
  84. * Gets a list of the mbmers connected to the room.
  85. */
  86. std::vector<Member> GetRoomMemberList() const;
  87. /**
  88. * Checks if the room is password protected
  89. */
  90. bool HasPassword() const;
  91. using UsernameBanList = std::vector<std::string>;
  92. using IPBanList = std::vector<std::string>;
  93. using BanList = std::pair<UsernameBanList, IPBanList>;
  94. /**
  95. * Creates the socket for this room. Will bind to default address if
  96. * server is empty string.
  97. */
  98. bool Create(const std::string& name, const std::string& description = "",
  99. const std::string& server = "", u16 server_port = DefaultRoomPort,
  100. const std::string& password = "",
  101. const u32 max_connections = MaxConcurrentConnections,
  102. const std::string& host_username = "", const GameInfo = {},
  103. std::unique_ptr<VerifyUser::Backend> verify_backend = nullptr,
  104. const BanList& ban_list = {}, bool enable_yuzu_mods = false);
  105. /**
  106. * Sets the verification GUID of the room.
  107. */
  108. void SetVerifyUID(const std::string& uid);
  109. /**
  110. * Gets the ban list (including banned forum usernames and IPs) of the room.
  111. */
  112. BanList GetBanList() const;
  113. /**
  114. * Destroys the socket
  115. */
  116. void Destroy();
  117. private:
  118. class RoomImpl;
  119. std::unique_ptr<RoomImpl> room_impl;
  120. };
  121. } // namespace Network