NetworkIDObject.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 A class you can derive from to make it easier to represent every networked object with an integer. This way you can refer to objects over the network.
  12. ///
  13. #if !defined(__NETWORK_ID_GENERATOR)
  14. #define __NETWORK_ID_GENERATOR
  15. #include "RakNetTypes.h"
  16. #include "RakMemoryOverride.h"
  17. #include "Export.h"
  18. namespace RakNet
  19. {
  20. /// Forward declarations
  21. class NetworkIDManager;
  22. typedef uint32_t NetworkIDType;
  23. /// \brief Unique shared ids for each object instance
  24. /// \details A class you can derive from to make it easier to represent every networked object with an integer. This way you can refer to objects over the network.
  25. /// One system should return true for IsNetworkIDAuthority() and the rest should return false. When an object needs to be created, have the the one system create the object.
  26. /// Then have that system send a message to all other systems, and include the value returned from GetNetworkID() in that packet. All other systems should then create the same
  27. /// class of object, and call SetNetworkID() on that class with the NetworkID in the packet.
  28. /// \see the manual for more information on this.
  29. class RAK_DLL_EXPORT NetworkIDObject
  30. {
  31. public:
  32. // Constructor. NetworkIDs, if IsNetworkIDAuthority() is true, are created here.
  33. NetworkIDObject();
  34. // Destructor. Used NetworkIDs, if any, are freed here.
  35. virtual ~NetworkIDObject();
  36. /// Sets the manager class from which to request unique network IDs
  37. /// Unlike previous versions, the NetworkIDObject relies on a manager class to provide IDs, rather than using statics,
  38. /// So you can have more than one set of IDs on the same system.
  39. virtual void SetNetworkIDManager( NetworkIDManager *manager);
  40. /// Returns what was passed to SetNetworkIDManager
  41. virtual NetworkIDManager * GetNetworkIDManager( void ) const;
  42. /// Returns the NetworkID that you can use to refer to this object over the network.
  43. /// \pre You must first call SetNetworkIDManager before using this function
  44. /// \retval UNASSIGNED_NETWORK_ID UNASSIGNED_NETWORK_ID is returned IsNetworkIDAuthority() is false and SetNetworkID() was not previously called. This is also returned if you call this function in the constructor.
  45. /// \retval 0-65534 Any other value is a valid NetworkID. NetworkIDs start at 0 and go to 65534, wrapping at that point.
  46. virtual NetworkID GetNetworkID( void );
  47. /// Sets the NetworkID for this instance. Usually this is called by the clients and determined from the servers. However, if you save multiplayer games you would likely use
  48. /// This on load as well.
  49. virtual void SetNetworkID( NetworkID id );
  50. /// Your class does not have to derive from NetworkIDObject, although that is the easiest way to implement this.
  51. /// If you want this to be a member object of another class, rather than inherit, then call SetParent() with a pointer to the parent class instance.
  52. /// GET_OBJECT_FROM_ID will then return the parent rather than this instance.
  53. virtual void SetParent( void *_parent );
  54. /// Return what was passed to SetParent
  55. /// \return The value passed to SetParent, or 0 if it was never called.
  56. virtual void* GetParent( void ) const;
  57. protected:
  58. /// The network ID of this object
  59. // networkID is assigned when networkIDManager is set.
  60. NetworkID networkID;
  61. NetworkIDManager *networkIDManager;
  62. /// The parent set by SetParent()
  63. void *parent;
  64. /// \internal, used by NetworkIDManager
  65. friend class NetworkIDManager;
  66. NetworkIDObject *nextInstanceForNetworkIDManager;
  67. };
  68. } // namespace RakNet
  69. #endif
粤ICP备19079148号