SQLiteClientLoggerPlugin.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #include "SQLiteClientLoggerPlugin.h"
  2. #include "MessageIdentifiers.h"
  3. #include "PacketizedTCP.h"
  4. #include "GetTime.h"
  5. static const char COLUMN_NAMES_DELIMITER=',';
  6. static const int MAX_COLUMN_NAMES_LENGTH=512;
  7. using namespace RakNet;
  8. SQLiteClientLoggerPlugin* SQLiteClientLoggerPlugin::logger;
  9. SQLiteClientLoggerPlugin::SQLiteClientLoggerPlugin()
  10. {
  11. logger=this;
  12. tickCount=0;
  13. recursiveCheck=false;
  14. memoryConstraint=0;
  15. }
  16. SQLiteClientLoggerPlugin::~SQLiteClientLoggerPlugin()
  17. {
  18. if (logger==this)
  19. logger=0;
  20. }
  21. void SQLiteClientLoggerPlugin::SetServerParameters(const SystemAddress &systemAddress, RakNet::RakString _dbIdentifier)
  22. {
  23. serverAddress=systemAddress;
  24. dbIdentifier=_dbIdentifier;
  25. }
  26. void SQLiteClientLoggerPlugin::SetMemoryConstraint(unsigned int constraint)
  27. {
  28. memoryConstraint=constraint;
  29. }
  30. void SQLiteClientLoggerPlugin::IncrementAutoTickCount(void)
  31. {
  32. tickCount++;
  33. }
  34. SQLLogResult SQLiteClientLoggerPlugin::SqlLog( bool isFunctionCall, const char *tableName, const char *columnNames, const char *file, const int line, const ParameterListHelper &parameterList )
  35. {
  36. if (recursiveCheck==true)
  37. return SQLLR_RECURSION;
  38. recursiveCheck=true;
  39. RakNet::BitStream bitStream;
  40. SerializeHeader(&bitStream, isFunctionCall, tableName, columnNames, file, line, parameterList.paramCount);
  41. // int i;
  42. //for (i=0; i < parameterList.paramCount; i++)
  43. // parameterList.parms[i].Serialize(&bitStream);
  44. if (parameterList.paramCount>=1)
  45. parameterList.p0.Serialize(&bitStream);
  46. if (parameterList.paramCount>=2)
  47. parameterList.p1.Serialize(&bitStream);
  48. if (parameterList.paramCount>=3)
  49. parameterList.p2.Serialize(&bitStream);
  50. if (parameterList.paramCount>=4)
  51. parameterList.p3.Serialize(&bitStream);
  52. if (parameterList.paramCount>=5)
  53. parameterList.p4.Serialize(&bitStream);
  54. if (parameterList.paramCount>=6)
  55. parameterList.p5.Serialize(&bitStream);
  56. if (parameterList.paramCount>=7)
  57. parameterList.p6.Serialize(&bitStream);
  58. if (parameterList.paramCount>=8)
  59. parameterList.p7.Serialize(&bitStream);
  60. if (parameterList.paramCount>=9)
  61. parameterList.p8.Serialize(&bitStream);
  62. if (parameterList.paramCount>=10)
  63. parameterList.p9.Serialize(&bitStream);
  64. if (parameterList.paramCount>=11)
  65. parameterList.p10.Serialize(&bitStream);
  66. if (parameterList.paramCount>=12)
  67. parameterList.p11.Serialize(&bitStream);
  68. if (parameterList.paramCount>=13)
  69. parameterList.p12.Serialize(&bitStream);
  70. if (parameterList.paramCount>=14)
  71. parameterList.p13.Serialize(&bitStream);
  72. if (parameterList.paramCount>=15)
  73. parameterList.p14.Serialize(&bitStream);
  74. if (memoryConstraint!=0 && tcpInterface)
  75. {
  76. if (tcpInterface->GetOutgoingDataBufferSize(serverAddress)+bitStream.GetNumberOfBytesUsed()>=memoryConstraint)
  77. {
  78. recursiveCheck=false;
  79. return SQLLR_WOULD_EXCEED_MEMORY_CONSTRAINT;
  80. }
  81. }
  82. SendUnified(&bitStream, LOW_PRIORITY, RELIABLE_ORDERED, 1, serverAddress, false);
  83. recursiveCheck=false;
  84. return SQLLR_OK;
  85. }
  86. /*
  87. SQLLogResult SQLiteClientLoggerPlugin::SqlLog( bool isFunctionCall, const char *tableName, const char *columnNames, const char *file, const int line )
  88. {
  89. RakNet::BitStream bitStream;
  90. SerializeHeader(&bitStream, isFunctionCall, tableName, columnNames, file, line, 0);
  91. SendUnified(&bitStream, LOW_PRIORITY, RELIABLE_ORDERED, 1, serverAddress, false);
  92. return SQLLR_OK;
  93. }
  94. SQLLogResult SQLiteClientLoggerPlugin::SqlLog( bool isFunctionCall, const char *tableName, const char *columnNames, const char *file, const int line, const LogParameter *p1 )
  95. {
  96. RakNet::BitStream bitStream;
  97. SerializeHeader(&bitStream, isFunctionCall, tableName, columnNames, file, line, 1);
  98. p1->Serialize(&bitStream);
  99. SendUnified(&bitStream, LOW_PRIORITY, RELIABLE_ORDERED, 1, serverAddress, false);
  100. return SQLLR_OK;
  101. }
  102. SQLLogResult SQLiteClientLoggerPlugin::SqlLog( bool isFunctionCall, const char *tableName, const char *columnNames, const char *file, const int line, const LogParameter *p1, const LogParameter *p2 )
  103. {
  104. RakNet::BitStream bitStream;
  105. SerializeHeader(&bitStream, isFunctionCall, tableName, columnNames, file, line, 2);
  106. p1->Serialize(&bitStream);
  107. p2->Serialize(&bitStream);
  108. SendUnified(&bitStream, LOW_PRIORITY, RELIABLE_ORDERED, 1, serverAddress, false);
  109. return SQLLR_OK;
  110. }
  111. SQLLogResult SQLiteClientLoggerPlugin::SqlLog( bool isFunctionCall, const char *tableName, const char *columnNames, const char *file, const int line, const LogParameter *p1, const LogParameter *p2, const LogParameter *p3 )
  112. {
  113. RakNet::BitStream bitStream;
  114. SerializeHeader(&bitStream, isFunctionCall, tableName, columnNames, file, line, 3);
  115. p1->Serialize(&bitStream);
  116. p2->Serialize(&bitStream);
  117. p3->Serialize(&bitStream);
  118. SendUnified(&bitStream, LOW_PRIORITY, RELIABLE_ORDERED, 1, serverAddress, false);
  119. return SQLLR_OK;
  120. }
  121. SQLLogResult SQLiteClientLoggerPlugin::SqlLog( bool isFunctionCall, const char *tableName, const char *columnNames, const char *file, const int line, const LogParameter *p1, const LogParameter *p2, const LogParameter *p3, const LogParameter *p4 )
  122. {
  123. RakNet::BitStream bitStream;
  124. SerializeHeader(&bitStream, isFunctionCall, tableName, columnNames, file, line, 4);
  125. p1->Serialize(&bitStream);
  126. p2->Serialize(&bitStream);
  127. p3->Serialize(&bitStream);
  128. p4->Serialize(&bitStream);
  129. SendUnified(&bitStream, LOW_PRIORITY, RELIABLE_ORDERED, 1, serverAddress, false);
  130. return SQLLR_OK;
  131. }
  132. SQLLogResult SQLiteClientLoggerPlugin::SqlLog( bool isFunctionCall, const char *tableName, const char *columnNames, const char *file, const int line, const LogParameter *p1, const LogParameter *p2, const LogParameter *p3, const LogParameter *p4, const LogParameter *p5 )
  133. {
  134. RakNet::BitStream bitStream;
  135. SerializeHeader(&bitStream, isFunctionCall, tableName, columnNames, file, line, 5);
  136. p1->Serialize(&bitStream);
  137. p2->Serialize(&bitStream);
  138. p3->Serialize(&bitStream);
  139. p4->Serialize(&bitStream);
  140. p5->Serialize(&bitStream);
  141. SendUnified(&bitStream, LOW_PRIORITY, RELIABLE_ORDERED, 1, serverAddress, false);
  142. return SQLLR_OK;
  143. }
  144. SQLLogResult SQLiteClientLoggerPlugin::SqlLog( bool isFunctionCall, const char *tableName, const char *columnNames, const char *file, const int line, const LogParameter *p1, const LogParameter *p2, const LogParameter *p3, const LogParameter *p4, const LogParameter *p5, const LogParameter *p6 )
  145. {
  146. RakNet::BitStream bitStream;
  147. SerializeHeader(&bitStream, isFunctionCall, tableName, columnNames, file, line, 6);
  148. p1->Serialize(&bitStream);
  149. p2->Serialize(&bitStream);
  150. p3->Serialize(&bitStream);
  151. p4->Serialize(&bitStream);
  152. p5->Serialize(&bitStream);
  153. p6->Serialize(&bitStream);
  154. SendUnified(&bitStream, LOW_PRIORITY, RELIABLE_ORDERED, 1, serverAddress, false);
  155. return SQLLR_OK;
  156. }
  157. SQLLogResult SQLiteClientLoggerPlugin::SqlLog( bool isFunctionCall, const char *tableName, const char *columnNames, const char *file, const int line, const LogParameter *p1, const LogParameter *p2, const LogParameter *p3, const LogParameter *p4, const LogParameter *p5, const LogParameter *p6, const LogParameter *p7 )
  158. {
  159. RakNet::BitStream bitStream;
  160. SerializeHeader(&bitStream, isFunctionCall, tableName, columnNames, file, line, 7);
  161. p1->Serialize(&bitStream);
  162. p2->Serialize(&bitStream);
  163. p3->Serialize(&bitStream);
  164. p4->Serialize(&bitStream);
  165. p5->Serialize(&bitStream);
  166. p6->Serialize(&bitStream);
  167. p7->Serialize(&bitStream);
  168. SendUnified(&bitStream, LOW_PRIORITY, RELIABLE_ORDERED, 1, serverAddress, false);
  169. return SQLLR_OK;
  170. }
  171. SQLLogResult SQLiteClientLoggerPlugin::SqlLog( bool isFunctionCall, const char *tableName, const char *columnNames, const char *file, const int line, const LogParameter *p1, const LogParameter *p2, const LogParameter *p3, const LogParameter *p4, const LogParameter *p5, const LogParameter *p6, const LogParameter *p7, const LogParameter *p8 )
  172. {
  173. RakNet::BitStream bitStream;
  174. SerializeHeader(&bitStream, isFunctionCall, tableName, columnNames, file, line, 8);
  175. p1->Serialize(&bitStream);
  176. p2->Serialize(&bitStream);
  177. p3->Serialize(&bitStream);
  178. p4->Serialize(&bitStream);
  179. p5->Serialize(&bitStream);
  180. p6->Serialize(&bitStream);
  181. p7->Serialize(&bitStream);
  182. p8->Serialize(&bitStream);
  183. SendUnified(&bitStream, LOW_PRIORITY, RELIABLE_ORDERED, 1, serverAddress, false);
  184. return SQLLR_OK;
  185. }
  186. SQLLogResult SQLiteClientLoggerPlugin::SqlLog( bool isFunctionCall, const char *tableName, const char *columnNames, const char *file, const int line, const LogParameter *p1, const LogParameter *p2, const LogParameter *p3, const LogParameter *p4, const LogParameter *p5, const LogParameter *p6, const LogParameter *p7, const LogParameter *p8, const LogParameter *p9 )
  187. {
  188. RakNet::BitStream bitStream;
  189. SerializeHeader(&bitStream, isFunctionCall, tableName, columnNames, file, line, 9);
  190. p1->Serialize(&bitStream);
  191. p2->Serialize(&bitStream);
  192. p3->Serialize(&bitStream);
  193. p4->Serialize(&bitStream);
  194. p5->Serialize(&bitStream);
  195. p6->Serialize(&bitStream);
  196. p7->Serialize(&bitStream);
  197. p8->Serialize(&bitStream);
  198. p9->Serialize(&bitStream);
  199. SendUnified(&bitStream, LOW_PRIORITY, RELIABLE_ORDERED, 1, serverAddress, false);
  200. return SQLLR_OK;
  201. }
  202. SQLLogResult SQLiteClientLoggerPlugin::SqlLog( bool isFunctionCall, const char *tableName, const char *columnNames, const char *file, const int line, const LogParameter *p1, const LogParameter *p2, const LogParameter *p3, const LogParameter *p4, const LogParameter *p5, const LogParameter *p6, const LogParameter *p7, const LogParameter *p8, const LogParameter *p9, const LogParameter *p10 )
  203. {
  204. RakNet::BitStream bitStream;
  205. SerializeHeader(&bitStream, isFunctionCall, tableName, columnNames, file, line, 10);
  206. p1->Serialize(&bitStream);
  207. p2->Serialize(&bitStream);
  208. p3->Serialize(&bitStream);
  209. p4->Serialize(&bitStream);
  210. p5->Serialize(&bitStream);
  211. p6->Serialize(&bitStream);
  212. p7->Serialize(&bitStream);
  213. p8->Serialize(&bitStream);
  214. p9->Serialize(&bitStream);
  215. p10->Serialize(&bitStream);
  216. SendUnified(&bitStream, LOW_PRIORITY, RELIABLE_ORDERED, 1, serverAddress, false);
  217. return SQLLR_OK;
  218. }
  219. SQLLogResult SQLiteClientLoggerPlugin::SqlLog( bool isFunctionCall, const char *tableName, const char *columnNames, const char *file, const int line, const LogParameter *p1, const LogParameter *p2, const LogParameter *p3, const LogParameter *p4, const LogParameter *p5, const LogParameter *p6, const LogParameter *p7, const LogParameter *p8, const LogParameter *p9, const LogParameter *p10, const LogParameter *p11 )
  220. {
  221. RakNet::BitStream bitStream;
  222. SerializeHeader(&bitStream, isFunctionCall, tableName, columnNames, file, line, 11);
  223. p1->Serialize(&bitStream);
  224. p2->Serialize(&bitStream);
  225. p3->Serialize(&bitStream);
  226. p4->Serialize(&bitStream);
  227. p5->Serialize(&bitStream);
  228. p6->Serialize(&bitStream);
  229. p7->Serialize(&bitStream);
  230. p8->Serialize(&bitStream);
  231. p9->Serialize(&bitStream);
  232. p10->Serialize(&bitStream);
  233. p11->Serialize(&bitStream);
  234. SendUnified(&bitStream, LOW_PRIORITY, RELIABLE_ORDERED, 1, serverAddress, false);
  235. return SQLLR_OK;
  236. }
  237. SQLLogResult SQLiteClientLoggerPlugin::SqlLog( bool isFunctionCall, const char *tableName, const char *columnNames, const char *file, const int line, const LogParameter *p1, const LogParameter *p2, const LogParameter *p3, const LogParameter *p4, const LogParameter *p5, const LogParameter *p6, const LogParameter *p7, const LogParameter *p8, const LogParameter *p9, const LogParameter *p10, const LogParameter *p11, const LogParameter *p12 )
  238. {
  239. RakNet::BitStream bitStream;
  240. SerializeHeader(&bitStream, isFunctionCall, tableName, columnNames, file, line, 12);
  241. p1->Serialize(&bitStream);
  242. p2->Serialize(&bitStream);
  243. p3->Serialize(&bitStream);
  244. p4->Serialize(&bitStream);
  245. p5->Serialize(&bitStream);
  246. p6->Serialize(&bitStream);
  247. p7->Serialize(&bitStream);
  248. p8->Serialize(&bitStream);
  249. p9->Serialize(&bitStream);
  250. p10->Serialize(&bitStream);
  251. p11->Serialize(&bitStream);
  252. p12->Serialize(&bitStream);
  253. SendUnified(&bitStream, LOW_PRIORITY, RELIABLE_ORDERED, 1, serverAddress, false);
  254. return SQLLR_OK;
  255. }
  256. */
  257. SQLLogResult SQLiteClientLoggerPlugin::CheckQuery(bool isFunction, const char *tableName, const char *columnNames, int numParameters)
  258. {
  259. (void) isFunction;
  260. if (recursiveCheck==true)
  261. return SQLLR_RECURSION;
  262. if (tableName==0 || tableName[0]==0)
  263. return SQLLR_TABLE_NAME_BLANK;
  264. if (isFunction==true)
  265. return SQLLR_OK;
  266. if (columnNames==0 || columnNames[0]==0)
  267. {
  268. if (numParameters==0)
  269. return SQLLR_OK;
  270. return SQLLR_TABLE_DESCRIPTOR_FORMAT_WRONG_PARAMETER_COUNT;
  271. }
  272. recursiveCheck=true;
  273. if (dbIdentifier.IsEmpty())
  274. {
  275. recursiveCheck=false;
  276. return SQLLR_NO_DATABASE_SET;
  277. }
  278. unsigned int parameterCount=1;
  279. unsigned int columnNamesIndex=0;
  280. for (columnNamesIndex=1; columnNamesIndex < 512 && columnNames[columnNamesIndex]; columnNamesIndex++)
  281. {
  282. if (columnNames[columnNamesIndex]==COLUMN_NAMES_DELIMITER)
  283. {
  284. if (columnNames[columnNamesIndex-1]==COLUMN_NAMES_DELIMITER)
  285. {
  286. recursiveCheck=false;
  287. return SQLLR_TABLE_DESCRIPTOR_FORMAT_INVALID_SYNTAX;
  288. }
  289. parameterCount++;
  290. }
  291. }
  292. recursiveCheck=false;
  293. if (columnNamesIndex==MAX_COLUMN_NAMES_LENGTH)
  294. {
  295. return SQLLR_COLUMN_NAMES_NOT_TERMINATED;
  296. }
  297. if (parameterCount!=numParameters)
  298. {
  299. return SQLLR_TABLE_DESCRIPTOR_FORMAT_WRONG_PARAMETER_COUNT;
  300. }
  301. return SQLLR_OK;
  302. }
  303. void SQLiteClientLoggerPlugin::SerializeHeader(RakNet::BitStream *bitStream, bool isFunctionCall, const char *tableName, const char *columnNames, const char *file, const int line, unsigned char parameterCount ) const
  304. {
  305. bitStream->Write((MessageID) ID_SQLLITE_LOGGER);
  306. bitStream->Write(dbIdentifier);
  307. bitStream->Write(tableName);
  308. bitStream->Write(line);
  309. bitStream->Write(file);
  310. bitStream->Write(tickCount);
  311. bitStream->Write(RakNet::GetTimeMS());
  312. bitStream->Write(isFunctionCall);
  313. bitStream->Write(parameterCount);
  314. if (isFunctionCall==false && parameterCount>=1)
  315. {
  316. int stringIndices[64];
  317. int strIndex=0;
  318. stringIndices[strIndex++]=0;
  319. char columnNamesCopy[MAX_COLUMN_NAMES_LENGTH];
  320. RakAssert(strlen(columnNames)<MAX_COLUMN_NAMES_LENGTH);
  321. strncpy(columnNamesCopy, columnNames, MAX_COLUMN_NAMES_LENGTH);
  322. columnNamesCopy[MAX_COLUMN_NAMES_LENGTH-1]=0;
  323. for (int i=0; columnNamesCopy[i]; i++)
  324. {
  325. if (columnNamesCopy[i]==COLUMN_NAMES_DELIMITER)
  326. {
  327. columnNamesCopy[i]=0;
  328. stringIndices[strIndex++]=i+1;
  329. }
  330. }
  331. RakAssert(strIndex==parameterCount);
  332. for (int i=0; i < parameterCount; i++)
  333. {
  334. bitStream->Write((char*)columnNamesCopy + stringIndices[i]);
  335. }
  336. }
  337. }
  338. void SQLiteClientLoggerPlugin::Update(void)
  339. {
  340. SQLite3ClientPlugin::Update();
  341. }
粤ICP备19079148号