DirectoryDeltaTransfer.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "RakNetworkFactory.h"
  11. #include "GetTime.h"
  12. #include "RakPeerInterface.h"
  13. #include "PacketEnumerations.h"
  14. #include "RakNetStatistics.h"
  15. #include "DirectoryDeltaTransfer.h"
  16. #include "FileListTransfer.h"
  17. #include <cstdio>
  18. #include <stdlib.h>
  19. #include <conio.h>
  20. #include "FileList.h"
  21. #include "DataCompressor.h"
  22. #include "FileListTransferCBInterface.h"
  23. #ifdef _WIN32
  24. #include <windows.h> // Sleep
  25. #else
  26. #include <unistd.h> // usleep
  27. #endif
  28. class TestCB : public FileListTransferCBInterface
  29. {
  30. public:
  31. void OnFile(
  32. unsigned fileIndex,
  33. char *filename,
  34. unsigned char *fileData,
  35. unsigned compressedTransmissionLength,
  36. unsigned finalDataLength,
  37. unsigned short setID,
  38. unsigned setCount,
  39. unsigned setTotalCompressedTransmissionLength,
  40. unsigned setTotalFinalLength)
  41. {
  42. printf("%i. %i/%i %s %ib->%ib / %ib->%ib\n", setID, fileIndex, setCount, filename, compressedTransmissionLength, finalDataLength, setTotalCompressedTransmissionLength, setTotalFinalLength);
  43. }
  44. } transferCallback;
  45. int main(void)
  46. {
  47. char ch;
  48. RakPeerInterface *rakPeer;
  49. // directoryDeltaTransfer is the main plugin that does the work for this sample.
  50. DirectoryDeltaTransfer directoryDeltaTransfer;
  51. // The fileListTransfer plugin is used by the DirectoryDeltaTransfer plugin and must also be registered (you could use this yourself too if you wanted, of course).
  52. FileListTransfer fileListTransfer;
  53. rakPeer = RakNetworkFactory::GetRakPeerInterface();
  54. rakPeer->AttachPlugin(&directoryDeltaTransfer);
  55. rakPeer->AttachPlugin(&fileListTransfer);
  56. directoryDeltaTransfer.SetFileListTransferPlugin(&fileListTransfer);
  57. printf("This sample demonstrates the plugin to incrementally transfer compressed\n");
  58. printf("deltas of directories. In essence, it's a simple autopatcher.\n");
  59. printf("Unlike the full autopatcher, it has no dependencies. It is suitable for\n");
  60. printf("patching from non-dedicated servers at runtime.\n");
  61. printf("Difficulty: Intermediate\n\n");
  62. printf("Enter listen port, or hit enter to choose automatically\n");
  63. unsigned short localPort;
  64. char str[256];
  65. gets(str);
  66. if (str[0]==0)
  67. localPort=60000;
  68. else
  69. localPort=atoi(str);
  70. if (rakPeer->Initialize(8,localPort,30,0)==false)
  71. {
  72. RakNetworkFactory::DestroyRakPeerInterface(rakPeer);
  73. printf("RakNet initialize failed. Possibly duplicate port.\n");
  74. return 1;
  75. }
  76. rakPeer->SetMaximumIncomingConnections(8);
  77. printf("Commands:\n");
  78. printf("(S)et application directory.\n");
  79. printf("(A)dd allowed uploads from subdirectory.\n");
  80. printf("(D)ownload from subdirectory.\n");
  81. printf("(C)lear allowed uploads.\n");
  82. printf("C(o)nnect to another system.\n");
  83. printf("(Q)uit.\n");
  84. Packet *p;
  85. while (1)
  86. {
  87. // Process packets
  88. p=rakPeer->Receive();
  89. while (p)
  90. {
  91. if (p->data[0]==ID_NEW_INCOMING_CONNECTION)
  92. printf("ID_NEW_INCOMING_CONNECTION\n");
  93. else if (p->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
  94. printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
  95. rakPeer->DeallocatePacket(p);
  96. p=rakPeer->Receive();
  97. }
  98. if (kbhit())
  99. {
  100. ch=getch();
  101. if (ch=='s')
  102. {
  103. printf("Enter application directory\n");
  104. gets(str);
  105. if (str[0]==0)
  106. strcpy(str, "C:/RakNet");
  107. directoryDeltaTransfer.SetApplicationDirectory(str);
  108. printf("This directory will be prefixed to upload and download subdirectories.\n");
  109. }
  110. else if (ch=='a')
  111. {
  112. printf("Enter uploads subdirectory\n");
  113. gets(str);
  114. directoryDeltaTransfer.AddUploadsFromSubdirectory(str);
  115. printf("%i files for upload.\n", directoryDeltaTransfer.GetNumberOfFilesForUpload());
  116. }
  117. else if (ch=='d')
  118. {
  119. char subdir[256];
  120. char outputSubdir[256];
  121. printf("Enter remote subdirectory to download from.\n");
  122. printf("This directory may be any uploaded directory, or a subdir therein.\n");
  123. gets(subdir);
  124. printf("Enter subdirectory to output to.\n");
  125. gets(outputSubdir);
  126. unsigned short setId;
  127. setId=directoryDeltaTransfer.DownloadFromSubdirectory(subdir, outputSubdir, true, rakPeer->GetPlayerIDFromIndex(0), &transferCallback, HIGH_PRIORITY, 0);
  128. if (setId==(unsigned short)-1)
  129. printf("Download failed. Host unreachable.\n");
  130. else
  131. printf("Downloading set %i\n", setId);
  132. }
  133. else if (ch=='c')
  134. {
  135. directoryDeltaTransfer.ClearUploads();
  136. printf("Uploads cleared.\n");
  137. }
  138. else if (ch=='o')
  139. {
  140. char host[256];
  141. printf("Enter host IP: ");
  142. gets(host);
  143. if (host[0]==0)
  144. strcpy(host, "127.0.0.1");
  145. unsigned short remotePort;
  146. printf("Enter host port: ");
  147. gets(str);
  148. if (str[0]==0)
  149. remotePort=60000;
  150. else
  151. remotePort=atoi(str);
  152. rakPeer->Connect(host, remotePort, 0, 0);
  153. printf("Connecting.\n");
  154. }
  155. else if (ch=='q')
  156. {
  157. printf("Bye!\n");
  158. break;
  159. }
  160. }
  161. }
  162. RakNetworkFactory::DestroyRakPeerInterface(rakPeer);
  163. return 0;
  164. }
粤ICP备19079148号