forked from facebookarchive/RakNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComprehensiveTest.cpp
More file actions
245 lines (220 loc) · 7.02 KB
/
Copy pathComprehensiveTest.cpp
File metadata and controls
245 lines (220 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* 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 "BitStream.h"
#include <stdlib.h> // For atoi
#include <cstring> // For strlen
#include "Rand.h"
#include "RakNetStatistics.h"
#include "MessageIdentifiers.h"
#include <stdio.h>
#include "GetTime.h"
using namespace RakNet;
#ifdef _WIN32
#include "WindowsIncludes.h" // Sleep
#else
#include <unistd.h> // usleep
#include <cstdio>
#endif
//#define _VERIFY_RECIPIENTS
#define _DO_PRINTF
#define NUM_PEERS 10
#define CONNECTIONS_PER_SYSTEM 4
int main(void)
{
RakPeerInterface *peers[NUM_PEERS];
int peerIndex;
float nextAction;
int i;
printf("This is just a test app to run a bit of everything to test for crashes.\n");
printf("Difficulty: Intermediate\n\n");
char data[8096];
int seed = 12345;
printf("Using seed %i\n", seed);
seedMT(seed);
for (i=0; i < NUM_PEERS; i++)
{
peers[i]=RakNet::RakPeerInterface::GetInstance();
peers[i]->SetMaximumIncomingConnections(CONNECTIONS_PER_SYSTEM);
RakNet::SocketDescriptor socketDescriptor(60000+i, 0);
peers[i]->Startup(NUM_PEERS, &socketDescriptor, 1);
peers[i]->SetOfflinePingResponse("Offline Ping Data", (int)strlen("Offline Ping Data")+1);
}
for (i=0; i < NUM_PEERS; i++)
{
peers[i]->Connect("127.0.0.1", 60000+(randomMT()%NUM_PEERS), 0, 0);
}
RakNet::TimeMS endTime = RakNet::GetTimeMS()+600000;
while (RakNet::GetTimeMS()<endTime)
{
nextAction = frandomMT();
if (nextAction < .04f)
{
// Initialize
peerIndex=randomMT()%NUM_PEERS;
RakNet::SocketDescriptor socketDescriptor(60000+peerIndex, 0);
peers[peerIndex]->Startup(NUM_PEERS, &socketDescriptor, 1);
peers[peerIndex]->Connect("127.0.0.1", 60000+randomMT() % NUM_PEERS, 0, 0);
}
else if (nextAction < .09f)
{
// Connect
peerIndex=randomMT()%NUM_PEERS;
peers[peerIndex]->Connect("127.0.0.1", 60000+randomMT() % NUM_PEERS, 0, 0);
}
else if (nextAction < .10f)
{
// Disconnect
peerIndex=randomMT()%NUM_PEERS;
// peers[peerIndex]->Shutdown(randomMT() % 100);
}
else if (nextAction < .12f)
{
// GetConnectionList
peerIndex=randomMT()%NUM_PEERS;
SystemAddress remoteSystems[NUM_PEERS];
unsigned short numSystems=NUM_PEERS;
peers[peerIndex]->GetConnectionList(remoteSystems, &numSystems);
if (numSystems>0)
{
#ifdef _DO_PRINTF
printf("%i: ", 60000+numSystems);
for (i=0; i < numSystems; i++)
{
printf("%i: ", remoteSystems[i].GetPort());
}
printf("\n");
#endif
}
}
else if (nextAction < .14f)
{
// Send
int dataLength;
PacketPriority priority;
PacketReliability reliability;
unsigned char orderingChannel;
SystemAddress target;
bool broadcast;
// data[0]=ID_RESERVED1+(randomMT()%10);
data[0]=ID_USER_PACKET_ENUM;
dataLength=3+(randomMT()%8000);
// dataLength=600+(randomMT()%7000);
priority=(PacketPriority)(randomMT()%(int)NUMBER_OF_PRIORITIES);
reliability=(PacketReliability)(randomMT()%((int)RELIABLE_SEQUENCED+1));
orderingChannel=randomMT()%32;
if ((randomMT()%NUM_PEERS)==0)
target=RakNet::UNASSIGNED_SYSTEM_ADDRESS;
else
target=peers[peerIndex]->GetSystemAddressFromIndex(randomMT()%NUM_PEERS);
broadcast=(bool)(randomMT()%2);
#ifdef _VERIFY_RECIPIENTS
broadcast=false; // Temporarily in so I can check recipients
#endif
peerIndex=randomMT()%NUM_PEERS;
sprintf(data+3, "dataLength=%i priority=%i reliability=%i orderingChannel=%i target=%i broadcast=%i\n", dataLength, priority, reliability, orderingChannel, target.GetPort(), broadcast);
//unsigned short localPort=60000+i;
#ifdef _VERIFY_RECIPIENTS
memcpy((char*)data+1, (char*)&target.port, sizeof(unsigned short));
#endif
data[dataLength-1]=0;
peers[peerIndex]->Send(data, dataLength, priority, reliability, orderingChannel, target, broadcast);
}
else if (nextAction < .18f)
{
int dataLength;
PacketPriority priority;
PacketReliability reliability;
unsigned char orderingChannel;
SystemAddress target;
bool broadcast;
data[0]=ID_USER_PACKET_ENUM+(randomMT()%10);
dataLength=3+(randomMT()%8000);
// dataLength=600+(randomMT()%7000);
priority=(PacketPriority)(randomMT()%(int)NUMBER_OF_PRIORITIES);
reliability=(PacketReliability)(randomMT()%((int)RELIABLE_SEQUENCED+1));
orderingChannel=randomMT()%32;
peerIndex=randomMT()%NUM_PEERS;
if ((randomMT()%NUM_PEERS)==0)
target=RakNet::UNASSIGNED_SYSTEM_ADDRESS;
else
target=peers[peerIndex]->GetSystemAddressFromIndex(randomMT()%NUM_PEERS);
broadcast=(bool)(randomMT()%2);
#ifdef _VERIFY_RECIPIENTS
broadcast=false; // Temporarily in so I can check recipients
#endif
sprintf(data+3, "dataLength=%i priority=%i reliability=%i orderingChannel=%i target=%i broadcast=%i\n", dataLength, priority, reliability, orderingChannel, target.GetPort(), broadcast);
#ifdef _VERIFY_RECIPIENTS
memcpy((char*)data, (char*)&target.port, sizeof(unsigned short));
#endif
data[dataLength-1]=0;
}
else if (nextAction < .181f)
{
// CloseConnection
SystemAddress target;
peerIndex=randomMT()%NUM_PEERS;
target=peers[peerIndex]->GetSystemAddressFromIndex(randomMT()%NUM_PEERS);
peers[peerIndex]->CloseConnection(target, (bool)(randomMT()%2), 0);
}
else if (nextAction < .20f)
{
// Offline Ping
peerIndex=randomMT()%NUM_PEERS;
peers[peerIndex]->Ping("127.0.0.1", 60000+(randomMT()%NUM_PEERS), (bool)(randomMT()%2));
}
else if (nextAction < .21f)
{
// Online Ping
SystemAddress target;
target=peers[peerIndex]->GetSystemAddressFromIndex(randomMT()%NUM_PEERS);
peerIndex=randomMT()%NUM_PEERS;
peers[peerIndex]->Ping(target);
}
else if (nextAction < .24f)
{
}
else if (nextAction < .25f)
{
// GetStatistics
SystemAddress target, mySystemAddress;
RakNetStatistics *rss;
mySystemAddress=peers[peerIndex]->GetInternalID();
target=peers[peerIndex]->GetSystemAddressFromIndex(randomMT()%NUM_PEERS);
peerIndex=randomMT()%NUM_PEERS;
rss=peers[peerIndex]->GetStatistics(mySystemAddress);
if (rss)
{
StatisticsToString(rss, data, 0);
#ifdef _DO_PRINTF
printf("Statistics for local system %i:\n%s", mySystemAddress.GetPort(), data);
#endif
}
rss=peers[peerIndex]->GetStatistics(target);
if (rss)
{
StatisticsToString(rss, data, 0);
#ifdef _DO_PRINTF
printf("Statistics for target system %i:\n%s", target.GetPort(), data);
#endif
}
}
for (i=0; i < NUM_PEERS; i++)
peers[i]->DeallocatePacket(peers[i]->Receive());
#ifdef _WIN32
Sleep(0);
#else
usleep(0);
#endif
}
for (i=0; i < NUM_PEERS; i++)
RakNet::RakPeerInterface::DestroyInstance(peers[i]);
return 0;
}