RakNetCommandParser.cpp 12 KB

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