PluginInterface2.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 \b RakNet's plugin functionality system, version 2. You can derive from this to create your own plugins.
  12. ///
  13. #ifndef __PLUGIN_INTERFACE_2_H
  14. #define __PLUGIN_INTERFACE_2_H
  15. #include "NativeFeatureIncludes.h"
  16. #include "RakNetTypes.h"
  17. #include "Export.h"
  18. #include "PacketPriority.h"
  19. namespace RakNet {
  20. /// Forward declarations
  21. class RakPeerInterface;
  22. class TCPInterface;
  23. struct Packet;
  24. struct InternalPacket;
  25. /// \defgroup PLUGIN_INTERFACE_GROUP PluginInterface2
  26. /// \defgroup PLUGINS_GROUP Plugins
  27. /// \ingroup PLUGIN_INTERFACE_GROUP
  28. /// For each message that arrives on an instance of RakPeer, the plugins get an opportunity to process them first. This enumeration represents what to do with the message
  29. /// \ingroup PLUGIN_INTERFACE_GROUP
  30. enum PluginReceiveResult
  31. {
  32. /// The plugin used this message and it shouldn't be given to the user.
  33. RR_STOP_PROCESSING_AND_DEALLOCATE=0,
  34. /// This message will be processed by other plugins, and at last by the user.
  35. RR_CONTINUE_PROCESSING,
  36. /// The plugin is going to hold on to this message. Do not deallocate it but do not pass it to other plugins either.
  37. RR_STOP_PROCESSING
  38. };
  39. /// Reasons why a connection was lost
  40. /// \ingroup PLUGIN_INTERFACE_GROUP
  41. enum PI2_LostConnectionReason
  42. {
  43. /// Called RakPeer::CloseConnection()
  44. LCR_CLOSED_BY_USER,
  45. /// Got ID_DISCONNECTION_NOTIFICATION
  46. LCR_DISCONNECTION_NOTIFICATION,
  47. /// GOT ID_CONNECTION_LOST
  48. LCR_CONNECTION_LOST
  49. };
  50. /// Returns why a connection attempt failed
  51. /// \ingroup PLUGIN_INTERFACE_GROUP
  52. enum PI2_FailedConnectionAttemptReason
  53. {
  54. FCAR_CONNECTION_ATTEMPT_FAILED,
  55. FCAR_ALREADY_CONNECTED,
  56. FCAR_NO_FREE_INCOMING_CONNECTIONS,
  57. FCAR_SECURITY_PUBLIC_KEY_MISMATCH,
  58. FCAR_CONNECTION_BANNED,
  59. FCAR_INVALID_PASSWORD,
  60. FCAR_INCOMPATIBLE_PROTOCOL,
  61. FCAR_IP_RECENTLY_CONNECTED,
  62. FCAR_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY,
  63. FCAR_OUR_SYSTEM_REQUIRES_SECURITY,
  64. FCAR_PUBLIC_KEY_MISMATCH
  65. };
  66. /// RakNet's plugin system. Each plugin processes the following events:
  67. /// -Connection attempts
  68. /// -The result of connection attempts
  69. /// -Each incoming message
  70. /// -Updates over time, when RakPeer::Receive() is called
  71. ///
  72. /// \ingroup PLUGIN_INTERFACE_GROUP
  73. class RAK_DLL_EXPORT PluginInterface2
  74. {
  75. public:
  76. PluginInterface2();
  77. virtual ~PluginInterface2();
  78. /// Called when the interface is attached
  79. virtual void OnAttach(void) {}
  80. /// Called when the interface is detached
  81. virtual void OnDetach(void) {}
  82. /// Update is called every time a packet is checked for .
  83. virtual void Update(void) {}
  84. /// OnReceive is called for every packet.
  85. /// \param[in] packet the packet that is being returned to the user
  86. /// \return True to allow the game and other plugins to get this message, false to absorb it
  87. virtual PluginReceiveResult OnReceive(Packet *packet) {(void) packet; return RR_CONTINUE_PROCESSING;}
  88. /// Called when RakPeer is initialized
  89. virtual void OnRakPeerStartup(void) {}
  90. /// Called when RakPeer is shutdown
  91. virtual void OnRakPeerShutdown(void) {}
  92. /// Called when a connection is dropped because the user called RakPeer::CloseConnection() for a particular system
  93. /// \param[in] systemAddress The system whose connection was closed
  94. /// \param[in] rakNetGuid The guid of the specified system
  95. /// \param[in] lostConnectionReason How the connection was closed: manually, connection lost, or notification of disconnection
  96. virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ){(void) systemAddress; (void) rakNetGUID; (void) lostConnectionReason;}
  97. /// Called when we got a new connection
  98. /// \param[in] systemAddress Address of the new connection
  99. /// \param[in] rakNetGuid The guid of the specified system
  100. /// \param[in] isIncoming If true, this is ID_NEW_INCOMING_CONNECTION, or the equivalent
  101. virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming) {(void) systemAddress; (void) rakNetGUID; (void) isIncoming;}
  102. /// Called when a connection attempt fails
  103. /// \param[in] packet Packet to be returned to the user
  104. /// \param[in] failedConnectionReason Why the connection failed
  105. virtual void OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason) {(void) packet; (void) failedConnectionAttemptReason;}
  106. /// Queried when attached to RakPeer
  107. /// Return true to call OnDirectSocketSend(), OnDirectSocketReceive(), OnReliabilityLayerNotification(), OnInternalPacket(), and OnAck()
  108. /// If true, then you cannot call RakPeer::AttachPlugin() or RakPeer::DetachPlugin() for this plugin, while RakPeer is active
  109. virtual bool UsesReliabilityLayer(void) const {return false;}
  110. /// Called on a send to the socket, per datagram, that does not go through the reliability layer
  111. /// \pre To be called, UsesReliabilityLayer() must return true
  112. /// \param[in] data The data being sent
  113. /// \param[in] bitsUsed How many bits long \a data is
  114. /// \param[in] remoteSystemAddress Which system this message is being sent to
  115. virtual void OnDirectSocketSend(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) {(void) data; (void) bitsUsed; (void) remoteSystemAddress;}
  116. /// Called on a receive from the socket, per datagram, that does not go through the reliability layer
  117. /// \pre To be called, UsesReliabilityLayer() must return true
  118. /// \param[in] data The data being sent
  119. /// \param[in] bitsUsed How many bits long \a data is
  120. /// \param[in] remoteSystemAddress Which system this message is being sent to
  121. virtual void OnDirectSocketReceive(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) {(void) data; (void) bitsUsed; (void) remoteSystemAddress;}
  122. /// Called when the reliability layer rejects a send or receive
  123. /// \pre To be called, UsesReliabilityLayer() must return true
  124. /// \param[in] bitsUsed How many bits long \a data is
  125. /// \param[in] remoteSystemAddress Which system this message is being sent to
  126. virtual void OnReliabilityLayerNotification(const char *errorMessage, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress, bool isError) {(void) errorMessage; (void) bitsUsed; (void) remoteSystemAddress; (void) isError;}
  127. /// Called on a send or receive of a message within the reliability layer
  128. /// \pre To be called, UsesReliabilityLayer() must return true
  129. /// \param[in] internalPacket The user message, along with all send data.
  130. /// \param[in] frameNumber The number of frames sent or received so far for this player depending on \a isSend . Indicates the frame of this user message.
  131. /// \param[in] remoteSystemAddress The player we sent or got this packet from
  132. /// \param[in] time The current time as returned by RakNet::GetTimeMS()
  133. /// \param[in] isSend Is this callback representing a send event or receive event?
  134. virtual void OnInternalPacket(InternalPacket *internalPacket, unsigned frameNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time, int isSend) {(void) internalPacket; (void) frameNumber; (void) remoteSystemAddress; (void) time; (void) isSend;}
  135. /// Called when we get an ack for a message we reliably sent
  136. /// \pre To be called, UsesReliabilityLayer() must return true
  137. /// \param[in] messageNumber The numerical identifier for which message this is
  138. /// \param[in] remoteSystemAddress The player we sent or got this packet from
  139. /// \param[in] time The current time as returned by RakNet::GetTimeMS()
  140. virtual void OnAck(unsigned int messageNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time) {(void) messageNumber; (void) remoteSystemAddress; (void) time;}
  141. /// System called RakPeerInterface::PushBackPacket
  142. /// \param[in] data The data being sent
  143. /// \param[in] bitsUsed How many bits long \a data is
  144. /// \param[in] remoteSystemAddress The player we sent or got this packet from
  145. virtual void OnPushBackPacket(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) {(void) data; (void) bitsUsed; (void) remoteSystemAddress;}
  146. RakPeerInterface *GetRakPeerInterface(void) const {return rakPeerInterface;}
  147. RakNetGUID GetMyGUIDUnified(void) const;
  148. /// \internal
  149. void SetRakPeerInterface( RakPeerInterface *ptr );
  150. #if _RAKNET_SUPPORT_TCPInterface==1
  151. /// \internal
  152. void SetTCPInterface( TCPInterface *ptr );
  153. #endif
  154. protected:
  155. // Send through either rakPeerInterface or tcpInterface, whichever is available
  156. void SendUnified( const RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast );
  157. void SendUnified( const char * data, const int length, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast );
  158. bool SendListUnified( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast );
  159. Packet *AllocatePacketUnified(unsigned dataSize);
  160. void PushBackPacketUnified(Packet *packet, bool pushAtHead);
  161. void DeallocPacketUnified(Packet *packet);
  162. // Filled automatically in when attached
  163. RakPeerInterface *rakPeerInterface;
  164. #if _RAKNET_SUPPORT_TCPInterface==1
  165. TCPInterface *tcpInterface;
  166. #endif
  167. };
  168. } // namespace RakNet
  169. #endif
粤ICP备19079148号