PacketLogger.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 This will write all incoming and outgoing network messages to the local console screen. See derived functions for other outputs
  12. ///
  13. #include "NativeFeatureIncludes.h"
  14. #if _RAKNET_SUPPORT_PacketLogger==1
  15. #ifndef __PACKET_LOGGER_H
  16. #define __PACKET_LOGGER_H
  17. #include "RakNetTypes.h"
  18. #include "PluginInterface2.h"
  19. #include "Export.h"
  20. namespace RakNet
  21. {
  22. /// Forward declarations
  23. class RakPeerInterface;
  24. /// \defgroup PACKETLOGGER_GROUP PacketLogger
  25. /// \brief Print out incoming messages to a target destination
  26. /// \details
  27. /// \ingroup PLUGINS_GROUP
  28. /// \brief Writes incoming and outgoing messages to the screen.
  29. /// This will write all incoming and outgoing messages to the console window, or to a file if you override it and give it this functionality.
  30. /// \ingroup PACKETLOGGER_GROUP
  31. class RAK_DLL_EXPORT PacketLogger : public PluginInterface2
  32. {
  33. public:
  34. // GetInstance() and DestroyInstance(instance*)
  35. STATIC_FACTORY_DECLARATIONS(PacketLogger)
  36. PacketLogger();
  37. virtual ~PacketLogger();
  38. // Translate the supplied parameters into an output line - overloaded version that takes a MessageIdentifier
  39. // and translates it into a string (numeric or textual representation based on printId); this calls the
  40. // second version which takes a const char* argument for the messageIdentifier
  41. virtual void FormatLine(char* into, const char* dir, const char* type, unsigned int reliableMessageNumber, unsigned int frame
  42. , unsigned char messageIdentifier, const BitSize_t bitLen, unsigned long long time, const SystemAddress& local, const SystemAddress& remote,
  43. unsigned int splitPacketId, unsigned int splitPacketIndex, unsigned int splitPacketCount, unsigned int orderingIndex);
  44. virtual void FormatLine(char* into, const char* dir, const char* type, unsigned int reliableMessageNumber, unsigned int frame
  45. , const char* idToPrint, const BitSize_t bitLen, unsigned long long time, const SystemAddress& local, const SystemAddress& remote,
  46. unsigned int splitPacketId, unsigned int splitPacketIndex, unsigned int splitPacketCount, unsigned int orderingIndex);
  47. /// Events on low level sends and receives. These functions may be called from different threads at the same time.
  48. virtual void OnDirectSocketSend(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress);
  49. virtual void OnDirectSocketReceive(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress);
  50. virtual void OnReliabilityLayerNotification(const char *errorMessage, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress, bool isError);
  51. virtual void OnInternalPacket(InternalPacket *internalPacket, unsigned frameNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time, int isSend);
  52. virtual void OnAck(unsigned int messageNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time);
  53. virtual void OnPushBackPacket(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress);
  54. /// Logs out a header for all the data
  55. virtual void LogHeader(void);
  56. /// Override this to log strings to wherever. Log should be threadsafe
  57. virtual void WriteLog(const char *str);
  58. // Write informational messages
  59. virtual void WriteMiscellaneous(const char *type, const char *msg);
  60. // Set to true to print ID_* instead of numbers
  61. virtual void SetPrintID(bool print);
  62. // Print or hide acks (clears up the screen not to print them but is worse for debugging)
  63. virtual void SetPrintAcks(bool print);
  64. /// Prepend this string to output logs.
  65. virtual void SetPrefix(const char *_prefix);
  66. /// Append this string to output logs. (newline is useful here)
  67. virtual void SetSuffix(const char *_suffix);
  68. static const char* BaseIDTOString(unsigned char Id);
  69. /// Log the direct sends and receives or not. Default true
  70. void SetLogDirectMessages(bool send);
  71. protected:
  72. virtual bool UsesReliabilityLayer(void) const {return true;}
  73. const char* IDTOString(unsigned char Id);
  74. virtual void AddToLog(const char *str);
  75. // Users should override this
  76. virtual const char* UserIDTOString(unsigned char Id);
  77. void GetLocalTime(char buffer[128]);
  78. bool logDirectMessages;
  79. bool printId, printAcks;
  80. char prefix[256];
  81. char suffix[256];
  82. };
  83. } // namespace RakNet
  84. #endif
  85. #endif // _RAKNET_SUPPORT_*
粤ICP备19079148号