main.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "RakSleep.h"
  12. #include "RelayPlugin.h"
  13. #include "Gets.h"
  14. #include "Kbhit.h"
  15. #include "BitStream.h"
  16. #include "MessageIdentifiers.h"
  17. using namespace RakNet;
  18. int main(void)
  19. {
  20. printf("Tests the RelayPlugin as a server.\n");
  21. printf("Difficulty: Beginner\n\n");
  22. char str[64], str2[64];
  23. RakNet::RakPeerInterface *peer=RakNet::RakPeerInterface::GetInstance();
  24. RelayPlugin *relayPlugin = RelayPlugin::GetInstance();
  25. peer->AttachPlugin(relayPlugin);
  26. // Get our input
  27. char ip[64], serverPort[30], listenPort[30];
  28. puts("Enter the port to listen on");
  29. Gets(listenPort,sizeof(listenPort));
  30. if (listenPort[0]==0)
  31. strcpy(listenPort, "1234");
  32. relayPlugin->SetAcceptAddParticipantRequests(true);
  33. // Connecting the client is very simple. 0 means we don't care about
  34. // a connectionValidationInteger, and false for low priority threads
  35. RakNet::SocketDescriptor socketDescriptor(atoi(listenPort),0);
  36. socketDescriptor.socketFamily=AF_INET;
  37. peer->Startup(8,&socketDescriptor, 1);
  38. peer->SetMaximumIncomingConnections(8);
  39. peer->SetOccasionalPing(true);
  40. puts("Enter IP to connect to, or enter for none");
  41. Gets(ip, sizeof(ip));
  42. peer->AllowConnectionResponseIPMigration(false);
  43. if (ip[0])
  44. {
  45. puts("Enter the port to connect to");
  46. Gets(serverPort,sizeof(serverPort));
  47. if (serverPort[0]==0)
  48. strcpy(serverPort, "1234");
  49. RakNet::ConnectionAttemptResult car = peer->Connect(ip, atoi(serverPort), 0, 0);
  50. RakAssert(car==RakNet::CONNECTION_ATTEMPT_STARTED);
  51. }
  52. peer->SetTimeoutTime(30000, UNASSIGNED_SYSTEM_ADDRESS);
  53. peer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS).ToString(str);
  54. printf("My GUID is %s\n", str);
  55. printf("(A)ddParticipantRequestFromClient\n");
  56. printf("(R)emoveParticipantRequestFromClient\n");
  57. printf("SendTo(P)articipant\n");
  58. printf("(S)endGroupMessage\n");
  59. printf("(J)oinGroupRequest\n");
  60. printf("(L)eaveGroup\n");
  61. printf("(G)etGroupList\n");
  62. printf("(Q)uit\n");
  63. char name[128];
  64. while (1)
  65. {
  66. if (kbhit())
  67. {
  68. char ch = getch();
  69. if (ch=='a' || ch=='A')
  70. {
  71. printf("Enter name of participant: ");
  72. Gets(name, sizeof(name));
  73. if (name[0])
  74. {
  75. relayPlugin->AddParticipantRequestFromClient(name,peer->GetGUIDFromIndex(0));
  76. printf("Done\n");
  77. }
  78. else
  79. {
  80. printf("Operation aborted\n");
  81. }
  82. }
  83. else if (ch=='r' || ch=='R')
  84. {
  85. relayPlugin->RemoveParticipantRequestFromClient(peer->GetGUIDFromIndex(0));
  86. printf("Done\n");
  87. }
  88. else if (ch=='p' || ch=='P')
  89. {
  90. char name[128];
  91. printf("Enter name of participant: ");
  92. Gets(name, sizeof(name));
  93. if (name[0])
  94. {
  95. printf("Enter message to send: ");
  96. char msg[256];
  97. Gets(msg, sizeof(msg));
  98. RakString msgRs = msg;
  99. BitStream msgBs;
  100. msgBs.WriteCompressed(msgRs);
  101. relayPlugin->SendToParticipant(peer->GetGUIDFromIndex(0), name, &msgBs, HIGH_PRIORITY, RELIABLE_ORDERED, 0 );
  102. printf("Done\n");
  103. }
  104. else
  105. {
  106. printf("Operation aborted\n");
  107. }
  108. }
  109. else if (ch=='s' || ch=='S')
  110. {
  111. printf("Enter message to send: ");
  112. char msg[256];
  113. Gets(msg, sizeof(msg));
  114. RakString msgRs = msg;
  115. BitStream msgBs;
  116. msgBs.Write(msgRs);
  117. relayPlugin->SendGroupMessage(peer->GetGUIDFromIndex(0), &msgBs, HIGH_PRIORITY, RELIABLE_ORDERED, 0 );
  118. printf("Done\n");
  119. }
  120. else if (ch=='j' || ch=='J')
  121. {
  122. printf("Enter group name to join: ");
  123. char msg[256];
  124. Gets(msg, sizeof(msg));
  125. relayPlugin->JoinGroupRequest(peer->GetGUIDFromIndex(0), msg);
  126. printf("Done\n");
  127. }
  128. else if (ch=='l' || ch=='l')
  129. {
  130. relayPlugin->LeaveGroup(peer->GetGUIDFromIndex(0));
  131. printf("Done\n");
  132. }
  133. else if (ch=='g' || ch=='G')
  134. {
  135. relayPlugin->GetGroupList(peer->GetGUIDFromIndex(0));
  136. printf("Done\n");
  137. }
  138. else if (ch=='q')
  139. {
  140. break;
  141. }
  142. }
  143. Packet *packet;
  144. for (packet=peer->Receive(); packet; peer->DeallocatePacket(packet), packet=peer->Receive())
  145. {
  146. packet->guid.ToString(str);
  147. packet->systemAddress.ToString(true,str2);
  148. if (packet->data[0]==ID_NEW_INCOMING_CONNECTION)
  149. {
  150. printf("ID_NEW_INCOMING_CONNECTION from %s on %s\n", str, str2);
  151. }
  152. else if (packet->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
  153. {
  154. printf("ID_CONNECTION_REQUEST_ACCEPTED from %s on %s\n", str, str2);
  155. }
  156. else if (packet->data[0]==ID_CONNECTION_LOST)
  157. {
  158. printf("ID_CONNECTION_LOST from %s on %s\n", str, str2);
  159. }
  160. else if (packet->data[0]==ID_RELAY_PLUGIN)
  161. {
  162. BitStream msgRs;
  163. RakString senderRs;
  164. BitStream bsIn(packet->data, packet->length, false);
  165. bsIn.IgnoreBytes(sizeof(MessageID));
  166. RelayPluginEnums rpe;
  167. bsIn.ReadCasted<MessageID>(rpe);
  168. switch (rpe)
  169. {
  170. case RPE_MESSAGE_TO_CLIENT_FROM_SERVER:
  171. {
  172. RakString senderName;
  173. bsIn.ReadCompressed(senderName);
  174. bsIn.AlignReadToByteBoundary();
  175. RakString dataInAsStr;
  176. bsIn.ReadCompressed(dataInAsStr);
  177. printf("RPE_MESSAGE_TO_CLIENT_FROM_SERVER from %s, data=%s\n", senderName.C_String(), dataInAsStr.C_String());
  178. }
  179. break;
  180. case RPE_ADD_CLIENT_NOT_ALLOWED:
  181. {
  182. RakString senderName;
  183. bsIn.ReadCompressed(senderName);
  184. printf("RPE_ADD_CLIENT_NOT_ALLOWED for %s\n", senderName.C_String());
  185. }
  186. break;
  187. case RPE_ADD_CLIENT_TARGET_NOT_CONNECTED:
  188. {
  189. RakString senderName;
  190. bsIn.ReadCompressed(senderName);
  191. printf("RPE_ADD_CLIENT_TARGET_NOT_CONNECTED for %s\n", senderName.C_String());
  192. }
  193. break;
  194. case RPE_ADD_CLIENT_NAME_ALREADY_IN_USE:
  195. {
  196. RakString senderName;
  197. bsIn.ReadCompressed(senderName);
  198. printf("RPE_ADD_CLIENT_NAME_ALREADY_IN_USE for %s\n", senderName.C_String());
  199. }
  200. break;
  201. case RPE_ADD_CLIENT_SUCCESS:
  202. {
  203. RakString senderName;
  204. bsIn.ReadCompressed(senderName);
  205. printf("RPE_ADD_CLIENT_SUCCESS for %s\n", senderName.C_String());
  206. }
  207. break;
  208. case RPE_USER_ENTERED_ROOM:
  209. {
  210. RakString whichUser;
  211. bsIn.ReadCompressed(whichUser);
  212. printf("RPE_USER_ENTERED_ROOM user=%s\n", whichUser.C_String());
  213. }
  214. break;
  215. case RPE_USER_LEFT_ROOM:
  216. {
  217. RakString whichUser;
  218. bsIn.ReadCompressed(whichUser);
  219. printf("RPE_USER_LEFT_ROOM user=%s\n", whichUser.C_String());
  220. }
  221. break;
  222. case RPE_GROUP_MSG_FROM_SERVER:
  223. {
  224. RakString senderName;
  225. bsIn.ReadCompressed(senderName);
  226. bsIn.AlignReadToByteBoundary();
  227. RakString dataInAsStr;
  228. bsIn.Read(dataInAsStr);
  229. printf("RPE_GROUP_MSG_FROM_SERVER from %s, data=%s\n", senderName.C_String(), dataInAsStr.C_String());
  230. }
  231. break;
  232. case RPE_GET_GROUP_LIST_REPLY_FROM_SERVER:
  233. {
  234. uint16_t chatRoomsSize, usersInRoomSize;
  235. bsIn.Read(chatRoomsSize);
  236. RakString roomName;
  237. printf("RPE_GET_GROUP_LIST_REPLY_FROM_SERVER %i rooms\n", chatRoomsSize);
  238. for (uint16_t chatRoomsIdx=0; chatRoomsIdx < chatRoomsSize; chatRoomsIdx++)
  239. {
  240. bsIn.ReadCompressed(roomName);
  241. bsIn.Read(usersInRoomSize);
  242. printf("%i. %s %i users\n", chatRoomsIdx+1, roomName.C_String(), usersInRoomSize);
  243. }
  244. }
  245. break;
  246. default:
  247. {
  248. RakAssert(0);
  249. }
  250. }
  251. }
  252. }
  253. RakSleep(30);
  254. }
  255. return 1;
  256. }
粤ICP备19079148号