room_member.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <functional>
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "common/common_types.h"
  10. #include "network/room.h"
  11. namespace Network {
  12. /// Information about the received WiFi packets.
  13. /// Acts as our own 802.11 header.
  14. struct WifiPacket {
  15. enum class PacketType : u8 { Beacon, Data, Authentication, AssociationResponse };
  16. PacketType type; ///< The type of 802.11 frame.
  17. std::vector<u8> data; ///< Raw 802.11 frame data, starting at the management frame header
  18. /// for management frames.
  19. MacAddress transmitter_address; ///< Mac address of the transmitter.
  20. MacAddress destination_address; ///< Mac address of the receiver.
  21. u8 channel; ///< WiFi channel where this frame was transmitted.
  22. };
  23. /// Represents a chat message.
  24. struct ChatEntry {
  25. std::string nickname; ///< Nickname of the client who sent this message.
  26. std::string message; ///< Body of the message.
  27. };
  28. /**
  29. * This is what a client [person joining a server] would use.
  30. * It also has to be used if you host a game yourself (You'd create both, a Room and a
  31. * RoomMembership for yourself)
  32. */
  33. class RoomMember final {
  34. public:
  35. enum class State : u8 {
  36. Idle, ///< Default state
  37. Error, ///< Some error [permissions to network device missing or something]
  38. Joining, ///< The client is attempting to join a room.
  39. Joined, ///< The client is connected to the room and is ready to send/receive packets.
  40. LostConnection, ///< Connection closed
  41. // Reasons why connection was rejected
  42. NameCollision, ///< Somebody is already using this name
  43. MacCollision, ///< Somebody is already using that mac-address
  44. WrongVersion, ///< The room version is not the same as for this RoomMember
  45. CouldNotConnect ///< The room is not responding to a connection attempt
  46. };
  47. struct MemberInformation {
  48. std::string nickname; ///< Nickname of the member.
  49. GameInfo game_info; ///< Name of the game they're currently playing, or empty if they're
  50. /// not playing anything.
  51. MacAddress mac_address; ///< MAC address associated with this member.
  52. };
  53. using MemberList = std::vector<MemberInformation>;
  54. // The handle for the callback functions
  55. template <typename T>
  56. using CallbackHandle = std::shared_ptr<std::function<void(const T&)>>;
  57. /**
  58. * Unbinds a callback function from the events.
  59. * @param handle The connection handle to disconnect
  60. */
  61. template <typename T>
  62. void Unbind(CallbackHandle<T> handle);
  63. RoomMember();
  64. ~RoomMember();
  65. /**
  66. * Returns the status of our connection to the room.
  67. */
  68. State GetState() const;
  69. /**
  70. * Returns information about the members in the room we're currently connected to.
  71. */
  72. const MemberList& GetMemberInformation() const;
  73. /**
  74. * Returns the nickname of the RoomMember.
  75. */
  76. const std::string& GetNickname() const;
  77. /**
  78. * Returns the MAC address of the RoomMember.
  79. */
  80. const MacAddress& GetMacAddress() const;
  81. /**
  82. * Returns information about the room we're currently connected to.
  83. */
  84. RoomInformation GetRoomInformation() const;
  85. /**
  86. * Returns whether we're connected to a server or not.
  87. */
  88. bool IsConnected() const;
  89. /**
  90. * Attempts to join a room at the specified address and port, using the specified nickname.
  91. * This may fail if the username is already taken.
  92. */
  93. void Join(const std::string& nickname, const char* server_addr = "127.0.0.1",
  94. const u16 serverPort = DefaultRoomPort, const u16 clientPort = 0,
  95. const MacAddress& preferred_mac = NoPreferredMac);
  96. /**
  97. * Sends a WiFi packet to the room.
  98. * @param packet The WiFi packet to send.
  99. */
  100. void SendWifiPacket(const WifiPacket& packet);
  101. /**
  102. * Sends a chat message to the room.
  103. * @param message The contents of the message.
  104. */
  105. void SendChatMessage(const std::string& message);
  106. /**
  107. * Sends the current game info to the room.
  108. * @param game_info The game information.
  109. */
  110. void SendGameInfo(const GameInfo& game_info);
  111. /**
  112. * Binds a function to an event that will be triggered every time the State of the member
  113. * changed. The function wil be called every time the event is triggered. The callback function
  114. * must not bind or unbind a function. Doing so will cause a deadlock
  115. * @param callback The function to call
  116. * @return A handle used for removing the function from the registered list
  117. */
  118. CallbackHandle<State> BindOnStateChanged(std::function<void(const State&)> callback);
  119. /**
  120. * Binds a function to an event that will be triggered every time a WifiPacket is received.
  121. * The function wil be called everytime the event is triggered.
  122. * The callback function must not bind or unbind a function. Doing so will cause a deadlock
  123. * @param callback The function to call
  124. * @return A handle used for removing the function from the registered list
  125. */
  126. CallbackHandle<WifiPacket> BindOnWifiPacketReceived(
  127. std::function<void(const WifiPacket&)> callback);
  128. /**
  129. * Binds a function to an event that will be triggered every time the RoomInformation changes.
  130. * The function wil be called every time the event is triggered.
  131. * The callback function must not bind or unbind a function. Doing so will cause a deadlock
  132. * @param callback The function to call
  133. * @return A handle used for removing the function from the registered list
  134. */
  135. CallbackHandle<RoomInformation> BindOnRoomInformationChanged(
  136. std::function<void(const RoomInformation&)> callback);
  137. /**
  138. * Binds a function to an event that will be triggered every time a ChatMessage is received.
  139. * The function wil be called every time the event is triggered.
  140. * The callback function must not bind or unbind a function. Doing so will cause a deadlock
  141. * @param callback The function to call
  142. * @return A handle used for removing the function from the registered list
  143. */
  144. CallbackHandle<ChatEntry> BindOnChatMessageRecieved(
  145. std::function<void(const ChatEntry&)> callback);
  146. /**
  147. * Leaves the current room.
  148. */
  149. void Leave();
  150. private:
  151. class RoomMemberImpl;
  152. std::unique_ptr<RoomMemberImpl> room_member_impl;
  153. };
  154. } // namespace Network