main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 <stdio.h>
  11. #include <stdlib.h>
  12. #include "Lobby2Client_Steam.h" // If Lobby2Client_Steam.h is included before SocketLayer.h, then it will use the steam send functions
  13. #include "Lobby2Message_Steam.h"
  14. #include "RakNetTime.h"
  15. #include "RakSleep.h"
  16. #include "RakNetTypes.h"
  17. #include "RakPeerInterface.h"
  18. #include "GetTime.h"
  19. #include "MessageIdentifiers.h"
  20. #include <windows.h>
  21. #include <Kbhit.h>
  22. #include "Gets.h"
  23. #include "FullyConnectedMesh2.h"
  24. #include "steam_api.h"
  25. using namespace RakNet;
  26. Lobby2MessageFactory_Steam *messageFactory;
  27. Lobby2Client_Steam *lobby2Client;
  28. RakNet::RakPeerInterface *rakPeer;
  29. RakNet::FullyConnectedMesh2 *fcm2;
  30. uint64_t lastRoom;
  31. void PrintCommands(void)
  32. {
  33. printf("a. GetRoomList\n");
  34. printf("b. LeaveRoom\n");
  35. printf("c. CreateRoom\n");
  36. printf("d. JoinRoom\n");
  37. printf("e. RefreshRoom\n");
  38. printf("f. SendRoomChatMessage\n");
  39. printf("g. ListRoomMembers\n");
  40. printf("?. Help\n");
  41. printf("(Escape). Quit\n");
  42. }
  43. struct SteamResults : public RakNet::Lobby2Callbacks
  44. {
  45. virtual void MessageResult(RakNet::Notification_Console_MemberJoinedRoom *message)
  46. {
  47. RakNet::Notification_Console_MemberJoinedRoom_Steam *msgSteam = (RakNet::Notification_Console_MemberJoinedRoom_Steam *) message;
  48. RakNet::RakString msg;
  49. msgSteam->DebugMsg(msg);
  50. printf("%s\n", msg.C_String());
  51. // Guy with the lower ID connects to the guy with the higher ID
  52. uint64_t mySteamId=SteamUser()->GetSteamID().ConvertToUint64();
  53. if (mySteamId < msgSteam->srcMemberId)
  54. {
  55. // Steam's NAT punch is implicit, so it takes a long time to connect. Give it extra time
  56. unsigned int sendConnectionAttemptCount=24;
  57. unsigned int timeBetweenSendConnectionAttemptsMS=500;
  58. ConnectionAttemptResult car = rakPeer->Connect(msgSteam->remoteSystem.ToString(false), msgSteam->remoteSystem.GetPort(), 0, 0, 0, 0, sendConnectionAttemptCount, timeBetweenSendConnectionAttemptsMS);
  59. RakAssert(car==CONNECTION_ATTEMPT_STARTED);
  60. }
  61. }
  62. virtual void MessageResult(RakNet::Console_SearchRooms *message)
  63. {
  64. RakNet::Console_SearchRooms_Steam *msgSteam = (RakNet::Console_SearchRooms_Steam *) message;
  65. RakNet::RakString msg;
  66. msgSteam->DebugMsg(msg);
  67. printf("%s\n", msg.C_String());
  68. if (msgSteam->roomIds.GetSize()>0)
  69. {
  70. lastRoom=msgSteam->roomIds[0];
  71. }
  72. }
  73. virtual void ExecuteDefaultResult(RakNet::Lobby2Message *message)
  74. {
  75. RakNet::RakString out;
  76. message->DebugMsg(out);
  77. printf("%s\n", out.C_String());
  78. }
  79. };
  80. int main(int argc, char **argv)
  81. {
  82. if (argc>1)
  83. {
  84. printf("Command arguments:\n");
  85. for (int i=1; i < argc; i++)
  86. {
  87. printf("%i. %s\n", i, argv[i]);
  88. }
  89. }
  90. SteamResults steamResults;
  91. rakPeer = RakNet::RakPeerInterface::GetInstance();
  92. fcm2 = RakNet::FullyConnectedMesh2::GetInstance();
  93. messageFactory = new Lobby2MessageFactory_Steam;
  94. lobby2Client = Lobby2Client_Steam::GetInstance();
  95. lobby2Client->AddCallbackInterface(&steamResults);
  96. lobby2Client->SetMessageFactory(messageFactory);
  97. SocketDescriptor sd(1234,0);
  98. rakPeer->Startup(32,&sd,1);
  99. rakPeer->SetMaximumIncomingConnections(32);
  100. rakPeer->AttachPlugin(fcm2);
  101. rakPeer->AttachPlugin(lobby2Client);
  102. // Connect manually in Notification_Console_MemberJoinedRoom
  103. fcm2->SetConnectOnNewRemoteConnection(false, "");
  104. RakNet::Lobby2Message* msg = messageFactory->Alloc(RakNet::L2MID_Client_Login);
  105. lobby2Client->SendMsg(msg);
  106. if (msg->resultCode!=L2RC_PROCESSING && msg->resultCode!=L2RC_SUCCESS)
  107. {
  108. printf("Steam must be running to play this game (SteamAPI_Init() failed).\n");
  109. printf("If this fails, steam_appid.txt was probably not in the working directory.\n");
  110. messageFactory->Dealloc(msg);
  111. return -1;
  112. }
  113. messageFactory->Dealloc(msg);
  114. PrintCommands();
  115. bool quit=false;
  116. char ch;
  117. while(!quit)
  118. {
  119. RakNet::Packet *packet;
  120. for (packet=rakPeer->Receive(); packet; rakPeer->DeallocatePacket(packet), packet=rakPeer->Receive())
  121. {
  122. switch (packet->data[0])
  123. {
  124. case ID_DISCONNECTION_NOTIFICATION:
  125. // Connection lost normally
  126. printf("ID_DISCONNECTION_NOTIFICATION\n");
  127. break;
  128. case ID_ALREADY_CONNECTED:
  129. // Connection lost normally
  130. printf("ID_ALREADY_CONNECTED\n");
  131. break;
  132. case ID_REMOTE_DISCONNECTION_NOTIFICATION: // Server telling the clients of another client disconnecting gracefully. You can manually broadcast this in a peer to peer enviroment if you want.
  133. printf("ID_REMOTE_DISCONNECTION_NOTIFICATION\n");
  134. break;
  135. case ID_REMOTE_CONNECTION_LOST: // Server telling the clients of another client disconnecting forcefully. You can manually broadcast this in a peer to peer enviroment if you want.
  136. printf("ID_REMOTE_CONNECTION_LOST\n");
  137. break;
  138. case ID_REMOTE_NEW_INCOMING_CONNECTION: // Server telling the clients of another client connecting. You can manually broadcast this in a peer to peer enviroment if you want.
  139. printf("ID_REMOTE_NEW_INCOMING_CONNECTION\n");
  140. break;
  141. case ID_CONNECTION_BANNED: // Banned from this server
  142. printf("We are banned from this server.\n");
  143. break;
  144. case ID_CONNECTION_ATTEMPT_FAILED:
  145. printf("Connection attempt failed\n");
  146. break;
  147. case ID_NO_FREE_INCOMING_CONNECTIONS:
  148. // Sorry, the server is full. I don't do anything here but
  149. // A real app should tell the user
  150. printf("ID_NO_FREE_INCOMING_CONNECTIONS\n");
  151. break;
  152. case ID_INVALID_PASSWORD:
  153. printf("ID_INVALID_PASSWORD\n");
  154. break;
  155. case ID_CONNECTION_LOST:
  156. // Couldn't deliver a reliable packet - i.e. the other system was abnormally
  157. // terminated
  158. printf("ID_CONNECTION_LOST\n");
  159. break;
  160. case ID_CONNECTION_REQUEST_ACCEPTED:
  161. // This tells the client they have connected
  162. printf("ID_CONNECTION_REQUEST_ACCEPTED to %s with GUID %s\n", packet->systemAddress.ToString(), packet->guid.ToString());
  163. break;
  164. case ID_NEW_INCOMING_CONNECTION:
  165. printf("ID_NEW_INCOMING_CONNECTION\n");
  166. break;
  167. case ID_FCM2_NEW_HOST:
  168. {
  169. if (packet->systemAddress==RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  170. printf("Got new host (ourselves)");
  171. else
  172. printf("Got new host %s, GUID=%s", packet->systemAddress.ToString(true), packet->guid.ToString());
  173. RakNet::BitStream bs(packet->data,packet->length,false);
  174. bs.IgnoreBytes(1);
  175. RakNetGUID oldHost;
  176. bs.Read(oldHost);
  177. // If the old host is different, then this message was due to losing connection to the host.
  178. if (oldHost!=packet->guid)
  179. printf(". Oldhost Guid=%s\n", oldHost.ToString());
  180. else
  181. printf("\n");
  182. }
  183. break;
  184. default:
  185. // It's a client, so just show the message
  186. printf("Unknown Message ID %i\n", packet->data[0]);
  187. break;
  188. }
  189. }
  190. if (kbhit())
  191. {
  192. ch=(char)getch();
  193. switch (ch)
  194. {
  195. case 'a':
  196. {
  197. RakNet::Lobby2Message* logoffMsg = messageFactory->Alloc(RakNet::L2MID_Console_SearchRooms);
  198. lobby2Client->SendMsg(logoffMsg);
  199. messageFactory->Dealloc(logoffMsg);
  200. }
  201. break;
  202. case 'b':
  203. {
  204. if (lobby2Client->GetRoomID()==0)
  205. {
  206. printf("Not in a room\n");
  207. break;
  208. }
  209. RakNet::Console_LeaveRoom_Steam* msg = (RakNet::Console_LeaveRoom_Steam*) messageFactory->Alloc(RakNet::L2MID_Console_LeaveRoom);
  210. msg->roomId=lobby2Client->GetRoomID();
  211. lobby2Client->SendMsg(msg);
  212. messageFactory->Dealloc(msg);
  213. }
  214. break;
  215. case 'c':
  216. {
  217. if (lobby2Client->GetRoomID()!=0)
  218. {
  219. printf("Already in a room\n");
  220. break;
  221. }
  222. RakNet::Console_CreateRoom_Steam* msg = (RakNet::Console_CreateRoom_Steam*) messageFactory->Alloc(RakNet::L2MID_Console_CreateRoom);
  223. // set the name of the lobby if it's ours
  224. char rgchLobbyName[256];
  225. msg->roomIsPublic=true;
  226. _snprintf( rgchLobbyName, sizeof( rgchLobbyName ), "%s's lobby", SteamFriends()->GetPersonaName() );
  227. msg->roomName=rgchLobbyName;
  228. msg->publicSlots=8;
  229. lobby2Client->SendMsg(msg);
  230. messageFactory->Dealloc(msg);
  231. }
  232. break;
  233. case 'd':
  234. {
  235. if (lobby2Client->GetRoomID()!=0)
  236. {
  237. printf("Already in a room\n");
  238. break;
  239. }
  240. RakNet::Console_JoinRoom_Steam* msg = (RakNet::Console_JoinRoom_Steam*) messageFactory->Alloc(RakNet::L2MID_Console_JoinRoom);
  241. printf("Enter room id, or enter for %" PRINTF_64_BIT_MODIFIER "u: ", lastRoom);
  242. char str[256];
  243. Gets(str, sizeof(str));
  244. if (str[0]==0)
  245. {
  246. msg->roomId=lastRoom;
  247. }
  248. else
  249. {
  250. msg->roomId=_atoi64(str);
  251. }
  252. lobby2Client->SendMsg(msg);
  253. messageFactory->Dealloc(msg);
  254. }
  255. break;
  256. case 'e':
  257. {
  258. if (lobby2Client->GetRoomID()==0)
  259. {
  260. printf("Not in a room\n");
  261. break;
  262. }
  263. RakNet::Console_GetRoomDetails_Steam* msg = (RakNet::Console_GetRoomDetails_Steam*) messageFactory->Alloc(RakNet::L2MID_Console_GetRoomDetails);
  264. msg->roomId=lobby2Client->GetRoomID();
  265. lobby2Client->SendMsg(msg);
  266. messageFactory->Dealloc(msg);
  267. }
  268. break;
  269. case 'f':
  270. {
  271. if (lobby2Client->GetRoomID()==0)
  272. {
  273. printf("Not in a room\n");
  274. break;
  275. }
  276. RakNet::Console_SendRoomChatMessage_Steam* msg = (RakNet::Console_SendRoomChatMessage_Steam*) messageFactory->Alloc(RakNet::L2MID_Console_SendRoomChatMessage);
  277. msg->message="Test chat message.";
  278. msg->roomId=lobby2Client->GetRoomID();
  279. lobby2Client->SendMsg(msg);
  280. messageFactory->Dealloc(msg);
  281. }
  282. break;
  283. case 'g':
  284. {
  285. DataStructures::OrderedList<uint64_t, uint64_t> roomMembers;
  286. lobby2Client->GetRoomMembers(roomMembers);
  287. for (unsigned int i=0; i < roomMembers.Size(); i++)
  288. {
  289. printf("%i. %s ID=%" PRINTF_64_BIT_MODIFIER "u\n", i+1, lobby2Client->GetRoomMemberName(roomMembers[i]), roomMembers[i]);
  290. }
  291. }
  292. break;
  293. case '?':
  294. {
  295. PrintCommands();
  296. }
  297. break;
  298. case 27:
  299. {
  300. quit=true;
  301. }
  302. break;
  303. }
  304. }
  305. RakSleep(30);
  306. }
  307. RakNet::Lobby2Message* logoffMsg = messageFactory->Alloc(RakNet::L2MID_Client_Logoff);
  308. lobby2Client->SendMsg(logoffMsg);
  309. messageFactory->Dealloc(logoffMsg);
  310. rakPeer->DetachPlugin(lobby2Client);
  311. rakPeer->DetachPlugin(fcm2);
  312. RakNet::RakPeerInterface::DestroyInstance(rakPeer);
  313. Lobby2Client_Steam::DestroyInstance(lobby2Client);
  314. RakNet::FullyConnectedMesh2::DestroyInstance(fcm2);
  315. return 1;
  316. }
粤ICP备19079148号