RemoteCommandEvent.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "network/connectionProtocol.h"
  24. #include "io/bitStream.h"
  25. #include "sim/simBase.h"
  26. #include "console/console.h"
  27. #include "console/consoleTypes.h"
  28. #include "network/netConnection.h"
  29. #include "network/netObject.h"
  30. #include "game/gameConnection.h"
  31. #include "network/serverQuery.h"
  32. //----------------------------------------------------------------
  33. // remote procedure call console functions
  34. //----------------------------------------------------------------
  35. class RemoteCommandEvent : public NetEvent
  36. {
  37. public:
  38. enum {
  39. MaxRemoteCommandArgs = 20,
  40. CommandArgsBits = 5
  41. };
  42. private:
  43. S32 mArgc;
  44. char *mArgv[MaxRemoteCommandArgs + 1];
  45. NetStringHandle mTagv[MaxRemoteCommandArgs + 1];
  46. static char mBuf[1024];
  47. public:
  48. RemoteCommandEvent(S32 argc=0, const char **argv=NULL, NetConnection *conn = NULL)
  49. {
  50. mArgc = argc;
  51. for(S32 i = 0; i < argc; i++)
  52. {
  53. if(argv[i][0] == StringTagPrefixByte)
  54. {
  55. char buffer[256];
  56. mTagv[i+1] = NetStringHandle(dAtoi(argv[i]+1));
  57. if(conn)
  58. {
  59. dSprintf(buffer + 1, sizeof(buffer) - 1, "%d", conn->getNetSendId(mTagv[i+1]));
  60. buffer[0] = StringTagPrefixByte;
  61. mArgv[i+1] = dStrdup(buffer);
  62. }
  63. }
  64. else
  65. mArgv[i+1] = dStrdup(argv[i]);
  66. }
  67. }
  68. #ifdef TORQUE_DEBUG_NET
  69. const char *getDebugName()
  70. {
  71. static char buffer[256];
  72. dSprintf(buffer, sizeof(buffer), "%s [%s]", getClassName(), gNetStringTable->lookupString(dAtoi(mArgv[1] + 1)) );
  73. return buffer;
  74. }
  75. #endif
  76. ~RemoteCommandEvent()
  77. {
  78. for(S32 i = 0; i < mArgc; i++)
  79. dFree(mArgv[i+1]);
  80. }
  81. virtual void pack(NetConnection* conn, BitStream *bstream)
  82. {
  83. bstream->writeInt(mArgc, CommandArgsBits);
  84. // write it out reversed... why?
  85. // automatic string substitution with later arguments -
  86. // handled automatically by the system.
  87. for(S32 i = 0; i < mArgc; i++)
  88. conn->packString(bstream, mArgv[i+1]);
  89. }
  90. virtual void write(NetConnection* conn, BitStream *bstream)
  91. {
  92. pack(conn, bstream);
  93. }
  94. virtual void unpack(NetConnection* conn, BitStream *bstream)
  95. {
  96. mArgc = bstream->readInt(CommandArgsBits);
  97. // read it out backwards
  98. for(S32 i = 0; i < mArgc; i++)
  99. {
  100. conn->unpackString(bstream, mBuf);
  101. mArgv[i+1] = dStrdup(mBuf);
  102. }
  103. }
  104. virtual void process(NetConnection *conn)
  105. {
  106. static char idBuf[10];
  107. // de-tag the command name
  108. for(S32 i = mArgc - 1; i >= 0; i--)
  109. {
  110. char *arg = mArgv[i+1];
  111. if(*arg == StringTagPrefixByte)
  112. {
  113. // it's a tag:
  114. U32 localTag = dAtoi(arg + 1);
  115. NetStringHandle tag = conn->translateRemoteStringId(localTag);
  116. NetStringTable::expandString( tag,
  117. mBuf,
  118. sizeof(mBuf),
  119. (mArgc - 1) - i,
  120. (const char**)(mArgv + i + 2) );
  121. dFree(mArgv[i+1]);
  122. mArgv[i+1] = dStrdup(mBuf);
  123. }
  124. }
  125. const char *rmtCommandName = dStrchr(mArgv[1], ' ') + 1;
  126. if(conn->isConnectionToServer())
  127. {
  128. dStrcpy(mBuf, "clientCmd");
  129. dStrcat(mBuf, rmtCommandName);
  130. char *temp = mArgv[1];
  131. mArgv[1] = mBuf;
  132. Con::execute(mArgc, (const char **) mArgv+1);
  133. mArgv[1] = temp;
  134. }
  135. else
  136. {
  137. dStrcpy(mBuf, "serverCmd");
  138. dStrcat(mBuf, rmtCommandName);
  139. char *temp = mArgv[1];
  140. dSprintf(idBuf, sizeof(idBuf), "%d", conn->getId());
  141. mArgv[0] = mBuf;
  142. mArgv[1] = idBuf;
  143. Con::execute(mArgc+1, (const char **) mArgv);
  144. mArgv[1] = temp;
  145. }
  146. }
  147. DECLARE_CONOBJECT(RemoteCommandEvent);
  148. };
  149. char RemoteCommandEvent::mBuf[1024];
  150. IMPLEMENT_CO_NETEVENT_V1(RemoteCommandEvent);
  151. static void sendRemoteCommand(NetConnection *conn, S32 argc, const char **argv)
  152. {
  153. if(U8(argv[0][0]) != StringTagPrefixByte)
  154. {
  155. Con::errorf(ConsoleLogEntry::Script, "Remote Command Error - command must be a tag.");
  156. return;
  157. }
  158. S32 i;
  159. for(i = argc - 1; i >= 0; i--)
  160. {
  161. if(argv[i][0] != 0)
  162. break;
  163. argc = i;
  164. }
  165. for(i = 0; i < argc; i++)
  166. conn->validateSendString(argv[i]);
  167. RemoteCommandEvent *cevt = new RemoteCommandEvent(argc, argv, conn);
  168. conn->postNetEvent(cevt);
  169. }
  170. ConsoleFunctionGroupBegin( Net, "Functions for use with the network; tagged strings and remote commands.");
  171. ConsoleFunction( commandToServer, void, 2, RemoteCommandEvent::MaxRemoteCommandArgs + 1, "( func [ , arg1, ... , argn ] ) Use the commandToServer function to issue a remote procedure call the server.\n"
  172. "All arguments may be in tagged or non-tagged format. See 'Remote Procedure Call Samples' below\n"
  173. "@param func The suffix of the remote procedure name to be executed on the client.\n"
  174. "@param arg1 ... argn - Optional arguments to be passed to the remote procedure.\n"
  175. "@return No return value.")
  176. {
  177. NetConnection *conn = NetConnection::getConnectionToServer();
  178. if(!conn)
  179. return;
  180. sendRemoteCommand(conn, argc - 1, argv + 1);
  181. }
  182. ConsoleFunction( commandToClient, void, 3, RemoteCommandEvent::MaxRemoteCommandArgs + 2, "( client, func [ , arg1, ... , argn ] ) Use the commandToClient function to issue a remote procedure call on a client.\n"
  183. "All arguments (excluding client) may be in tagged or non-tagged format. See 'Remote Procedure Call Samples' below\n"
  184. "@param client The numeric ID of a client gameConnection.\n"
  185. "@param func The suffix of the remote procedure name to be executed on the client.\n"
  186. "@param arg1 ... argn - Optional arguments to be passed to the remote procedure.\n"
  187. "@return No return value.")
  188. {
  189. NetConnection *conn;
  190. if(!Sim::findObject(argv[1], conn))
  191. return;
  192. sendRemoteCommand(conn, argc - 2, argv + 2);
  193. }
  194. ConsoleFunction(removeTaggedString, void, 2, 2, "( tag ) Use the removeTaggedSTring function to remove a previously tagged string from the NetStringTable.\n"
  195. "@param tag A number tag ID.\n"
  196. "@return No return value")
  197. {
  198. gNetStringTable->removeString(dAtoi(argv[1]+1), true);
  199. }
  200. ConsoleFunction( addTaggedString, const char*, 2, 2, "( string ) Use the addTaggedString function to tag a new string and add it to the NetStringTable.\n"
  201. "@param string The string to tagged and placed in the NetStringTable. Tagging ignores case, so tagging the same string (excluding case differences) will be ignored as a duplicated tag.\n"
  202. "@return Returns a string (containing a numeric value) equivalent to the string ID for the newly tagged string")
  203. {
  204. NetStringHandle s(argv[1]);
  205. gNetStringTable->incStringRefScript(s.getIndex());
  206. char *ret = Con::getReturnBuffer(10);
  207. ret[0] = StringTagPrefixByte;
  208. dSprintf(ret + 1, 9, "%d", s.getIndex());
  209. return ret;
  210. }
  211. ConsoleFunction( getTaggedString, const char*, 2, 2, "( tag ) Use the getTaggedString function to convert a tag to a string. This is not the same a detag() which can only be used within the context of a function that receives a tag. This function can be used any time and anywhere to convert a tag to a string.\n"
  212. "@param tag A numeric tag ID.\n"
  213. "@return Returns the string corresponding to the tag ID")
  214. {
  215. const char *indexPtr = argv[1];
  216. if (*indexPtr == StringTagPrefixByte)
  217. indexPtr++;
  218. return gNetStringTable->lookupString(dAtoi(indexPtr));
  219. }
  220. ConsoleFunction( buildTaggedString, const char*, 2, 11, "( format , <arg1, ...arg9> ) Use the buildTaggedString function to build a tagged string using the specified format.\n"
  221. "@param enable A boolean value. If set to true, network packet logging is enabled, otherwise it is disabled.\n"
  222. "@return No return value")
  223. {
  224. const char *indexPtr = argv[1];
  225. if (*indexPtr == StringTagPrefixByte)
  226. indexPtr++;
  227. const char *fmtString = gNetStringTable->lookupString(dAtoi(indexPtr));
  228. char *strBuffer = Con::getReturnBuffer(512);
  229. const char *fmtStrPtr = fmtString;
  230. char *strBufPtr = strBuffer;
  231. S32 strMaxLength = 511;
  232. if (!fmtString)
  233. goto done;
  234. //build the string
  235. while (*fmtStrPtr)
  236. {
  237. //look for an argument tag
  238. if (*fmtStrPtr == '%')
  239. {
  240. if (fmtStrPtr[1] >= '1' && fmtStrPtr[1] <= '9')
  241. {
  242. S32 argIndex = S32(fmtStrPtr[1] - '0') + 1;
  243. if (argIndex >= argc)
  244. goto done;
  245. const char *argStr = argv[argIndex];
  246. if (!argStr)
  247. goto done;
  248. S32 strLength = dStrlen(argStr);
  249. if (strLength > strMaxLength)
  250. goto done;
  251. dStrcpy(strBufPtr, argStr);
  252. strBufPtr += strLength;
  253. strMaxLength -= strLength;
  254. fmtStrPtr += 2;
  255. continue;
  256. }
  257. }
  258. //if we don't continue, just copy the character
  259. if (strMaxLength <= 0)
  260. goto done;
  261. *strBufPtr++ = *fmtStrPtr++;
  262. strMaxLength--;
  263. }
  264. done:
  265. *strBufPtr = '\0';
  266. return strBuffer;
  267. }
  268. ConsoleFunctionGroupEnd( Net );