RPC4Sample.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "RPC4Plugin.h"
  11. #include "RakPeerInterface.h"
  12. #include <stdio.h>
  13. #include "Kbhit.h"
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include "RakSleep.h"
  17. #include "BitStream.h"
  18. #include "MessageIdentifiers.h"
  19. #include "Gets.h"
  20. using namespace RakNet;
  21. RakPeerInterface *rakPeer;
  22. void CFunc1( RakNet::BitStream *bitStream, Packet *packet )
  23. {
  24. printf("CFunc1 ");
  25. RakNet::RakString data;
  26. int offset=bitStream->GetReadOffset();
  27. bool read = bitStream->ReadCompressed(data);
  28. RakAssert(read);
  29. printf("%s\n", data.C_String());
  30. };
  31. void CFunc2( RakNet::BitStream *bitStream, Packet *packet )
  32. {
  33. printf("CFunc2 ");
  34. RakNet::RakString data;
  35. int offset=bitStream->GetReadOffset();
  36. bool read = bitStream->ReadCompressed(data);
  37. RakAssert(read);
  38. printf("%s\n", data.C_String());
  39. };
  40. void CFunc3( RakNet::BitStream *bitStream, RakNet::BitStream *returnData, Packet *packet )
  41. {
  42. printf("CFunc3 ");
  43. RakNet::RakString data;
  44. int offset=bitStream->GetReadOffset();
  45. bool read = bitStream->ReadCompressed(data);
  46. RakAssert(read);
  47. printf("%s\n", data.C_String());
  48. returnData->WriteCompressed("CFunc3 returnData");
  49. };
  50. int main(void)
  51. {
  52. printf("Demonstration of the RPC4 plugin.\n");
  53. printf("Difficulty: Beginner\n\n");
  54. rakPeer=RakNet::RakPeerInterface::GetInstance();
  55. // Holds user data
  56. char ip[64], serverPort[30], clientPort[30];
  57. // Get our input
  58. puts("Enter the port to listen on");
  59. Gets(clientPort,sizeof(clientPort));
  60. if (clientPort[0]==0)
  61. strcpy(clientPort, "0");
  62. RakNet::SocketDescriptor sd1(atoi(clientPort),0);
  63. rakPeer->Startup(8,&sd1,1);
  64. rakPeer->SetMaximumIncomingConnections(8);
  65. puts("Enter IP to connect to, or enter for none");
  66. Gets(ip, sizeof(ip));
  67. rakPeer->AllowConnectionResponseIPMigration(false);
  68. RPC4 rpc;
  69. rakPeer->AttachPlugin(&rpc);
  70. rpc.RegisterSlot("Event1", CFunc1, 0);
  71. rpc.RegisterSlot("Event1", CFunc2, 0);
  72. rpc.RegisterBlockingFunction("Blocking", CFunc3);
  73. RakNet::Packet *packet;
  74. if (ip[0])
  75. {
  76. puts("Enter the port to connect to");
  77. Gets(serverPort,sizeof(serverPort));
  78. rakPeer->Connect(ip, atoi(serverPort), 0, 0);
  79. RakSleep(1000);
  80. for (packet=rakPeer->Receive(); packet; rakPeer->DeallocatePacket(packet), packet=rakPeer->Receive())
  81. ;
  82. RakNet::BitStream testBs;
  83. testBs.WriteCompressed("testData");
  84. // rpc.Signal("Event1", &testBs, HIGH_PRIORITY,RELIABLE_ORDERED,0,rakPeer->GetSystemAddressFromIndex(0),false, true);
  85. RakSleep(100);
  86. for (packet=rakPeer->Receive(); packet; rakPeer->DeallocatePacket(packet), rakPeer->Receive())
  87. ;
  88. // Needs 2 program instances, because while the call is blocking rakPeer2->Receive() isn't getting called
  89. RakNet::BitStream testBlockingReturn;
  90. rpc.CallBlocking("Blocking", &testBs, HIGH_PRIORITY,RELIABLE_ORDERED,0,rakPeer->GetSystemAddressFromIndex(0),&testBlockingReturn);
  91. RakNet::RakString data;
  92. bool read = testBlockingReturn.ReadCompressed(data);
  93. printf("%s\n", data.C_String());
  94. }
  95. else
  96. {
  97. printf("Waiting for connections.\n");
  98. while (1)
  99. {
  100. RakSleep(100);
  101. for (packet=rakPeer->Receive(); packet; rakPeer->DeallocatePacket(packet), packet=rakPeer->Receive())
  102. ;
  103. }
  104. }
  105. rakPeer->Shutdown(100,0);
  106. RakNet::RakPeerInterface::DestroyInstance(rakPeer);
  107. return 1;
  108. }
粤ICP备19079148号