SQLiteServerLoggerSample.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "RakPeerInterface.h"
  2. #include "SQLiteServerLoggerPlugin.h"
  3. #include "BitStream.h"
  4. #include "RakSleep.h"
  5. #include "Kbhit.h"
  6. #include "GetTime.h"
  7. #include "PacketizedTCP.h"
  8. int main(void)
  9. {
  10. printf("Demonstration of SQLiteServerLoggerPlugin.\n");
  11. RakNet::PacketizedTCP packetizedTCP;
  12. RakNet::SQLiteServerLoggerPlugin loggerPlugin;
  13. // printf("Enable DXT compression (y/n)? ");
  14. // loggerPlugin.SetEnableDXTCompression(getche()=='y');
  15. loggerPlugin.SetEnableDXTCompression(true);
  16. loggerPlugin.SetSessionManagementMode(RakNet::SQLiteServerLoggerPlugin::CREATE_SHARED_NAMED_DB_HANDLE, true, "");
  17. /*
  18. // printf("Enter path to DB file to create, or enter for memory.\n");
  19. char filePath[256];
  20. Gets(filePath,sizeof(filePath));
  21. filePath[0]=0;
  22. if (filePath[0]==0)
  23. strcpy(filePath, "C:\\EchoChamber\\logger.sqlite");
  24. sqlite3 *database;
  25. if (sqlite3_open_v2(filePath, &database, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0)!=SQLITE_OK)
  26. return 1;
  27. loggerPlugin.AddDBHandle("loggerDb", database);
  28. */
  29. packetizedTCP.AttachPlugin(&loggerPlugin);
  30. packetizedTCP.Start(38123,8);
  31. printf("\nStarted.\n");
  32. bool quit=false;
  33. bool isProcessing=false;
  34. RakNet::SQLiteServerLoggerPlugin::ProcessingStatus processingStatusNew;
  35. RakNet::SQLiteServerLoggerPlugin::ProcessingStatus processingStatusOld;
  36. memset(&processingStatusOld,0,sizeof(processingStatusOld));
  37. RakNet::SystemAddress sa;
  38. while (quit==false || isProcessing==true)
  39. {
  40. RakNet::Packet *p;
  41. for (p = packetizedTCP.Receive(); p; packetizedTCP.DeallocatePacket(p), p = packetizedTCP.Receive())
  42. {
  43. ;
  44. }
  45. sa = packetizedTCP.HasNewIncomingConnection();
  46. if (sa!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  47. printf("New incoming connection from %s\n", sa.ToString(true));
  48. sa = packetizedTCP.HasLostConnection();
  49. if (sa!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  50. printf("Lost connection from %s\n", sa.ToString(true));
  51. sa = packetizedTCP.HasFailedConnectionAttempt();
  52. sa = packetizedTCP.HasCompletedConnectionAttempt();
  53. RakSleep(0);
  54. if (kbhit())
  55. {
  56. if (getch()=='q')
  57. {
  58. printf("Quitting as soon as threads finish.\n");
  59. packetizedTCP.Stop();
  60. quit=true;
  61. }
  62. }
  63. loggerPlugin.GetProcessingStatus(&processingStatusNew);
  64. if (memcmp(&processingStatusNew,&processingStatusOld,sizeof(processingStatusOld))!=0)
  65. {
  66. printf("buffered=%i cpuWait=%i cpuDo=%i cpuDone=%i sqlWait=%i sqlDo=%i sqlDone=%i\n",
  67. processingStatusNew.packetsBuffered,
  68. processingStatusNew.cpuPendingProcessing,processingStatusNew.cpuNumThreadsWorking,processingStatusNew.cpuProcessedAwaitingDeallocation,
  69. processingStatusNew.sqlPendingProcessing,processingStatusNew.sqlNumThreadsWorking,processingStatusNew.sqlProcessedAwaitingDeallocation
  70. );
  71. memcpy(&processingStatusOld,&processingStatusNew,sizeof(processingStatusOld));
  72. }
  73. if (processingStatusNew.cpuNumThreadsWorking==processingStatusNew.cpuPendingProcessing==processingStatusNew.cpuProcessedAwaitingDeallocation==
  74. processingStatusNew.packetsBuffered==processingStatusNew.sqlNumThreadsWorking==processingStatusNew.sqlPendingProcessing==processingStatusNew.sqlProcessedAwaitingDeallocation==0)
  75. isProcessing=false;
  76. else
  77. isProcessing=true;
  78. }
  79. loggerPlugin.CloseAllSessions();
  80. return 1;
  81. }
粤ICP备19079148号