CloudServerSample.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "CloudServerHelper.h"
  11. #include "MessageIdentifiers.h"
  12. #include "BitStream.h"
  13. #include "FullyConnectedMesh2.h"
  14. #include "TwoWayAuthentication.h"
  15. #include "CloudClient.h"
  16. #include "DynDNS.h"
  17. #include "RakPeerInterface.h"
  18. #include "RakSleep.h"
  19. #include "ConnectionGraph2.h"
  20. int main(int argc, char **argv)
  21. {
  22. // Used to update DNS
  23. RakNet::DynDNS dynDNS;
  24. RakNet::CloudServerHelper_DynDns cloudServerHelper(&dynDNS);
  25. if (!cloudServerHelper.ParseCommandLineParameters(argc, argv))
  26. return 1;
  27. // ---- RAKPEER -----
  28. RakNet::RakPeerInterface *rakPeer;
  29. rakPeer=RakNet::RakPeerInterface::GetInstance();
  30. // ---- PLUGINS -----
  31. // Used to load balance clients, allow for client to client discovery
  32. RakNet::CloudServer cloudServer;
  33. // Used to update the local cloudServer
  34. RakNet::CloudClient cloudClient;
  35. // Used to determine the host of the server fully connected mesh, as well as to connect servers automatically
  36. RakNet::FullyConnectedMesh2 fullyConnectedMesh2;
  37. // Used for servers to verify each other - otherwise any system could pose as a server
  38. // Could also be used to verify and restrict clients if paired with the MessageFilter plugin
  39. RakNet::TwoWayAuthentication twoWayAuthentication;
  40. // Used to tell servers about each other
  41. RakNet::ConnectionGraph2 connectionGraph2;
  42. rakPeer->AttachPlugin(&cloudServer);
  43. rakPeer->AttachPlugin(&cloudClient);
  44. rakPeer->AttachPlugin(&fullyConnectedMesh2);
  45. rakPeer->AttachPlugin(&twoWayAuthentication);
  46. rakPeer->AttachPlugin(&connectionGraph2);
  47. if (!cloudServerHelper.StartRakPeer(rakPeer))
  48. return 1;
  49. RakNet::CloudServerHelperFilter sampleFilter; // Keeps clients from updating stuff to the server they are not supposed to
  50. sampleFilter.serverGuid=rakPeer->GetMyGUID();
  51. cloudServerHelper.SetupPlugins(&cloudServer, &sampleFilter, &cloudClient, &fullyConnectedMesh2, &twoWayAuthentication,&connectionGraph2, cloudServerHelper.serverToServerPassword);
  52. int ret;
  53. do
  54. {
  55. ret = cloudServerHelper.JoinCloud(rakPeer, &cloudServer, &cloudClient, &fullyConnectedMesh2, &twoWayAuthentication, &connectionGraph2, dynDNS.GetMyPublicIP());
  56. } while (ret==2);
  57. if (ret==1)
  58. return 1;
  59. // Should now be connect to the cloud, using authentication and FullyConnectedMesh2
  60. printf("Running.\n");
  61. RakNet::Packet *packet;
  62. while (1)
  63. {
  64. for (packet=rakPeer->Receive(); packet; rakPeer->DeallocatePacket(packet), packet=rakPeer->Receive())
  65. {
  66. cloudServerHelper.OnPacket(packet, rakPeer, &cloudClient, &cloudServer, &fullyConnectedMesh2, &twoWayAuthentication, &connectionGraph2);
  67. }
  68. // Update() returns false on DNS update failure
  69. if (!cloudServerHelper.Update())
  70. break;
  71. // Any additional server processing beyond hosting the CloudServer can go here
  72. RakSleep(30);
  73. }
  74. RakNet::RakPeerInterface::DestroyInstance(rakPeer);
  75. return 0;
  76. }
粤ICP备19079148号