main.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "RakPeerInterface.h"
  11. #include "MessageIdentifiers.h"
  12. #include "BitStream.h"
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #ifdef _WIN32
  17. #include "Kbhit.h"
  18. #include "WindowsIncludes.h" // Sleep
  19. #else
  20. #include "Kbhit.h"
  21. #include <unistd.h> // usleep
  22. #include <strings.h>
  23. //linux doesn't have stricmp but strcasecmp is same functionality
  24. #define stricmp strcasecmp
  25. #endif
  26. #include "Gets.h"
  27. int main()
  28. {
  29. RakNet::Packet *packet;
  30. RakNet::RakPeerInterface *rakPeer;
  31. bool isConnected=false;
  32. rakPeer=RakNet::RakPeerInterface::GetInstance();
  33. char command[512];
  34. printf("This sample demonstrates connecting to the command console.\n");
  35. printf("using the RakNet transport protocol\n");
  36. printf("It's the equivalent of a secure telnet client\n");
  37. printf("See the 'CommandConsoleServer' project.\n");
  38. printf("Difficulty: Intermediate\n\n");
  39. printf("RakNet secure command console.\n");
  40. printf("Commands:\n");
  41. printf("/Connect\n");
  42. printf("/Disconnect\n");
  43. printf("/Quit\n");
  44. printf("Any other command goes to the remote console\n");
  45. while (1)
  46. {
  47. if (kbhit())
  48. {
  49. Gets(command,sizeof(command));
  50. if (stricmp(command, "/quit")==0)
  51. {
  52. printf("Goodbye.\n");
  53. rakPeer->Shutdown(500, 0);
  54. return 0;
  55. }
  56. else if (stricmp(command, "/disconnect")==0)
  57. {
  58. if (isConnected)
  59. {
  60. rakPeer->Shutdown(500, 0);
  61. isConnected=false;
  62. printf("Disconnecting.\n");
  63. }
  64. else
  65. {
  66. printf("Not currently connected.\n");
  67. }
  68. }
  69. else if (stricmp(command, "/connect")==0)
  70. {
  71. if (isConnected)
  72. {
  73. printf("Disconnect first.\n");
  74. }
  75. else
  76. {
  77. char ip[128];
  78. char remotePort[64];
  79. char password[512];
  80. char localPort[64];
  81. printf("Enter remote IP: ");
  82. do {
  83. Gets(ip, sizeof(ip));
  84. } while(ip[0]==0);
  85. printf("Enter remote port: ");
  86. do {
  87. Gets(remotePort,sizeof(remotePort));
  88. } while(remotePort[0]==0);
  89. printf("Enter local port (enter for 0): ");
  90. Gets(localPort,sizeof(localPort));
  91. if (localPort[0]==0)
  92. {
  93. strcpy(localPort, "0");
  94. }
  95. printf("Enter console password (enter for none): ");
  96. Gets(password,sizeof(password));
  97. RakNet::SocketDescriptor socketDescriptor((int) atoi(localPort),0);
  98. if (rakPeer->Startup(1, &socketDescriptor, 1)==RakNet::RAKNET_STARTED)
  99. {
  100. int passwordLen;
  101. if (password[0])
  102. passwordLen=(int) strlen(password)+1;
  103. else
  104. passwordLen=0;
  105. if (rakPeer->Connect(ip, (int) atoi(remotePort), password, passwordLen)==RakNet::CONNECTION_ATTEMPT_STARTED)
  106. printf("Connecting...\nNote: if the password is wrong the other system will ignore us.\n");
  107. else
  108. {
  109. printf("Connect call failed.\n");
  110. rakPeer->Shutdown(0, 0);
  111. }
  112. }
  113. else
  114. printf("Initialize call failed.\n");
  115. }
  116. }
  117. else
  118. {
  119. if (isConnected)
  120. {
  121. RakNet::BitStream str;
  122. str.Write((unsigned char) ID_TRANSPORT_STRING);
  123. str.Write(command, (int) strlen(command)+1);
  124. rakPeer->Send(&str, MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  125. }
  126. else
  127. {
  128. printf("You must be connected to send commands.\n");
  129. }
  130. }
  131. }
  132. packet = rakPeer->Receive();
  133. if (packet)
  134. {
  135. switch (packet->data[0])
  136. {
  137. case ID_DISCONNECTION_NOTIFICATION:
  138. printf("The server disconnected us.\n");
  139. isConnected=false;
  140. break;
  141. case ID_CONNECTION_BANNED:
  142. printf("We are banned from this server.\n");
  143. isConnected=false;
  144. break;
  145. case ID_CONNECTION_ATTEMPT_FAILED:
  146. printf("Connection attempt failed.\nThe password was wrong or there is no responsive machine at that IP/port.\n");
  147. isConnected=false;
  148. break;
  149. case ID_NO_FREE_INCOMING_CONNECTIONS:
  150. printf("Server is full.\n");
  151. isConnected=false;
  152. break;
  153. case ID_CONNECTION_LOST:
  154. printf("We lost the connection.\n");
  155. isConnected=false;
  156. break;
  157. case ID_CONNECTION_REQUEST_ACCEPTED:
  158. printf("Connection accepted.\n");
  159. isConnected=true;
  160. break;
  161. case ID_TRANSPORT_STRING:
  162. printf("%s", packet->data+1);
  163. break;
  164. }
  165. rakPeer->DeallocatePacket(packet);
  166. }
  167. // This sleep keeps RakNet responsive
  168. #ifdef _WIN32
  169. Sleep(30);
  170. #else
  171. usleep(30 * 1000);
  172. #endif
  173. }
  174. return 0;
  175. }
粤ICP备19079148号