room.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "common/common_types.h"
  10. namespace Network {
  11. constexpr u32 network_version = 1; ///< The version of this Room and RoomMember
  12. constexpr u16 DefaultRoomPort = 1234;
  13. constexpr size_t NumChannels = 1; // Number of channels used for the connection
  14. struct RoomInformation {
  15. std::string name; ///< Name of the server
  16. u32 member_slots; ///< Maximum number of members in this room
  17. };
  18. struct GameInfo {
  19. std::string name{""};
  20. u64 id{0};
  21. };
  22. using MacAddress = std::array<u8, 6>;
  23. /// A special MAC address that tells the room we're joining to assign us a MAC address
  24. /// automatically.
  25. constexpr MacAddress NoPreferredMac = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  26. // 802.11 broadcast MAC address
  27. constexpr MacAddress BroadcastMac = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  28. // The different types of messages that can be sent. The first byte of each packet defines the type
  29. enum RoomMessageTypes : u8 {
  30. IdJoinRequest = 1,
  31. IdJoinSuccess,
  32. IdRoomInformation,
  33. IdSetGameInfo,
  34. IdWifiPacket,
  35. IdChatMessage,
  36. IdNameCollision,
  37. IdMacCollision,
  38. IdVersionMismatch,
  39. IdCloseRoom
  40. };
  41. /// This is what a server [person creating a server] would use.
  42. class Room final {
  43. public:
  44. enum class State : u8 {
  45. Open, ///< The room is open and ready to accept connections.
  46. Closed, ///< The room is not opened and can not accept connections.
  47. };
  48. struct Member {
  49. std::string nickname; ///< The nickname of the member.
  50. GameInfo game_info; ///< The current game of the member
  51. MacAddress mac_address; ///< The assigned mac address of the member.
  52. };
  53. Room();
  54. ~Room();
  55. /**
  56. * Gets the current state of the room.
  57. */
  58. State GetState() const;
  59. /**
  60. * Gets the room information of the room.
  61. */
  62. const RoomInformation& GetRoomInformation() const;
  63. /**
  64. * Gets a list of the mbmers connected to the room.
  65. */
  66. std::vector<Member> GetRoomMemberList() const;
  67. /**
  68. * Creates the socket for this room. Will bind to default address if
  69. * server is empty string.
  70. */
  71. void Create(const std::string& name, const std::string& server = "",
  72. u16 server_port = DefaultRoomPort);
  73. /**
  74. * Destroys the socket
  75. */
  76. void Destroy();
  77. private:
  78. class RoomImpl;
  79. std::unique_ptr<RoomImpl> room_impl;
  80. };
  81. } // namespace Network