MaximumConnectTest.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 "MaximumConnectTest.h"
  11. /*
  12. What is being done here is having 8 peers all connect to eachother over the max defined connection.
  13. It runs the connect, wait 20 seconds then see the current connections.
  14. Success conditions:
  15. All extra connections Refused.
  16. Failure conditions:
  17. There are more connected than allowed.
  18. The connect function fails, the test is not even done.
  19. GetMaximumIncomingConnections returns wrong value.
  20. RakPeerInterface Functions used, tested indirectly by its use:
  21. Startup
  22. Connect
  23. SetMaximumIncomingConnections
  24. Receive
  25. DeallocatePacket
  26. GetSystemList
  27. RakPeerInterface Functions Explicitly Tested:
  28. SetMaximumIncomingConnections
  29. GetMaximumIncomingConnections
  30. */
  31. int MaximumConnectTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses)
  32. {
  33. const int peerNum= 8;
  34. const int maxConnections=4;//Max allowed connections for test
  35. RakPeerInterface *peerList[peerNum];//A list of 8 peers
  36. Packet *packet;
  37. destroyList.Clear(false,_FILE_AND_LINE_);
  38. int connReturn;
  39. //Initializations of the arrays
  40. for (int i=0;i<peerNum;i++)
  41. {
  42. peerList[i]=RakPeerInterface::GetInstance();
  43. destroyList.Push(peerList[i],_FILE_AND_LINE_);
  44. peerList[i]->Startup(maxConnections, &SocketDescriptor(60000+i,0), 1);
  45. peerList[i]->SetMaximumIncomingConnections(maxConnections);
  46. connReturn=peerList[i]->GetMaximumIncomingConnections();
  47. if (connReturn!=maxConnections)
  48. {
  49. if (isVerbose)
  50. {
  51. printf("Getmaxconnections wrong for peer %i, %i should be the value but the value is %i.Fail\n",i,maxConnections,connReturn);
  52. DebugTools::ShowError("",!noPauses && isVerbose,__LINE__,__FILE__);
  53. }
  54. }
  55. }
  56. //Connect all the peers together
  57. for (int i=0;i<peerNum;i++)
  58. {
  59. for (int j=i+1;j<peerNum;j++)//Start at i+1 so don't connect two of the same together.
  60. {
  61. if (peerList[i]->Connect("127.0.0.1", 60000+j, 0,0)!=CONNECTION_ATTEMPT_STARTED)
  62. {
  63. if (isVerbose)
  64. DebugTools::ShowError("Problem while calling connect.\n",!noPauses && isVerbose,__LINE__,__FILE__);
  65. return 1;//This fails the test, don't bother going on.
  66. }
  67. }
  68. }
  69. TimeMS entryTime=GetTimeMS();//Loop entry time
  70. while(GetTimeMS()-entryTime<20000)//Run for 20 Secoonds
  71. {
  72. for (int i=0;i<peerNum;i++)//Receive for all peers
  73. {
  74. packet=peerList[i]->Receive();
  75. if (isVerbose&&packet)
  76. printf("For peer %i\n",i);
  77. while(packet)
  78. {
  79. switch (packet->data[0])
  80. {
  81. case ID_REMOTE_DISCONNECTION_NOTIFICATION:
  82. if (isVerbose)
  83. printf("Another client has disconnected.\n");
  84. break;
  85. case ID_REMOTE_CONNECTION_LOST:
  86. if (isVerbose)
  87. printf("Another client has lost the connection.\n");
  88. break;
  89. case ID_REMOTE_NEW_INCOMING_CONNECTION:
  90. if (isVerbose)
  91. printf("Another client has connected.\n");
  92. break;
  93. case ID_CONNECTION_REQUEST_ACCEPTED:
  94. if (isVerbose)
  95. printf("Our connection request has been accepted.\n");
  96. break;
  97. case ID_CONNECTION_ATTEMPT_FAILED:
  98. if (isVerbose)
  99. printf("A connection has failed.\n");//Should happen in this test
  100. break;
  101. case ID_NEW_INCOMING_CONNECTION:
  102. if (isVerbose)
  103. printf("A connection is incoming.\n");
  104. break;
  105. case ID_NO_FREE_INCOMING_CONNECTIONS:
  106. if (isVerbose)
  107. printf("The server is full.\n");
  108. break;
  109. case ID_ALREADY_CONNECTED:
  110. if (isVerbose)
  111. printf("Already connected\n");//Shouldn't happen
  112. break;
  113. case ID_DISCONNECTION_NOTIFICATION:
  114. if (isVerbose)
  115. printf("We have been disconnected.\n");
  116. break;
  117. case ID_CONNECTION_LOST:
  118. if (isVerbose)
  119. printf("Connection lost.\n");
  120. break;
  121. default:
  122. break;
  123. }
  124. peerList[i]->DeallocatePacket(packet);
  125. // Stay in the loop as long as there are more packets.
  126. packet = peerList[i]->Receive();
  127. }
  128. }
  129. RakSleep(0);//If needed for testing
  130. }
  131. DataStructures::List< SystemAddress > systemList;
  132. DataStructures::List< RakNetGUID > guidList;
  133. for (int i=0;i<peerNum;i++)
  134. {
  135. peerList[i]->GetSystemList(systemList,guidList);
  136. int connNum=guidList.Size();//Get the number of connections for the current peer
  137. if (connNum>maxConnections)//Did we connect to more?
  138. {
  139. if (isVerbose)
  140. {
  141. printf("More connections were allowed to peer %i, %i total.Fail\n",i,connNum);
  142. DebugTools::ShowError("",!noPauses && isVerbose,__LINE__,__FILE__);
  143. }
  144. return 2;
  145. }
  146. }
  147. if (isVerbose)
  148. printf("Pass\n");
  149. return 0;
  150. }
  151. RakString MaximumConnectTest::GetTestName()
  152. {
  153. return "MaximumConnectTest";
  154. }
  155. RakString MaximumConnectTest::ErrorCodeToString(int errorCode)
  156. {
  157. switch (errorCode)
  158. {
  159. case 0:
  160. return "No error";
  161. break;
  162. case 1:
  163. return "The connect function failed";
  164. break;
  165. case 2:
  166. return "An extra connection was allowed";
  167. break;
  168. case 3:
  169. return "GetMaximumIncomingConnectionsn returned wrong value";
  170. break;
  171. default:
  172. return "Undefined Error";
  173. }
  174. }
  175. MaximumConnectTest::MaximumConnectTest(void)
  176. {
  177. }
  178. MaximumConnectTest::~MaximumConnectTest(void)
  179. {
  180. }
  181. void MaximumConnectTest::DestroyPeers()
  182. {
  183. int theSize=destroyList.Size();
  184. for (int i=0; i < theSize; i++)
  185. RakPeerInterface::DestroyInstance(destroyList[i]);
  186. }
粤ICP备19079148号