SocketDescriptor SocketDescriptor::Accept() { int fd = ::accept(Get(), NULL, NULL); return fd >= 0 ? SocketDescriptor(fd) : SocketDescriptor(); }
/** * The main loop for the network. Does the receiving of data, and the notifications of the sever objects. */ void Network::run() { requestStop = false; Packet* packet; ServerPeer* peer; rakServer->Startup(maxClients, 20, &SocketDescriptor(port,this->hostname), 1); rakServer->SetMaximumIncomingConnections(maxClients); REGISTER_STATIC_RPC(rakServer, ServerPeer::NumClients); while (!requestStop) { packet = rakServer->Receive(); if (packet) { switch(packet->data[0]) { case ID_NEW_INCOMING_CONNECTION: printf("Someone is attempting to connect from: %s\n", packet->systemAddress.ToString()); new ServerPeer(rakServer, packet->systemAddress); break; case ID_CONNECTION_LOST: printf("Lost connection to client from %s!\n", packet->systemAddress.ToString()); peer = ServerPeer::getPeer(packet->systemAddress.binaryAddress); if (peer != NULL) delete peer; break; case ID_DISCONNECTION_NOTIFICATION: printf("Client closed connection from %s\n", packet->systemAddress.ToString()); peer = ServerPeer::getPeer(packet->systemAddress.binaryAddress); if (peer != NULL) delete peer; break; default: printf("Message with identifier %i has arrived from %s\n", packet->data[0], packet->systemAddress.ToString()); printf("Message with identifier %s has arrived from %s\n", packet->data, packet->systemAddress.ToString()); peer = ServerPeer::getPeer(packet->systemAddress.binaryAddress); } rakServer->DeallocatePacket(packet); } } }
bool Application::Init() { std::ifstream inData; std::string serverip; inData.open("serverip.txt"); inData >> serverip; srand( RakNet::GetTime() ); hge_->System_SetState(HGE_FRAMEFUNC, Application::Loop); hge_->System_SetState(HGE_WINDOWED, true); hge_->System_SetState(HGE_USESOUND, false); hge_->System_SetState(HGE_TITLE, "Movement"); hge_->System_SetState(HGE_LOGFILE, "movement.log"); hge_->System_SetState(HGE_DONTSUSPEND, true); hge_->System_SetState(HGE_SCREENWIDTH, S_SCREEN_WIDTH); hge_->System_SetState(HGE_SCREENHEIGHT, S_SCREEN_HEIGHT); if(hge_->System_Initiate()) { ships_.push_back(new Ship(rand() % 3 + 1, S_SCREEN_WIDTH * 0.5f, S_SCREEN_HEIGHT * 0.5f)); //ships_.push_back(new Ship(rand() % 3 + 1, rand() % 500 + 100, rand() % 400 + 100)); ships_.at(0)->SetName("My Ship"); if (rakpeer_->Startup(1,30,&SocketDescriptor(), 1)) { rakpeer_->SetOccasionalPing(true); return rakpeer_->Connect(serverip.c_str(), 1691, 0, 0); } } return false; }
void NetworkClient::SetupConnection(string ip, int poort) { serverIP = ip; serverPort = poort; sMessage = new RakNet::BitStream(); rMessage = new RakNet::BitStream(); peer->Startup(1, 0, &SocketDescriptor(), 1); peer->Connect(ip.c_str(), poort, 0, 0); }
SocketDescriptor SocketDescriptor::Accept() { #if defined(__linux__) && !defined(__BIONIC__) && !defined(KOBO) int fd = ::accept4(Get(), nullptr, nullptr, SOCK_CLOEXEC); #else int fd = ::accept(Get(), nullptr, nullptr); #endif return fd >= 0 ? SocketDescriptor(fd) : Undefined(); }
ServerApp::ServerApp() : rakpeer_(RakNetworkFactory::GetRakPeerInterface()), newID(0) { rakpeer_->Startup(100, 30, &SocketDescriptor(1691, 0), 1); rakpeer_->SetMaximumIncomingConnections(100); rakpeer_->SetOccasionalPing(true); std::cout << "Server Started" << std::endl; prevTime = RakNet::GetTime(); Math::InitRNG(); InitEnemyList(); base_hp = 50; }
/* What is being done here is having 8 peers all connect to eachother and be connected. Then it check if they all connect. If so send data in ordered reliable mode for 100 loops. Possible ideas for changes: Possibly use rakpeerinterfaces GetSystemList() for number of connected peers instead of manually tracking. Would be slower though, shouldn't be significant at this number but the recieve speed it part of the test. Success conditions: Peers connect and receive all packets in order. No disconnections allowed in this version of the test. Failure conditions: If cannot connect to all peers for 20 seconds. All packets are not recieved. All packets are not in order. Disconnection. */ int EightPeerTest::RunTest(DataStructures::List<RakNet::RakString> params,bool isVerbose,bool noPauses) { const int peerNum= 8; RakPeerInterface *peerList[peerNum];//A list of 8 peers int connectionAmount[peerNum];//Counter for me to keep track of connection requests and accepts int recievedFromList[peerNum][peerNum];//Counter for me to keep track of packets received int lastNumberReceivedFromList[peerNum][peerNum];//Counter for me to keep track of last recieved sequence number Packet *packet; RakNet::BitStream bitStream; destroyList.Clear(false,__FILE__,__LINE__); //Initializations of the arrays for (int i=0;i<peerNum;i++) { peerList[i]=RakNetworkFactory::GetRakPeerInterface(); destroyList.Push(peerList[i],__FILE__,__LINE__); connectionAmount[i]=0; for (int j=0;j<peerNum;j++) { recievedFromList[i][j]=0; lastNumberReceivedFromList[i][j]=0; } peerList[i]->Startup(peerNum*2, 30, &SocketDescriptor(60000+i,0), 1); peerList[i]->SetMaximumIncomingConnections(peerNum); } //Connect all the peers together for (int i=0;i<peerNum;i++) { for (int j=i+1;j<peerNum;j++)//Start at i+1 so don't connect two of the same together. { if (!peerList[i]->Connect("127.0.0.1", 60000+j, 0,0)) { if (isVerbose) { DebugTools::ShowError("Problem while calling connect. \n",!noPauses && isVerbose,__LINE__,__FILE__); } return 1;//This fails the test, don't bother going on. } } } RakNetTime entryTime=RakNet::GetTime();//Loop entry time bool initialConnectOver=false;//Our initial connect all has been done. for (int k=0;k<100;)//Quit after we send 100 messages while connected, if not all connected and not failure, otherwise fail after 20 seconds and exit { bool allConnected=true;//Start true, only one failed case makes it all fail for (int i=0;i<peerNum;i++)//Make sure all peers are connected to eachother { if (connectionAmount[i]<peerNum-1) { allConnected=false; } } if (RakNet::GetTime()-entryTime>20000 &&!initialConnectOver &&!allConnected)//failed for 20 seconds { if (isVerbose) DebugTools::ShowError("Failed to connect to all peers after 20 seconds",!noPauses && isVerbose,__LINE__,__FILE__); return 2; break; } if (allConnected) { if(!initialConnectOver) initialConnectOver=true; for (int i=0;i<peerNum;i++)//Have all peers send a message to all peers { bitStream.Reset(); bitStream.Write((unsigned char) (ID_USER_PACKET_ENUM+1)); bitStream.Write(k); bitStream.Write(i); peerList[i]->Send(&bitStream, HIGH_PRIORITY, RELIABLE_ORDERED ,0, UNASSIGNED_SYSTEM_ADDRESS, true); } k++; } if (k>=97)//This is our last 3 loops, give it time to send packet and arrive on interface, 2 seconds is more than enough { RakSleep(2000); } for (int i=0;i<peerNum;i++)//Receive for all peers { if (allConnected)//If all connected try to make the data more visually appealing by bunching it in one receive { int waittime=0; do { packet=peerList[i]->Receive(); waittime++; if (!packet) { RakSleep(1); } if (waittime>1000)//Check for packet every millisec and if one second has passed move on, don't block execution { break; } } while(!packet);//For testing purposes wait for packet a little while, go if not recieved } else//Otherwise just keep recieving quickly until connected { packet=peerList[i]->Receive(); } if (isVerbose) printf("For peer %i with %i connected peers.\n",i,connectionAmount[i]); while(packet) { switch (packet->data[0]) { case ID_REMOTE_DISCONNECTION_NOTIFICATION: if (isVerbose) { printf("Another client has disconnected.\n"); DebugTools::ShowError("Test failed.\n",!noPauses && isVerbose,__LINE__,__FILE__); } return 3; break; case ID_REMOTE_CONNECTION_LOST: if (isVerbose) { printf("Another client has lost the connection.\n"); DebugTools::ShowError("Test failed.\n",!noPauses && isVerbose,__LINE__,__FILE__); } return 3; break; case ID_REMOTE_NEW_INCOMING_CONNECTION: if (isVerbose) printf("Another client has connected.\n"); break; case ID_CONNECTION_REQUEST_ACCEPTED: if (isVerbose) printf("Our connection request has been accepted.\n"); connectionAmount[i]++; break; case ID_CONNECTION_ATTEMPT_FAILED: if (isVerbose) DebugTools::ShowError("A connection has failed.\n Test failed.\n",!noPauses && isVerbose,__LINE__,__FILE__); return 2; break; case ID_NEW_INCOMING_CONNECTION: if (isVerbose) printf("A connection is incoming.\n"); connectionAmount[i]++;//For this test assume connection. Test will fail if connection fails. break; case ID_NO_FREE_INCOMING_CONNECTIONS://Should not happend if (isVerbose) { printf("The server is full. This shouldn't happen in this test ever.\n"); DebugTools::ShowError("Test failed.\n",!noPauses && isVerbose,__LINE__,__FILE__); } return 2; break; case ID_ALREADY_CONNECTED: if (isVerbose) printf("Already connected\n");//Shouldn't happen break; case ID_DISCONNECTION_NOTIFICATION: if (isVerbose) { printf("We have been disconnected.\n"); DebugTools::ShowError("Test failed.\n",!noPauses && isVerbose,__LINE__,__FILE__); } return 3; break; case ID_CONNECTION_LOST: allConnected=false; connectionAmount[i]--; if (isVerbose) { printf("Connection lost.\n"); DebugTools::ShowError("Test failed.\n",!noPauses && isVerbose,__LINE__,__FILE__); } return 3; break; default: if (packet->data[0]==ID_USER_PACKET_ENUM+1) { int thePeerNum; int sequenceNum; bitStream.Reset(); bitStream.Write((char*)packet->data, packet->length); bitStream.IgnoreBits(8); bitStream.Read(sequenceNum); bitStream.Read(thePeerNum); if (isVerbose) printf("Message %i from %i\n",sequenceNum,thePeerNum ); if (thePeerNum>=0&&thePeerNum<peerNum) { if (lastNumberReceivedFromList[i][thePeerNum]==sequenceNum) { lastNumberReceivedFromList[i][thePeerNum]++; } else { if (isVerbose) { printf("Packets out of order"); DebugTools::ShowError("Test failed.\n",!noPauses && isVerbose,__LINE__,__FILE__); } return 4; } recievedFromList[i][thePeerNum]++;} } break; } peerList[i]->DeallocatePacket(packet); // Stay in the loop as long as there are more packets. packet = peerList[i]->Receive(); } } RakSleep(0);//If needed for testing } for (int i=0;i<peerNum;i++) { for (int j=0;j<peerNum;j++) { if (i!=j) { if (isVerbose) printf("%i recieved %i packets from %i\n",i,recievedFromList[i][j],j); if (recievedFromList[i][j]!=100) { if (isVerbose) { printf("Not all packets recieved. it was in reliable ordered mode so that means test failed or wait time needs increasing\n"); DebugTools::ShowError("Test failed.\n",!noPauses && isVerbose,__LINE__,__FILE__); } return 5; } } } } printf("All packets recieved in order,pass\n"); return 0; }
/* Description: Tests: virtual void RakPeerInterface::AddToSecurityExceptionList ( const char * ip ) virtual void RakPeerInterface::AddToBanList ( const char * IP, TimeMS milliseconds = 0 ) virtual void RakPeerInterface::GetIncomingPassword ( char * passwordData, int * passwordDataLength ) virtual void RakPeerInterface::InitializeSecurity ( const char * pubKeyE, const char * pubKeyN, const char * privKeyP, const char * privKeyQ ) virtual bool RakPeerInterface::IsBanned ( const char * IP ) virtual bool RakPeerInterface::IsInSecurityExceptionList ( const char * ip ) virtual void RakPeerInterface::RemoveFromSecurityExceptionList ( const char * ip ) virtual void RakPeerInterface::RemoveFromBanList ( const char * IP ) virtual void RakPeerInterface::SetIncomingPassword ( const char * passwordData, int passwordDataLength ) virtual void ClearBanList (void)=0 Success conditions: All functions pass tests. Failure conditions: Any function fails test. Client connects with no password Client connects with wrong password Client failed to connect with correct password Client was banned but connected anyways GetIncomingPassword returned wrong password IsBanned does not show localhost as banned Localhost was not unbanned Client failed to connect after banlist removal Client failed to connect after banlist removal with clear function Client did not connect encrypted Client connected encrypted but shouldn't have IsInSecurityExceptionList does not register localhost addition RakPeerInterface Functions used, tested indirectly by its use: Startup SetMaximumIncomingConnections Receive DeallocatePacket Send IsConnected GetStatistics RakPeerInterface Functions Explicitly Tested: SetIncomingPassword GetIncomingPassword AddToBanList IsBanned RemoveFromBanList ClearBanList InitializeSecurity //Disabled because of RakNetStatistics changes AddToSecurityExceptionList //Disabled because of RakNetStatistics changes IsInSecurityExceptionList //Disabled because of RakNetStatistics changes RemoveFromSecurityExceptionList //Disabled because of RakNetStatistics changes */ int SecurityFunctionsTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses) { char thePassword[]="password"; server=RakPeerInterface::GetInstance(); client=RakPeerInterface::GetInstance(); client->Startup(1,&SocketDescriptor(),1); server->Startup(1,&SocketDescriptor(60000,0),1); server->SetMaximumIncomingConnections(1); server->SetIncomingPassword(thePassword,(int)strlen(thePassword)); char returnedPass[22]; int returnedLen=22; server->GetIncomingPassword(returnedPass,&returnedLen); returnedPass[returnedLen]=0;//Password is a data block convert to null terminated string to make the test easier if (strcmp(returnedPass,thePassword)!=0) { if (isVerbose) { printf("%s was returned but %s is the password\n",returnedPass,thePassword); DebugTools::ShowError("GetIncomingPassword returned wrong password\n",!noPauses && isVerbose,__LINE__,__FILE__); } return 5; } SystemAddress serverAddress; serverAddress.SetBinaryAddress("127.0.0.1"); serverAddress.port=60000; TimeMS entryTime=GetTimeMS(); if (isVerbose) printf("Testing if no password is rejected\n"); while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000) { if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true)) { client->Connect("127.0.0.1",serverAddress.port,0,0); } RakSleep(100); } if (CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)) { if (isVerbose) DebugTools::ShowError("Client connected with no password\n",!noPauses && isVerbose,__LINE__,__FILE__); return 1; } if (isVerbose) printf("Testing if incorrect password is rejected\n"); char badPass[]="badpass"; entryTime=GetTimeMS(); while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000) { if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true)) { client->Connect("127.0.0.1",serverAddress.port,badPass,(int)strlen(badPass)); } RakSleep(100); } if (CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)) { if (isVerbose) DebugTools::ShowError("Client connected with wrong password\n",!noPauses && isVerbose,__LINE__,__FILE__); return 2; } if (isVerbose) printf("Testing if correct password is accepted\n"); entryTime=GetTimeMS(); while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000) { if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true)) { client->Connect("127.0.0.1",serverAddress.port,thePassword,(int)strlen(thePassword)); } RakSleep(100); } if (!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)) { if (isVerbose) DebugTools::ShowError("Client failed to connect with correct password\n",!noPauses && isVerbose,__LINE__,__FILE__); return 3; } while(CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))//disconnect client { client->CloseConnection (serverAddress,true,0,LOW_PRIORITY); } if (isVerbose) printf("Testing if connection is rejected after adding to ban list\n"); server->AddToBanList("127.0.0.1",0); entryTime=GetTimeMS(); while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000) { if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true)) { client->Connect("127.0.0.1",serverAddress.port,thePassword,(int)strlen(thePassword)); } RakSleep(100); } if(!server->IsBanned("127.0.0.1")) { if (isVerbose) DebugTools::ShowError("IsBanned does not show localhost as banned\n",!noPauses && isVerbose,__LINE__,__FILE__); return 6; } if (CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)) { if (isVerbose) DebugTools::ShowError("Client was banned but connected anyways\n",!noPauses && isVerbose,__LINE__,__FILE__); return 4; } if (isVerbose) printf("Testing if connection is accepted after ban removal by RemoveFromBanList\n"); server->RemoveFromBanList("127.0.0.1"); if(server->IsBanned("127.0.0.1")) { if (isVerbose) DebugTools::ShowError("Localhost was not unbanned\n",!noPauses && isVerbose,__LINE__,__FILE__); return 7; } entryTime=GetTimeMS(); while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000) { if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true)) { client->Connect("127.0.0.1",serverAddress.port,thePassword,(int)strlen(thePassword)); } RakSleep(100); } if (!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)) { if (isVerbose) DebugTools::ShowError("Client failed to connect after banlist removal\n",!noPauses && isVerbose,__LINE__,__FILE__); return 8; } while(CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))//disconnect client { client->CloseConnection (serverAddress,true,0,LOW_PRIORITY); } if (isVerbose) printf("Testing if connection is rejected after adding to ban list\n"); server->AddToBanList("127.0.0.1",0); entryTime=GetTimeMS(); while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000) { if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true)) { client->Connect("127.0.0.1",serverAddress.port,thePassword,(int)strlen(thePassword)); } RakSleep(100); } if(!server->IsBanned("127.0.0.1")) { if (isVerbose) DebugTools::ShowError("IsBanned does not show localhost as banned\n",!noPauses && isVerbose,__LINE__,__FILE__); return 6; } if (CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)) { if (isVerbose) DebugTools::ShowError("Client was banned but connected anyways\n",!noPauses && isVerbose,__LINE__,__FILE__); return 4; } if (isVerbose) printf("Testing if connection is accepted after ban removal by ClearBanList\n"); server->ClearBanList(); if(server->IsBanned("127.0.0.1")) { if (isVerbose) DebugTools::ShowError("Localhost was not unbanned\n",!noPauses && isVerbose,__LINE__,__FILE__); return 7; } entryTime=GetTimeMS(); while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000) { if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true)) { client->Connect("127.0.0.1",serverAddress.port,thePassword,(int)strlen(thePassword)); } RakSleep(100); } if (!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)) { if (isVerbose) DebugTools::ShowError("Client failed to connect after banlist removal with clear function\n",!noPauses && isVerbose,__LINE__,__FILE__); return 9; } while(CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))//disconnect client { client->CloseConnection (serverAddress,true,0,LOW_PRIORITY); } /*//Disabled because of statistics changes if (isVerbose) printf("Testing InitializeSecurity on server\n"); //----------------------------- // RSACrypt is a using namespace RakNet; class that handles RSA encryption/decryption internally RSACrypt rsacrypt; uint32_t e; uint32_t modulus[RAKNET_RSA_FACTOR_LIMBS]; uint32_t p[RAKNET_RSA_FACTOR_LIMBS/2],q[RAKNET_RSA_FACTOR_LIMBS/2]; printf("Generating %i bit key. This will take a while...\n", RAKNET_RSA_FACTOR_LIMBS*32); rsacrypt.generatePrivateKey(RAKNET_RSA_FACTOR_LIMBS); e=rsacrypt.getPublicExponent(); rsacrypt.getPublicModulus(modulus); rsacrypt.getPrivateP(p); rsacrypt.getPrivateQ(q); RakPeerInterface::DestroyInstance(server); server=RakPeerInterface::GetInstance(); server->InitializeSecurity(0,0,(char*)p, (char*)q); server->Startup(1,30,&SocketDescriptor(60000,0),1); server->SetMaximumIncomingConnections(1); server->SetIncomingPassword(thePassword,strlen(thePassword)); if (isVerbose) printf("Testing if client connects encrypted\n"); entryTime=GetTimeMS(); while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000) { if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true)) { client->Connect("127.0.0.1",serverAddress.port,thePassword,strlen(thePassword)); } RakSleep(100); } char str2[]="AAAAAAAAAA"; str2[0]=(char)(ID_USER_PACKET_ENUM+1); client->Send(str2,(int) strlen(str2)+1, HIGH_PRIORITY, RELIABLE_ORDERED ,0, UNASSIGNED_SYSTEM_ADDRESS, true); client->Send(str2,(int) strlen(str2)+1, HIGH_PRIORITY, RELIABLE_ORDERED ,0, UNASSIGNED_SYSTEM_ADDRESS, true); Packet *packet; entryTime=GetTimeMS(); while(GetTimeMS()-entryTime<1000) { for (packet=server->Receive(); packet;server->DeallocatePacket(packet), packet=server->Receive()) { } } RakNetStatistics *rss; rss=client->GetStatistics(serverAddress); if (rss->encryptionBitsSent<=0)//If we did connect encrypted we should see encryptionBitsSent { if (isVerbose) DebugTools::ShowError("Client did not connect encrypted\n",!noPauses && isVerbose,__LINE__,__FILE__); return 10; } while(CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))//disconnect client { client->CloseConnection (serverAddress,true,0,LOW_PRIORITY); } //Destroy to clear statistics RakPeerInterface::DestroyInstance(client); client=RakPeerInterface::GetInstance(); client->Startup(1,30,&SocketDescriptor(),1); if (isVerbose) printf("Testing AddToSecurityExceptionList client should connect without encryption\n"); server->AddToSecurityExceptionList("127.0.0.1"); if (!server->IsInSecurityExceptionList("127.0.0.1")) { if (isVerbose) DebugTools::ShowError("IsInSecurityExceptionList does not register localhost addition\n",!noPauses && isVerbose,__LINE__,__FILE__); return 12; } entryTime=GetTimeMS(); while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000) { if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true)) { client->Connect("127.0.0.1",serverAddress.port,thePassword,strlen(thePassword)); } RakSleep(100); } str2[0]=(char)(ID_USER_PACKET_ENUM+1); client->Send(str2,(int) strlen(str2)+1, HIGH_PRIORITY, RELIABLE_ORDERED ,0, UNASSIGNED_SYSTEM_ADDRESS, true); client->Send(str2,(int) strlen(str2)+1, HIGH_PRIORITY, RELIABLE_ORDERED ,0, UNASSIGNED_SYSTEM_ADDRESS, true); // Packet *packet; entryTime=GetTimeMS(); while(GetTimeMS()-entryTime<1000) { for (packet=server->Receive(); packet;server->DeallocatePacket(packet), packet=server->Receive()) { } } rss=client->GetStatistics(serverAddress); if (rss->encryptionBitsSent>0)//If we did connect encrypted we should see encryptionBitsSent { if (isVerbose) DebugTools::ShowError("Client connected encrypted but shouldn't have\n",!noPauses && isVerbose,__LINE__,__FILE__); return 11; } if (isVerbose) printf("Testing RemoveFromSecurityExceptionList\n"); while(CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))//disconnect client { client->CloseConnection (serverAddress,true,0,LOW_PRIORITY); } server->RemoveFromSecurityExceptionList("127.0.0.1"); if (isVerbose) printf("Testing if client connects encrypted\n"); entryTime=GetTimeMS(); while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000) { if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true)) { client->Connect("127.0.0.1",serverAddress.port,thePassword,strlen(thePassword)); } RakSleep(100); } str2[0]=(char)(ID_USER_PACKET_ENUM+1); client->Send(str2,(int) strlen(str2)+1, HIGH_PRIORITY, RELIABLE_ORDERED ,0, UNASSIGNED_SYSTEM_ADDRESS, true); client->Send(str2,(int) strlen(str2)+1, HIGH_PRIORITY, RELIABLE_ORDERED ,0, UNASSIGNED_SYSTEM_ADDRESS, true); entryTime=GetTimeMS(); while(GetTimeMS()-entryTime<1000) { for (packet=server->Receive(); packet;server->DeallocatePacket(packet), packet=server->Receive()) { } } rss=client->GetStatistics(serverAddress); if (rss->encryptionBitsSent<=0)//If we did connect encrypted we should see encryptionBitsSent { if (isVerbose) DebugTools::ShowError("Client did not connect encrypted\n",!noPauses && isVerbose,__LINE__,__FILE__); return 10; } */ return 0; }
void RunAsClient(void) { rakClient = RakNetworkFactory::GetRakPeerInterface(); // Apple is defined in "Common.h", and it's our class derived from NetworkIDObject Apple *serverApple = 0; #if DOLOG clientLogger.SetPrefix("CLIENT> "); clientLogger.SetSuffix("\n"); rakClient->AttachPlugin(&clientLogger); #endif char serverip[256]; printf("Enter ip of the server (ENTER defaults to 127.0.0.1): "); gets(serverip); if (serverip[0]==0) strcpy(serverip, "127.0.0.1"); char serverport[256]; printf("Enter port of the server (ENTER defaults to 60000): "); gets(serverport); if (serverport[0]==0) strcpy(serverport, "60000"); rakClient->Startup(1, 30, &SocketDescriptor(), 1); rakClient->Connect(serverip, atoi(serverport), 0,0); // In a client/server architecture, only the server can create NetworkIDs clientNetworkIDManager.SetIsNetworkIDAuthority(false); rakClient->SetNetworkIDManager(&clientNetworkIDManager); // Clear keyboard buffer before entering loop while(kbhit()) getch(); do{ Packet *p = rakClient->Receive(); while(p){ unsigned char msgID = p->data[0]; // Check if the server has sent us the NetworkID of his Apple object if (msgID==ID_USER_SERVERAPPLE_CREATED) { RakNet::BitStream bs(p->data, p->length, false); // Ignore the message ID bs.IgnoreBits(8); NetworkID appleNetworkID; bs.Read(appleNetworkID); // Create the object on the client side serverApple = new GrannySmith; serverApple->SetNetworkIDManager(&clientNetworkIDManager); serverApple->SetNetworkID(appleNetworkID); // Now that we have the object created, we can use RPC on it. const char *hello="Hello World!!!!!"; rakClient->RPC(CLASS_MEMBER_ID(Apple,func1), hello, (unsigned int) (strlen(hello)+1)*8, HIGH_PRIORITY, RELIABLE_ORDERED,0, UNASSIGNED_SYSTEM_ADDRESS, true, 0, serverApple->GetNetworkID(),0); rakClient->RPC(CLASS_MEMBER_ID(Apple,func2), hello, (unsigned int) (strlen(hello)+1)*8, HIGH_PRIORITY, RELIABLE_ORDERED,0, UNASSIGNED_SYSTEM_ADDRESS, true, 0, serverApple->GetNetworkID(),0); rakClient->RPC(CLASS_MEMBER_ID(Apple,func3), hello, (unsigned int) (strlen(hello)+1)*8, HIGH_PRIORITY, RELIABLE_ORDERED,0, UNASSIGNED_SYSTEM_ADDRESS, true, 0, serverApple->GetNetworkID(),0); } rakClient->DeallocatePacket(p); p = rakClient->Receive(); } // Sleep for a while, so we don't eat all the CPU time with such a simple program. // In a real game, you probably won't need this, as you want your game to run at full speed. RakSleep(30); } while(!kbhit()); // Shutdown if (serverApple) delete serverApple; rakClient->Shutdown(300); RakNetworkFactory::DestroyRakPeerInterface(rakClient); }
int ReliableOrderedConvertedTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses) { RakPeerInterface *sender, *receiver; unsigned int packetNumberSender[32],packetNumberReceiver[32], receivedPacketNumberReceiver, receivedTimeReceiver; char str[256]; char ip[32]; TimeMS sendInterval, nextSend, currentTime, quitTime; unsigned short remotePort, localPort; unsigned char streamNumberSender,streamNumberReceiver; BitStream bitStream; Packet *packet; bool doSend=false; for (int i=0; i < 32; i++) { packetNumberSender[i]=0; packetNumberReceiver[i]=0; } /* if (argc==2) { fp = fopen(argv[1],"wt"); SetMalloc_Ex(LoggedMalloc); SetRealloc_Ex(LoggedRealloc); SetFree_Ex(LoggedFree); } else */ fp=0; destroyList.Clear(false,_FILE_AND_LINE_); sender =RakPeerInterface::GetInstance(); destroyList.Push( sender ,_FILE_AND_LINE_); //sender->ApplyNetworkSimulator(.02, 100, 50); /* if (str[0]==0) sendInterval=30; else sendInterval=atoi(str);*///possible future params sendInterval=30; /* printf("Enter remote IP: "); Gets(ip, sizeof(ip)); if (ip[0]==0)*/ strcpy(ip, "127.0.0.1"); /* printf("Enter remote port: "); Gets(str, sizeof(str)); if (str[0]==0)*/ strcpy(str, "60000"); remotePort=atoi(str); /* printf("Enter local port: "); Gets(str, sizeof(str)); if (str[0]==0)*/ strcpy(str, "0"); localPort=atoi(str); if (isVerbose) printf("Connecting...\n"); sender->Startup(1, &SocketDescriptor(localPort,0), 1); sender->Connect(ip, remotePort, 0, 0); receiver =RakPeerInterface::GetInstance(); destroyList.Push( receiver ,_FILE_AND_LINE_); /* printf("Enter local port: "); Gets(str, sizeof(str)); if (str[0]==0)*/ strcpy(str, "60000"); localPort=atoi(str); if (isVerbose) printf("Waiting for connections...\n"); receiver->Startup(32, &SocketDescriptor(localPort,0), 1); receiver->SetMaximumIncomingConnections(32); // if (sender) // sender->ApplyNetworkSimulator(128000, 50, 100); // if (receiver) // receiver->ApplyNetworkSimulator(128000, 50, 100); /*printf("How long to run this test for, in seconds?\n"); Gets(str, sizeof(str)); if (str[0]==0)*/ strcpy(str, "12"); currentTime = GetTimeMS(); quitTime = atoi(str) * 1000 + currentTime; nextSend=currentTime; while (currentTime < quitTime) //while (1) { packet = sender->Receive(); while (packet) { // PARSE TYPES switch(packet->data[0]) { case ID_CONNECTION_REQUEST_ACCEPTED: if (isVerbose) printf("ID_CONNECTION_REQUEST_ACCEPTED\n"); doSend=true; nextSend=currentTime; break; case ID_NO_FREE_INCOMING_CONNECTIONS: if (isVerbose) printf("ID_NO_FREE_INCOMING_CONNECTIONS\n"); break; case ID_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("ID_DISCONNECTION_NOTIFICATION\n"); break; case ID_CONNECTION_LOST: if (isVerbose) printf("ID_CONNECTION_LOST\n"); break; case ID_CONNECTION_ATTEMPT_FAILED: if (isVerbose) printf("Connection attempt failed\n"); break; } sender->DeallocatePacket(packet); packet = sender->Receive(); } while (doSend && currentTime > nextSend) { streamNumberSender=0; // streamNumber = randomMT() % 32; // Do the send bitStream.Reset(); bitStream.Write((unsigned char) (ID_USER_PACKET_ENUM+1)); bitStream.Write(packetNumberSender[streamNumberSender]++); bitStream.Write(streamNumberSender); bitStream.Write(currentTime); char *pad; int padLength = (randomMT() % 5000) + 1; pad = new char [padLength]; bitStream.Write(pad, padLength); delete [] pad; // Send on a random priority with a random stream // if (sender->Send(&bitStream, HIGH_PRIORITY, (PacketReliability) (RELIABLE + (randomMT() %2)) ,streamNumber, UNASSIGNED_SYSTEM_ADDRESS, true)==false) if (sender->Send(&bitStream, HIGH_PRIORITY, RELIABLE_ORDERED ,streamNumberSender, UNASSIGNED_SYSTEM_ADDRESS, true)==false) packetNumberSender[streamNumberSender]--; // Didn't finish connecting yet? RakNetStatistics *rssSender; rssSender=sender->GetStatistics(sender->GetSystemAddressFromIndex(0)); if (isVerbose) printf("Snd: %i.\n", packetNumberSender[streamNumberSender]); nextSend+=sendInterval; // Test halting // if (rand()%20==0) // nextSend+=1000; } packet = receiver->Receive(); while (packet) { switch(packet->data[0]) { case ID_NEW_INCOMING_CONNECTION: if (isVerbose) printf("ID_NEW_INCOMING_CONNECTION\n"); break; case ID_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("ID_DISCONNECTION_NOTIFICATION\n"); break; case ID_CONNECTION_LOST: if (isVerbose) printf("ID_CONNECTION_LOST\n"); break; case ID_USER_PACKET_ENUM+1: bitStream.Reset(); bitStream.Write((char*)packet->data, packet->length); bitStream.IgnoreBits(8); // Ignore ID_USER_PACKET_ENUM+1 bitStream.Read(receivedPacketNumberReceiver); bitStream.Read(streamNumberReceiver); bitStream.Read(receivedTimeReceiver); if (receivedPacketNumberReceiver!=packetNumberReceiver[streamNumberReceiver]) { //WARNING: If you modify the below code make sure the whole string remains in bounds, sprintf will NOT do it for you. //The error string is 512 in length //Note: Removed buffer checking because chance is insignificant, left code if wanted in future. Needs limits.h ISO C standard. /* int maxIntWorkingCopy= INT_MAX; int maxIntCharLen =0; while (maxIntWorkingCopy>0) {maxIntCharLen++; maxIntWorkingCopy/=10; } if (strlen(lastError)>maxIntCharLen* 3 +27)//512 should be a good len for now {*/ sprintf(lastError,"Expecting %i got %i (channel %i).",packetNumberReceiver[streamNumberReceiver], receivedPacketNumberReceiver, streamNumberReceiver); /* } else { sprintf(lastError,"Did not get what was expected. More details can be given if the error string buffer size is increased."); }*/ if (isVerbose) { RakNetStatistics *rssSender,*rssReceiver; char message[2048]; rssSender=sender->GetStatistics(sender->GetSystemAddressFromIndex(0)); rssReceiver=receiver->GetStatistics(receiver->GetSystemAddressFromIndex(0)); StatisticsToString(rssSender, message, 2); printf("Server stats %s\n", message); StatisticsToString(rssReceiver, message, 2); printf("Client stats%s", message); DebugTools::ShowError(lastError,!noPauses && isVerbose,__LINE__,__FILE__); } return 1; } else if (isVerbose) { printf("Got %i.Channel %i.Len %i.", packetNumberReceiver[streamNumberReceiver], streamNumberReceiver, packet->length); printf("Sent=%u Received=%u Diff=%i.\n", receivedTimeReceiver, currentTime, (int)currentTime - (int) receivedTimeReceiver); } packetNumberReceiver[streamNumberReceiver]++; break; } receiver->DeallocatePacket(packet); packet = receiver->Receive(); } RakSleep(0); currentTime=GetTimeMS(); } if (isVerbose) { RakNetStatistics *rssSender,*rssReceiver; char message[2048]; rssSender=sender->GetStatistics(sender->GetSystemAddressFromIndex(0)); rssReceiver=receiver->GetStatistics(receiver->GetSystemAddressFromIndex(0)); StatisticsToString(rssSender, message, 2); printf("Server stats %s\n", message); StatisticsToString(rssReceiver, message, 2); printf("Client stats%s", message); } if (fp) fclose(fp); return 0; }
/* What is being done here is having 8 peers all connect to eachother over the max defined connection. It runs the connect, wait 20 seconds then see the current connections. Success conditions: All extra connections Refused. Failure conditions: There are more connected than allowed. The connect function fails, the test is not even done. GetMaximumIncomingConnections returns wrong value. RakPeerInterface Functions used, tested indirectly by its use: Startup Connect SetMaximumIncomingConnections Receive DeallocatePacket GetSystemList RakPeerInterface Functions Explicitly Tested: SetMaximumIncomingConnections GetMaximumIncomingConnections */ int MaximumConnectTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses) { const int peerNum= 8; const int maxConnections=4;//Max allowed connections for test RakPeerInterface *peerList[peerNum];//A list of 8 peers Packet *packet; destroyList.Clear(false,_FILE_AND_LINE_); int connReturn; //Initializations of the arrays for (int i=0;i<peerNum;i++) { peerList[i]=RakPeerInterface::GetInstance(); destroyList.Push(peerList[i],_FILE_AND_LINE_); peerList[i]->Startup(maxConnections, &SocketDescriptor(60000+i,0), 1); peerList[i]->SetMaximumIncomingConnections(maxConnections); connReturn=peerList[i]->GetMaximumIncomingConnections(); if (connReturn!=maxConnections) { if (isVerbose) { printf("Getmaxconnections wrong for peer %i, %i should be the value but the value is %i.Fail\n",i,maxConnections,connReturn); DebugTools::ShowError("",!noPauses && isVerbose,__LINE__,__FILE__); } } } //Connect all the peers together for (int i=0;i<peerNum;i++) { for (int j=i+1;j<peerNum;j++)//Start at i+1 so don't connect two of the same together. { if (peerList[i]->Connect("127.0.0.1", 60000+j, 0,0)!=CONNECTION_ATTEMPT_STARTED) { if (isVerbose) DebugTools::ShowError("Problem while calling connect.\n",!noPauses && isVerbose,__LINE__,__FILE__); return 1;//This fails the test, don't bother going on. } } } TimeMS entryTime=GetTimeMS();//Loop entry time while(GetTimeMS()-entryTime<20000)//Run for 20 Secoonds { for (int i=0;i<peerNum;i++)//Receive for all peers { packet=peerList[i]->Receive(); if (isVerbose&&packet) printf("For peer %i\n",i); while(packet) { switch (packet->data[0]) { case ID_REMOTE_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("Another client has disconnected.\n"); break; case ID_REMOTE_CONNECTION_LOST: if (isVerbose) printf("Another client has lost the connection.\n"); break; case ID_REMOTE_NEW_INCOMING_CONNECTION: if (isVerbose) printf("Another client has connected.\n"); break; case ID_CONNECTION_REQUEST_ACCEPTED: if (isVerbose) printf("Our connection request has been accepted.\n"); break; case ID_CONNECTION_ATTEMPT_FAILED: if (isVerbose) printf("A connection has failed.\n");//Should happen in this test break; case ID_NEW_INCOMING_CONNECTION: if (isVerbose) printf("A connection is incoming.\n"); break; case ID_NO_FREE_INCOMING_CONNECTIONS: if (isVerbose) printf("The server is full.\n"); break; case ID_ALREADY_CONNECTED: if (isVerbose) printf("Already connected\n");//Shouldn't happen break; case ID_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("We have been disconnected.\n"); break; case ID_CONNECTION_LOST: if (isVerbose) printf("Connection lost.\n"); break; default: break; } peerList[i]->DeallocatePacket(packet); // Stay in the loop as long as there are more packets. packet = peerList[i]->Receive(); } } RakSleep(0);//If needed for testing } DataStructures::List< SystemAddress > systemList; DataStructures::List< RakNetGUID > guidList; for (int i=0;i<peerNum;i++) { peerList[i]->GetSystemList(systemList,guidList); int connNum=guidList.Size();//Get the number of connections for the current peer if (connNum>maxConnections)//Did we connect to more? { if (isVerbose) { printf("More connections were allowed to peer %i, %i total.Fail\n",i,connNum); DebugTools::ShowError("",!noPauses && isVerbose,__LINE__,__FILE__); } return 2; } } if (isVerbose) printf("Pass\n"); return 0; }
/* Description: Tests what happens if two instances of RakNet connect to each other at the same time. This has caused handshaking problems in the past. Success conditions: Everything connects and sends normally. Failure conditions: Expected values from ping/pong do not occur within expected time. */ int CrossConnectionConvertTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses) { static const unsigned short SERVER_PORT=1234; // char serverMode[32]; char serverIP[64]; strcpy(serverIP,"127.0.0.1"); char clientIP[64]; RakPeerInterface *server,*client; unsigned short clientPort; bool gotNotification; server=RakPeerInterface::GetInstance(); destroyList.Clear(false,_FILE_AND_LINE_); destroyList.Push(server,_FILE_AND_LINE_); client=RakPeerInterface::GetInstance(); destroyList.Push(client,_FILE_AND_LINE_); server->Startup(1,&SocketDescriptor(SERVER_PORT,0), 1); server->SetMaximumIncomingConnections(1); client->Startup(1,&SocketDescriptor(0,0), 1); client->Ping(serverIP,SERVER_PORT,false); // PacketLogger pl; // pl.LogHeader(); // rakPeer->AttachPlugin(&pl); TimeMS connectionAttemptTime=0,connectionResultDeterminationTime=0,nextTestStartTime=0; TimeMS entryTime=GetTimeMS();//Loop entry time bool printedYet=false; while(GetTimeMS()-entryTime<10000)//Run for 10 Secoonds { Packet *p; printedYet=false; for (p=server->Receive(); p; server->DeallocatePacket(p), p=server->Receive()) { if (isVerbose&&!printedYet) { printf("Server:\n"); printedYet=true; } if (p->data[0]==ID_NEW_INCOMING_CONNECTION) { if (isVerbose) printf("ID_NEW_INCOMING_CONNECTION\n"); gotNotification=true; } else if (p->data[0]==ID_CONNECTION_REQUEST_ACCEPTED) { if (isVerbose) printf("ID_CONNECTION_REQUEST_ACCEPTED\n"); gotNotification=true; } else if (p->data[0]==ID_UNCONNECTED_PING) { if (isVerbose) printf("ID_PING\n"); connectionAttemptTime=GetTimeMS()+1000; p->systemAddress.ToString(false,clientIP); clientPort=p->systemAddress.port; gotNotification=false; } else if (p->data[0]==ID_UNCONNECTED_PONG) { if (isVerbose) printf("ID_UNCONNECTED_PONG\n"); TimeMS sendPingTime; BitStream bs(p->data,p->length,false); bs.IgnoreBytes(1); bs.Read(sendPingTime); TimeMS rtt = GetTimeMS() - sendPingTime; if (rtt/2<=500) connectionAttemptTime=GetTimeMS()+1000-rtt/2; else connectionAttemptTime=GetTimeMS(); gotNotification=false; } } printedYet=false; for (p=client->Receive(); p; client->DeallocatePacket(p), p=client->Receive()) { if (isVerbose&&!printedYet) { printf("Client:\n"); printedYet=true; } if (p->data[0]==ID_NEW_INCOMING_CONNECTION) { if (isVerbose) printf("ID_NEW_INCOMING_CONNECTION\n"); gotNotification=true; } else if (p->data[0]==ID_CONNECTION_REQUEST_ACCEPTED) { if (isVerbose) printf("ID_CONNECTION_REQUEST_ACCEPTED\n"); gotNotification=true; } else if (p->data[0]==ID_UNCONNECTED_PING) { if (isVerbose) printf("ID_PING\n"); connectionAttemptTime=GetTimeMS()+1000; p->systemAddress.ToString(false,clientIP); clientPort=p->systemAddress.port; gotNotification=false; } else if (p->data[0]==ID_UNCONNECTED_PONG) { if (isVerbose) printf("ID_UNCONNECTED_PONG\n"); TimeMS sendPingTime; BitStream bs(p->data,p->length,false); bs.IgnoreBytes(1); bs.Read(sendPingTime); TimeMS rtt = GetTimeMS() - sendPingTime; if (rtt/2<=500) connectionAttemptTime=GetTimeMS()+1000-rtt/2; else connectionAttemptTime=GetTimeMS(); gotNotification=false; } } if (connectionAttemptTime!=0 && GetTimeMS()>=connectionAttemptTime) { if (isVerbose) printf("Attemping connection\n"); connectionAttemptTime=0; server->Connect(clientIP,clientPort,0,0); client->Connect(serverIP,SERVER_PORT,0,0); connectionResultDeterminationTime=GetTimeMS()+2000; } if (connectionResultDeterminationTime!=0 && GetTimeMS()>=connectionResultDeterminationTime) { connectionResultDeterminationTime=0; if (gotNotification==false) { DebugTools::ShowError("Did not recieve expected response. \n",!noPauses && isVerbose,__LINE__,__FILE__); return 1; } SystemAddress sa; sa.SetBinaryAddress(serverIP); sa.port=SERVER_PORT; client->CancelConnectionAttempt(sa); sa.SetBinaryAddress(clientIP); sa.port=clientPort; server->CancelConnectionAttempt(sa); server->CloseConnection(server->GetSystemAddressFromIndex(0),true,0); client->CloseConnection(client->GetSystemAddressFromIndex(0),true,0); //if (isServer==false) nextTestStartTime=GetTimeMS()+1000; } if (nextTestStartTime!=0 && GetTimeMS()>=nextTestStartTime) { client->Ping(serverIP,SERVER_PORT,false); nextTestStartTime=0; } RakSleep(0); } if (isVerbose) printf("Test succeeded.\n"); return 0; }
/* What is being done here is having 8 peers all connect to eachother, disconnect, connect again. Do this for about 10 seconds. Then allow them all to connect for one last time. This test also tests the cancelpendingconnections. Also tests nonblocking connects, the simpler one PeerConnectDisconnect tests without it Good ideas for changes: After the last check run a eightpeers like test an add the conditions of that test as well. Make sure that if we initiate the connection we get a proper message and if not we get a proper message. Add proper conditions. Randomize sending the disconnect notes Success conditions: All connected normally and pending requests get canceled normally. Failure conditions: Doesn't reconnect normally. During the very first connect loop any connect returns false. Connect function returns false and peer is not connected to anything. Pending request is not canceled. */ int PeerConnectDisconnectWithCancelPendingTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses) { const int peerNum= 8; const int maxConnections=peerNum*3;//Max allowed connections for test set to times 3 to eliminate problem variables RakPeerInterface *peerList[peerNum];//A list of 8 peers SystemAddress currentSystem; Packet *packet; destroyList.Clear(false,_FILE_AND_LINE_); //Initializations of the arrays for (int i=0;i<peerNum;i++) { peerList[i]=RakPeerInterface::GetInstance(); destroyList.Push(peerList[i],_FILE_AND_LINE_); peerList[i]->Startup(maxConnections, &SocketDescriptor(60000+i,0), 1); peerList[i]->SetMaximumIncomingConnections(maxConnections); } //Connect all the peers together for (int i=0;i<peerNum;i++) { for (int j=i+1;j<peerNum;j++)//Start at i+1 so don't connect two of the same together. { if (peerList[i]->Connect("127.0.0.1", 60000+j, 0,0)!=CONNECTION_ATTEMPT_STARTED) { if (isVerbose) DebugTools::ShowError("Problem while calling connect.",!noPauses && isVerbose,__LINE__,__FILE__); return 1;//This fails the test, don't bother going on. } } } TimeMS entryTime=GetTimeMS();//Loop entry time DataStructures::List< SystemAddress > systemList; DataStructures::List< RakNetGUID > guidList; printf("Entering disconnect loop \n"); bool printedYet; while(GetTimeMS()-entryTime<10000)//Run for 10 Secoonds { //Disconnect all peers IF connected to any for (int i=0;i<peerNum;i++) { peerList[i]->GetSystemList(systemList,guidList);//Get connectionlist int len=systemList.Size(); for (int j=0;j<len;j++)//Disconnect them all { peerList[i]->CloseConnection (systemList[j],true,0,LOW_PRIORITY); } } RakSleep(100); //Clear pending if not finished for (int i=0;i<peerNum;i++) { for (int j=i+1;j<peerNum;j++)//Start at i+1 so don't connect two of the same together. { currentSystem.SetBinaryAddress("127.0.0.1"); currentSystem.port=60000+j; peerList[i]->CancelConnectionAttempt(currentSystem); //Make sure a connection is not pending before trying to connect. } } RakSleep(100); //Connect for (int i=0;i<peerNum;i++) { for (int j=i+1;j<peerNum;j++)//Start at i+1 so don't connect two of the same together. { if (peerList[i]->Connect("127.0.0.1", 60000+j, 0,0)!=CONNECTION_ATTEMPT_STARTED) { currentSystem.SetBinaryAddress("127.0.0.1"); currentSystem.port=60000+j; peerList[i]->GetSystemList(systemList,guidList);//Get connectionlist int len=systemList.Size(); if(CommonFunctions::ConnectionStateMatchesOptions (peerList[i],currentSystem,false,true,true))//Did we drop the pending connection? { if (isVerbose) DebugTools::ShowError("Did not cancel the pending request \n",!noPauses && isVerbose,__LINE__,__FILE__); return 3; } if (!CommonFunctions::ConnectionStateMatchesOptions (peerList[i],currentSystem,true,true,true,true)) { if (isVerbose) DebugTools::ShowError("Problem while calling connect. \n",!noPauses && isVerbose,__LINE__,__FILE__); return 1;//This fails the test, don't bother going on. } } } } for (int i=0;i<peerNum;i++)//Receive for all peers { printedYet=false; packet=peerList[i]->Receive(); while(packet) { if (isVerbose&&!printedYet) { printf("For peer %i\n",i); printedYet=true; } switch (packet->data[0]) { case ID_REMOTE_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("Another client has disconnected.\n"); break; case ID_REMOTE_CONNECTION_LOST: if (isVerbose) printf("Another client has lost the connection.\n"); break; case ID_REMOTE_NEW_INCOMING_CONNECTION: if (isVerbose) printf("Another client has connected.\n"); break; case ID_CONNECTION_REQUEST_ACCEPTED: if (isVerbose) printf("Our connection request has been accepted.\n"); break; case ID_CONNECTION_ATTEMPT_FAILED: if (isVerbose) printf("A connection has failed.\n"); break; case ID_NEW_INCOMING_CONNECTION: if (isVerbose) printf("A connection is incoming.\n"); break; case ID_NO_FREE_INCOMING_CONNECTIONS: if (isVerbose) printf("The server is full.\n"); break; case ID_ALREADY_CONNECTED: if (isVerbose) printf("Already connected\n"); break; case ID_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("We have been disconnected.\n"); break; case ID_CONNECTION_LOST: if (isVerbose) printf("Connection lost.\n"); break; default: break; } peerList[i]->DeallocatePacket(packet); // Stay in the loop as long as there are more packets. packet = peerList[i]->Receive(); } } RakSleep(0);//If needed for testing } while(GetTimeMS()-entryTime<2000)//Run for 2 Secoonds to process incoming disconnects { for (int i=0;i<peerNum;i++)//Receive for all peers { printedYet=false; packet=peerList[i]->Receive(); while(packet) { if (isVerbose&&!printedYet) { printf("For peer %i\n",i); printedYet=true; } switch (packet->data[0]) { case ID_REMOTE_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("Another client has disconnected.\n"); break; case ID_REMOTE_CONNECTION_LOST: if (isVerbose) printf("Another client has lost the connection.\n"); break; case ID_REMOTE_NEW_INCOMING_CONNECTION: if (isVerbose) printf("Another client has connected.\n"); break; case ID_CONNECTION_REQUEST_ACCEPTED: if (isVerbose) printf("Our connection request has been accepted.\n"); break; case ID_CONNECTION_ATTEMPT_FAILED: if (isVerbose) printf("A connection has failed.\n"); break; case ID_NEW_INCOMING_CONNECTION: if (isVerbose) printf("A connection is incoming.\n"); break; case ID_NO_FREE_INCOMING_CONNECTIONS: if (isVerbose) printf("The server is full.\n"); break; case ID_ALREADY_CONNECTED: if (isVerbose) printf("Already connected\n"); break; case ID_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("We have been disconnected.\n"); break; case ID_CONNECTION_LOST: if (isVerbose) printf("Connection lost.\n"); break; default: break; } peerList[i]->DeallocatePacket(packet); // Stay in the loop as long as there are more packets. packet = peerList[i]->Receive(); } } RakSleep(0);//If needed for testing } //Connect for (int i=0;i<peerNum;i++) { for (int j=i+1;j<peerNum;j++)//Start at i+1 so don't connect two of the same together. { currentSystem.SetBinaryAddress("127.0.0.1"); currentSystem.port=60000+j; peerList[i]->CancelConnectionAttempt(currentSystem); //Make sure a connection is not pending before trying to connect. if (peerList[i]->Connect("127.0.0.1", 60000+j, 0,0)!=CONNECTION_ATTEMPT_STARTED) { peerList[i]->GetSystemList(systemList,guidList);//Get connectionlist int len=systemList.Size(); if (len==0)//No connections, should not fail. { if (isVerbose) DebugTools::ShowError("Problem while calling connect \n",!noPauses && isVerbose,__LINE__,__FILE__); return 1;//This fails the test, don't bother going on. } } } } entryTime=GetTimeMS(); while(GetTimeMS()-entryTime<5000)//Run for 5 Secoonds { for (int i=0;i<peerNum;i++)//Receive for all peers { printedYet=false; packet=peerList[i]->Receive(); while(packet) { if (isVerbose&&!printedYet) { printf("For peer %i\n",i); printedYet=true; } switch (packet->data[0]) { case ID_REMOTE_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("Another client has disconnected.\n"); break; case ID_REMOTE_CONNECTION_LOST: if (isVerbose) printf("Another client has lost the connection.\n"); break; case ID_REMOTE_NEW_INCOMING_CONNECTION: if (isVerbose) printf("Another client has connected.\n"); break; case ID_CONNECTION_REQUEST_ACCEPTED: if (isVerbose) printf("Our connection request has been accepted.\n"); break; case ID_CONNECTION_ATTEMPT_FAILED: if (isVerbose) printf("A connection has failed.\n"); break; case ID_NEW_INCOMING_CONNECTION: if (isVerbose) printf("A connection is incoming.\n"); break; case ID_NO_FREE_INCOMING_CONNECTIONS: if (isVerbose) printf("The server is full.\n"); break; case ID_ALREADY_CONNECTED: if (isVerbose) printf("Already connected\n"); break; case ID_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("We have been disconnected.\n"); break; case ID_CONNECTION_LOST: if (isVerbose) printf("Connection lost.\n"); break; default: break; } peerList[i]->DeallocatePacket(packet); // Stay in the loop as long as there are more packets. packet = peerList[i]->Receive(); } } RakSleep(0);//If needed for testing } for (int i=0;i<peerNum;i++) { peerList[i]->GetSystemList(systemList,guidList); int connNum=guidList.Size();//Get the number of connections for the current peer if (connNum!=peerNum-1)//Did we connect to all? { if (isVerbose) DebugTools::ShowError("Not all peers reconnected normally.\n ",!noPauses && isVerbose,__LINE__,__FILE__); return 2; } } if (isVerbose) printf("Pass\n"); return 0; }
/* What is being done here is having 256 clients connect to one server, disconnect, connect again. Do this for about 10 seconds. Then allow them all to connect for one last time. This one has a nonblocking recieve so doesn't wait for connects or anything. Just rapid connecting disconnecting. Good ideas for changes: After the last check run a eightpeers like test an add the conditions of that test as well. Make sure that if we initiate the connection we get a proper message and if not we get a proper message. Add proper conditions. Randomize sending the disconnect notes Success conditions: All connected normally. Failure conditions: Doesn't reconnect normally. During the very first connect loop any connect returns false. Connect function returns false and peer is not connected to anything. */ int ManyClientsOneServerNonBlockingTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses) { const int clientNum= 256; RakPeerInterface *clientList[clientNum];//A list of clients RakPeerInterface *server;//The server SystemAddress currentSystem; //SystemAddress currentSystem; Packet *packet; destroyList.Clear(false,_FILE_AND_LINE_); //Initializations of the arrays for (int i=0;i<clientNum;i++) { clientList[i]=RakPeerInterface::GetInstance(); destroyList.Push(clientList[i],_FILE_AND_LINE_); clientList[i]->Startup(1,&SocketDescriptor(), 1); } server=RakPeerInterface::GetInstance(); destroyList.Push(server,_FILE_AND_LINE_); server->Startup(clientNum, &SocketDescriptor(60000,0), 1); server->SetMaximumIncomingConnections(clientNum); //Connect all the clients to the server for (int i=0;i<clientNum;i++) { if (clientList[i]->Connect("127.0.0.1", 60000, 0,0)!=CONNECTION_ATTEMPT_STARTED) { if (isVerbose) DebugTools::ShowError("Problem while calling connect.\n",!noPauses && isVerbose,__LINE__,__FILE__); return 1;//This fails the test, don't bother going on. } } TimeMS entryTime=GetTimeMS();//Loop entry time DataStructures::List< SystemAddress > systemList; DataStructures::List< RakNetGUID > guidList; if (isVerbose) printf("Entering disconnect loop \n"); while(GetTimeMS()-entryTime<10000)//Run for 10 Secoonds { //Disconnect all clients IF connected to any from client side for (int i=0;i<clientNum;i++) { clientList[i]->GetSystemList(systemList,guidList);//Get connectionlist int len=systemList.Size(); for (int j=0;j<len;j++)//Disconnect them all { clientList[i]->CloseConnection (systemList[j],true,0,LOW_PRIORITY); } } //RakSleep(100); //Connect for (int i=0;i<clientNum;i++) { currentSystem.SetBinaryAddress("127.0.0.1"); currentSystem.port=60000; if(!CommonFunctions::ConnectionStateMatchesOptions (clientList[i],currentSystem,true,true,true,true) )//Are we connected or is there a pending operation ? { if (clientList[i]->Connect("127.0.0.1", 60000, 0,0)!=CONNECTION_ATTEMPT_STARTED) { if (isVerbose) DebugTools::ShowError("Problem while calling connect. \n",!noPauses && isVerbose,__LINE__,__FILE__); return 1;//This fails the test, don't bother going on. } } } //Server receive packet=server->Receive(); if (isVerbose&&packet) printf("For server\n"); while(packet) { switch (packet->data[0]) { case ID_REMOTE_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("Another client has disconnected.\n"); break; case ID_REMOTE_CONNECTION_LOST: if (isVerbose) printf("Another client has lost the connection.\n"); break; case ID_REMOTE_NEW_INCOMING_CONNECTION: if (isVerbose) printf("Another client has connected.\n"); break; case ID_CONNECTION_REQUEST_ACCEPTED: if (isVerbose) printf("Our connection request has been accepted.\n"); break; case ID_CONNECTION_ATTEMPT_FAILED: if (isVerbose) printf("A connection has failed.\n"); break; case ID_NEW_INCOMING_CONNECTION: if (isVerbose) printf("A connection is incoming.\n"); break; case ID_NO_FREE_INCOMING_CONNECTIONS: if (isVerbose) printf("The server is full.\n"); break; case ID_ALREADY_CONNECTED: if (isVerbose) printf("Already connected\n"); break; case ID_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("We have been disconnected.\n"); break; case ID_CONNECTION_LOST: if (isVerbose) printf("Connection lost.\n"); break; default: break; } server->DeallocatePacket(packet); // Stay in the loop as long as there are more packets. packet = server->Receive(); } for (int i=0;i<clientNum;i++)//Receive for all peers { packet=clientList[i]->Receive(); if (isVerbose&&packet) printf("For peer %i\n",i); while(packet) { switch (packet->data[0]) { case ID_REMOTE_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("Another client has disconnected.\n"); break; case ID_REMOTE_CONNECTION_LOST: if (isVerbose) printf("Another client has lost the connection.\n"); break; case ID_REMOTE_NEW_INCOMING_CONNECTION: if (isVerbose) printf("Another client has connected.\n"); break; case ID_CONNECTION_REQUEST_ACCEPTED: if (isVerbose) printf("Our connection request has been accepted.\n"); break; case ID_CONNECTION_ATTEMPT_FAILED: if (isVerbose) printf("A connection has failed.\n"); break; case ID_NEW_INCOMING_CONNECTION: if (isVerbose) printf("A connection is incoming.\n"); break; case ID_NO_FREE_INCOMING_CONNECTIONS: if (isVerbose) printf("The server is full.\n"); break; case ID_ALREADY_CONNECTED: if (isVerbose) printf("Already connected\n"); break; case ID_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("We have been disconnected.\n"); break; case ID_CONNECTION_LOST: if (isVerbose) printf("Connection lost.\n"); break; default: break; } clientList[i]->DeallocatePacket(packet); // Stay in the loop as long as there are more packets. packet = clientList[i]->Receive(); } } RakSleep(0);//If needed for testing } entryTime=GetTimeMS(); while(GetTimeMS()-entryTime<2000)//Run for 2 Secoonds to process incoming disconnects { //Server receive packet=server->Receive(); if (isVerbose&&packet) printf("For server\n"); while(packet) { switch (packet->data[0]) { case ID_REMOTE_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("Another client has disconnected.\n"); break; case ID_REMOTE_CONNECTION_LOST: if (isVerbose) printf("Another client has lost the connection.\n"); break; case ID_REMOTE_NEW_INCOMING_CONNECTION: if (isVerbose) printf("Another client has connected.\n"); break; case ID_CONNECTION_REQUEST_ACCEPTED: if (isVerbose) printf("Our connection request has been accepted.\n"); break; case ID_CONNECTION_ATTEMPT_FAILED: if (isVerbose) printf("A connection has failed.\n"); break; case ID_NEW_INCOMING_CONNECTION: if (isVerbose) printf("A connection is incoming.\n"); break; case ID_NO_FREE_INCOMING_CONNECTIONS: if (isVerbose) printf("The server is full.\n"); break; case ID_ALREADY_CONNECTED: if (isVerbose) printf("Already connected\n"); break; case ID_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("We have been disconnected.\n"); break; case ID_CONNECTION_LOST: if (isVerbose) printf("Connection lost.\n"); break; default: break; } server->DeallocatePacket(packet); // Stay in the loop as long as there are more packets. packet = server->Receive(); } for (int i=0;i<clientNum;i++)//Receive for all peers { packet=clientList[i]->Receive(); if (isVerbose&&packet) printf("For peer %i\n",i); while(packet) { switch (packet->data[0]) { case ID_REMOTE_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("Another client has disconnected.\n"); break; case ID_REMOTE_CONNECTION_LOST: if (isVerbose) printf("Another client has lost the connection.\n"); break; case ID_REMOTE_NEW_INCOMING_CONNECTION: if (isVerbose) printf("Another client has connected.\n"); break; case ID_CONNECTION_REQUEST_ACCEPTED: if (isVerbose) printf("Our connection request has been accepted.\n"); break; case ID_CONNECTION_ATTEMPT_FAILED: if (isVerbose) printf("A connection has failed.\n"); break; case ID_NEW_INCOMING_CONNECTION: if (isVerbose) printf("A connection is incoming.\n"); break; case ID_NO_FREE_INCOMING_CONNECTIONS: if (isVerbose) printf("The server is full.\n"); break; case ID_ALREADY_CONNECTED: if (isVerbose) printf("Already connected\n"); break; case ID_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("We have been disconnected.\n"); break; case ID_CONNECTION_LOST: if (isVerbose) printf("Connection lost.\n"); break; default: break; } clientList[i]->DeallocatePacket(packet); // Stay in the loop as long as there are more packets. packet = clientList[i]->Receive(); } } RakSleep(0);//If needed for testing } //Connect for (int i=0;i<clientNum;i++) { currentSystem.SetBinaryAddress("127.0.0.1"); currentSystem.port=60000; if(!CommonFunctions::ConnectionStateMatchesOptions (clientList[i],currentSystem,true,true,true,true) )//Are we connected or is there a pending operation ? { if (clientList[i]->Connect("127.0.0.1", 60000, 0,0)!=CONNECTION_ATTEMPT_STARTED) { clientList[i]->GetSystemList(systemList,guidList);//Get connectionlist int len=systemList.Size(); if (isVerbose) DebugTools::ShowError("Problem while calling connect. \n",!noPauses && isVerbose,__LINE__,__FILE__); return 1;//This fails the test, don't bother going on. } } } entryTime=GetTimeMS(); while(GetTimeMS()-entryTime<5000)//Run for 5 Secoonds { //Server receive packet=server->Receive(); if (isVerbose&&packet) printf("For server\n"); while(packet) { switch (packet->data[0]) { case ID_REMOTE_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("Another client has disconnected.\n"); break; case ID_REMOTE_CONNECTION_LOST: if (isVerbose) printf("Another client has lost the connection.\n"); break; case ID_REMOTE_NEW_INCOMING_CONNECTION: if (isVerbose) printf("Another client has connected.\n"); break; case ID_CONNECTION_REQUEST_ACCEPTED: if (isVerbose) printf("Our connection request has been accepted.\n"); break; case ID_CONNECTION_ATTEMPT_FAILED: if (isVerbose) printf("A connection has failed.\n"); break; case ID_NEW_INCOMING_CONNECTION: if (isVerbose) printf("A connection is incoming.\n"); break; case ID_NO_FREE_INCOMING_CONNECTIONS: if (isVerbose) printf("The server is full.\n"); break; case ID_ALREADY_CONNECTED: if (isVerbose) printf("Already connected\n"); break; case ID_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("We have been disconnected.\n"); break; case ID_CONNECTION_LOST: if (isVerbose) printf("Connection lost.\n"); break; default: break; } server->DeallocatePacket(packet); // Stay in the loop as long as there are more packets. packet = server->Receive(); } for (int i=0;i<clientNum;i++)//Receive for all clients { packet=clientList[i]->Receive(); if (isVerbose&&packet) printf("For peer %i\n",i); while(packet) { switch (packet->data[0]) { case ID_REMOTE_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("Another client has disconnected.\n"); break; case ID_REMOTE_CONNECTION_LOST: if (isVerbose) printf("Another client has lost the connection.\n"); break; case ID_REMOTE_NEW_INCOMING_CONNECTION: if (isVerbose) printf("Another client has connected.\n"); break; case ID_CONNECTION_REQUEST_ACCEPTED: if (isVerbose) printf("Our connection request has been accepted.\n"); break; case ID_CONNECTION_ATTEMPT_FAILED: if (isVerbose) printf("A connection has failed.\n"); break; case ID_NEW_INCOMING_CONNECTION: if (isVerbose) printf("A connection is incoming.\n"); break; case ID_NO_FREE_INCOMING_CONNECTIONS: if (isVerbose) printf("The server is full.\n"); break; case ID_ALREADY_CONNECTED: if (isVerbose) printf("Already connected\n"); break; case ID_DISCONNECTION_NOTIFICATION: if (isVerbose) printf("We have been disconnected.\n"); break; case ID_CONNECTION_LOST: if (isVerbose) printf("Connection lost.\n"); break; default: break; } clientList[i]->DeallocatePacket(packet); // Stay in the loop as long as there are more packets. packet = clientList[i]->Receive(); } } RakSleep(0);//If needed for testing } for (int i=0;i<clientNum;i++) { clientList[i]->GetSystemList(systemList,guidList); int connNum=guidList.Size();//Get the number of connections for the current peer if (connNum!=1)//Did we connect to all? { if (isVerbose) printf("Not all clients reconnected normally.\nFailed on clients number %i\n",i); if (isVerbose) DebugTools::ShowError("",!noPauses && isVerbose,__LINE__,__FILE__); return 2; } } if (isVerbose) printf("Pass\n"); return 0; }
int PingTestsTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses) { RakPeerInterface *sender,*sender2, *receiver; destroyList.Clear(false,_FILE_AND_LINE_); TestHelpers::StandardClientPrep(sender,destroyList); TestHelpers::StandardClientPrep(sender2,destroyList); receiver=RakPeerInterface::GetInstance(); destroyList.Push(receiver,_FILE_AND_LINE_); receiver->Startup(2, &SocketDescriptor(60000,0), 1); receiver->SetMaximumIncomingConnections(2); Packet * packet; SystemAddress currentSystem; currentSystem.SetBinaryAddress("127.0.0.1"); currentSystem.port=60000; printf("Connecting sender2\n"); if (!TestHelpers::WaitAndConnectTwoPeersLocally(sender2,receiver,5000)) { if (isVerbose) DebugTools::ShowError("Could not connect after 5 seconds\n",!noPauses && isVerbose,__LINE__,__FILE__); return 2; } printf("Getting ping data for lastping and lowestping\n"); sender2->SetOccasionalPing(false);//Test the lowest ping and such without occassionalping,occasional ping comes later RakTimer timer(1500); int lastPing=0; int lowestPing=0; TimeMS nextPing=0; while(!timer.IsExpired()) { for (packet=receiver->Receive();packet;receiver->DeallocatePacket(packet),packet=receiver->Receive()) { if (isVerbose) printf("Receive packet id %i\n",packet->data[0]); } for (packet=sender2->Receive();packet;sender2->DeallocatePacket(packet),packet=sender2->Receive()) { if (isVerbose) printf("Send packet id %i\n",packet->data[0]); } if (GetTimeMS()>nextPing) { sender2->Ping(currentSystem); nextPing=GetTimeMS()+30; } RakSleep(3); } int averagePing=sender2->GetAveragePing(currentSystem); if (isVerbose) printf("Average Ping time %i\n",averagePing); lastPing=sender2->GetLastPing(currentSystem); lowestPing=sender2->GetLowestPing(currentSystem); if (isVerbose) printf("Last Ping time %i\n",lastPing); if (isVerbose) printf("Lowest Ping time %i\n",lowestPing); int returnVal=TestAverageValue(averagePing,__LINE__, noPauses, isVerbose); if (returnVal!=0) { return returnVal; } if (lastPing>100)//100 MS for localhost? { if (isVerbose) DebugTools::ShowError("Problem with the last ping time,greater then 100MS for localhost\n",!noPauses && isVerbose,__LINE__,__FILE__); return 3; } if (lowestPing>10)//The lowest ping for localhost should drop below 10MS at least once { if (isVerbose) DebugTools::ShowError("The lowest ping for localhost should drop below 10MS at least once\n",!noPauses && isVerbose,__LINE__,__FILE__); return 4; } if (lastPing<lowestPing) { if (isVerbose) DebugTools::ShowError("There is a problem if the lastping is lower than the lowestping stat\n",!noPauses && isVerbose,__LINE__,__FILE__); return 5; } CommonFunctions::DisconnectAndWait(sender2,"127.0.0.1",60000);//Eliminate variables. printf("Connecting sender\n"); if (!TestHelpers::WaitAndConnectTwoPeersLocally(sender,receiver,5000)) { if (isVerbose) DebugTools::ShowError("Could not connect after 5 seconds\n",!noPauses && isVerbose,__LINE__,__FILE__); return 2; } lastPing=0; lowestPing=0; sender->SetOccasionalPing(true); printf("Testing SetOccasionalPing\n"); timer.Start(); while(!timer.IsExpired()) { for (packet=receiver->Receive();packet;receiver->DeallocatePacket(packet),packet=receiver->Receive()) { if (isVerbose) printf("Receive packet id %i\n",packet->data[0]); } for (packet=sender->Receive();packet;sender->DeallocatePacket(packet),packet=sender->Receive()) { if (isVerbose) printf("Send packet id %i\n",packet->data[0]); } RakSleep(3); } averagePing=sender->GetAveragePing(currentSystem); if (isVerbose) printf("Average Ping time %i\n",averagePing); returnVal=TestAverageValue(averagePing,__LINE__, noPauses, isVerbose); if (returnVal!=0) { return returnVal; } return 0; }
/* What is being done here is having 256 clients connect to a server, disconnect, connect again. Do this for about 10 seconds. Then allow them all to connect for one last time. This version waits for connect and such in a loop, blocking execution so it is a blocking test. Good ideas for changes: After the last check run a eightpeers like test an add the conditions of that test as well. Make sure that if we initiate the connection we get a proper message and if not we get a proper message. Add proper conditions. Randomize sending the disconnect notes Success conditions: All connected normally. Failure conditions: Doesn't reconnect normally. During the very first connect loop any connect returns false. Connect function returns false and peer is not connected to anything and does not have anything pending. */ int ManyClientsOneServerBlockingTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses) { const int clientNum= 256; RakPeerInterface *clientList[clientNum];//A list of clients RakPeerInterface *server; SystemAddress currentSystem; destroyList.Clear(false,_FILE_AND_LINE_); //Initializations of the arrays for (int i=0;i<clientNum;i++) { clientList[i]=RakPeerInterface::GetInstance(); destroyList.Push(clientList[i],_FILE_AND_LINE_); clientList[i]->Startup(1,&SocketDescriptor(), 1); } server=RakPeerInterface::GetInstance(); destroyList.Push(server,_FILE_AND_LINE_); server->Startup(clientNum, &SocketDescriptor(60000,0), 1); server->SetMaximumIncomingConnections(clientNum); //Connect all the clients to the server for (int i=0;i<clientNum;i++) { if (clientList[i]->Connect("127.0.0.1", 60000, 0,0)!=CONNECTION_ATTEMPT_STARTED) { if (isVerbose) DebugTools::ShowError("Problem while calling connect.\n",!noPauses && isVerbose,__LINE__,__FILE__); return 1;//This fails the test, don't bother going on. } } TimeMS entryTime=GetTimeMS();//Loop entry time DataStructures::List< SystemAddress > systemList; DataStructures::List< RakNetGUID > guidList; printf("Entering disconnect loop \n"); while(GetTimeMS()-entryTime<10000)//Run for 10 Secoonds { //Disconnect all clients IF connected to any from client side for (int i=0;i<clientNum;i++) { clientList[i]->GetSystemList(systemList,guidList);//Get connectionlist int len=systemList.Size(); for (int j=0;j<len;j++)//Disconnect them all { clientList[i]->CloseConnection (systemList[j],true,0,LOW_PRIORITY); } } RakSleep(100); //Connect for (int i=0;i<clientNum;i++) { currentSystem.SetBinaryAddress("127.0.0.1"); currentSystem.port=60000; if(!CommonFunctions::ConnectionStateMatchesOptions (clientList[i],currentSystem,true,true,true,true) )//Are we connected or is there a pending operation ? { if (clientList[i]->Connect("127.0.0.1", 60000, 0,0)!=CONNECTION_ATTEMPT_STARTED) { if (isVerbose) DebugTools::ShowError("Problem while calling connect. \n",!noPauses && isVerbose,__LINE__,__FILE__); return 1;//This fails the test, don't bother going on. } } } WaitAndPrintResults(clientList,clientNum,isVerbose,server); } WaitAndPrintResults(clientList,clientNum,isVerbose,server); printf("Connecting clients\n"); //Connect for (int i=0;i<clientNum;i++) { currentSystem.SetBinaryAddress("127.0.0.1"); currentSystem.port=60000; if(!CommonFunctions::ConnectionStateMatchesOptions (clientList[i],currentSystem,true,true,true,true) )//Are we connected or is there a pending operation ? { printf("Calling Connect() for client %i.\n",i); if (clientList[i]->Connect("127.0.0.1", 60000, 0,0)!=CONNECTION_ATTEMPT_STARTED) { clientList[i]->GetSystemList(systemList,guidList);//Get connectionlist int len=systemList.Size(); if (isVerbose) DebugTools::ShowError("Problem while calling connect. \n",!noPauses && isVerbose,__LINE__,__FILE__); return 1;//This fails the test, don't bother going on. } } else { if (CommonFunctions::ConnectionStateMatchesOptions (clientList[i],currentSystem,false,false,false,true)==false) printf("Not calling Connect() for client %i because it is disconnecting.\n",i); else if (CommonFunctions::ConnectionStateMatchesOptions (clientList[i],currentSystem,false,true,true)==false) printf("Not calling Connect() for client %i because it is connecting.\n",i); else if (CommonFunctions::ConnectionStateMatchesOptions (clientList[i],currentSystem,true)==false) printf("Not calling Connect() for client %i because it is connected).\n",i); } } WaitAndPrintResults(clientList,clientNum,isVerbose,server); for (int i=0;i<clientNum;i++) { clientList[i]->GetSystemList(systemList,guidList); int connNum=guidList.Size();//Get the number of connections for the current peer if (connNum!=1)//Did we connect all? { if (isVerbose) { printf("Not all clients reconnected normally.\nFailed on client number %i\n",i); DebugTools::ShowError("",!noPauses && isVerbose,__LINE__,__FILE__); } return 2; } } if (isVerbose) printf("Pass\n"); return 0; }