CrossConnectionTest.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 Tests connecting two peers at the same time with the internet simulator running.
  12. #include "RakPeerInterface.h"
  13. #include "PacketLogger.h"
  14. #include "Rand.h"
  15. #include "Kbhit.h"
  16. #include <stdio.h> // Printf
  17. #include "RakSleep.h"
  18. #include "MessageIdentifiers.h"
  19. #include "BitStream.h"
  20. #include "GetTime.h"
  21. using namespace RakNet;
  22. int main()
  23. {
  24. printf("An internal test to test two peers connecting to each other\n");
  25. printf("at the same time. This causes bugs so I fix them here\n");
  26. RakPeerInterface *rakPeer1, *rakPeer2;
  27. rakPeer1=RakPeerInterface::GetInstance();
  28. rakPeer2=RakPeerInterface::GetInstance();
  29. rakPeer1->SetMaximumIncomingConnections(8);
  30. rakPeer2->SetMaximumIncomingConnections(8);
  31. int gotConnectionRequestAccepted[2];
  32. int gotNewIncomingConnection[2];
  33. Packet *packet;
  34. SocketDescriptor sd1(60000,0);
  35. SocketDescriptor sd2(2000,0);
  36. unsigned short numSystems[2];
  37. while (1)
  38. {
  39. gotConnectionRequestAccepted[0]=0;
  40. gotConnectionRequestAccepted[1]=0;
  41. gotNewIncomingConnection[0]=0;
  42. gotNewIncomingConnection[1]=0;
  43. numSystems[0]=0;
  44. numSystems[1]=0;
  45. rakPeer1->Startup(1,&sd1, 1);
  46. rakPeer2->Startup(1,&sd2, 1);
  47. RakSleep(100);
  48. rakPeer1->Connect("127.0.0.1", 2000, 0, 0);
  49. rakPeer2->Connect("127.0.0.1", 60000, 0, 0);
  50. RakSleep(100);
  51. for (packet=rakPeer1->Receive(); packet; rakPeer1->DeallocatePacket(packet), packet=rakPeer1->Receive())
  52. {
  53. if (packet->data[0]==ID_NEW_INCOMING_CONNECTION)
  54. gotNewIncomingConnection[0]++;
  55. else if (packet->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
  56. gotConnectionRequestAccepted[0]++;
  57. else if (packet->data[0]==ID_CONNECTION_ATTEMPT_FAILED)
  58. printf("Error on rakPeer1, got ID_CONNECTION_ATTEMPT_FAILED\n");
  59. else if (packet->data[0]==ID_ALREADY_CONNECTED)
  60. printf("Got ID_ALREADY_CONNECTED on rakPeer1, (not necessarily a failure)\n");
  61. }
  62. for (packet=rakPeer2->Receive(); packet; rakPeer2->DeallocatePacket(packet), packet=rakPeer2->Receive())
  63. {
  64. if (packet->data[0]==ID_NEW_INCOMING_CONNECTION)
  65. gotNewIncomingConnection[1]++;
  66. else if (packet->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
  67. gotConnectionRequestAccepted[1]++;
  68. else if (packet->data[0]==ID_CONNECTION_ATTEMPT_FAILED)
  69. printf("Error on rakPeer2, got ID_CONNECTION_ATTEMPT_FAILED\n");
  70. else if (packet->data[0]==ID_ALREADY_CONNECTED)
  71. printf("Got ID_ALREADY_CONNECTED on rakPeer2, (not necessarily a failure)\n");
  72. }
  73. rakPeer1->GetConnectionList(0,&numSystems[0]);
  74. rakPeer2->GetConnectionList(0,&numSystems[1]);
  75. if (gotConnectionRequestAccepted[0]==1 && gotConnectionRequestAccepted[1]==1)
  76. {
  77. printf("Test passed\n");
  78. }
  79. else if (numSystems[0]!=1 || numSystems[1]!=1)
  80. {
  81. printf("Test failed, system 1 has %i connections and system 2 has %i connections.\n", numSystems[0], numSystems[1]);
  82. }
  83. else if (gotConnectionRequestAccepted[0]==0 && gotConnectionRequestAccepted[1]==0)
  84. {
  85. printf("Test failed, ID_CONNECTION_REQUEST_ACCEPTED is false for both instances\n");
  86. }
  87. else if (gotNewIncomingConnection[0]==1 && gotNewIncomingConnection[1]==1)
  88. {
  89. printf("Test failed, ID_NEW_INCOMING_CONNECTION is true for both instances\n");
  90. }
  91. else if (gotConnectionRequestAccepted[0]==0 && gotConnectionRequestAccepted[1]==0)
  92. {
  93. printf("Test failed, ID_NEW_INCOMING_CONNECTION is false for both instances\n");
  94. }
  95. else if (gotConnectionRequestAccepted[0]==1 && gotNewIncomingConnection[1]==0)
  96. {
  97. printf("Test failed, ID_CONNECTION_REQUEST_ACCEPTED for first instance, but not ID_NEW_INCOMING_CONNECTION for second\n");
  98. }
  99. else if (gotConnectionRequestAccepted[1]==1 && gotNewIncomingConnection[0]==0)
  100. {
  101. printf("Test failed, ID_CONNECTION_REQUEST_ACCEPTED for second instance, but not ID_NEW_INCOMING_CONNECTION for first\n");
  102. }
  103. else if (gotConnectionRequestAccepted[0]+gotConnectionRequestAccepted[1]!=1)
  104. {
  105. printf("Test failed, does not have exactly one instance of ID_CONNECTION_REQUEST_ACCEPTED\n");
  106. }
  107. else if (gotNewIncomingConnection[0]+gotNewIncomingConnection[1]!=1)
  108. {
  109. printf("Test failed, does not have exactly one instance of ID_NEW_INCOMING_CONNECTION\n");
  110. }
  111. else if (gotConnectionRequestAccepted[0]+
  112. gotConnectionRequestAccepted[1]+
  113. gotNewIncomingConnection[0]+
  114. gotNewIncomingConnection[1]!=2)
  115. {
  116. printf("Test failed, does not have exactly one instance of ID_CONNECTION_REQUEST_ACCEPTED and one instance of ID_NEW_INCOMING_CONNECTION\n");
  117. }
  118. else
  119. printf("Test passed\n");
  120. rakPeer1->Shutdown(0);
  121. rakPeer2->Shutdown(0);
  122. RakSleep(100);
  123. }
  124. return 0;
  125. }
粤ICP备19079148号