MasterServerMain.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // This is my own internal test program for the master server but serves as a good example.
  11. #include "MasterCommon.h"
  12. #include "MasterServer.h"
  13. #include "RakNetworkFactory.h"
  14. #include "RakPeerInterface.h"
  15. #include <cstdio>
  16. #include <cstring>
  17. #ifdef WIN32
  18. #include <conio.h>
  19. #else
  20. #include "../Unix/kbhit.h"
  21. #endif
  22. #ifdef _WIN32
  23. #include <conio.h>
  24. #include <windows.h> // Sleep
  25. #else
  26. #include "../Unix/kbhit.h"
  27. #include <unistd.h> // usleep
  28. #endif
  29. #define READCHAR(arg) gets(arg); ch=arg[0];
  30. int main(void)
  31. {
  32. MasterServer masterServer;
  33. int serverListSize;
  34. const char *outputString;
  35. int outputInt;
  36. bool identiferFound;
  37. int index;
  38. char ch;
  39. Packet *p;
  40. RakPeerInterface *testGameMasterServer;
  41. testGameMasterServer = RakNetworkFactory::GetRakPeerInterface();
  42. testGameMasterServer->Initialize(10, 60000, 0);
  43. testGameMasterServer->SetMaximumIncomingConnections(8);
  44. testGameMasterServer->AttachPlugin(&masterServer);
  45. printf("This project shows how to use the master server.\n");
  46. printf("The master server is a plugin that maintains a list of games uplodated.\n");
  47. printf("by the master client.\n");
  48. printf("Difficulty: Beginner\n\n");
  49. printf("(p)rint\n(q)uit\n");
  50. char buff[256];
  51. while (1)
  52. {
  53. if (kbhit())
  54. {
  55. READCHAR(buff);
  56. if (ch=='q')
  57. break;
  58. else if (ch=='p')
  59. {
  60. serverListSize=masterServer.GetServerListSize();
  61. if (serverListSize==0)
  62. {
  63. printf("No servers in list\n");
  64. }
  65. else
  66. {
  67. for (index=0; index < serverListSize; index++)
  68. {
  69. printf("%i. ", index);
  70. outputString=masterServer.GetServerListRuleAsString(index, "IP", &identiferFound);
  71. if (identiferFound)
  72. printf("%s:", outputString);
  73. else
  74. printf("NO_IP:");
  75. outputInt=masterServer.GetServerListRuleAsInt(index, "Port", &identiferFound);
  76. if (identiferFound)
  77. printf("%i ", outputInt);
  78. else
  79. printf("NO_PORT ");
  80. outputString=masterServer.GetServerListRuleAsString(index, "Game type", &identiferFound);
  81. if (identiferFound)
  82. printf("%s ", outputString);
  83. else
  84. printf("NIL_GT ");
  85. outputString=masterServer.GetServerListRuleAsString(index, "Game name", &identiferFound);
  86. if (identiferFound)
  87. printf("%s ", outputString);
  88. else
  89. printf("NIL_GN ");
  90. outputInt=masterServer.GetServerListRuleAsInt(index, "Score", &identiferFound);
  91. if (identiferFound)
  92. printf("%i\n", outputInt);
  93. else
  94. printf("NO_SCORE\n");
  95. }
  96. }
  97. }
  98. ch=0;
  99. }
  100. p = testGameMasterServer->Receive();
  101. while (p)
  102. {
  103. // Ignore any game packets. The master server plugin handles everything.
  104. testGameMasterServer->DeallocatePacket(p);
  105. // Call Receive every update to keep the plugin going
  106. p = testGameMasterServer->Receive();
  107. }
  108. #ifdef _WIN32
  109. Sleep(30);
  110. #else
  111. usleep(30 * 1000);
  112. #endif
  113. }
  114. return 0;
  115. }
粤ICP备19079148号