
00001 #include "CommandParserInterface.h" 00002 #include "TransportInterface.h" 00003 #include <string.h> 00004 #include "RakAssert.h" 00005 #include <stdio.h> 00006 #if defined(_XBOX) || defined(X360) 00007 00008 #elif defined(_WIN32) 00009 // IP_DONTFRAGMENT is different between winsock 1 and winsock 2. Therefore, Winsock2.h must be linked againt Ws2_32.lib 00010 // winsock.h must be linked against WSock32.lib. If these two are mixed up the flag won't work correctly 00011 #include <winsock2.h> 00012 #else 00013 #include <sys/socket.h> 00014 #include <netinet/in.h> 00015 #include <arpa/inet.h> 00016 #endif 00017 00018 #include "LinuxStrings.h" 00019 00020 #ifdef _MSC_VER 00021 #pragma warning( push ) 00022 #endif 00023 00024 const unsigned char CommandParserInterface::VARIABLE_NUMBER_OF_PARAMETERS=255; 00025 00026 int RegisteredCommandComp( const char* const & key, const RegisteredCommand &data ) 00027 { 00028 return _stricmp(key,data.command); 00029 } 00030 00031 CommandParserInterface::CommandParserInterface() {} 00032 CommandParserInterface::~CommandParserInterface() {} 00033 00034 void CommandParserInterface::ParseConsoleString(char *str, const char delineator, unsigned char delineatorToggle, unsigned *numParameters, char **parameterList, unsigned parameterListLength) 00035 { 00036 unsigned strIndex, parameterListIndex; 00037 unsigned strLen; 00038 bool replaceDelineator=true; 00039 00040 strLen = (unsigned) strlen(str); 00041 00042 // Replace every instance of delineator, \n, \r with 0 00043 for (strIndex=0; strIndex < strLen; strIndex++) 00044 { 00045 if (str[strIndex]==delineator && replaceDelineator) 00046 str[strIndex]=0; 00047 00048 if (str[strIndex]=='\n' || str[strIndex]=='\r') 00049 str[strIndex]=0; 00050 00051 if (str[strIndex]==delineatorToggle) 00052 { 00053 str[strIndex]=0; 00054 replaceDelineator=!replaceDelineator; 00055 } 00056 } 00057 00058 // Fill up parameterList starting at each non-0 00059 for (strIndex=0, parameterListIndex=0; strIndex < strLen; ) 00060 { 00061 if (str[strIndex]!=0) 00062 { 00063 parameterList[parameterListIndex]=str+strIndex; 00064 parameterListIndex++; 00065 RakAssert(parameterListIndex < parameterListLength); 00066 if (parameterListIndex >= parameterListLength) 00067 break; 00068 00069 strIndex++; 00070 while (str[strIndex]!=0 && strIndex < strLen) 00071 strIndex++; 00072 } 00073 else 00074 strIndex++; 00075 } 00076 00077 parameterList[parameterListIndex]=0; 00078 *numParameters=parameterListIndex; 00079 } 00080 void CommandParserInterface::SendCommandList(TransportInterface *transport, SystemAddress systemAddress) 00081 { 00082 unsigned i; 00083 if (commandList.Size()) 00084 { 00085 for (i=0; i < commandList.Size(); i++) 00086 { 00087 transport->Send(systemAddress, "%s", commandList[i].command); 00088 if (i < commandList.Size()-1) 00089 transport->Send(systemAddress, ", "); 00090 } 00091 transport->Send(systemAddress, "\r\n"); 00092 } 00093 else 00094 transport->Send(systemAddress, "No registered commands\r\n"); 00095 } 00096 void CommandParserInterface::RegisterCommand(unsigned char parameterCount, const char *command, const char *commandHelp) 00097 { 00098 RegisteredCommand rc; 00099 rc.command=command; 00100 rc.commandHelp=commandHelp; 00101 rc.parameterCount=parameterCount; 00102 commandList.Insert( command, rc, true, __FILE__, __LINE__); 00103 } 00104 bool CommandParserInterface::GetRegisteredCommand(const char *command, RegisteredCommand *rc) 00105 { 00106 bool objectExists; 00107 unsigned index; 00108 index=commandList.GetIndexFromKey(command, &objectExists); 00109 if (objectExists) 00110 *rc=commandList[index]; 00111 return objectExists; 00112 } 00113 void CommandParserInterface::OnTransportChange(TransportInterface *transport) 00114 { 00115 (void) transport; 00116 } 00117 void CommandParserInterface::OnNewIncomingConnection(SystemAddress systemAddress, TransportInterface *transport) 00118 { 00119 (void) systemAddress; 00120 (void) transport; 00121 } 00122 void CommandParserInterface::OnConnectionLost(SystemAddress systemAddress, TransportInterface *transport) 00123 { 00124 (void) systemAddress; 00125 (void) transport; 00126 } 00127 void CommandParserInterface::ReturnResult(bool res, const char *command,TransportInterface *transport, SystemAddress systemAddress) 00128 { 00129 if (res) 00130 transport->Send(systemAddress, "%s returned true.\r\n", command); 00131 else 00132 transport->Send(systemAddress, "%s returned false.\r\n", command); 00133 } 00134 void CommandParserInterface::ReturnResult(int res, const char *command,TransportInterface *transport, SystemAddress systemAddress) 00135 { 00136 transport->Send(systemAddress, "%s returned %i.\r\n", command, res); 00137 } 00138 void CommandParserInterface::ReturnResult(const char *command, TransportInterface *transport, SystemAddress systemAddress) 00139 { 00140 transport->Send(systemAddress, "Successfully called %s.\r\n", command); 00141 } 00142 void CommandParserInterface::ReturnResult(char *res, const char *command, TransportInterface *transport, SystemAddress systemAddress) 00143 { 00144 transport->Send(systemAddress, "%s returned %s.\r\n", command, res); 00145 } 00146 void CommandParserInterface::ReturnResult(SystemAddress res, const char *command, TransportInterface *transport, SystemAddress systemAddress) 00147 { 00148 #if !defined(_XBOX) && !defined(_X360) 00149 in_addr in; 00150 in.s_addr = systemAddress.binaryAddress; 00151 inet_ntoa( in ); 00152 transport->Send(systemAddress, "%s returned %s %i:%i\r\n", command,inet_ntoa( in ),res.binaryAddress, res.port); 00153 #else 00154 transport->Send(systemAddress, "%s returned %i:%i\r\n", command,res.binaryAddress, res.port); 00155 #endif 00156 } 00157 SystemAddress CommandParserInterface::IntegersToSystemAddress(int binaryAddress, int port) 00158 { 00159 SystemAddress systemAddress; 00160 systemAddress.binaryAddress=binaryAddress; 00161 systemAddress.port=(unsigned short)port; 00162 return systemAddress; 00163 } 00164 00165 #ifdef _MSC_VER 00166 #pragma warning( pop ) 00167 #endif 00168
Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.