MessageFilterTest.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "RakPeerInterface.h"
  11. #include "MessageFilter.h"
  12. #include "MessageIdentifiers.h"
  13. #include "RakSleep.h"
  14. #include <stdio.h>
  15. int main()
  16. {
  17. // The message filter parses all incoming messages and only allows messages of a certain type
  18. RakNet::MessageFilter messageFilter;
  19. RakNet::RakPeerInterface *peer1, *peer2;
  20. char message;
  21. RakNet::Packet *packet;
  22. peer1=RakNet::RakPeerInterface::GetInstance();
  23. peer2=RakNet::RakPeerInterface::GetInstance();
  24. // Set up the filter rules.
  25. // All new connections go to filter 0
  26. messageFilter.SetAutoAddNewConnectionsToFilter(0);
  27. // Filter 0 only allows ID_USER_PACKET_ENUM
  28. messageFilter.SetAllowMessageID(true, ID_USER_PACKET_ENUM, ID_USER_PACKET_ENUM, 0);
  29. // Filter 1 only allows ID_USER_PACKET_ENUM
  30. messageFilter.SetAllowMessageID(true, ID_USER_PACKET_ENUM+1, ID_USER_PACKET_ENUM+1, 1);
  31. // Use the filter on peer 1.
  32. peer1->AttachPlugin(&messageFilter);
  33. // Connect the systems to each other
  34. RakNet::SocketDescriptor socketDescriptor(60000,0);
  35. peer1->Startup(1,&socketDescriptor, 1);
  36. peer1->SetMaximumIncomingConnections(1);
  37. socketDescriptor.port=60001;
  38. peer2->Startup(1,&socketDescriptor, 1);
  39. peer2->Connect("127.0.0.1", 60000,0,0);
  40. // Wait for the connection to complete
  41. while (1)
  42. {
  43. packet = peer1->Receive();
  44. if (packet && packet->data[0]==ID_NEW_INCOMING_CONNECTION)
  45. {
  46. printf("Connected.\n");
  47. peer1->DeallocatePacket(packet);
  48. break;
  49. }
  50. peer1->DeallocatePacket(packet);
  51. RakSleep(0);
  52. }
  53. printf("Peer 2 sending ID_USER_PACKET_ENUM+1 and ID_USER_PACKET_ENUM\n");
  54. // Have peer 2 send a disallowed message, then the allowed message.
  55. message=ID_USER_PACKET_ENUM+1;
  56. peer2->Send(&message, 1, HIGH_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  57. // Allowed message
  58. message=ID_USER_PACKET_ENUM;
  59. peer2->Send(&message, 1, HIGH_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  60. RakSleep(1000);
  61. // Print out the messages that peer 1 got
  62. printf("We should get ID_USER_PACKET_ENUM and not ID_USER_PACKET_ENUM+1.\n");
  63. packet = peer1->Receive();
  64. while (packet)
  65. {
  66. switch (packet->data[0])
  67. {
  68. case ID_USER_PACKET_ENUM:
  69. printf("ID_USER_PACKET_ENUM\n");
  70. printf("User switched to group 1\n");
  71. // Switch the sender to group 1 now so that ID_USER_PACKET_ENUM+1 is allowed.
  72. messageFilter.SetSystemFilterSet(packet, 1);
  73. break;
  74. case ID_USER_PACKET_ENUM+1:
  75. printf("ID_USER_PACKET_ENUM+1\n");
  76. break;
  77. }
  78. peer1->DeallocatePacket(packet);
  79. RakSleep(0);
  80. packet = peer1->Receive();
  81. }
  82. // Have peer 2 send the messages again.
  83. message=ID_USER_PACKET_ENUM+1;
  84. peer2->Send(&message, 1, HIGH_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  85. message=ID_USER_PACKET_ENUM;
  86. peer2->Send(&message, 1, HIGH_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  87. RakSleep(1000);
  88. printf("We should now NOT get ID_USER_PACKET_ENUM and should get ID_USER_PACKET_ENUM+1\n");
  89. packet = peer1->Receive();
  90. while (packet)
  91. {
  92. switch (packet->data[0])
  93. {
  94. case ID_USER_PACKET_ENUM:
  95. printf("ID_USER_PACKET_ENUM\n");
  96. break;
  97. case ID_USER_PACKET_ENUM+1:
  98. printf("ID_USER_PACKET_ENUM+1\n");
  99. break;
  100. }
  101. peer1->DeallocatePacket(packet);
  102. packet = peer1->Receive();
  103. RakSleep(0);
  104. }
  105. printf("Done.\n");
  106. RakNet::RakPeerInterface::DestroyInstance(peer1);
  107. RakNet::RakPeerInterface::DestroyInstance(peer2);
  108. return 0;
  109. }
粤ICP备19079148号