1 #include "ReliableOrderedConvertedTest.h"
2 
3 FILE *fp;
4 int memoryUsage=0;
5 
6 char lastError[512];
7 
LoggedMalloc(size_t size,const char * file,unsigned int line)8 void* ReliableOrderedConvertedTest::LoggedMalloc(size_t size, const char *file, unsigned int line)
9 {
10 	memoryUsage+=(int)size;
11 	if (fp)
12 		fprintf(fp,"Alloc %s:%i %i bytes %i total\n", file,line,size,memoryUsage);
13 	char *p = (char*) malloc(size+sizeof(size));
14 	memcpy(p,&size,sizeof(size));
15 	return p+sizeof(size);
16 }
LoggedFree(void * p,const char * file,unsigned int line)17 void ReliableOrderedConvertedTest::LoggedFree(void *p, const char *file, unsigned int line)
18 {
19 	char *realP=(char*)p-sizeof(size_t);
20 	size_t allocatedSize;
21 	memcpy(&allocatedSize,realP,sizeof(size_t));
22 	memoryUsage-=(int)allocatedSize;
23 	if (fp)
24 		fprintf(fp,"Free %s:%i %i bytes %i total\n", file,line,allocatedSize,memoryUsage);
25 	free(realP);
26 }
LoggedRealloc(void * p,size_t size,const char * file,unsigned int line)27 void* ReliableOrderedConvertedTest::LoggedRealloc(void *p, size_t size, const char *file, unsigned int line)
28 {
29 	char *realP=(char*)p-sizeof(size_t);
30 	size_t allocatedSize;
31 	memcpy(&allocatedSize,realP,sizeof(size_t));
32 	memoryUsage-=(int)allocatedSize;
33 	memoryUsage+=(int)size;
34 	p = realloc(realP,size+sizeof(size));
35 	memcpy(p,&size,sizeof(size));
36 	if (fp)
37 		fprintf(fp,"Realloc %s:%i %i to %i bytes %i total\n", file,line,allocatedSize,size,memoryUsage);
38 	return (char*)p+sizeof(size);
39 }
40 
41 /*
42 What is being done here is having a server connect to a client.
43 
44 Packets are sent at 30 millisecond intervals for 12 seconds.
45 
46 Length and sequence are checked for each packet.
47 
48 Success conditions:
49 All packets are correctly recieved in order.
50 
51 Failure conditions:
52 
53 All packets are not  correctly recieved.
54 All packets are not in order.
55 
56 */
57 
RunTest(DataStructures::List<RakNet::RakString> params,bool isVerbose,bool noPauses)58 int ReliableOrderedConvertedTest::RunTest(DataStructures::List<RakNet::RakString> params,bool isVerbose,bool noPauses)
59 {
60 
61 	RakPeerInterface *sender, *receiver;
62 	unsigned int packetNumberSender[32],packetNumberReceiver[32], receivedPacketNumberReceiver, receivedTimeReceiver;
63 	char str[256];
64 	char ip[32];
65 	RakNetTime sendInterval, nextSend, currentTime, quitTime;
66 	unsigned short remotePort, localPort;
67 	unsigned char streamNumberSender,streamNumberReceiver;
68 	RakNet::BitStream bitStream;
69 	Packet *packet;
70 	bool doSend=false;
71 
72 	for (int i=0; i < 32; i++)
73 	{
74 		packetNumberSender[i]=0;
75 		packetNumberReceiver[i]=0;
76 
77 	}
78 
79 	/*
80 	if (argc==2)
81 	{
82 	fp = fopen(argv[1],"wt");
83 	SetMalloc_Ex(LoggedMalloc);
84 	SetRealloc_Ex(LoggedRealloc);
85 	SetFree_Ex(LoggedFree);
86 	}
87 	else
88 	*/
89 	fp=0;
90 	destroyList.Clear(false,__FILE__,__LINE__);
91 
92 	sender =RakNetworkFactory::GetRakPeerInterface();
93 	destroyList.Push(	sender ,__FILE__,__LINE__);
94 	//sender->ApplyNetworkSimulator(.02, 100, 50);
95 
96 	/*
97 	if (str[0]==0)
98 	sendInterval=30;
99 	else
100 	sendInterval=atoi(str);*///possible future params
101 
102 	sendInterval=30;
103 
104 	/*
105 	printf("Enter remote IP: ");
106 	gets(ip);
107 	if (ip[0]==0)*/
108 	strcpy(ip, "127.0.0.1");
109 
110 	/*
111 	printf("Enter remote port: ");
112 	gets(str);
113 	if (str[0]==0)*/
114 	strcpy(str, "60000");
115 	remotePort=atoi(str);
116 	/*
117 	printf("Enter local port: ");
118 	gets(str);
119 	if (str[0]==0)*/
120 	strcpy(str, "0");
121 	localPort=atoi(str);
122 
123 	if (isVerbose)
124 		printf("Connecting...\n");
125 
126 	sender->Startup(1, 30, &SocketDescriptor(localPort,0), 1);
127 	sender->Connect(ip, remotePort, 0, 0);
128 
129 	receiver =RakNetworkFactory::GetRakPeerInterface();
130 	destroyList.Push(	receiver ,__FILE__,__LINE__);
131 
132 	/*
133 	printf("Enter local port: ");
134 	gets(str);
135 	if (str[0]==0)*/
136 	strcpy(str, "60000");
137 	localPort=atoi(str);
138 
139 	if (isVerbose)
140 		printf("Waiting for connections...\n");
141 
142 	receiver->Startup(32, 30, &SocketDescriptor(localPort,0), 1);
143 	receiver->SetMaximumIncomingConnections(32);
144 
145 	//	if (sender)
146 	//		sender->ApplyNetworkSimulator(128000, 50, 100);
147 	//	if (receiver)
148 	//		receiver->ApplyNetworkSimulator(128000, 50, 100);
149 
150 	/*printf("How long to run this test for, in seconds?\n");
151 	gets(str);
152 	if (str[0]==0)*/
153 	strcpy(str, "12");
154 
155 	currentTime = RakNet::GetTime();
156 	quitTime = atoi(str) * 1000 + currentTime;
157 
158 	nextSend=currentTime;
159 
160 	while (currentTime < quitTime)
161 		//while (1)
162 	{
163 
164 		packet = sender->Receive();
165 		while (packet)
166 		{
167 			// PARSE TYPES
168 			switch(packet->data[0])
169 			{
170 			case ID_CONNECTION_REQUEST_ACCEPTED:
171 				if (isVerbose)
172 					printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
173 				doSend=true;
174 				nextSend=currentTime;
175 				break;
176 			case ID_NO_FREE_INCOMING_CONNECTIONS:
177 				if (isVerbose)
178 					printf("ID_NO_FREE_INCOMING_CONNECTIONS\n");
179 				break;
180 			case ID_DISCONNECTION_NOTIFICATION:
181 				if (isVerbose)
182 					printf("ID_DISCONNECTION_NOTIFICATION\n");
183 				break;
184 			case ID_CONNECTION_LOST:
185 				if (isVerbose)
186 					printf("ID_CONNECTION_LOST\n");
187 				break;
188 			case ID_CONNECTION_ATTEMPT_FAILED:
189 				if (isVerbose)
190 					printf("Connection attempt failed\n");
191 				break;
192 			}
193 
194 			sender->DeallocatePacket(packet);
195 			packet = sender->Receive();
196 		}
197 
198 		while (doSend && currentTime > nextSend)
199 		{
200 			streamNumberSender=0;
201 			//	streamNumber = randomMT() % 32;
202 			// Do the send
203 			bitStream.Reset();
204 			bitStream.Write((unsigned char) (ID_USER_PACKET_ENUM+1));
205 			bitStream.Write(packetNumberSender[streamNumberSender]++);
206 			bitStream.Write(streamNumberSender);
207 			bitStream.Write(currentTime);
208 			char *pad;
209 			int padLength = (randomMT() % 5000) + 1;
210 			pad = new char [padLength];
211 			bitStream.Write(pad, padLength);
212 			delete [] pad;
213 			// Send on a random priority with a random stream
214 			// if (sender->Send(&bitStream, HIGH_PRIORITY, (PacketReliability) (RELIABLE + (randomMT() %2)) ,streamNumber, UNASSIGNED_SYSTEM_ADDRESS, true)==false)
215 			if (sender->Send(&bitStream, HIGH_PRIORITY, RELIABLE_ORDERED ,streamNumberSender, UNASSIGNED_SYSTEM_ADDRESS, true)==false)
216 				packetNumberSender[streamNumberSender]--; // Didn't finish connecting yet?
217 
218 			RakNetStatistics *rssSender;
219 			rssSender=sender->GetStatistics(sender->GetSystemAddressFromIndex(0));
220 			if (isVerbose)
221 				printf("Snd: %i.\n", packetNumberSender[streamNumberSender]);
222 
223 			nextSend+=sendInterval;
224 
225 			// Test halting
226 			//	if (rand()%20==0)
227 			//		nextSend+=1000;
228 		}
229 
230 		packet = receiver->Receive();
231 		while (packet)
232 		{
233 			switch(packet->data[0])
234 			{
235 			case ID_NEW_INCOMING_CONNECTION:
236 				if (isVerbose)
237 					printf("ID_NEW_INCOMING_CONNECTION\n");
238 				break;
239 			case ID_DISCONNECTION_NOTIFICATION:
240 				if (isVerbose)
241 					printf("ID_DISCONNECTION_NOTIFICATION\n");
242 				break;
243 			case ID_CONNECTION_LOST:
244 				if (isVerbose)
245 					printf("ID_CONNECTION_LOST\n");
246 				break;
247 			case ID_USER_PACKET_ENUM+1:
248 				bitStream.Reset();
249 				bitStream.Write((char*)packet->data, packet->length);
250 				bitStream.IgnoreBits(8); // Ignore ID_USER_PACKET_ENUM+1
251 				bitStream.Read(receivedPacketNumberReceiver);
252 				bitStream.Read(streamNumberReceiver);
253 				bitStream.Read(receivedTimeReceiver);
254 
255 				if (receivedPacketNumberReceiver!=packetNumberReceiver[streamNumberReceiver])
256 				{
257 
258 					//WARNING: If you modify the below code make sure the whole string remains in bounds, sprintf will NOT do it for you.
259 					//The error string is 512 in length
260 
261 					//Note: Removed buffer checking because chance is insignificant, left code if wanted in future. Needs limits.h ISO C standard.
262 
263 					/*
264 					int maxIntWorkingCopy= INT_MAX;
265 
266 					int maxIntCharLen =0;
267 
268 					while (maxIntWorkingCopy>0)
269 					{maxIntCharLen++;
270 					maxIntWorkingCopy/=10;
271 					}
272 
273 					if (strlen(lastError)>maxIntCharLen* 3 +27)//512 should be a good len for now
274 					{*/
275 
276 					sprintf(lastError,"Expecting %i got %i (channel %i).",packetNumberReceiver[streamNumberReceiver], receivedPacketNumberReceiver, streamNumberReceiver);
277 
278 					/*
279 					}
280 					else
281 					{
282 					sprintf(lastError,"Did not get what was expected. More details can be given if the error string buffer size is increased.");
283 
284 					}*/
285 
286 					if (isVerbose)
287 					{
288 
289 						RakNetStatistics *rssSender,*rssReceiver;
290 						char message[2048];
291 
292 						rssSender=sender->GetStatistics(sender->GetSystemAddressFromIndex(0));
293 
294 						rssReceiver=receiver->GetStatistics(receiver->GetSystemAddressFromIndex(0));
295 						StatisticsToString(rssSender, message, 2);
296 						printf("Server stats %s\n", message);
297 						StatisticsToString(rssReceiver, message, 2);
298 						printf("Client stats%s", message);
299 
300 						DebugTools::ShowError(lastError,!noPauses && isVerbose,__LINE__,__FILE__);
301 					}
302 
303 					return 1;
304 				}
305 				else
306 					if (isVerbose)
307 					{
308 						printf("Got %i.Channel %i.Len %i.", packetNumberReceiver[streamNumberReceiver], streamNumberReceiver, packet->length);
309 
310 						printf("Sent=%u Received=%u Diff=%i.\n", receivedTimeReceiver, currentTime, (int)currentTime - (int) receivedTimeReceiver);
311 					}
312 
313 					packetNumberReceiver[streamNumberReceiver]++;
314 					break;
315 			}
316 
317 			receiver->DeallocatePacket(packet);
318 			packet = receiver->Receive();
319 		}
320 
321 		RakSleep(0);
322 
323 		currentTime=RakNet::GetTime();
324 	}
325 
326 	if (isVerbose)
327 	{
328 
329 		RakNetStatistics *rssSender,*rssReceiver;
330 		char message[2048];
331 
332 		rssSender=sender->GetStatistics(sender->GetSystemAddressFromIndex(0));
333 
334 		rssReceiver=receiver->GetStatistics(receiver->GetSystemAddressFromIndex(0));
335 		StatisticsToString(rssSender, message, 2);
336 		printf("Server stats %s\n", message);
337 		StatisticsToString(rssReceiver, message, 2);
338 		printf("Client stats%s", message);
339 	}
340 
341 	if (fp)
342 		fclose(fp);
343 
344 	return 0;
345 }
346 
GetTestName()347 RakNet::RakString ReliableOrderedConvertedTest::GetTestName()
348 {
349 
350 	return "ReliableOrderedConvertedTest";
351 
352 }
353 
ErrorCodeToString(int errorCode)354 RakNet::RakString ReliableOrderedConvertedTest::ErrorCodeToString(int errorCode)
355 {
356 
357 	RakNet::RakString returnString;
358 
359 	switch (errorCode)
360 	{
361 
362 	case 0:
363 		return "No error";
364 		break;
365 
366 	case 1:
367 		returnString= "The very last error for this object was ";
368 		returnString+=lastError;
369 		return returnString;
370 		break;
371 
372 	default:
373 		return "Undefined Error";
374 	}
375 }
376 
ReliableOrderedConvertedTest(void)377 ReliableOrderedConvertedTest::ReliableOrderedConvertedTest(void)
378 {
379 }
380 
~ReliableOrderedConvertedTest(void)381 ReliableOrderedConvertedTest::~ReliableOrderedConvertedTest(void)
382 {
383 }
DestroyPeers()384 void ReliableOrderedConvertedTest::DestroyPeers()
385 {
386 
387 	int theSize=destroyList.Size();
388 
389 	for (int i=0; i < theSize; i++)
390 		RakNetworkFactory::DestroyRakPeerInterface(destroyList[i]);
391 
392 }
393