CommandParserInterface.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "CommandParserInterface.h"
  11. #include "TransportInterface.h"
  12. #include <string.h>
  13. #include "RakAssert.h"
  14. #include <stdio.h>
  15. #if defined(WINDOWS_STORE_RT)
  16. #elif defined(_WIN32)
  17. // IP_DONTFRAGMENT is different between winsock 1 and winsock 2. Therefore, Winsock2.h must be linked againt Ws2_32.lib
  18. // winsock.h must be linked against WSock32.lib. If these two are mixed up the flag won't work correctly
  19. #include <winsock2.h>
  20. #else
  21. #include <sys/socket.h>
  22. #include <netinet/in.h>
  23. #include <arpa/inet.h>
  24. #endif
  25. #include "LinuxStrings.h"
  26. using namespace RakNet;
  27. #ifdef _MSC_VER
  28. #pragma warning( push )
  29. #endif
  30. const unsigned char CommandParserInterface::VARIABLE_NUMBER_OF_PARAMETERS=255;
  31. int RakNet::RegisteredCommandComp( const char* const & key, const RegisteredCommand &data )
  32. {
  33. return _stricmp(key,data.command);
  34. }
  35. CommandParserInterface::CommandParserInterface() {}
  36. CommandParserInterface::~CommandParserInterface() {}
  37. void CommandParserInterface::ParseConsoleString(char *str, const char delineator, unsigned char delineatorToggle, unsigned *numParameters, char **parameterList, unsigned parameterListLength)
  38. {
  39. unsigned strIndex, parameterListIndex;
  40. unsigned strLen;
  41. bool replaceDelineator=true;
  42. strLen = (unsigned) strlen(str);
  43. // Replace every instance of delineator, \n, \r with 0
  44. for (strIndex=0; strIndex < strLen; strIndex++)
  45. {
  46. if (str[strIndex]==delineator && replaceDelineator)
  47. str[strIndex]=0;
  48. if (str[strIndex]=='\n' || str[strIndex]=='\r')
  49. str[strIndex]=0;
  50. if (str[strIndex]==delineatorToggle)
  51. {
  52. str[strIndex]=0;
  53. replaceDelineator=!replaceDelineator;
  54. }
  55. }
  56. // Fill up parameterList starting at each non-0
  57. for (strIndex=0, parameterListIndex=0; strIndex < strLen; )
  58. {
  59. if (str[strIndex]!=0)
  60. {
  61. parameterList[parameterListIndex]=str+strIndex;
  62. parameterListIndex++;
  63. RakAssert(parameterListIndex < parameterListLength);
  64. if (parameterListIndex >= parameterListLength)
  65. break;
  66. strIndex++;
  67. while (str[strIndex]!=0 && strIndex < strLen)
  68. strIndex++;
  69. }
  70. else
  71. strIndex++;
  72. }
  73. parameterList[parameterListIndex]=0;
  74. *numParameters=parameterListIndex;
  75. }
  76. void CommandParserInterface::SendCommandList(TransportInterface *transport, const SystemAddress &systemAddress)
  77. {
  78. unsigned i;
  79. if (commandList.Size())
  80. {
  81. for (i=0; i < commandList.Size(); i++)
  82. {
  83. transport->Send(systemAddress, "%s", commandList[i].command);
  84. if (i < commandList.Size()-1)
  85. transport->Send(systemAddress, ", ");
  86. }
  87. transport->Send(systemAddress, "\r\n");
  88. }
  89. else
  90. transport->Send(systemAddress, "No registered commands\r\n");
  91. }
  92. void CommandParserInterface::RegisterCommand(unsigned char parameterCount, const char *command, const char *commandHelp)
  93. {
  94. RegisteredCommand rc;
  95. rc.command=command;
  96. rc.commandHelp=commandHelp;
  97. rc.parameterCount=parameterCount;
  98. commandList.Insert( command, rc, true, _FILE_AND_LINE_);
  99. }
  100. bool CommandParserInterface::GetRegisteredCommand(const char *command, RegisteredCommand *rc)
  101. {
  102. bool objectExists;
  103. unsigned index;
  104. index=commandList.GetIndexFromKey(command, &objectExists);
  105. if (objectExists)
  106. *rc=commandList[index];
  107. return objectExists;
  108. }
  109. void CommandParserInterface::OnTransportChange(TransportInterface *transport)
  110. {
  111. (void) transport;
  112. }
  113. void CommandParserInterface::OnNewIncomingConnection(const SystemAddress &systemAddress, TransportInterface *transport)
  114. {
  115. (void) systemAddress;
  116. (void) transport;
  117. }
  118. void CommandParserInterface::OnConnectionLost(const SystemAddress &systemAddress, TransportInterface *transport)
  119. {
  120. (void) systemAddress;
  121. (void) transport;
  122. }
  123. void CommandParserInterface::ReturnResult(bool res, const char *command,TransportInterface *transport, const SystemAddress &systemAddress)
  124. {
  125. if (res)
  126. transport->Send(systemAddress, "%s returned true.\r\n", command);
  127. else
  128. transport->Send(systemAddress, "%s returned false.\r\n", command);
  129. }
  130. void CommandParserInterface::ReturnResult(int res, const char *command,TransportInterface *transport, const SystemAddress &systemAddress)
  131. {
  132. transport->Send(systemAddress, "%s returned %i.\r\n", command, res);
  133. }
  134. void CommandParserInterface::ReturnResult(const char *command, TransportInterface *transport, const SystemAddress &systemAddress)
  135. {
  136. transport->Send(systemAddress, "Successfully called %s.\r\n", command);
  137. }
  138. void CommandParserInterface::ReturnResult(char *res, const char *command, TransportInterface *transport, const SystemAddress &systemAddress)
  139. {
  140. transport->Send(systemAddress, "%s returned %s.\r\n", command, res);
  141. }
  142. void CommandParserInterface::ReturnResult(SystemAddress res, const char *command, TransportInterface *transport, const SystemAddress &systemAddress)
  143. {
  144. char addr[128];
  145. systemAddress.ToString(false,addr);
  146. char addr2[128];
  147. res.ToString(false,addr2);
  148. transport->Send(systemAddress, "%s returned %s %s:%i\r\n", command,addr,addr2,res.GetPort());
  149. }
  150. #ifdef _MSC_VER
  151. #pragma warning( pop )
  152. #endif
粤ICP备19079148号