PacketLoggerTest.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include <cstdio>
  11. #include <cstring>
  12. #include <stdlib.h>
  13. #include "GetTime.h"
  14. #include "Rand.h"
  15. #include "RakPeerInterface.h"
  16. #include "MessageIdentifiers.h"
  17. #include "Gets.h"
  18. #include "PacketLogger.h"
  19. #include <assert.h>
  20. #include "Kbhit.h"
  21. #ifdef _WIN32
  22. #include "WindowsIncludes.h" // Sleep
  23. #else
  24. #include <unistd.h> // usleep
  25. #endif
  26. static const int NUM_PEERS=2;
  27. RakNet::RakPeerInterface *rakPeer[NUM_PEERS];
  28. RakNet::PacketLogger messageHandler[NUM_PEERS];
  29. void PrintConnections(void);
  30. int main(void)
  31. {
  32. int i;
  33. for (i=0; i < NUM_PEERS; i++)
  34. rakPeer[i]=RakNet::RakPeerInterface::GetInstance();
  35. printf("Packet Logger Test.\n");
  36. printf("Displays all packets being sent or received.\n");
  37. printf("Overwrite PacketLogger::Log to render output into your own program.\n");
  38. printf("Difficulty: Intermediate\n\n");
  39. printf("Comma delimited log format:\n");
  40. printf("1. Send or receive,\n");
  41. printf("2. Raw (direct socket send) OR Ack (Acknowledgement) OR\nTms (Timestamped packet),\n");
  42. printf("3. Message number,\n");
  43. printf("4. Packet Number (Independent for send & receive).\n(Each Packet may contain multiple messages),\n");
  44. printf("5. Packet ID (or a string for RPC calls),\n");
  45. printf("6. Bits used by the message (does not include 2-4 byte RakNet header),\n");
  46. printf("7. Time the message is sent,\n");
  47. printf("8. Local System (binary IP followed by port),\n");
  48. printf("9. Remote System (binary IP followed by port)\n\n");
  49. int peerIndex;
  50. // Initialize the message handlers
  51. for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
  52. {
  53. rakPeer[peerIndex]->AttachPlugin(&messageHandler[peerIndex]);
  54. rakPeer[peerIndex]->SetMaximumIncomingConnections(NUM_PEERS);
  55. }
  56. // Initialize the peers
  57. for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
  58. {
  59. RakNet::SocketDescriptor socketDescriptor(60000+peerIndex,0);
  60. rakPeer[peerIndex]->Startup(NUM_PEERS, &socketDescriptor, 1);
  61. }
  62. printf("Connecting two systems...\n\n");
  63. messageHandler[0].LogHeader();
  64. // Connect each peer to the prior peer
  65. for (peerIndex=1; peerIndex < NUM_PEERS; peerIndex++)
  66. {
  67. rakPeer[peerIndex]->Connect("127.0.0.1", 60000+peerIndex-1, 0, 0);
  68. }
  69. #ifdef WIN32
  70. Sleep(5000);
  71. #else
  72. usleep(5000*1000);
  73. #endif
  74. for (i=0; i < NUM_PEERS; i++)
  75. RakNet::RakPeerInterface::DestroyInstance(rakPeer[i]);
  76. printf("Press enter to continue.\n");
  77. char temp[256];
  78. Gets(temp,sizeof(temp));
  79. return 1;
  80. }
粤ICP备19079148号