room_member.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <memory>
  6. #include <string>
  7. #include <vector>
  8. #include "common/common_types.h"
  9. #include "network/room.h"
  10. namespace Network {
  11. /// Information about the received WiFi packets.
  12. /// Acts as our own 802.11 header.
  13. struct WifiPacket {
  14. enum class PacketType : u8 { Beacon, Data, Authentication, AssociationResponse };
  15. PacketType type; ///< The type of 802.11 frame.
  16. std::vector<u8> data; ///< Raw 802.11 frame data, starting at the management frame header
  17. /// for management frames.
  18. MacAddress transmitter_address; ///< Mac address of the transmitter.
  19. MacAddress destination_address; ///< Mac address of the receiver.
  20. u8 channel; ///< WiFi channel where this frame was transmitted.
  21. };
  22. /// Represents a chat message.
  23. struct ChatEntry {
  24. std::string nickname; ///< Nickname of the client who sent this message.
  25. std::string message; ///< Body of the message.
  26. };
  27. /**
  28. * This is what a client [person joining a server] would use.
  29. * It also has to be used if you host a game yourself (You'd create both, a Room and a
  30. * RoomMembership for yourself)
  31. */
  32. class RoomMember final {
  33. public:
  34. enum class State : u8 {
  35. Idle, ///< Default state
  36. Error, ///< Some error [permissions to network device missing or something]
  37. Joining, ///< The client is attempting to join a room.
  38. Joined, ///< The client is connected to the room and is ready to send/receive packets.
  39. LostConnection, ///< Connection closed
  40. // Reasons why connection was rejected
  41. NameCollision, ///< Somebody is already using this name
  42. MacCollision, ///< Somebody is already using that mac-address
  43. WrongVersion, ///< The room version is not the same as for this RoomMember
  44. CouldNotConnect ///< The room is not responding to a connection attempt
  45. };
  46. struct MemberInformation {
  47. std::string nickname; ///< Nickname of the member.
  48. std::string game_name; ///< Name of the game they're currently playing, or empty if they're
  49. /// not playing anything.
  50. MacAddress mac_address; ///< MAC address associated with this member.
  51. };
  52. using MemberList = std::vector<MemberInformation>;
  53. RoomMember();
  54. ~RoomMember();
  55. /**
  56. * Returns the status of our connection to the room.
  57. */
  58. State GetState() const;
  59. /**
  60. * Returns information about the members in the room we're currently connected to.
  61. */
  62. const MemberList& GetMemberInformation() const;
  63. /**
  64. * Returns the nickname of the RoomMember.
  65. */
  66. const std::string& GetNickname() const;
  67. /**
  68. * Returns the MAC address of the RoomMember.
  69. */
  70. const MacAddress& GetMacAddress() const;
  71. /**
  72. * Returns information about the room we're currently connected to.
  73. */
  74. RoomInformation GetRoomInformation() const;
  75. /**
  76. * Returns whether we're connected to a server or not.
  77. */
  78. bool IsConnected() const;
  79. /**
  80. * Attempts to join a room at the specified address and port, using the specified nickname.
  81. * This may fail if the username is already taken.
  82. */
  83. void Join(const std::string& nickname, const char* server_addr = "127.0.0.1",
  84. const u16 serverPort = DefaultRoomPort, const u16 clientPort = 0,
  85. const MacAddress& preferred_mac = NoPreferredMac);
  86. /**
  87. * Sends a WiFi packet to the room.
  88. * @param packet The WiFi packet to send.
  89. */
  90. void SendWifiPacket(const WifiPacket& packet);
  91. /**
  92. * Sends a chat message to the room.
  93. * @param message The contents of the message.
  94. */
  95. void SendChatMessage(const std::string& message);
  96. /**
  97. * Sends the current game name to the room.
  98. * @param game_name The game name.
  99. */
  100. void SendGameName(const std::string& game_name);
  101. /**
  102. * Leaves the current room.
  103. */
  104. void Leave();
  105. private:
  106. class RoomMemberImpl;
  107. std::unique_ptr<RoomMemberImpl> room_member_impl;
  108. };
  109. } // namespace Network