SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RakNetCommandParser.cpp
Go to the documentation of this file.
1 /*
2 * Original work: Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2016-2018, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
16 #include "slikenet/NativeFeatureIncludes.h"
17 #if _RAKNET_SUPPORT_RakNetCommandParser==1
18
19 #include "slikenet/commandparser.h"
20 #include "slikenet/TransportInterface.h"
21 #include "slikenet/peerinterface.h"
22 #include "slikenet/BitStream.h"
23 #include "slikenet/assert.h"
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28 using namespace SLNet;
29
30 STATIC_FACTORY_DEFINITIONS(RakNetCommandParser,RakNetCommandParser);
31
32 RakNetCommandParser::RakNetCommandParser()
33 {
34 RegisterCommand(4, "Startup","( unsigned int maxConnections, unsigned short localPort, const char *forceHostAddress );");
35 RegisterCommand(0,"InitializeSecurity","();");
36 RegisterCommand(0,"DisableSecurity","( void );");
37 RegisterCommand(1,"AddToSecurityExceptionList","( const char *ip );");
38 RegisterCommand(1,"RemoveFromSecurityExceptionList","( const char *ip );");
39 RegisterCommand(1,"IsInSecurityExceptionList","( const char *ip );");
40 RegisterCommand(1,"SetMaximumIncomingConnections","( unsigned short numberAllowed );");
41 RegisterCommand(0,"GetMaximumIncomingConnections","( void ) const;");
42 RegisterCommand(4,"Connect","( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength );");
43 RegisterCommand(2,"Disconnect","( unsigned int blockDuration, unsigned char orderingChannel=0 );");
44 RegisterCommand(0,"IsActive","( void ) const;");
45 RegisterCommand(0,"GetConnectionList","() const;");
46 RegisterCommand(3,"CloseConnection","( const SystemAddress target, bool sendDisconnectionNotification, unsigned char orderingChannel=0 );");
47 RegisterCommand(2,"IsConnected","( );");
48 RegisterCommand(1,"GetIndexFromSystemAddress","( const SystemAddress systemAddress );");
49 RegisterCommand(1,"GetSystemAddressFromIndex","( unsigned int index );");
50 RegisterCommand(2,"AddToBanList","( const char *IP, SLNet::TimeMS milliseconds=0 );");
51 RegisterCommand(1,"RemoveFromBanList","( const char *IP );");
52 RegisterCommand(0,"ClearBanList","( void );");
53 RegisterCommand(1,"IsBanned","( const char *IP );");
54 RegisterCommand(1,"Ping1","( const SystemAddress target );");
55 RegisterCommand(3,"Ping2","( const char* host, unsigned short remotePort, bool onlyReplyOnAcceptingConnections );");
56 RegisterCommand(1,"GetAveragePing","( const SystemAddress systemAddress );");
57 RegisterCommand(1,"GetLastPing","( const SystemAddress systemAddress ) const;");
58 RegisterCommand(1,"GetLowestPing","( const SystemAddress systemAddress ) const;");
59 RegisterCommand(1,"SetOccasionalPing","( bool doPing );");
60 RegisterCommand(2,"SetOfflinePingResponse","( const char *data, const unsigned int length );");
61 RegisterCommand(0,"GetInternalID","( void ) const;");
62 RegisterCommand(1,"GetExternalID","( const SystemAddress target ) const;");
63 RegisterCommand(2,"SetTimeoutTime","( SLNet::TimeMS timeMS, const SystemAddress target );");
64 // RegisterCommand(1,"SetMTUSize","( int size );");
65 RegisterCommand(0,"GetMTUSize","( void ) const;");
66 RegisterCommand(0,"GetNumberOfAddresses","( void );");
67 RegisterCommand(1,"GetLocalIP","( unsigned int index );");
68 RegisterCommand(1,"AllowConnectionResponseIPMigration","( bool allow );");
69 RegisterCommand(4,"AdvertiseSystem","( const char *host, unsigned short remotePort, const char *data, int dataLength );");
70 RegisterCommand(2,"SetIncomingPassword","( const char* passwordData, int passwordDataLength );");
71 RegisterCommand(0,"GetIncomingPassword","( void );");
72 RegisterCommand(0,"IsNetworkSimulatorActive","( void );");
73 }
74 RakNetCommandParser::~RakNetCommandParser()
75 {
76 }
77 void RakNetCommandParser::SetRakPeerInterface(SLNet::RakPeerInterface *rakPeer)
78 {
79 peer=rakPeer;
80 }
81 bool RakNetCommandParser::OnCommand(const char *command, unsigned numParameters, char **parameterList, TransportInterface *transport, const SystemAddress &systemAddress, const char *originalString)
82 {
83 (void) originalString;
84 (void) numParameters;
85
86 if (peer==0)
87 return false;
88
89 if (strcmp(command, "Startup")==0)
90 {
91 SLNet::SocketDescriptor socketDescriptor((unsigned short)atoi(parameterList[1]), parameterList[2]);
92 ReturnResult(peer->Startup((unsigned short)atoi(parameterList[0]), &socketDescriptor, 1), command, transport, systemAddress);
93 }
94 else if (strcmp(command, "InitializeSecurity")==0)
95 {
96 ReturnResult(peer->InitializeSecurity(parameterList[0],parameterList[1]), command, transport, systemAddress);
97 }
98 else if (strcmp(command, "DisableSecurity")==0)
99 {
100 peer->DisableSecurity();
101 ReturnResult(command, transport, systemAddress);
102 }
103 else if (strcmp(command, "AddToSecurityExceptionList")==0)
104 {
105 peer->AddToSecurityExceptionList(parameterList[1]);
106 ReturnResult(command, transport, systemAddress);
107 }
108 else if (strcmp(command, "RemoveFromSecurityExceptionList")==0)
109 {
110 peer->RemoveFromSecurityExceptionList(parameterList[1]);
111 ReturnResult(command, transport, systemAddress);
112 }
113 else if (strcmp(command, "IsInSecurityExceptionList")==0)
114 {
115 ReturnResult(peer->IsInSecurityExceptionList(parameterList[1]),command, transport, systemAddress);
116 }
117 else if (strcmp(command, "SetMaximumIncomingConnections")==0)
118 {
119 peer->SetMaximumIncomingConnections((unsigned short)atoi(parameterList[0]));
120 ReturnResult(command, transport, systemAddress);
121 }
122 else if (strcmp(command, "GetMaximumIncomingConnections")==0)
123 {
124 ReturnResult((int) peer->GetMaximumIncomingConnections(), command, transport, systemAddress);
125 }
126 else if (strcmp(command, "Connect")==0)
127 {
128 ReturnResult(peer->Connect(parameterList[0], (unsigned short)atoi(parameterList[1]),parameterList[2],atoi(parameterList[3]))== SLNet::CONNECTION_ATTEMPT_STARTED, command, transport, systemAddress);
129 }
130 else if (strcmp(command, "Disconnect")==0)
131 {
132 peer->Shutdown(atoi(parameterList[0]), (unsigned char)atoi(parameterList[1]));
133 ReturnResult(command, transport, systemAddress);
134 }
135 else if (strcmp(command, "IsActive")==0)
136 {
137 ReturnResult(peer->IsActive(), command, transport, systemAddress);
138 }
139 else if (strcmp(command, "GetConnectionList")==0)
140 {
141 SystemAddress remoteSystems[32];
142 unsigned short count=32;
143 unsigned i;
144 if (peer->GetConnectionList(remoteSystems, &count))
145 {
146 if (count==0)
147 {
148 transport->Send(systemAddress, "GetConnectionList() returned no systems connected.\r\n");
149 }
150 else
151 {
152 transport->Send(systemAddress, "GetConnectionList() returned:\r\n");
153 for (i=0; i < count; i++)
154 {
155 char str1[64];
156 remoteSystems[i].ToString(true, str1, static_cast<size_t>(64));
157 transport->Send(systemAddress, "%i %s\r\n", i, str1);
158 }
159 }
160 }
161 else
162 transport->Send(systemAddress, "GetConnectionList() returned false.\r\n");
163 }
164 else if (strcmp(command, "CloseConnection")==0)
165 {
166 peer->CloseConnection(SystemAddress(parameterList[0]), atoi(parameterList[1])!=0,(unsigned char)atoi(parameterList[2]));
167 ReturnResult(command, transport, systemAddress);
168 }
169 else if (strcmp(command, "GetConnectionState")==0)
170 {
171 ReturnResult((int) peer->GetConnectionState(SystemAddress(parameterList[0])), command, transport, systemAddress);
172 }
173 else if (strcmp(command, "GetIndexFromSystemAddress")==0)
174 {
175 ReturnResult(peer->GetIndexFromSystemAddress(SystemAddress(parameterList[0])), command, transport, systemAddress);
176 }
177 else if (strcmp(command, "GetSystemAddressFromIndex")==0)
178 {
179 ReturnResult(peer->GetSystemAddressFromIndex(atoi(parameterList[0])), command, transport, systemAddress);
180 }
181 else if (strcmp(command, "AddToBanList")==0)
182 {
183 peer->AddToBanList(parameterList[0], atoi(parameterList[1]));
184 ReturnResult(command, transport, systemAddress);
185 }
186 else if (strcmp(command, "RemoveFromBanList")==0)
187 {
188 peer->RemoveFromBanList(parameterList[0]);
189 ReturnResult(command, transport, systemAddress);
190 }
191 else if (strcmp(command, "ClearBanList")==0)
192 {
193 peer->ClearBanList();
194 ReturnResult(command, transport, systemAddress);
195 }
196 else if (strcmp(command, "IsBanned")==0)
197 {
198 ReturnResult(peer->IsBanned(parameterList[0]), command, transport, systemAddress);
199 }
200 else if (strcmp(command, "Ping1")==0)
201 {
202 peer->Ping(SystemAddress(parameterList[0]));
203 ReturnResult(command, transport, systemAddress);
204 }
205 else if (strcmp(command, "Ping2")==0)
206 {
207 peer->Ping(parameterList[0], (unsigned short) atoi(parameterList[1]), atoi(parameterList[2])!=0);
208 ReturnResult(command, transport, systemAddress);
209 }
210 else if (strcmp(command, "GetAveragePing")==0)
211 {
212 ReturnResult(peer->GetAveragePing(SystemAddress(parameterList[0])), command, transport, systemAddress);
213 }
214 else if (strcmp(command, "GetLastPing")==0)
215 {
216 ReturnResult(peer->GetLastPing(SystemAddress(parameterList[0])), command, transport, systemAddress);
217 }
218 else if (strcmp(command, "GetLowestPing")==0)
219 {
220 ReturnResult(peer->GetLowestPing(SystemAddress(parameterList[0])), command, transport, systemAddress);
221 }
222 else if (strcmp(command, "SetOccasionalPing")==0)
223 {
224 peer->SetOccasionalPing(atoi(parameterList[0])!=0);
225 ReturnResult(command, transport, systemAddress);
226 }
227 else if (strcmp(command, "SetOfflinePingResponse")==0)
228 {
229 peer->SetOfflinePingResponse(parameterList[0], atoi(parameterList[1]));
230 ReturnResult(command, transport, systemAddress);
231 }
232 else if (strcmp(command, "GetInternalID")==0)
233 {
234 ReturnResult(peer->GetInternalID(), command, transport, systemAddress);
235 }
236 else if (strcmp(command, "GetExternalID")==0)
237 {
238 ReturnResult(peer->GetExternalID(SystemAddress(parameterList[0])), command, transport, systemAddress);
239 }
240 else if (strcmp(command, "SetTimeoutTime")==0)
241 {
242 peer->SetTimeoutTime(atoi(parameterList[0]), SystemAddress(parameterList[1]));
243 ReturnResult(command, transport, systemAddress);
244 }
245 /*
246 else if (strcmp(command, "SetMTUSize")==0)
247 {
248 ReturnResult(peer->SetMTUSize(atoi(parameterList[0]), UNASSIGNED_SYSTEM_ADDRESS), command, transport, systemAddress);
249 }
250 */
251 else if (strcmp(command, "GetMTUSize")==0)
252 {
253 ReturnResult(peer->GetMTUSize(UNASSIGNED_SYSTEM_ADDRESS), command, transport, systemAddress);
254 }
255 else if (strcmp(command, "GetNumberOfAddresses")==0)
256 {
257 ReturnResult((int)peer->GetNumberOfAddresses(), command, transport, systemAddress);
258 }
259 else if (strcmp(command, "GetLocalIP")==0)
260 {
261 ReturnResult((char*) peer->GetLocalIP(atoi(parameterList[0])), command, transport, systemAddress);
262 }
263 else if (strcmp(command, "AllowConnectionResponseIPMigration")==0)
264 {
265 peer->AllowConnectionResponseIPMigration(atoi(parameterList[0])!=0);
266 ReturnResult(command, transport, systemAddress);
267 }
268 else if (strcmp(command, "AdvertiseSystem")==0)
269 {
270 peer->AdvertiseSystem(parameterList[0], (unsigned short) atoi(parameterList[1]),parameterList[2],atoi(parameterList[3]));
271 ReturnResult(command, transport, systemAddress);
272 }
273 else if (strcmp(command, "SetIncomingPassword")==0)
274 {
275 peer->SetIncomingPassword(parameterList[0], atoi(parameterList[1]));
276 ReturnResult(command, transport, systemAddress);
277 }
278 else if (strcmp(command, "GetIncomingPassword")==0)
279 {
280 char password[256];
281 int passwordLength;
282 peer->GetIncomingPassword(password, &passwordLength);
283 if (passwordLength)
284 ReturnResult((char*)password, command, transport, systemAddress);
285 else
286 ReturnResult(0, command, transport, systemAddress);
287 }
288
289 return true;
290 }
291 const char *RakNetCommandParser::GetName(void) const
292 {
293 return "RakNet";
294 }
295 void RakNetCommandParser::SendHelp(TransportInterface *transport, const SystemAddress &systemAddress)
296 {
297 if (peer)
298 {
299 transport->Send(systemAddress, "The RakNet parser provides mirror functions to RakPeer\r\n");
300 transport->Send(systemAddress, "SystemAddresss take two parameters: send <BinaryAddress> <Port>.\r\n");
301 transport->Send(systemAddress, "For bool, send 1 or 0.\r\n");
302 }
303 else
304 {
305 transport->Send(systemAddress, "Parser not active. Call SetRakPeerInterface.\r\n");
306 }
307 }
308
309 #endif // _RAKNET_SUPPORT_*