MasterClientMain.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 client but serves as a good example.
  11. // Right now it is hardcoded to connect to 127.0.0.1. You would run the master server first, then run this.
  12. #include "MasterCommon.h"
  13. #include "MasterClient.h"
  14. #include "StringCompressor.h"
  15. #include "BitStream.h"
  16. #include "RakPeerInterface.h"
  17. #include "RakNetworkFactory.h"
  18. #include <cstdio>
  19. #include <cstring>
  20. #include <stdlib.h>
  21. #ifdef WIN32
  22. #include <conio.h>
  23. #else
  24. #include "../Unix/kbhit.h"
  25. #endif
  26. #ifdef _WIN32
  27. #include <conio.h>
  28. #include <windows.h> // Sleep
  29. #else
  30. #include "../Unix/kbhit.h"
  31. #include <unistd.h> // usleep
  32. #endif
  33. #define READCHAR(arg) gets(arg); ch=arg[0];
  34. // remove this
  35. #include "PacketEnumerations.h"
  36. int main(void)
  37. {
  38. printf("DEPRECIATED - Use LightweightDatabase instead\n");
  39. BitStream bitStream;
  40. char str[256];
  41. char ch;
  42. MasterClient masterClient;
  43. RakPeerInterface *testGameServerOrClient;
  44. unsigned int serverListSize, index;
  45. bool identiferFound;
  46. const char *outputString;
  47. int outputInt;
  48. Packet *p;
  49. // Create a fake game
  50. testGameServerOrClient = RakNetworkFactory::GetRakPeerInterface();
  51. testGameServerOrClient->Initialize(8, 60003, 0);
  52. testGameServerOrClient->SetMaximumIncomingConnections(8);
  53. testGameServerOrClient->AttachPlugin(&masterClient);
  54. printf("This project shows how to use the master client.\n");
  55. printf("The master client is used by game servers to advertise themselves.\n");
  56. printf("On the master server and by game clients to download a list of game servers\n");
  57. printf("Difficulty: Intermediate\n\n");
  58. if (masterClient.Connect("127.0.0.1", 60000))
  59. printf("Master client connecting...\n");
  60. else
  61. printf("Master client failed to start or connect.\n");
  62. printf("(Q)uit\n(q)uery master server\n(l)ist server\n(d)elist server\n(D)isconnect from the master server.\n(a)dd rule\n(r)emove rule\n(p)ing server list\n(C)onnect to the master server.\n(c)onnect to another game, using NAT punch-through with master server, bypassing most NATs\n(SPACE) print server list\n");
  63. char buff[256];
  64. while (1)
  65. {
  66. if (kbhit())
  67. {
  68. READCHAR(buff);
  69. if (ch=='Q')
  70. break;
  71. if (ch=='q')
  72. {
  73. masterClient.ClearQueryRules();
  74. printf("Enter query key 1/2 or enter for none: ");
  75. gets(str);
  76. masterClient.AddQueryRule(str);
  77. printf("Enter query key 2/2 or enter for none: ");
  78. gets(str);
  79. masterClient.AddQueryRule(str);
  80. masterClient.QueryMasterServer();
  81. printf("Server queried. Press space to see server list.\n");
  82. }
  83. else if (ch=='l')
  84. {
  85. printf("Uploading game server. Query to see it.\n");
  86. masterClient.ListServer();
  87. }
  88. else if (ch=='d')
  89. {
  90. printf("Server delisted. Query to update our own list.\n");
  91. masterClient.DelistServer();
  92. }
  93. else if (ch=='D')
  94. {
  95. printf("Disconnected.\n");
  96. PlayerID playerId;
  97. testGameServerOrClient->IPToPlayerID("127.0.0.1", 60000, &playerId);
  98. testGameServerOrClient->CloseConnection(playerId, true, 0);
  99. }
  100. else if (ch=='C')
  101. {
  102. if (masterClient.Connect("127.0.0.1", 60000))
  103. printf("Master client connecting...\n");
  104. else
  105. printf("Master client failed to start or connect.\n");
  106. }
  107. else if (ch=='a')
  108. {
  109. printf("Adding sample rules. Query to update our own list.\n");
  110. masterClient.PostRule("Game name", "My big game o' death.", 0);
  111. masterClient.PostRule("Game type", "Death match", 0);
  112. masterClient.PostRule("Score",0, 100);
  113. }
  114. else if (ch=='r')
  115. {
  116. printf("Removing rules. Query to update our own list.\n");
  117. masterClient.RemoveRule("Game type");
  118. masterClient.RemoveRule("Game name");
  119. masterClient.RemoveRule("Score");
  120. }
  121. else if (ch=='p')
  122. {
  123. printf("Pinging any servers in our list\n");
  124. masterClient.PingServers();
  125. }
  126. else if (ch=='c')
  127. {
  128. char ip[22];
  129. printf("Sending connection attempt notification to master server\n");
  130. printf("Enter IP of server from game list: ");
  131. gets(ip);
  132. printf("Enter port: ");
  133. gets(str);
  134. if (ip[0]!=0 && str[0]!=0)
  135. {
  136. masterClient.ConnectionAttemptNotification(ip, atoi(str));
  137. printf("Sent connection attempt notification to the server the master server\n");
  138. }
  139. else
  140. {
  141. printf("Aborting...");
  142. }
  143. }
  144. else if (ch==' ')
  145. {
  146. serverListSize=masterClient.GetServerListSize();
  147. if (serverListSize==0)
  148. {
  149. printf("No servers in list\n");
  150. }
  151. else
  152. {
  153. for (index=0; index < serverListSize; index++)
  154. {
  155. printf("%i. ", index);
  156. outputString=masterClient.GetServerListRuleAsString(index, "IP", &identiferFound);
  157. if (identiferFound)
  158. printf("%s:", outputString);
  159. else
  160. printf("NO_IP:");
  161. outputInt=masterClient.GetServerListRuleAsInt(index, "Port", &identiferFound);
  162. if (identiferFound)
  163. printf("%i ", outputInt);
  164. else
  165. printf("NO_PORT ");
  166. outputInt=masterClient.GetServerListRuleAsInt(index, "Ping", &identiferFound);
  167. if (identiferFound)
  168. printf("%i ", outputInt);
  169. else
  170. printf("NO_PING ");
  171. outputString=masterClient.GetServerListRuleAsString(index, "Game type", &identiferFound);
  172. if (identiferFound)
  173. printf("%s ", outputString);
  174. else
  175. printf("NO_GT ");
  176. outputString=masterClient.GetServerListRuleAsString(index, "Game name", &identiferFound);
  177. if (identiferFound)
  178. printf("%s ", outputString);
  179. else
  180. printf("NO_GN ");
  181. outputInt=masterClient.GetServerListRuleAsInt(index, "Score", &identiferFound);
  182. if (identiferFound)
  183. printf("%i\n", outputInt);
  184. else
  185. printf("NO_SCORE\n");
  186. }
  187. }
  188. }
  189. ch=0;
  190. }
  191. p = testGameServerOrClient->Receive();
  192. while (p)
  193. {
  194. // Ignore any game packets. The master server plugin handles everything.
  195. testGameServerOrClient->DeallocatePacket(p);
  196. // Call Receive every update to keep the plugin going
  197. p = testGameServerOrClient->Receive();
  198. }
  199. #ifdef _WIN32
  200. Sleep(30);
  201. #else
  202. usleep(30 * 1000);
  203. #endif
  204. }
  205. masterClient.Disconnect();
  206. RakNetworkFactory::DestroyRakPeerInterface(testGameServerOrClient);
  207. return 0;
  208. }
粤ICP备19079148号