SQLite3ClientPlugin.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "SQLite3ClientPlugin.h"
  2. #include "MessageIdentifiers.h"
  3. #include "BitStream.h"
  4. using namespace RakNet;
  5. void SQLite3PluginResultInterface_Printf::_sqlite3_exec(
  6. RakNet::RakString inputStatement,
  7. unsigned int queryId,
  8. RakNet::RakString dbIdentifier,
  9. const SQLite3Table &table,
  10. RakNet::RakString errorMsg)
  11. {
  12. if (errorMsg.IsEmpty()==false)
  13. {
  14. printf("Error for query: %s\n", inputStatement.C_String());
  15. printf("%s\n", errorMsg.C_String());
  16. return;
  17. }
  18. unsigned int idx;
  19. for (idx=0; idx < table.columnNames.Size(); idx++)
  20. printf("%s ", table.columnNames[idx].C_String());
  21. printf("\n");
  22. if (table.rows.Size()==0)
  23. {
  24. printf("<NO ROWS>\n");
  25. }
  26. else
  27. {
  28. for (idx=0; idx < table.rows.Size(); idx++)
  29. {
  30. unsigned int idx2;
  31. for (idx2=0; idx2 < table.rows[idx]->entries.Size(); idx2++)
  32. printf("%s ", table.rows[idx]->entries[idx2].C_String());
  33. printf("\n");
  34. }
  35. }
  36. }
  37. void SQLite3PluginResultInterface_Printf::OnUnknownDBIdentifier(
  38. RakNet::RakString inputStatement,
  39. unsigned int queryId,
  40. RakNet::RakString dbIdentifier)
  41. {
  42. printf("Unknown DB %s\n", dbIdentifier.C_String());
  43. }
  44. SQLite3ClientPlugin::SQLite3ClientPlugin()
  45. {
  46. nextQueryId=0;
  47. }
  48. SQLite3ClientPlugin::~SQLite3ClientPlugin()
  49. {
  50. }
  51. void SQLite3ClientPlugin::AddResultHandler(SQLite3PluginResultInterface *res)
  52. {
  53. resultHandlers.Push(res,_FILE_AND_LINE_);
  54. }
  55. void SQLite3ClientPlugin::RemoveResultHandler(SQLite3PluginResultInterface *res)
  56. {
  57. unsigned int idx = resultHandlers.GetIndexOf(res);
  58. if (idx!=-1)
  59. resultHandlers.RemoveAtIndex(idx);
  60. }
  61. void SQLite3ClientPlugin::ClearResultHandlers(void)
  62. {
  63. resultHandlers.Clear(true,_FILE_AND_LINE_);
  64. }
  65. unsigned int SQLite3ClientPlugin::_sqlite3_exec(RakNet::RakString dbIdentifier, RakNet::RakString inputStatement,
  66. PacketPriority priority, PacketReliability reliability, char orderingChannel, const SystemAddress &systemAddress)
  67. {
  68. RakNet::BitStream bsOut;
  69. bsOut.Write((MessageID)ID_SQLite3_EXEC);
  70. bsOut.Write(nextQueryId);
  71. bsOut.Write(dbIdentifier);
  72. bsOut.Write(inputStatement);
  73. bsOut.Write(true);
  74. SendUnified(&bsOut, priority,reliability,orderingChannel,systemAddress,false);
  75. ++nextQueryId;
  76. return nextQueryId-1;
  77. }
  78. PluginReceiveResult SQLite3ClientPlugin::OnReceive(Packet *packet)
  79. {
  80. switch (packet->data[0])
  81. {
  82. case ID_SQLite3_EXEC:
  83. {
  84. unsigned int queryId;
  85. RakNet::RakString dbIdentifier;
  86. RakNet::RakString inputStatement;
  87. RakNet::BitStream bsIn(packet->data, packet->length, false);
  88. bsIn.IgnoreBytes(sizeof(MessageID));
  89. bsIn.Read(queryId);
  90. bsIn.Read(dbIdentifier);
  91. bsIn.Read(inputStatement);
  92. bool isRequest;
  93. bsIn.Read(isRequest);
  94. if (isRequest)
  95. {
  96. // Server code
  97. RakAssert(0);
  98. }
  99. else
  100. {
  101. // Client code
  102. RakNet::RakString errorMsgStr;
  103. SQLite3Table inputTable;
  104. // Read it
  105. bsIn.Read(errorMsgStr);
  106. inputTable.Deserialize(&bsIn);
  107. unsigned int idx;
  108. for (idx=0; idx < resultHandlers.Size(); idx++)
  109. resultHandlers[idx]->_sqlite3_exec(inputStatement, queryId, dbIdentifier, inputTable,errorMsgStr);
  110. }
  111. return RR_STOP_PROCESSING_AND_DEALLOCATE;
  112. }
  113. break;
  114. case ID_SQLite3_UNKNOWN_DB:
  115. {
  116. unsigned int queryId;
  117. RakNet::RakString dbIdentifier;
  118. RakNet::RakString inputStatement;
  119. RakNet::BitStream bsIn(packet->data, packet->length, false);
  120. bsIn.IgnoreBytes(sizeof(MessageID));
  121. bsIn.Read(queryId);
  122. bsIn.Read(dbIdentifier);
  123. bsIn.Read(inputStatement);
  124. unsigned int idx;
  125. for (idx=0; idx < resultHandlers.Size(); idx++)
  126. resultHandlers[idx]->OnUnknownDBIdentifier(inputStatement, queryId, dbIdentifier);
  127. }
  128. }
  129. return RR_CONTINUE_PROCESSING;
  130. }
粤ICP备19079148号