forked from facebookarchive/RakNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMasterClientMain.cpp
More file actions
225 lines (208 loc) · 6.51 KB
/
Copy pathMasterClientMain.cpp
File metadata and controls
225 lines (208 loc) · 6.51 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
/*
* 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.
*
*/
// This is my own internal test program for the master client but serves as a good example.
// Right now it is hardcoded to connect to 127.0.0.1. You would run the master server first, then run this.
#include "MasterCommon.h"
#include "MasterClient.h"
#include "StringCompressor.h"
#include "BitStream.h"
#include "RakPeerInterface.h"
#include "RakNetworkFactory.h"
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#ifdef WIN32
#include <conio.h>
#else
#include "../Unix/kbhit.h"
#endif
#ifdef _WIN32
#include <conio.h>
#include <windows.h> // Sleep
#else
#include "../Unix/kbhit.h"
#include <unistd.h> // usleep
#endif
#define READCHAR(arg) gets(arg); ch=arg[0];
// remove this
#include "PacketEnumerations.h"
int main(void)
{
printf("DEPRECIATED - Use LightweightDatabase instead\n");
BitStream bitStream;
char str[256];
char ch;
MasterClient masterClient;
RakPeerInterface *testGameServerOrClient;
unsigned int serverListSize, index;
bool identiferFound;
const char *outputString;
int outputInt;
Packet *p;
// Create a fake game
testGameServerOrClient = RakNetworkFactory::GetRakPeerInterface();
testGameServerOrClient->Initialize(8, 60003, 0);
testGameServerOrClient->SetMaximumIncomingConnections(8);
testGameServerOrClient->AttachPlugin(&masterClient);
printf("This project shows how to use the master client.\n");
printf("The master client is used by game servers to advertise themselves.\n");
printf("On the master server and by game clients to download a list of game servers\n");
printf("Difficulty: Intermediate\n\n");
if (masterClient.Connect("127.0.0.1", 60000))
printf("Master client connecting...\n");
else
printf("Master client failed to start or connect.\n");
printf("(Q)uit\n(q)uery master server\n(l)ist server\n(d)elist server\n(D)isconnect from the master server.\n(a)dd rule\n(r)emove rule\n(p)ing server list\n(C)onnect to the master server.\n(c)onnect to another game, using NAT punch-through with master server, bypassing most NATs\n(SPACE) print server list\n");
char buff[256];
while (1)
{
if (kbhit())
{
READCHAR(buff);
if (ch=='Q')
break;
if (ch=='q')
{
masterClient.ClearQueryRules();
printf("Enter query key 1/2 or enter for none: ");
gets(str);
masterClient.AddQueryRule(str);
printf("Enter query key 2/2 or enter for none: ");
gets(str);
masterClient.AddQueryRule(str);
masterClient.QueryMasterServer();
printf("Server queried. Press space to see server list.\n");
}
else if (ch=='l')
{
printf("Uploading game server. Query to see it.\n");
masterClient.ListServer();
}
else if (ch=='d')
{
printf("Server delisted. Query to update our own list.\n");
masterClient.DelistServer();
}
else if (ch=='D')
{
printf("Disconnected.\n");
PlayerID playerId;
testGameServerOrClient->IPToPlayerID("127.0.0.1", 60000, &playerId);
testGameServerOrClient->CloseConnection(playerId, true, 0);
}
else if (ch=='C')
{
if (masterClient.Connect("127.0.0.1", 60000))
printf("Master client connecting...\n");
else
printf("Master client failed to start or connect.\n");
}
else if (ch=='a')
{
printf("Adding sample rules. Query to update our own list.\n");
masterClient.PostRule("Game name", "My big game o' death.", 0);
masterClient.PostRule("Game type", "Death match", 0);
masterClient.PostRule("Score",0, 100);
}
else if (ch=='r')
{
printf("Removing rules. Query to update our own list.\n");
masterClient.RemoveRule("Game type");
masterClient.RemoveRule("Game name");
masterClient.RemoveRule("Score");
}
else if (ch=='p')
{
printf("Pinging any servers in our list\n");
masterClient.PingServers();
}
else if (ch=='c')
{
char ip[22];
printf("Sending connection attempt notification to master server\n");
printf("Enter IP of server from game list: ");
gets(ip);
printf("Enter port: ");
gets(str);
if (ip[0]!=0 && str[0]!=0)
{
masterClient.ConnectionAttemptNotification(ip, atoi(str));
printf("Sent connection attempt notification to the server the master server\n");
}
else
{
printf("Aborting...");
}
}
else if (ch==' ')
{
serverListSize=masterClient.GetServerListSize();
if (serverListSize==0)
{
printf("No servers in list\n");
}
else
{
for (index=0; index < serverListSize; index++)
{
printf("%i. ", index);
outputString=masterClient.GetServerListRuleAsString(index, "IP", &identiferFound);
if (identiferFound)
printf("%s:", outputString);
else
printf("NO_IP:");
outputInt=masterClient.GetServerListRuleAsInt(index, "Port", &identiferFound);
if (identiferFound)
printf("%i ", outputInt);
else
printf("NO_PORT ");
outputInt=masterClient.GetServerListRuleAsInt(index, "Ping", &identiferFound);
if (identiferFound)
printf("%i ", outputInt);
else
printf("NO_PING ");
outputString=masterClient.GetServerListRuleAsString(index, "Game type", &identiferFound);
if (identiferFound)
printf("%s ", outputString);
else
printf("NO_GT ");
outputString=masterClient.GetServerListRuleAsString(index, "Game name", &identiferFound);
if (identiferFound)
printf("%s ", outputString);
else
printf("NO_GN ");
outputInt=masterClient.GetServerListRuleAsInt(index, "Score", &identiferFound);
if (identiferFound)
printf("%i\n", outputInt);
else
printf("NO_SCORE\n");
}
}
}
ch=0;
}
p = testGameServerOrClient->Receive();
while (p)
{
// Ignore any game packets. The master server plugin handles everything.
testGameServerOrClient->DeallocatePacket(p);
// Call Receive every update to keep the plugin going
p = testGameServerOrClient->Receive();
}
#ifdef _WIN32
Sleep(30);
#else
usleep(30 * 1000);
#endif
}
masterClient.Disconnect();
RakNetworkFactory::DestroyRakPeerInterface(testGameServerOrClient);
return 0;
}