| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- /*
- * Copyright (c) 2014, Oculus VR, Inc.
- * All rights reserved.
- *
- * This source code is licensed under the BSD-style license found in the
- * LICENSE file in the root directory of this source tree. An additional grant
- * of patent rights can be found in the PATENTS file in the same directory.
- *
- */
- #include "RakPeerInterface.h"
- #include "GetTime.h"
- #include "MessageIdentifiers.h"
- #include "RakNetStatistics.h"
- #include <cstdio>
- #include <memory.h>
- #include <cstring>
- #include <stdlib.h>
- #include "Gets.h"
- using namespace RakNet;
- #ifdef WIN32
- #include "Kbhit.h"
- #else
- #include "Kbhit.h"
- #endif
- int main(void)
- {
- RakPeerInterface *rakServer;
- RakPeerInterface *rakClient;
- char ch;
- char str[255], remoteIP[255];
- char randomData[8192];
- int localPort, remotePort;
- int packetSize;
- int sendinterval;
- RakNet::TimeMS time;
- RakNet::Packet *p;
- RakNet::TimeMS lastPacketReceipt, lastNotification, lastSend;
- #ifndef _WIN32
- char buff[256];
- #endif
-
- memset(randomData, 255, sizeof(randomData));
-
- printf("This project is used to test two systems sending to each other with\n");
- printf("variable message sends rates and payload sizes\n");
- printf("Difficulty: Beginner\n\n");
- printf("Start relay (s)erver or start (c)lient?\n");
- #ifndef _WIN32
- Gets(buff,sizeof(buff));
- ch=buff[0];
- #else
- ch=getch();
- #endif
- if (ch=='s' || ch=='S')
- {
- printf("Acting as server.\n");
- rakServer=RakNet::RakPeerInterface::GetInstance();
- rakClient=0;
- }
- else
- {
- printf("Acting as client.\n");
- rakClient=RakNet::RakPeerInterface::GetInstance();
- rakServer=0;
- }
- printf("Enter local port: ");
- Gets(str, sizeof(str));
- if (str[0]==0)
- {
- if (rakServer)
- localPort=60000;
- else
- localPort=0;
- }
- else
- localPort=atoi(str);
- if (rakServer)
- {
- RakNet::SocketDescriptor socketDescriptor(localPort,0);
- rakServer->Startup(100, &socketDescriptor, 1);
- rakServer->SetMaximumIncomingConnections(100);
- }
- else
- {
- printf("Enter remote IP: ");
- Gets(remoteIP,sizeof(remoteIP));
- if (remoteIP[0]==0)
- strcpy(remoteIP, "127.0.0.1");
- printf("Enter remote port: ");
- Gets(str, sizeof(str));
- if (str[0]==0)
- remotePort=60000;
- else
- remotePort=atoi(str);
- RakNet::SocketDescriptor socketDescriptor(localPort,0);
- rakClient->Startup(1, &socketDescriptor, 1);
- rakClient->Connect(remoteIP, remotePort, 0, 0);
- }
- printf("Entering loop.\nHit 'h' for help.\nHit 'q' to quit\n'i' to increase send interval\n'd' to decrease send interval\n'+' to increase packet size\n'-' to decrease packet size.\nSpace to show current statistics\n");
- sendinterval=128;
- packetSize=64;
- lastPacketReceipt=lastNotification=RakNet::GetTimeMS();
- lastSend=0;
- while (1)
- {
- time=RakNet::GetTimeMS();
- if (kbhit())
- {
- #ifndef _WIN32
- Gets(buff,sizeof(buff));
- ch=buff[0];
- #else
- ch=getch();
- #endif
- if (ch=='q')
- {
- printf("Quitting\n");
- break;
- }
- else if (ch=='i')
- {
- sendinterval*=2;
- printf("Send interval is now %i\n", sendinterval);
- }
- else if (ch=='d')
- {
- if (sendinterval>1)
- sendinterval/=2;
- printf("Send interval is now %i\n", sendinterval);
- }
- if (ch=='h')
- printf("Hit 'h' for help.\nHit 'q' to quit\n'i' to increase send interval\n'd' to decrease send interval\n'+' to increase packet size\n'-' to decrease packet size.\nSpace to show current statistics\n");
- else if (ch=='+')
- {
- if (packetSize < 8192)
- packetSize*=2;
- printf("Packet size is now %i\n", packetSize);
- }
- else if (ch=='-')
- {
- if (packetSize>1)
- packetSize/=2;
- printf("Packet size is now %i\n", packetSize);
- }
- else if (ch==' ')
- {
- if (rakServer)
- {
- StatisticsToString(rakServer->GetStatistics(rakServer->GetSystemAddressFromIndex(0)), randomData, 1);
- printf("%s", randomData);
- }
- else
- {
- StatisticsToString(rakClient->GetStatistics(rakClient->GetSystemAddressFromIndex(0)), randomData, 1);
- printf("%s", randomData);
- }
- printf("Send interval is %i\n", sendinterval);
- printf("Packet size is %i\n", packetSize);
- printf("\n");
- }
-
- ch=0;
- }
- // get packets
- if (rakServer)
- p = rakServer->Receive();
- else
- p=rakClient->Receive();
- if (p)
- {
- lastPacketReceipt=RakNet::GetTimeMS();
- switch (p->data[0])
- {
- case ID_CONNECTION_REQUEST_ACCEPTED:
- printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
- break;
- case ID_DISCONNECTION_NOTIFICATION:
- // Connection lost normally
- printf("ID_DISCONNECTION_NOTIFICATION\n");
- break;
- case ID_NEW_INCOMING_CONNECTION:
- // Somebody connected. We have their IP now
- printf("ID_NEW_INCOMING_CONNECTION\n");
- break;
- case ID_CONNECTION_LOST:
- // Couldn't deliver a reliable packet - i.e. the other system was abnormally
- // terminated
- printf("ID_CONNECTION_LOST\n");
- break;
- default:
- // Relay
- if (rakServer)
- rakServer->Send((char*)p->data, p->length, HIGH_PRIORITY, RELIABLE_ORDERED, 0, p->systemAddress, true);
- }
- if (rakServer)
- rakServer->DeallocatePacket(p);
- else
- rakClient->DeallocatePacket(p);
- }
- if ((rakServer && rakServer->NumberOfConnections()>0) ||
- (rakClient && rakClient->NumberOfConnections()>0))
- {
- // Do sends
- if (lastSend + (RakNet::TimeMS)sendinterval < time)
- {
- if (rakServer)
- {
- rakServer->Send((char*)randomData, packetSize, HIGH_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
- }
- else if (rakClient)
- {
- rakClient->Send((char*)randomData, packetSize, HIGH_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
- }
- lastSend=time;
- }
- if (lastPacketReceipt + 500 < (unsigned int)time && lastNotification + 500 < (unsigned int)time)
- {
- lastNotification=time;
- printf("Warning: No packets for %i ms. Possibly a spike.\n", time - lastPacketReceipt);
- }
- }
- }
- if (rakServer)
- RakNet::RakPeerInterface::DestroyInstance(rakServer);
- else
- RakNet::RakPeerInterface::DestroyInstance(rakClient);
- return 1;
- }
|