Lobby2ServerSample_PGSQL.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include "Lobby2Message.h"
  11. #include "RakPeerInterface.h"
  12. #include "MessageIdentifiers.h"
  13. #include "Kbhit.h"
  14. #include "RakSleep.h"
  15. #include "Lobby2Server_PGSQL.h"
  16. #include "Lobby2Message_PGSQL.h"
  17. #include "ProfanityFilter.h"
  18. #include <ctype.h>
  19. #include <stdlib.h>
  20. #include "Gets.h"
  21. #ifdef __INTEGRATE_LOBBY2_WITH_ROOMS_PLUGIN
  22. #include "RoomsPlugin.h"
  23. #endif
  24. //#define _ALSO_ACT_AS_NAT_PUNCH_SERVER
  25. #include "NatPunchthroughServer.h"
  26. #include "UDPProxyCoordinator.h"
  27. #include "UDPProxyServer.h"
  28. #include "NatTypeDetectionServer.h"
  29. #include "SocketLayer.h"
  30. static const char *COORDINATOR_PASSWORD="Dummy Coordinator Password";
  31. #ifdef _ALSO_ACT_AS_NAT_PUNCH_SERVER
  32. #endif
  33. void main(void)
  34. {
  35. printf("The 2nd interation of the lobby server.\n");
  36. printf("Difficulty: Intermediate\n\n");
  37. char serverPort[30];
  38. RakNet::RakPeerInterface *rakPeer=RakNet::RakPeerInterface::GetInstance();
  39. rakPeer->SetTimeoutTime(5000,RakNet::UNASSIGNED_SYSTEM_ADDRESS);
  40. //rakPeer->SetTimeoutTime(3000,RakNet::UNASSIGNED_SYSTEM_ADDRESS);
  41. puts("Enter the rakPeer port to listen on");
  42. serverPort[0]=0;
  43. Gets(serverPort,sizeof(serverPort));
  44. if (serverPort[0]==0)
  45. strcpy(serverPort, "61111");
  46. RakNet::SocketDescriptor socketDescriptor(atoi(serverPort),0);
  47. rakPeer->SetMaximumIncomingConnections(32);
  48. if (rakPeer->Startup(32,&socketDescriptor, 1)!=RakNet::RAKNET_STARTED)
  49. {
  50. printf("Startup call failed\n");
  51. return;
  52. }
  53. else
  54. printf("Started on port %i\n", socketDescriptor.port);
  55. // Attach the plugin Lobby2Server
  56. // The class factory will create messages with server functionality
  57. RakNet::Lobby2Server_PGSQL lobby2Server;
  58. rakPeer->AttachPlugin(&lobby2Server);
  59. RakNet::Lobby2MessageFactory_PGSQL messageFactory;
  60. lobby2Server.SetMessageFactory(&messageFactory);
  61. // This is optional:
  62. #ifdef __INTEGRATE_LOBBY2_WITH_ROOMS_PLUGIN
  63. RakNet::RoomsPlugin roomsPluginServer;
  64. rakPeer->AttachPlugin(&roomsPluginServer);
  65. lobby2Server.SetRoomsPlugin(&roomsPluginServer);
  66. RakNet::ProfanityFilter profanityFilter;
  67. profanityFilter.AddWord("Penis");
  68. roomsPluginServer.SetProfanityFilter(&profanityFilter);
  69. roomsPluginServer.roomsContainer.AddTitle("Test Title Name");
  70. #endif
  71. printf("Enter database password:\n");
  72. char connectionString[256],password[128];
  73. char username[256];
  74. strcpy(username, "postgres");
  75. password[0]=0;
  76. Gets(password,sizeof(password));
  77. if (password[0]==0) strcpy(password, "aaaa");
  78. strcpy(connectionString, "user=");
  79. strcat(connectionString, username);
  80. strcat(connectionString, " password=");
  81. strcat(connectionString, password);
  82. if (lobby2Server.ConnectToDB(connectionString, 4)==false)
  83. {
  84. printf("Database connection failed\n");
  85. return;
  86. }
  87. printf("Lobby2Server started and waiting for connections\n");
  88. RakNet::Lobby2Server::ConfigurationProperties c;
  89. c.requiresEmailAddressValidationToLogin=false;
  90. c.requiresTitleToLogin=true;
  91. c.accountRegistrationRequiresCDKey=false;
  92. c.accountRegistrationRequiredAgeYears=0;
  93. lobby2Server.SetConfigurationProperties(c);
  94. #ifdef _ALSO_ACT_AS_NAT_PUNCH_SERVER
  95. RakNet::NatPunchthroughServer natPunchthroughServer;
  96. RakNet::UDPProxyCoordinator udpProxyCoordinator;
  97. RakNet::UDPProxyServer udpProxyServer;
  98. RakNet::NatTypeDetectionServer natTypeDetectionServer;
  99. udpProxyCoordinator.SetRemoteLoginPassword(COORDINATOR_PASSWORD);
  100. rakPeer->AttachPlugin(&natPunchthroughServer);
  101. rakPeer->AttachPlugin(&udpProxyServer);
  102. rakPeer->AttachPlugin(&udpProxyCoordinator);
  103. rakPeer->AttachPlugin(&natTypeDetectionServer);
  104. char ipListStr[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ][ 128 ];
  105. RakNet::SystemAddress ipList[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ];
  106. for (int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++)
  107. ipList[i].ToString(false,ipListStr[i]);
  108. RakNet::SocketLayer::GetMyIP( ipList );
  109. natTypeDetectionServer.Startup(ipListStr[1], ipListStr[2], ipListStr[3]);
  110. // Login proxy server to proxy coordinator
  111. // Normally the proxy server is on a different computer. Here, we login to our own IP address since the plugin is on the same system
  112. // This makes it take high CPU usage, comment out of not wanted
  113. udpProxyServer.LoginToCoordinator(COORDINATOR_PASSWORD, rakPeer->GetMyBoundAddress());
  114. #endif
  115. RakNet::Packet *packet;
  116. // Loop for input
  117. while (1)
  118. {
  119. for (packet=rakPeer->Receive(); packet; rakPeer->DeallocatePacket(packet), packet=rakPeer->Receive())
  120. {
  121. switch (packet->data[0])
  122. {
  123. case ID_DISCONNECTION_NOTIFICATION:
  124. // Connection lost normally
  125. printf("ID_DISCONNECTION_NOTIFICATION\n");
  126. break;
  127. case ID_NEW_INCOMING_CONNECTION:
  128. // Connection lost normally
  129. printf("ID_NEW_INCOMING_CONNECTION\n");
  130. printf("Allowing all operations from remote client for testing (insecure)\n");
  131. printf("Use Lobby2Server::ExecuteCommand for local server operations\n");
  132. // For this test, allow all operations
  133. lobby2Server.AddAdminAddress(packet->systemAddress);
  134. lobby2Server.AddRankingAddress(packet->systemAddress);
  135. break;
  136. case ID_CONNECTION_LOST:
  137. // Couldn't deliver a reliable packet - i.e. the other system was abnormally
  138. // terminated
  139. printf("ID_CONNECTION_LOST\n");
  140. break;
  141. }
  142. }
  143. // This sleep keeps RakNet responsive
  144. RakSleep(30);
  145. //printf("%i ", lobby2Server.GetUsers().Size());
  146. }
  147. RakNet::RakPeerInterface::DestroyInstance(rakPeer);
  148. }
粤ICP备19079148号