LogCommandParser.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_LogCommandParser==1
  12. #include "LogCommandParser.h"
  13. #include "TransportInterface.h"
  14. #include <memory.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdarg.h>
  18. #include "LinuxStrings.h"
  19. #ifdef _MSC_VER
  20. #pragma warning( push )
  21. #endif
  22. using namespace RakNet;
  23. STATIC_FACTORY_DEFINITIONS(LogCommandParser,LogCommandParser);
  24. LogCommandParser::LogCommandParser()
  25. {
  26. RegisterCommand(CommandParserInterface::VARIABLE_NUMBER_OF_PARAMETERS,"Subscribe","[<ChannelName>] - Subscribes to a named channel, or all channels");
  27. RegisterCommand(CommandParserInterface::VARIABLE_NUMBER_OF_PARAMETERS,"Unsubscribe","[<ChannelName>] - Unsubscribes from a named channel, or all channels");
  28. memset(channelNames,0,sizeof(channelNames));
  29. }
  30. LogCommandParser::~LogCommandParser()
  31. {
  32. }
  33. bool LogCommandParser::OnCommand(const char *command, unsigned numParameters, char **parameterList, TransportInterface *transport, const SystemAddress &systemAddress, const char *originalString)
  34. {
  35. (void) originalString;
  36. if (strcmp(command, "Subscribe")==0)
  37. {
  38. unsigned channelIndex;
  39. if (numParameters==0)
  40. {
  41. Subscribe(systemAddress, 0);
  42. transport->Send(systemAddress, "Subscribed to all channels.\r\n");
  43. }
  44. else if (numParameters==1)
  45. {
  46. if ((channelIndex=Subscribe(systemAddress, parameterList[0]))!=(unsigned)-1)
  47. {
  48. transport->Send(systemAddress, "You are now subscribed to channel %s.\r\n", channelNames[channelIndex]);
  49. }
  50. else
  51. {
  52. transport->Send(systemAddress, "Cannot find channel %s.\r\n", parameterList[0]);
  53. PrintChannels(systemAddress, transport);
  54. }
  55. }
  56. else
  57. {
  58. transport->Send(systemAddress, "Subscribe takes either 0 or 1 parameters.\r\n");
  59. }
  60. }
  61. else if (strcmp(command, "Unsubscribe")==0)
  62. {
  63. unsigned channelIndex;
  64. if (numParameters==0)
  65. {
  66. Unsubscribe(systemAddress, 0);
  67. transport->Send(systemAddress, "Unsubscribed from all channels.\r\n");
  68. }
  69. else if (numParameters==1)
  70. {
  71. if ((channelIndex=Unsubscribe(systemAddress, parameterList[0]))!=(unsigned)-1)
  72. {
  73. transport->Send(systemAddress, "You are now unsubscribed from channel %s.\r\n", channelNames[channelIndex]);
  74. }
  75. else
  76. {
  77. transport->Send(systemAddress, "Cannot find channel %s.\r\n", parameterList[0]);
  78. PrintChannels(systemAddress, transport);
  79. }
  80. }
  81. else
  82. {
  83. transport->Send(systemAddress, "Unsubscribe takes either 0 or 1 parameters.\r\n");
  84. }
  85. }
  86. return true;
  87. }
  88. const char *LogCommandParser::GetName(void) const
  89. {
  90. return "Logger";
  91. }
  92. void LogCommandParser::SendHelp(TransportInterface *transport, const SystemAddress &systemAddress)
  93. {
  94. transport->Send(systemAddress, "The logger will accept user log data via the Log(...) function.\r\n");
  95. transport->Send(systemAddress, "Each log is associated with a named channel.\r\n");
  96. transport->Send(systemAddress, "You can subscribe to or unsubscribe from named channels.\r\n");
  97. PrintChannels(systemAddress, transport);
  98. }
  99. void LogCommandParser::AddChannel(const char *channelName)
  100. {
  101. unsigned channelIndex=0;
  102. channelIndex = GetChannelIndexFromName(channelName);
  103. // Each channel can only be added once.
  104. RakAssert(channelIndex==(unsigned)-1);
  105. unsigned i;
  106. for (i=0; i < 32; i++)
  107. {
  108. if (channelNames[i]==0)
  109. {
  110. // Assuming a persistent static string.
  111. channelNames[i]=channelName;
  112. return;
  113. }
  114. }
  115. // No more available channels - max 32 with this implementation where I save subscribed channels with bit operations
  116. RakAssert(0);
  117. }
  118. void LogCommandParser::WriteLog(const char *channelName, const char *format, ...)
  119. {
  120. if (channelName==0 || format==0)
  121. return;
  122. unsigned channelIndex;
  123. channelIndex = GetChannelIndexFromName(channelName);
  124. if (channelIndex==(unsigned)-1)
  125. {
  126. AddChannel(channelName);
  127. }
  128. char text[REMOTE_MAX_TEXT_INPUT];
  129. va_list ap;
  130. va_start(ap, format);
  131. _vsnprintf(text, REMOTE_MAX_TEXT_INPUT, format, ap);
  132. va_end(ap);
  133. text[REMOTE_MAX_TEXT_INPUT-1]=0;
  134. // Make sure that text ends in \r\n
  135. int textLen;
  136. textLen=(int)strlen(text);
  137. if (textLen==0)
  138. return;
  139. if (text[textLen-1]=='\n')
  140. {
  141. text[textLen-1]=0;
  142. }
  143. if (textLen < REMOTE_MAX_TEXT_INPUT-4)
  144. strcat(text, "\r\n");
  145. else
  146. {
  147. text[textLen-3]='\r';
  148. text[textLen-2]='\n';
  149. text[textLen-1]=0;
  150. }
  151. // For each user that subscribes to this channel, send to them.
  152. unsigned i;
  153. for (i=0; i < remoteUsers.Size(); i++)
  154. {
  155. if (remoteUsers[i].channels & (1 << channelIndex))
  156. {
  157. trans->Send(remoteUsers[i].systemAddress, text);
  158. }
  159. }
  160. }
  161. void LogCommandParser::PrintChannels(const SystemAddress &systemAddress, TransportInterface *transport) const
  162. {
  163. unsigned i;
  164. bool anyChannels=false;
  165. transport->Send(systemAddress, "CHANNELS:\r\n");
  166. for (i=0; i < 32; i++)
  167. {
  168. if (channelNames[i])
  169. {
  170. transport->Send(systemAddress, "%i. %s\r\n", i+1,channelNames[i]);
  171. anyChannels=true;
  172. }
  173. }
  174. if (anyChannels==false)
  175. transport->Send(systemAddress, "None.\r\n");
  176. }
  177. void LogCommandParser::OnNewIncomingConnection(const SystemAddress &systemAddress, TransportInterface *transport)
  178. {
  179. (void) systemAddress;
  180. (void) transport;
  181. }
  182. void LogCommandParser::OnConnectionLost(const SystemAddress &systemAddress, TransportInterface *transport)
  183. {
  184. (void) transport;
  185. Unsubscribe(systemAddress, 0);
  186. }
  187. unsigned LogCommandParser::Unsubscribe(const SystemAddress &systemAddress, const char *channelName)
  188. {
  189. unsigned i;
  190. for (i=0; i < remoteUsers.Size(); i++)
  191. {
  192. if (remoteUsers[i].systemAddress==systemAddress)
  193. {
  194. if (channelName==0)
  195. {
  196. // Unsubscribe from all and delete this user.
  197. remoteUsers[i]=remoteUsers[remoteUsers.Size()-1];
  198. remoteUsers.RemoveFromEnd();
  199. return 0;
  200. }
  201. else
  202. {
  203. unsigned channelIndex;
  204. channelIndex = GetChannelIndexFromName(channelName);
  205. if (channelIndex!=(unsigned)-1)
  206. {
  207. remoteUsers[i].channels&=0xFFFF ^ (1<<channelIndex); // Unset this bit
  208. }
  209. return channelIndex;
  210. }
  211. }
  212. }
  213. return (unsigned)-1;
  214. }
  215. unsigned LogCommandParser::Subscribe(const SystemAddress &systemAddress, const char *channelName)
  216. {
  217. unsigned i;
  218. unsigned channelIndex=(unsigned)-1;
  219. if (channelName)
  220. {
  221. channelIndex = GetChannelIndexFromName(channelName);
  222. if (channelIndex==(unsigned)-1)
  223. return channelIndex;
  224. }
  225. for (i=0; i < remoteUsers.Size(); i++)
  226. {
  227. if (remoteUsers[i].systemAddress==systemAddress)
  228. {
  229. if (channelName)
  230. remoteUsers[i].channels|=1<<channelIndex; // Set this bit for an existing user
  231. else
  232. remoteUsers[i].channels=0xFFFF;
  233. return channelIndex;
  234. }
  235. }
  236. // Make a new user
  237. SystemAddressAndChannel newUser;
  238. newUser.systemAddress = systemAddress;
  239. if (channelName)
  240. newUser.channels=1<<channelIndex;
  241. else
  242. newUser.channels=0xFFFF;
  243. remoteUsers.Insert(newUser, _FILE_AND_LINE_);
  244. return channelIndex;
  245. }
  246. unsigned LogCommandParser::GetChannelIndexFromName(const char *channelName)
  247. {
  248. unsigned i;
  249. for (i=0; i < 32; i++)
  250. {
  251. if (channelNames[i]==0)
  252. return (unsigned) -1;
  253. if (_stricmp(channelNames[i], channelName)==0)
  254. return i;
  255. }
  256. return (unsigned)-1;
  257. }
  258. void LogCommandParser::OnTransportChange(TransportInterface *transport)
  259. {
  260. // I don't want users to have to pass TransportInterface *transport to Log.
  261. trans=transport;
  262. }
  263. #ifdef _MSC_VER
  264. #pragma warning( pop )
  265. #endif
  266. #endif // _RAKNET_SUPPORT_*
粤ICP备19079148号