RakNetStuff.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // I tried to put most of the RakNet stuff here, but some of it had to go to CDemo.h too
  11. #ifndef __RAKNET_ADDITIONS_FOR_IRRLICHT_DEMO_H
  12. #define __RAKNET_ADDITIONS_FOR_IRRLICHT_DEMO_H
  13. #include "RakPeerInterface.h"
  14. #include "ReplicaManager3.h"
  15. #include "NatPunchthroughClient.h"
  16. #include "CloudClient.h"
  17. #include "FullyConnectedMesh2.h"
  18. #include "UDPProxyClient.h"
  19. #include "TCPInterface.h"
  20. #include "HTTPConnection.h"
  21. #include "../Samples/PHPDirectoryServer2/PHPDirectoryServer2.h"
  22. #include "vector3d.h"
  23. #include "IAnimatedMeshSceneNode.h"
  24. #include "MessageIdentifiers.h"
  25. class ReplicaManager3Irrlicht;
  26. class CDemo;
  27. class PlayerReplica;
  28. // All externs defined in the corresponding CPP file
  29. // Most of these classes has a manual entry, all of them have a demo
  30. extern RakNet::RakPeerInterface *rakPeer; // Basic communication
  31. extern RakNet::NetworkIDManager *networkIDManager; // Unique IDs per network object
  32. extern ReplicaManager3Irrlicht *replicaManager3; // Autoreplicate network objects
  33. extern RakNet::NatPunchthroughClient *natPunchthroughClient; // Connect peer to peer through routers
  34. extern RakNet::CloudClient *cloudClient; // Used to upload game instance to the cloud
  35. extern RakNet::FullyConnectedMesh2 *fullyConnectedMesh2; // Used to find out who is the session host
  36. extern PlayerReplica *playerReplica; // Network object that represents the player
  37. // A NAT punchthrough and proxy server Jenkins Software is hosting for free, should usually be online
  38. #define DEFAULT_NAT_PUNCHTHROUGH_FACILITATOR_PORT 61111
  39. #define DEFAULT_NAT_PUNCHTHROUGH_FACILITATOR_IP "natpunch.jenkinssoftware.com"
  40. void InstantiateRakNetClasses(void);
  41. void DeinitializeRakNetClasses(void);
  42. // Base RakNet custom classes for Replica Manager 3, setup peer to peer networking
  43. class BaseIrrlichtReplica : public RakNet::Replica3
  44. {
  45. public:
  46. BaseIrrlichtReplica();
  47. virtual ~BaseIrrlichtReplica();
  48. virtual RakNet::RM3ConstructionState QueryConstruction(RakNet::Connection_RM3 *destinationConnection, RakNet::ReplicaManager3 *replicaManager3) {return QueryConstruction_PeerToPeer(destinationConnection);}
  49. virtual bool QueryRemoteConstruction(RakNet::Connection_RM3 *sourceConnection) {return QueryRemoteConstruction_PeerToPeer(sourceConnection);}
  50. virtual void DeallocReplica(RakNet::Connection_RM3 *sourceConnection) {delete this;}
  51. virtual RakNet::RM3QuerySerializationResult QuerySerialization(RakNet::Connection_RM3 *destinationConnection) {return QuerySerialization_PeerToPeer(destinationConnection);}
  52. virtual void SerializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection);
  53. virtual bool DeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection);
  54. virtual RakNet::RM3SerializationResult Serialize(RakNet::SerializeParameters *serializeParameters);
  55. virtual void Deserialize(RakNet::DeserializeParameters *deserializeParameters);
  56. virtual void SerializeDestruction(RakNet::BitStream *destructionBitstream, RakNet::Connection_RM3 *destinationConnection) {}
  57. virtual bool DeserializeDestruction(RakNet::BitStream *destructionBitstream, RakNet::Connection_RM3 *sourceConnection) {return true;}
  58. virtual RakNet::RM3ActionOnPopConnection QueryActionOnPopConnection(RakNet::Connection_RM3 *droppedConnection) const {return QueryActionOnPopConnection_PeerToPeer(droppedConnection);}
  59. /// This function is not derived from Replica3, it's specific to this app
  60. /// Called from CDemo::UpdateRakNet
  61. virtual void Update(RakNet::TimeMS curTime);
  62. // Set when the object is constructed
  63. CDemo *demo;
  64. // real is written on the owner peer, read on the remote peer
  65. irr::core::vector3df position;
  66. RakNet::TimeMS creationTime;
  67. };
  68. // Game classes automatically updated by ReplicaManager3
  69. class PlayerReplica : public BaseIrrlichtReplica, irr::scene::IAnimationEndCallBack
  70. {
  71. public:
  72. PlayerReplica();
  73. virtual ~PlayerReplica();
  74. // Every function below, before Update overriding a function in Replica3
  75. virtual void WriteAllocationID(RakNet::Connection_RM3 *destinationConnection, RakNet::BitStream *allocationIdBitstream) const;
  76. virtual void SerializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection);
  77. virtual bool DeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection);
  78. virtual RakNet::RM3SerializationResult Serialize(RakNet::SerializeParameters *serializeParameters);
  79. virtual void Deserialize(RakNet::DeserializeParameters *deserializeParameters);
  80. virtual void PostDeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection);
  81. virtual void PreDestruction(RakNet::Connection_RM3 *sourceConnection);
  82. virtual void Update(RakNet::TimeMS curTime);
  83. void UpdateAnimation(irr::scene::EMD2_ANIMATION_TYPE anim);
  84. float GetRotationDifference(float r1, float r2);
  85. virtual void OnAnimationEnd(irr::scene::IAnimatedMeshSceneNode* node);
  86. void PlayAttackAnimation(void);
  87. // playerName is only sent in SerializeConstruction, since it doesn't change
  88. RakNet::RakString playerName;
  89. // Networked rotation
  90. float rotationAroundYAxis;
  91. // Interpolation variables, not networked
  92. irr::core::vector3df positionDeltaPerMS;
  93. float rotationDeltaPerMS;
  94. RakNet::TimeMS interpEndTime, lastUpdate;
  95. // Updated based on the keypresses, to control remote animation
  96. bool isMoving;
  97. // Only instantiated for remote systems, you never see yourself
  98. irr::scene::IAnimatedMeshSceneNode* model;
  99. irr::scene::EMD2_ANIMATION_TYPE curAnim;
  100. // deathTimeout is set from the local player
  101. RakNet::TimeMS deathTimeout;
  102. bool IsDead(void) const;
  103. // isDead is set from network packets for remote players
  104. bool isDead;
  105. // List of all players, including our own
  106. static DataStructures::List<PlayerReplica*> playerList;
  107. };
  108. class BallReplica : public BaseIrrlichtReplica
  109. {
  110. public:
  111. BallReplica();
  112. virtual ~BallReplica();
  113. // Every function except update is overriding a function in Replica3
  114. virtual void WriteAllocationID(RakNet::Connection_RM3 *destinationConnection, RakNet::BitStream *allocationIdBitstream) const;
  115. virtual void SerializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection);
  116. virtual bool DeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection);
  117. virtual RakNet::RM3SerializationResult Serialize(RakNet::SerializeParameters *serializeParameters);
  118. virtual void Deserialize(RakNet::DeserializeParameters *deserializeParameters);
  119. virtual void PostDeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection);
  120. virtual void PreDestruction(RakNet::Connection_RM3 *sourceConnection);
  121. virtual void Update(RakNet::TimeMS curTime);
  122. // shotDirection is networked
  123. irr::core::vector3df shotDirection;
  124. // shotlifetime is calculated, not networked
  125. RakNet::TimeMS shotLifetime;
  126. };
  127. class Connection_RM3Irrlicht : public RakNet::Connection_RM3 {
  128. public:
  129. Connection_RM3Irrlicht(const RakNet::SystemAddress &_systemAddress, RakNet::RakNetGUID _guid, CDemo *_demo) : RakNet::Connection_RM3(_systemAddress, _guid) {demo=_demo;}
  130. virtual ~Connection_RM3Irrlicht() {}
  131. virtual RakNet::Replica3 *AllocReplica(RakNet::BitStream *allocationId, RakNet::ReplicaManager3 *replicaManager3);
  132. protected:
  133. CDemo *demo;
  134. };
  135. class ReplicaManager3Irrlicht : public RakNet::ReplicaManager3
  136. {
  137. public:
  138. virtual RakNet::Connection_RM3* AllocConnection(const RakNet::SystemAddress &systemAddress, RakNet::RakNetGUID rakNetGUID) const {
  139. return new Connection_RM3Irrlicht(systemAddress,rakNetGUID,demo);
  140. }
  141. virtual void DeallocConnection(RakNet::Connection_RM3 *connection) const {
  142. delete connection;
  143. }
  144. CDemo *demo;
  145. };
  146. #endif
粤ICP备19079148号