CommandParserInterface.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 CommandParserInterface.h
  11. /// \brief Contains CommandParserInterface , from which you derive custom command parsers
  12. ///
  13. #ifndef __COMMAND_PARSER_INTERFACE
  14. #define __COMMAND_PARSER_INTERFACE
  15. #include "RakMemoryOverride.h"
  16. #include "RakNetTypes.h"
  17. #include "DS_OrderedList.h"
  18. #include "Export.h"
  19. namespace RakNet
  20. {
  21. /// Forward declarations
  22. class TransportInterface;
  23. /// \internal
  24. /// Contains the information related to one command registered with RegisterCommand()
  25. /// Implemented so I can have an automatic help system via SendCommandList()
  26. struct RAK_DLL_EXPORT RegisteredCommand
  27. {
  28. const char *command;
  29. const char *commandHelp;
  30. unsigned char parameterCount;
  31. };
  32. /// List of commands registered with RegisterCommand()
  33. int RAK_DLL_EXPORT RegisteredCommandComp( const char* const & key, const RegisteredCommand &data );
  34. /// \brief The interface used by command parsers.
  35. /// \details CommandParserInterface provides a set of functions and interfaces that plug into the ConsoleServer class.
  36. /// Each CommandParserInterface works at the same time as other interfaces in the system.
  37. class RAK_DLL_EXPORT CommandParserInterface
  38. {
  39. public:
  40. CommandParserInterface();
  41. virtual ~CommandParserInterface();
  42. /// You are responsible for overriding this function and returning a static string, which will identifier your parser.
  43. /// This should return a static string
  44. /// \return The name that you return.
  45. virtual const char *GetName(void) const=0;
  46. /// \brief A callback for when \a systemAddress has connected to us.
  47. /// \param[in] systemAddress The player that has connected.
  48. /// \param[in] transport The transport interface that sent us this information. Can be used to send messages to this or other players.
  49. virtual void OnNewIncomingConnection(const SystemAddress &systemAddress, TransportInterface *transport);
  50. /// \brief A callback for when \a systemAddress has disconnected, either gracefully or forcefully
  51. /// \param[in] systemAddress The player that has disconnected.
  52. /// \param[in] transport The transport interface that sent us this information.
  53. virtual void OnConnectionLost(const SystemAddress &systemAddress, TransportInterface *transport);
  54. /// \brief A callback for when you are expected to send a brief description of your parser to \a systemAddress
  55. /// \param[in] transport The transport interface we can use to write to
  56. /// \param[in] systemAddress The player that requested help.
  57. virtual void SendHelp(TransportInterface *transport, const SystemAddress &systemAddress)=0;
  58. /// \brief Given \a command with parameters \a parameterList , do whatever processing you wish.
  59. /// \param[in] command The command to process
  60. /// \param[in] numParameters How many parameters were passed along with the command
  61. /// \param[in] parameterList The list of parameters. parameterList[0] is the first parameter and so on.
  62. /// \param[in] transport The transport interface we can use to write to
  63. /// \param[in] systemAddress The player that sent this command.
  64. /// \param[in] originalString The string that was actually sent over the network, in case you want to do your own parsing
  65. virtual bool OnCommand(const char *command, unsigned numParameters, char **parameterList, TransportInterface *transport, const SystemAddress &systemAddress, const char *originalString)=0;
  66. /// \brief This is called every time transport interface is registered.
  67. /// \details If you want to save a copy of the TransportInterface pointer
  68. /// This is the place to do it
  69. /// \param[in] transport The new TransportInterface
  70. virtual void OnTransportChange(TransportInterface *transport);
  71. /// \internal
  72. /// Scan commandList and return the associated array
  73. /// \param[in] command The string to find
  74. /// \param[out] rc Contains the result of this operation
  75. /// \return True if we found the command, false otherwise
  76. virtual bool GetRegisteredCommand(const char *command, RegisteredCommand *rc);
  77. /// \internal
  78. /// Goes through str, replacing the delineating character with 0's.
  79. /// \param[in] str The string sent by the transport interface
  80. /// \param[in] delineator The character to scan for to use as a delineator
  81. /// \param[in] delineatorToggle When encountered the delineator replacement is toggled on and off
  82. /// \param[out] numParameters How many pointers were written to \a parameterList
  83. /// \param[out] parameterList An array of pointers to characters. Will hold pointers to locations inside \a str
  84. /// \param[in] parameterListLength How big the \a parameterList array is
  85. static void ParseConsoleString(char *str, const char delineator, unsigned char delineatorToggle, unsigned *numParameters, char **parameterList, unsigned parameterListLength);
  86. /// \internal
  87. /// Goes through the variable commandList and sends the command portion of each struct
  88. /// \param[in] transport The transport interface we can use to write to
  89. /// \param[in] systemAddress The player to write to
  90. virtual void SendCommandList(TransportInterface *transport, const SystemAddress &systemAddress);
  91. static const unsigned char VARIABLE_NUMBER_OF_PARAMETERS;
  92. // Currently only takes static strings - doesn't make a copy of what you pass.
  93. // parameterCount is the number of parameters that the sender has to include with the command.
  94. // Pass 255 to parameterCount to indicate variable number of parameters
  95. /// Registers a command.
  96. /// \param[in] parameterCount How many parameters your command requires. If you want to accept a variable number of commands, pass CommandParserInterface::VARIABLE_NUMBER_OF_PARAMETERS
  97. /// \param[in] command A pointer to a STATIC string that has your command. I keep a copy of the pointer here so don't deallocate the string.
  98. /// \param[in] commandHelp A pointer to a STATIC string that has the help information for your command. I keep a copy of the pointer here so don't deallocate the string.
  99. virtual void RegisterCommand(unsigned char parameterCount, const char *command, const char *commandHelp);
  100. /// \brief Just writes a string to the remote system based on the result ( \a res ) of your operation
  101. /// \details This is not necessary to call, but makes it easier to return results of function calls.
  102. /// \param[in] res The result to write
  103. /// \param[in] command The command that this result came from
  104. /// \param[in] transport The transport interface that will be written to
  105. /// \param[in] systemAddress The player this result will be sent to
  106. virtual void ReturnResult(bool res, const char *command, TransportInterface *transport, const SystemAddress &systemAddress);
  107. virtual void ReturnResult(char *res, const char *command, TransportInterface *transport, const SystemAddress &systemAddress);
  108. virtual void ReturnResult(SystemAddress res, const char *command, TransportInterface *transport, const SystemAddress &systemAddress);
  109. virtual void ReturnResult(int res, const char *command,TransportInterface *transport, const SystemAddress &systemAddress);
  110. /// \brief Just writes a string to the remote system when you are calling a function that has no return value.
  111. /// \details This is not necessary to call, but makes it easier to return results of function calls.
  112. /// \param[in] res The result to write
  113. /// \param[in] command The command that this result came from
  114. /// \param[in] transport The transport interface that will be written to
  115. /// \param[in] systemAddress The player this result will be sent to
  116. virtual void ReturnResult(const char *command,TransportInterface *transport, const SystemAddress &systemAddress);
  117. protected:
  118. DataStructures::OrderedList<const char*, RegisteredCommand, RegisteredCommandComp> commandList;
  119. };
  120. } // namespace RakNet
  121. #endif
粤ICP备19079148号