RelayPlugin.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file
  11. /// \brief Contains the class RelayPlugin
  12. ///
  13. #include "NativeFeatureIncludes.h"
  14. #if _RAKNET_SUPPORT_RelayPlugin==1
  15. #ifndef __RELAY_PLUGIN_H
  16. #define __RELAY_PLUGIN_H
  17. #include "PluginInterface2.h"
  18. #include "RakString.h"
  19. #include "DS_Hash.h"
  20. #ifdef _MSC_VER
  21. #pragma warning( push )
  22. #endif
  23. /// \defgroup RELAY_PLUGIN_GROUP RelayPlugin
  24. /// \brief A simple class to relay messages from one system to another through an intermediary
  25. /// \ingroup PLUGINS_GROUP
  26. namespace RakNet
  27. {
  28. /// Forward declarations
  29. class RakPeerInterface;
  30. enum RelayPluginEnums
  31. {
  32. // Server handled messages
  33. RPE_MESSAGE_TO_SERVER_FROM_CLIENT,
  34. RPE_ADD_CLIENT_REQUEST_FROM_CLIENT,
  35. RPE_REMOVE_CLIENT_REQUEST_FROM_CLIENT,
  36. RPE_GROUP_MESSAGE_FROM_CLIENT,
  37. RPE_JOIN_GROUP_REQUEST_FROM_CLIENT,
  38. RPE_LEAVE_GROUP_REQUEST_FROM_CLIENT,
  39. RPE_GET_GROUP_LIST_REQUEST_FROM_CLIENT,
  40. // Client handled messages
  41. RPE_MESSAGE_TO_CLIENT_FROM_SERVER,
  42. RPE_ADD_CLIENT_NOT_ALLOWED,
  43. RPE_ADD_CLIENT_TARGET_NOT_CONNECTED,
  44. RPE_ADD_CLIENT_NAME_ALREADY_IN_USE,
  45. RPE_ADD_CLIENT_SUCCESS,
  46. RPE_USER_ENTERED_ROOM,
  47. RPE_USER_LEFT_ROOM,
  48. RPE_GROUP_MSG_FROM_SERVER,
  49. RPE_GET_GROUP_LIST_REPLY_FROM_SERVER,
  50. RPE_JOIN_GROUP_SUCCESS,
  51. RPE_JOIN_GROUP_FAILURE,
  52. };
  53. /// \brief A simple class to relay messages from one system to another, identifying remote systems by a string.
  54. /// \ingroup RELAY_PLUGIN_GROUP
  55. class RAK_DLL_EXPORT RelayPlugin : public PluginInterface2
  56. {
  57. public:
  58. // GetInstance() and DestroyInstance(instance*)
  59. STATIC_FACTORY_DECLARATIONS(RelayPlugin)
  60. /// Constructor
  61. RelayPlugin();
  62. /// Destructor
  63. virtual ~RelayPlugin();
  64. /// \brief Forward messages from any system, to the system specified by the combination of key and guid. The sending system only needs to know the key.
  65. /// \param[in] key A string to identify the target's RakNetGUID. This is so the sending system does not need to know the RakNetGUID of the target system. The key should be unique among all guids added. If the key is not unique, only one system will be sent to (at random).
  66. /// \param[in] guid The RakNetGuid of the system to send to. If this system disconnects, it is removed from the internal hash
  67. /// \return RPE_ADD_CLIENT_TARGET_NOT_CONNECTED, RPE_ADD_CLIENT_NAME_ALREADY_IN_USE, or RPE_ADD_CLIENT_OK
  68. RelayPluginEnums AddParticipantOnServer(const RakString &key, const RakNetGUID &guid);
  69. /// \brief Remove a chat participant
  70. void RemoveParticipantOnServer(const RakNetGUID &guid);
  71. /// \brief If true, then if the client calls AddParticipantRequestFromClient(), the server will call AddParticipantOnServer() automatically
  72. /// Defaults to false
  73. /// \param[in] accept true to accept, false to not.
  74. void SetAcceptAddParticipantRequests(bool accept);
  75. /// \brief Request from the client for the server to call AddParticipantOnServer()
  76. /// \pre The server must have called SetAcceptAddParticipantRequests(true) or the request will be ignored
  77. /// \param[in] key A string to identify out system. Passed to \a key on AddParticipantOnServer()
  78. /// \param[in] relayPluginServerGuid the RakNetGUID of the system running RelayPlugin
  79. void AddParticipantRequestFromClient(const RakString &key, const RakNetGUID &relayPluginServerGuid);
  80. /// \brief Remove yourself as a participant
  81. void RemoveParticipantRequestFromClient(const RakNetGUID &relayPluginServerGuid);
  82. /// \brief Request that the server relay \a bitStream to the system designated by \a key
  83. /// \param[in] relayPluginServerGuid the RakNetGUID of the system running RelayPlugin
  84. /// \param[in] destinationGuid The key value passed to AddParticipant() earlier on the server. If this was not done, the server will not relay the message (it will be silently discarded).
  85. /// \param[in] bitStream The data to relay
  86. /// \param[in] priority See the parameter of the same name in RakPeerInterface::Send()
  87. /// \param[in] reliability See the parameter of the same name in RakPeerInterface::Send()
  88. /// \param[in] orderingChannel See the parameter of the same name in RakPeerInterface::Send()
  89. void SendToParticipant(const RakNetGUID &relayPluginServerGuid, const RakString &destinationGuid, BitStream *bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel);
  90. void SendGroupMessage(const RakNetGUID &relayPluginServerGuid, BitStream *bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel);
  91. void JoinGroupRequest(const RakNetGUID &relayPluginServerGuid, RakString groupName);
  92. void LeaveGroup(const RakNetGUID &relayPluginServerGuid);
  93. void GetGroupList(const RakNetGUID &relayPluginServerGuid);
  94. /// \internal
  95. virtual PluginReceiveResult OnReceive(Packet *packet);
  96. /// \internal
  97. virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
  98. struct StrAndGuidAndRoom
  99. {
  100. RakString str;
  101. RakNetGUID guid;
  102. RakString currentRoom;
  103. };
  104. struct StrAndGuid
  105. {
  106. RakString str;
  107. RakNetGUID guid;
  108. };
  109. struct RP_Group
  110. {
  111. RakString roomName;
  112. DataStructures::List<StrAndGuid> usersInRoom;
  113. };
  114. protected:
  115. RelayPlugin::RP_Group* JoinGroup(RakNetGUID userGuid, RakString roomName);
  116. RelayPlugin::RP_Group* JoinGroup(RP_Group* room, StrAndGuidAndRoom **strAndGuidSender);
  117. void LeaveGroup(StrAndGuidAndRoom **strAndGuidSender);
  118. void NotifyUsersInRoom(RP_Group *room, int msg, const RakString& message);
  119. void SendMessageToRoom(StrAndGuidAndRoom **strAndGuidSender, BitStream* message);
  120. void SendChatRoomsList(RakNetGUID target);
  121. void OnGroupMessageFromClient(Packet *packet);
  122. void OnJoinGroupRequestFromClient(Packet *packet);
  123. void OnLeaveGroupRequestFromClient(Packet *packet);
  124. DataStructures::Hash<RakString, StrAndGuidAndRoom*, 8096, RakNet::RakString::ToInteger> strToGuidHash;
  125. DataStructures::Hash<RakNetGUID, StrAndGuidAndRoom*, 8096, RakNet::RakNetGUID::ToUint32> guidToStrHash;
  126. DataStructures::List<RP_Group*> chatRooms;
  127. bool acceptAddParticipantRequests;
  128. };
  129. } // End namespace
  130. #endif
  131. #ifdef _MSC_VER
  132. #pragma warning( pop )
  133. #endif
  134. #endif // _RAKNET_SUPPORT_*
粤ICP备19079148号