1 #include "RakNetworkFactory.h"
2 #include "RakPeerInterface.h"
3 #include "GetTime.h"
4 #include "MessageIdentifiers.h"
5 #include "RakNetStatistics.h"
6 #include <cstdio>
7 #include <memory.h>
8 #include <cstring>
9 #include <stdlib.h>
10 
11 #ifdef WIN32
12 #include "Kbhit.h"
13 #else
14 #include "Kbhit.h"
15 #endif
16 
main(void)17 int main(void)
18 {
19 	RakPeerInterface *rakServer;
20 	RakPeerInterface *rakClient;
21 	char ch;
22 	char str[255], remoteIP[255];
23 	char randomData[8192];
24 	int localPort, remotePort;
25 	int packetSize;
26 	int sendinterval;
27 	RakNetTime time;
28 	Packet *p;
29 	RakNetTime lastPacketReceipt, lastNotification, lastSend;
30 	#ifndef _WIN32
31 	char buff[256];
32 	#endif
33 
34 	memset(randomData, 255, sizeof(randomData));
35 
36 	printf("This project is used to test two systems sending two each other with\n");
37 	printf("variable message sends rates and payload sizes\n");
38 	printf("Difficulty: Beginner\n\n");
39 
40 	printf("Start relay (s)erver or start (c)lient?\n");
41 #ifndef _WIN32
42 	gets(buff);
43 	ch=buff[0];
44 #else
45 	ch=getch();
46 #endif
47 	if (ch=='s' || ch=='S')
48 	{
49 		printf("Acting as server.\n");
50 		rakServer=RakNetworkFactory::GetRakPeerInterface();
51 		rakClient=0;
52 	}
53 	else
54 	{
55 		printf("Acting as client.\n");
56 		rakClient=RakNetworkFactory::GetRakPeerInterface();
57 		rakServer=0;
58 	}
59 
60 	printf("Enter local port: ");
61 	gets(str);
62 	if (str[0]==0)
63 	{
64 		if (rakServer)
65 			localPort=60000;
66 		else
67 			localPort=0;
68 	}
69 	else
70 		localPort=atoi(str);
71 
72 	if (rakServer)
73 	{
74 		printf("(H)igh priority thread or (R)egular?\n");
75 #ifndef _WIN32
76 		gets(buff);
77 		ch=buff[0];
78 #else
79 		ch=getch();
80 #endif
81 		if (ch=='h' || ch=='H')
82 		{
83 			SocketDescriptor socketDescriptor(localPort,0);
84 			rakServer->Startup(100, 0, &socketDescriptor, 1);
85 			printf("Server started under high priority\n");
86 		}
87 		else
88 		{
89 			SocketDescriptor socketDescriptor(localPort,0);
90 			rakServer->Startup(100, 30, &socketDescriptor, 1);
91 			printf("Server started under regular priority\n");
92 		}
93 		rakServer->SetMaximumIncomingConnections(100);
94 	}
95 	else
96 	{
97 		printf("Enter remote IP: ");
98 		gets(remoteIP);
99 		if (remoteIP[0]==0)
100 			strcpy(remoteIP, "127.0.0.1");
101 		printf("Enter remote port: ");
102 		gets(str);
103 		if (str[0]==0)
104 			remotePort=60000;
105 		else
106 			remotePort=atoi(str);
107 		printf("(H)igh priority thread or (R)egular?\n");
108 #ifndef _WIN32
109 		gets(buff);
110 		ch=buff[0];
111 #else
112 		ch=getch();
113 #endif
114 
115 		if (ch=='h' || ch=='H')
116 		{
117 			SocketDescriptor socketDescriptor(localPort,0);
118 			rakClient->Startup(1, 0, &socketDescriptor, 1);
119 			printf("Client started under high priority\n");
120 		}
121 		else
122 		{
123 			SocketDescriptor socketDescriptor(localPort,0);
124 			rakClient->Startup(1, 30, &socketDescriptor, 1);
125 			printf("Client started under regular priority.  Attempting connection...\n");
126 		}
127 		rakClient->Connect(remoteIP, remotePort, 0, 0);
128 	}
129 
130 	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");
131 
132 	sendinterval=128;
133 	packetSize=64;
134 	lastPacketReceipt=lastNotification=RakNet::GetTime();
135 	lastSend=0;
136 
137 	while (1)
138 	{
139 		time=RakNet::GetTime();
140 
141 		if (kbhit())
142 		{
143 #ifndef _WIN32
144 			gets(buff);
145 			ch=buff[0];
146 #else
147 			ch=getch();
148 #endif
149 			if (ch=='q')
150 			{
151 				printf("Quitting\n");
152 				break;
153 			}
154 			else if (ch=='i')
155 			{
156 				sendinterval*=2;
157 				printf("Send interval is now %i\n", sendinterval);
158 			}
159 			else if (ch=='d')
160 			{
161 				if (sendinterval>1)
162 					sendinterval/=2;
163 				printf("Send interval is now %i\n", sendinterval);
164 			}
165 			if (ch=='h')
166 				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");
167 			else if (ch=='+')
168 			{
169 				if (packetSize < 8192)
170 					packetSize*=2;
171 				printf("Packet size is now %i\n", packetSize);
172 			}
173 			else if (ch=='-')
174 			{
175 				if (packetSize>1)
176 					packetSize/=2;
177 				printf("Packet size is now %i\n", packetSize);
178 			}
179 			else if (ch==' ')
180 			{
181 				if (rakServer)
182 				{
183 					StatisticsToString(rakServer->GetStatistics(rakServer->GetSystemAddressFromIndex(0)), randomData, 1);
184 					printf("%s", randomData);
185 				}
186 				else
187 				{
188 					StatisticsToString(rakClient->GetStatistics(rakClient->GetSystemAddressFromIndex(0)), randomData, 1);
189 					printf("%s", randomData);
190 				}
191 
192 				printf("Send interval is %i\n", sendinterval);
193 				printf("Packet size is %i\n", packetSize);
194 				printf("\n");
195 			}
196 
197 
198 			ch=0;
199 		}
200 
201 		// get packets
202 		if (rakServer)
203 			p = rakServer->Receive();
204 		else
205 			p=rakClient->Receive();
206 
207 		if (p)
208 		{
209 			lastPacketReceipt=RakNet::GetTime();
210 
211 			switch (p->data[0])
212 			{
213 			case ID_CONNECTION_REQUEST_ACCEPTED:
214 				printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
215 				break;
216 			case ID_DISCONNECTION_NOTIFICATION:
217 				// Connection lost normally
218 				printf("ID_DISCONNECTION_NOTIFICATION\n");
219 				break;
220 
221 			case ID_NEW_INCOMING_CONNECTION:
222 				// Somebody connected.  We have their IP now
223 				printf("ID_NEW_INCOMING_CONNECTION\n");
224 				break;
225 
226 			case ID_MODIFIED_PACKET:
227 				// Cheater!
228 				printf("ID_MODIFIED_PACKET\n");
229 				break;
230 
231 			case ID_CONNECTION_LOST:
232 				// Couldn't deliver a reliable packet - i.e. the other system was abnormally
233 				// terminated
234 				printf("ID_CONNECTION_LOST\n");
235 				break;
236 			default:
237 				// Relay
238 				if (rakServer)
239 					rakServer->Send((char*)p->data, p->length, HIGH_PRIORITY, RELIABLE_ORDERED, 0, p->systemAddress, true);
240 			}
241 
242 			if (rakServer)
243 				rakServer->DeallocatePacket(p);
244 			else
245 				rakClient->DeallocatePacket(p);
246 		}
247 
248 		if ((rakServer && rakServer->NumberOfConnections()>0) ||
249 			(rakClient && rakClient->NumberOfConnections()>0))
250 		{
251 			// Do sends
252 			if (lastSend + (RakNetTime)sendinterval < time)
253 			{
254 				if (rakServer)
255 				{
256 					rakServer->Send((char*)randomData, packetSize, HIGH_PRIORITY, RELIABLE_ORDERED, 0, UNASSIGNED_SYSTEM_ADDRESS, true);
257 				}
258 				else if (rakClient)
259 				{
260 					rakClient->Send((char*)randomData, packetSize, HIGH_PRIORITY, RELIABLE_ORDERED, 0, UNASSIGNED_SYSTEM_ADDRESS, true);
261 				}
262 
263 				lastSend=time;
264 			}
265 
266 			if (lastPacketReceipt + 500 < (unsigned int)time && lastNotification + 500 < (unsigned int)time)
267 			{
268 				lastNotification=time;
269 				printf("Warning: No packets for %i ms.  Possibly a spike.\n", time - lastPacketReceipt);
270 			}
271 		}
272 	}
273 
274 	if (rakServer)
275 		RakNetworkFactory::DestroyRakPeerInterface(rakServer);
276 	else
277 		RakNetworkFactory::DestroyRakPeerInterface(rakClient);
278 
279 	return 1;
280 }
281