ConnectionGraph2.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 ConnectionGraph2.h
  11. /// \brief Connection graph plugin, version 2. Tells new systems about existing and new connections
  12. ///
  13. #include "NativeFeatureIncludes.h"
  14. #if _RAKNET_SUPPORT_ConnectionGraph2==1
  15. #ifndef __CONNECTION_GRAPH_2_H
  16. #define __CONNECTION_GRAPH_2_H
  17. #include "RakMemoryOverride.h"
  18. #include "RakNetTypes.h"
  19. #include "PluginInterface2.h"
  20. #include "DS_List.h"
  21. #include "DS_WeightedGraph.h"
  22. #include "GetTime.h"
  23. #include "Export.h"
  24. namespace RakNet
  25. {
  26. /// Forward declarations
  27. class RakPeerInterface;
  28. /// \brief A one hop connection graph.
  29. /// \details Sends ID_REMOTE_CONNECTION_LOST, ID_REMOTE_DISCONNECTION_NOTIFICATION, ID_REMOTE_NEW_INCOMING_CONNECTION<BR>
  30. /// All identifiers are followed by SystemAddress, then RakNetGUID
  31. /// Also stores the list for you, which you can access with GetConnectionListForRemoteSystem
  32. /// \ingroup CONNECTION_GRAPH_GROUP
  33. class RAK_DLL_EXPORT ConnectionGraph2 : public PluginInterface2
  34. {
  35. public:
  36. // GetInstance() and DestroyInstance(instance*)
  37. STATIC_FACTORY_DECLARATIONS(ConnectionGraph2)
  38. ConnectionGraph2();
  39. ~ConnectionGraph2();
  40. /// \brief Given a remote system identified by RakNetGUID, return the list of SystemAddresses and RakNetGUIDs they are connected to
  41. /// \param[in] remoteSystemGuid Which system we are referring to. This only works for remote systems, not ourselves.
  42. /// \param[out] saOut A preallocated array to hold the output list of SystemAddress. Can be 0 if you don't care.
  43. /// \param[out] guidOut A preallocated array to hold the output list of RakNetGUID. Can be 0 if you don't care.
  44. /// \param[in,out] outLength On input, the size of \a saOut and \a guidOut. On output, modified to reflect the number of elements actually written
  45. /// \return True if \a remoteSystemGuid was found. Otherwise false, and \a saOut, \a guidOut remain unchanged. \a outLength will be set to 0.
  46. bool GetConnectionListForRemoteSystem(RakNetGUID remoteSystemGuid, SystemAddress *saOut, RakNetGUID *guidOut, unsigned int *outLength);
  47. /// Returns if g1 is connected to g2
  48. bool ConnectionExists(RakNetGUID g1, RakNetGUID g2);
  49. /// Returns the average ping between two systems in the connection graph. Returns -1 if no connection exists between those systems
  50. uint16_t GetPingBetweenSystems(RakNetGUID g1, RakNetGUID g2) const;
  51. /// Returns the system with the lowest average ping among all its connections.
  52. /// If you need one system in the peer to peer group to relay data, have the FullyConnectedMesh2 host call this function after host migration, and use that system
  53. RakNetGUID GetLowestAveragePingSystem(void) const;
  54. /// \brief If called with false, then new connections are only added to the connection graph when you call ProcessNewConnection();
  55. /// \details This is useful if you want to perform validation before connecting a system to a mesh, or if you want a submesh (for example a server cloud)
  56. /// \param[in] b True to automatically call ProcessNewConnection() on any new connection, false to not do so. Defaults to true.
  57. void SetAutoProcessNewConnections(bool b);
  58. /// \brief Returns value passed to SetAutoProcessNewConnections()
  59. /// \return Value passed to SetAutoProcessNewConnections(), or the default of true if it was never called
  60. bool GetAutoProcessNewConnections(void) const;
  61. /// \brief If you call SetAutoProcessNewConnections(false);, then you will need to manually call ProcessNewConnection() on new connections
  62. /// \details On ID_NEW_INCOMING_CONNECTION or ID_CONNECTION_REQUEST_ACCEPTED, adds that system to the graph
  63. /// Do not call ProcessNewConnection() manually otherwise
  64. /// \param[in] The packet->SystemAddress member
  65. /// \param[in] The packet->guid member
  66. void AddParticipant(const SystemAddress &systemAddress, RakNetGUID rakNetGUID);
  67. /// Get the participants added with AddParticipant()
  68. /// \param[out] participantList Participants added with AddParticipant();
  69. void GetParticipantList(DataStructures::OrderedList<RakNetGUID, RakNetGUID> &participantList);
  70. /// \internal
  71. struct SystemAddressAndGuid
  72. {
  73. SystemAddress systemAddress;
  74. RakNetGUID guid;
  75. uint16_t sendersPingToThatSystem;
  76. };
  77. /// \internal
  78. static int SystemAddressAndGuidComp( const SystemAddressAndGuid &key, const SystemAddressAndGuid &data );
  79. /// \internal
  80. struct RemoteSystem
  81. {
  82. DataStructures::OrderedList<SystemAddressAndGuid,SystemAddressAndGuid,ConnectionGraph2::SystemAddressAndGuidComp> remoteConnections;
  83. RakNetGUID guid;
  84. };
  85. /// \internal
  86. static int RemoteSystemComp( const RakNetGUID &key, RemoteSystem * const &data );
  87. protected:
  88. /// \internal
  89. virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
  90. /// \internal
  91. virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming);
  92. /// \internal
  93. virtual PluginReceiveResult OnReceive(Packet *packet);
  94. // List of systems I am connected to, which in turn stores which systems they are connected to
  95. DataStructures::OrderedList<RakNetGUID, RemoteSystem*, ConnectionGraph2::RemoteSystemComp> remoteSystems;
  96. bool autoProcessNewConnections;
  97. };
  98. } // namespace RakNet
  99. #endif // #ifndef __CONNECTION_GRAPH_2_H
  100. #endif // _RAKNET_SUPPORT_*
粤ICP备19079148号