main.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /// \file
  11. /// \brief Test the command console implementations
  12. #include "TCPInterface.h"
  13. #include "ConsoleServer.h"
  14. #include "RakNetCommandParser.h"
  15. #include "TelnetTransport.h"
  16. #include "RakPeerInterface.h"
  17. #include "LogCommandParser.h"
  18. #include "GetTime.h"
  19. #include "RakNetTransport2.h"
  20. #include "LinuxStrings.h"
  21. #include <stdio.h>
  22. void TestTCPInterface(void);
  23. void TestCommandServer(RakNet::TransportInterface *ti, unsigned short port, RakNet::RakPeerInterface *rakPeer);
  24. int main(void)
  25. {
  26. RakNet::RakPeerInterface *rakPeer = RakNet::RakPeerInterface::GetInstance();
  27. RakNet::SocketDescriptor sd(60000,0);
  28. rakPeer->Startup(128,&sd,1);
  29. rakPeer->SetMaximumIncomingConnections(128);
  30. RakNet::TelnetTransport tt;
  31. TestCommandServer(&tt, 23, rakPeer); // Uncomment to use Telnet as a client. Telnet uses port 23 by default.
  32. // RakNet::RakNetTransport2 rt2;
  33. // rakPeer->AttachPlugin(&rt2);
  34. // TestCommandServer(&rt2, 60000,rakPeer); // Uncomment to use RakNet as a client
  35. return 1;
  36. }
  37. void TestCommandServer(RakNet::TransportInterface *ti, unsigned short port, RakNet::RakPeerInterface *rakPeer)
  38. {
  39. RakNet::ConsoleServer consoleServer;
  40. RakNet::RakNetCommandParser rcp;
  41. RakNet::LogCommandParser lcp;
  42. RakNet::TimeMS lastLog=0;
  43. printf("This sample demonstrates the command console server, which can be.\n");
  44. printf("a standalone application or part of your game server. It allows you to\n");
  45. printf("easily parse text strings sent from a client using either secure RakNet\n");
  46. printf("or Telnet.\n");
  47. printf("See the 'CommandConsoleClient' project for the RakNet client.\n");
  48. printf("Difficulty: Intermediate\n\n");
  49. printf("Command server started on port %i.\n", port);
  50. consoleServer.AddCommandParser(&rcp);
  51. consoleServer.AddCommandParser(&lcp);
  52. consoleServer.SetTransportProvider(ti, port);
  53. consoleServer.SetPrompt("> "); // Show this character when waiting for user input
  54. rcp.SetRakPeerInterface(rakPeer);
  55. lcp.AddChannel("TestChannel");
  56. while (1)
  57. {
  58. consoleServer.Update();
  59. // Ignore raknet packets for this sample.
  60. rakPeer->DeallocatePacket(rakPeer->Receive());
  61. if (RakNet::GetTimeMS() > lastLog + 4000)
  62. {
  63. lcp.WriteLog("TestChannel", "Test of logger");
  64. lastLog=RakNet::GetTimeMS();
  65. }
  66. #ifdef _WIN32
  67. Sleep( 30 );
  68. #else
  69. usleep( 30 * 1000 );
  70. #endif
  71. }
  72. }
粤ICP备19079148号