00001 #include "NativeFeatureIncludes.h"
00002 #if _RAKNET_SUPPORT_RakNetCommandParser==1
00003
00004 #include "RakNetCommandParser.h"
00005 #include "TransportInterface.h"
00006 #include "RakPeerInterface.h"
00007 #include "BitStream.h"
00008 #include "RakAssert.h"
00009 #include <stdio.h>
00010 #include <string.h>
00011 #include <stdlib.h>
00012
00013 #ifdef _MSC_VER
00014 #pragma warning( push )
00015 #endif
00016
00017 RakNetCommandParser::RakNetCommandParser()
00018 {
00019 RegisterCommand(4, "Startup","( unsigned short maxConnections, int _threadSleepTimer, unsigned short localPort, const char *forceHostAddress );");
00020 RegisterCommand(0,"InitializeSecurity","();");
00021 RegisterCommand(0,"DisableSecurity","( void );");
00022 RegisterCommand(1,"AddToSecurityExceptionList","( const char *ip );");
00023 RegisterCommand(1,"RemoveFromSecurityExceptionList","( const char *ip );");
00024 RegisterCommand(1,"IsInSecurityExceptionList","( const char *ip );");
00025 RegisterCommand(1,"SetMaximumIncomingConnections","( unsigned short numberAllowed );");
00026 RegisterCommand(0,"GetMaximumIncomingConnections","( void ) const;");
00027 RegisterCommand(4,"Connect","( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength );");
00028 RegisterCommand(2,"Disconnect","( unsigned int blockDuration, unsigned char orderingChannel=0 );");
00029 RegisterCommand(0,"IsActive","( void ) const;");
00030 RegisterCommand(0,"GetConnectionList","() const;");
00031 RegisterCommand(4,"CloseConnection","( const SystemAddress target, bool sendDisconnectionNotification, unsigned char orderingChannel=0 );");
00032 RegisterCommand(2,"IsConnected","( );");
00033 RegisterCommand(2,"GetIndexFromSystemAddress","( const SystemAddress systemAddress );");
00034 RegisterCommand(1,"GetSystemAddressFromIndex","( int index );");
00035 RegisterCommand(2,"AddToBanList","( const char *IP, RakNetTime milliseconds=0 );");
00036 RegisterCommand(1,"RemoveFromBanList","( const char *IP );");
00037 RegisterCommand(0,"ClearBanList","( void );");
00038 RegisterCommand(1,"IsBanned","( const char *IP );");
00039 RegisterCommand(2,"Ping1","( const SystemAddress target );");
00040 RegisterCommand(3,"Ping2","( const char* host, unsigned short remotePort, bool onlyReplyOnAcceptingConnections );");
00041 RegisterCommand(2,"GetAveragePing","( const SystemAddress systemAddress );");
00042 RegisterCommand(2,"GetLastPing","( const SystemAddress systemAddress ) const;");
00043 RegisterCommand(2,"GetLowestPing","( const SystemAddress systemAddress ) const;");
00044 RegisterCommand(1,"SetOccasionalPing","( bool doPing );");
00045 RegisterCommand(2,"SetOfflinePingResponse","( const char *data, const unsigned int length );");
00046 RegisterCommand(0,"GetInternalID","( void ) const;");
00047 RegisterCommand(2,"GetExternalID","( const SystemAddress target ) const;");
00048 RegisterCommand(3,"SetTimeoutTime","( RakNetTime timeMS, const SystemAddress target );");
00049 RegisterCommand(1,"SetMTUSize","( int size );");
00050 RegisterCommand(0,"GetMTUSize","( void ) const;");
00051 RegisterCommand(0,"GetNumberOfAddresses","( void );");
00052 RegisterCommand(1,"GetLocalIP","( unsigned int index );");
00053 RegisterCommand(1,"AllowConnectionResponseIPMigration","( bool allow );");
00054 RegisterCommand(4,"AdvertiseSystem","( const char *host, unsigned short remotePort, const char *data, int dataLength );");
00055 RegisterCommand(2,"SetIncomingPassword","( const char* passwordData, int passwordDataLength );");
00056 RegisterCommand(0,"GetIncomingPassword","( void );");
00057 RegisterCommand(3,"ApplyNetworkSimulator","( float packetloss, unsigned short minExtraPing, unsigned short extraPingVariance);");
00058 RegisterCommand(0,"IsNetworkSimulatorActive","( void );");
00059 }
00060 RakNetCommandParser::~RakNetCommandParser()
00061 {
00062 }
00063 void RakNetCommandParser::SetRakPeerInterface(RakPeerInterface *rakPeer)
00064 {
00065 peer=rakPeer;
00066 }
00067 bool RakNetCommandParser::OnCommand(const char *command, unsigned numParameters, char **parameterList, TransportInterface *transport, SystemAddress systemAddress, const char *originalString)
00068 {
00069 (void) originalString;
00070 (void) numParameters;
00071
00072 if (peer==0)
00073 return false;
00074
00075 if (strcmp(command, "Startup")==0)
00076 {
00077 SocketDescriptor socketDescriptor((unsigned short)atoi(parameterList[1]), parameterList[3]);
00078 ReturnResult(peer->Startup((unsigned short)atoi(parameterList[0]), atoi(parameterList[2]), &socketDescriptor, 1), command, transport, systemAddress);
00079 }
00080 else if (strcmp(command, "InitializeSecurity")==0)
00081 {
00082 peer->InitializeSecurity(0,0,0,0);
00083 ReturnResult(command, transport, systemAddress);
00084 }
00085 else if (strcmp(command, "DisableSecurity")==0)
00086 {
00087 peer->DisableSecurity();
00088 ReturnResult(command, transport, systemAddress);
00089 }
00090 else if (strcmp(command, "AddToSecurityExceptionList")==0)
00091 {
00092 peer->AddToSecurityExceptionList(parameterList[1]);
00093 ReturnResult(command, transport, systemAddress);
00094 }
00095 else if (strcmp(command, "RemoveFromSecurityExceptionList")==0)
00096 {
00097 peer->RemoveFromSecurityExceptionList(parameterList[1]);
00098 ReturnResult(command, transport, systemAddress);
00099 }
00100 else if (strcmp(command, "IsInSecurityExceptionList")==0)
00101 {
00102 ReturnResult(peer->IsInSecurityExceptionList(parameterList[1]),command, transport, systemAddress);
00103 }
00104 else if (strcmp(command, "SetMaximumIncomingConnections")==0)
00105 {
00106 peer->SetMaximumIncomingConnections((unsigned short)atoi(parameterList[0]));
00107 ReturnResult(command, transport, systemAddress);
00108 }
00109 else if (strcmp(command, "GetMaximumIncomingConnections")==0)
00110 {
00111 ReturnResult(peer->GetMaximumIncomingConnections(), command, transport, systemAddress);
00112 }
00113 else if (strcmp(command, "Connect")==0)
00114 {
00115 ReturnResult(peer->Connect(parameterList[0], (unsigned short)atoi(parameterList[1]),parameterList[2],atoi(parameterList[3])), command, transport, systemAddress);
00116 }
00117 else if (strcmp(command, "Disconnect")==0)
00118 {
00119 peer->Shutdown(atoi(parameterList[0]), (unsigned char)atoi(parameterList[1]));
00120 ReturnResult(command, transport, systemAddress);
00121 }
00122 else if (strcmp(command, "IsActive")==0)
00123 {
00124 ReturnResult(peer->IsActive(), command, transport, systemAddress);
00125 }
00126 else if (strcmp(command, "GetConnectionList")==0)
00127 {
00128 SystemAddress remoteSystems[32];
00129 unsigned short count=32;
00130 unsigned i;
00131 if (peer->GetConnectionList(remoteSystems, &count))
00132 {
00133 if (count==0)
00134 {
00135 transport->Send(systemAddress, "GetConnectionList() returned no systems connected.\r\n");
00136 }
00137 else
00138 {
00139 transport->Send(systemAddress, "GetConnectionList() returned:\r\n");
00140 for (i=0; i < count; i++)
00141 {
00142 char str1[64];
00143 remoteSystems[i].ToString(false, str1);
00144 transport->Send(systemAddress, "%i %s %i:%i\r\n", i, str1, remoteSystems[i].binaryAddress, remoteSystems[i].port);
00145 }
00146 }
00147 }
00148 else
00149 transport->Send(systemAddress, "GetConnectionList() returned false.\r\n");
00150 }
00151 else if (strcmp(command, "CloseConnection")==0)
00152 {
00153 peer->CloseConnection(IntegersToSystemAddress(atoi(parameterList[0]), atoi(parameterList[1])),atoi(parameterList[2])!=0,(unsigned char)atoi(parameterList[3]));
00154 ReturnResult(command, transport, systemAddress);
00155 }
00156 else if (strcmp(command, "IsConnected")==0)
00157 {
00158 ReturnResult(peer->IsConnected(IntegersToSystemAddress(atoi(parameterList[0]), atoi(parameterList[1]))), command, transport, systemAddress);
00159 }
00160 else if (strcmp(command, "GetIndexFromSystemAddress")==0)
00161 {
00162 ReturnResult(peer->GetIndexFromSystemAddress(IntegersToSystemAddress(atoi(parameterList[0]), atoi(parameterList[1]))), command, transport, systemAddress);
00163 }
00164 else if (strcmp(command, "GetSystemAddressFromIndex")==0)
00165 {
00166 ReturnResult(peer->GetSystemAddressFromIndex(atoi(parameterList[0])), command, transport, systemAddress);
00167 }
00168 else if (strcmp(command, "AddToBanList")==0)
00169 {
00170 peer->AddToBanList(parameterList[0], atoi(parameterList[1]));
00171 ReturnResult(command, transport, systemAddress);
00172 }
00173 else if (strcmp(command, "RemoveFromBanList")==0)
00174 {
00175 peer->RemoveFromBanList(parameterList[0]);
00176 ReturnResult(command, transport, systemAddress);
00177 }
00178 else if (strcmp(command, "ClearBanList")==0)
00179 {
00180 peer->ClearBanList();
00181 ReturnResult(command, transport, systemAddress);
00182 }
00183 else if (strcmp(command, "IsBanned")==0)
00184 {
00185 ReturnResult(peer->IsBanned(parameterList[0]), command, transport, systemAddress);
00186 }
00187 else if (strcmp(command, "Ping1")==0)
00188 {
00189 peer->Ping(IntegersToSystemAddress(atoi(parameterList[0]), atoi(parameterList[1])));
00190 ReturnResult(command, transport, systemAddress);
00191 }
00192 else if (strcmp(command, "Ping2")==0)
00193 {
00194 peer->Ping(parameterList[0], (unsigned short) atoi(parameterList[1]), atoi(parameterList[2])!=0);
00195 ReturnResult(command, transport, systemAddress);
00196 }
00197 else if (strcmp(command, "GetAveragePing")==0)
00198 {
00199 ReturnResult(peer->GetAveragePing(IntegersToSystemAddress(atoi(parameterList[0]), atoi(parameterList[1]))), command, transport, systemAddress);
00200 }
00201 else if (strcmp(command, "GetLastPing")==0)
00202 {
00203 ReturnResult(peer->GetLastPing(IntegersToSystemAddress(atoi(parameterList[0]), atoi(parameterList[1]))), command, transport, systemAddress);
00204 }
00205 else if (strcmp(command, "GetLowestPing")==0)
00206 {
00207 ReturnResult(peer->GetLowestPing(IntegersToSystemAddress(atoi(parameterList[0]), atoi(parameterList[1]))), command, transport, systemAddress);
00208 }
00209 else if (strcmp(command, "SetOccasionalPing")==0)
00210 {
00211 peer->SetOccasionalPing(atoi(parameterList[0])!=0);
00212 ReturnResult(command, transport, systemAddress);
00213 }
00214 else if (strcmp(command, "SetOfflinePingResponse")==0)
00215 {
00216 peer->SetOfflinePingResponse(parameterList[0], atoi(parameterList[1]));
00217 ReturnResult(command, transport, systemAddress);
00218 }
00219 else if (strcmp(command, "GetInternalID")==0)
00220 {
00221 ReturnResult(peer->GetInternalID(), command, transport, systemAddress);
00222 }
00223 else if (strcmp(command, "GetExternalID")==0)
00224 {
00225 ReturnResult(peer->GetExternalID(IntegersToSystemAddress(atoi(parameterList[0]), atoi(parameterList[1]))), command, transport, systemAddress);
00226 }
00227 else if (strcmp(command, "SetTimeoutTime")==0)
00228 {
00229 peer->SetTimeoutTime(atoi(parameterList[0]), IntegersToSystemAddress(atoi(parameterList[0]), atoi(parameterList[1])));
00230 ReturnResult(command, transport, systemAddress);
00231 }
00232
00233
00234
00235
00236
00237
00238 else if (strcmp(command, "GetMTUSize")==0)
00239 {
00240 ReturnResult(peer->GetMTUSize(UNASSIGNED_SYSTEM_ADDRESS), command, transport, systemAddress);
00241 }
00242 else if (strcmp(command, "GetNumberOfAddresses")==0)
00243 {
00244 ReturnResult((int)peer->GetNumberOfAddresses(), command, transport, systemAddress);
00245 }
00246 else if (strcmp(command, "GetLocalIP")==0)
00247 {
00248 ReturnResult((char*) peer->GetLocalIP(atoi(parameterList[0])), command, transport, systemAddress);
00249 }
00250 else if (strcmp(command, "AllowConnectionResponseIPMigration")==0)
00251 {
00252 peer->AllowConnectionResponseIPMigration(atoi(parameterList[0])!=0);
00253 ReturnResult(command, transport, systemAddress);
00254 }
00255 else if (strcmp(command, "AdvertiseSystem")==0)
00256 {
00257 peer->AdvertiseSystem(parameterList[0], (unsigned short) atoi(parameterList[1]),parameterList[2],atoi(parameterList[3]));
00258 ReturnResult(command, transport, systemAddress);
00259 }
00260 else if (strcmp(command, "ApplyNetworkSimulator")==0)
00261 {
00262 peer->ApplyNetworkSimulator((float) atof(parameterList[0]), (unsigned short) atoi(parameterList[1]),(unsigned short) atoi(parameterList[2]));
00263 ReturnResult(command, transport, systemAddress);
00264 }
00265 else if (strcmp(command, "IsNetworkSimulatorActive")==0)
00266 {
00267 ReturnResult(peer->IsNetworkSimulatorActive(), command, transport, systemAddress);
00268 }
00269 else if (strcmp(command, "SetIncomingPassword")==0)
00270 {
00271 peer->SetIncomingPassword(parameterList[0], atoi(parameterList[1]));
00272 ReturnResult(command, transport, systemAddress);
00273 }
00274 else if (strcmp(command, "GetIncomingPassword")==0)
00275 {
00276 char password[256];
00277 int passwordLength;
00278 peer->GetIncomingPassword(password, &passwordLength);
00279 if (passwordLength)
00280 ReturnResult((char*)password, command, transport, systemAddress);
00281 else
00282 ReturnResult(0, command, transport, systemAddress);
00283 }
00284
00285 return true;
00286 }
00287 const char *RakNetCommandParser::GetName(void) const
00288 {
00289 return "RakNet";
00290 }
00291 void RakNetCommandParser::SendHelp(TransportInterface *transport, SystemAddress systemAddress)
00292 {
00293 if (peer)
00294 {
00295 transport->Send(systemAddress, "The RakNet parser provides mirror functions to RakPeer\r\n");
00296 transport->Send(systemAddress, "SystemAddresss take two parameters: send <BinaryAddress> <Port>.\r\n");
00297 transport->Send(systemAddress, "For bool, send 1 or 0.\r\n");
00298 }
00299 else
00300 {
00301 transport->Send(systemAddress, "Parser not active. Call SetRakPeerInterface.\r\n");
00302 }
00303 }
00304
00305 #ifdef _MSC_VER
00306 #pragma warning( pop )
00307 #endif
00308
00309 #endif // _RAKNET_SUPPORT_*