netDownload.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "core/dnet.h"
  24. #include "console/simBase.h"
  25. #include "console/engineAPI.h"
  26. #include "sim/netConnection.h"
  27. #include "core/stream/bitStream.h"
  28. #include "core/stream/fileStream.h"
  29. #include "sim/netObject.h"
  30. class FileDownloadRequestEvent : public NetEvent
  31. {
  32. public:
  33. typedef NetEvent Parent;
  34. enum
  35. {
  36. MaxFileNames = 31,
  37. };
  38. U32 nameCount;
  39. char mFileNames[MaxFileNames][256];
  40. FileDownloadRequestEvent(Vector<char *> *nameList = NULL)
  41. {
  42. nameCount = 0;
  43. if(nameList)
  44. {
  45. nameCount = nameList->size();
  46. if(nameCount > MaxFileNames)
  47. nameCount = MaxFileNames;
  48. for(U32 i = 0; i < nameCount; i++)
  49. {
  50. dStrcpy(mFileNames[i], (*nameList)[i], 256);
  51. //Con::printf("Sending request for file %s", mFileNames[i]);
  52. }
  53. }
  54. }
  55. virtual void pack(NetConnection *, BitStream *bstream)
  56. {
  57. bstream->writeRangedU32(nameCount, 0, MaxFileNames);
  58. for(U32 i = 0; i < nameCount; i++)
  59. bstream->writeString(mFileNames[i]);
  60. }
  61. virtual void write(NetConnection *, BitStream *bstream)
  62. {
  63. bstream->writeRangedU32(nameCount, 0, MaxFileNames);
  64. for(U32 i = 0; i < nameCount; i++)
  65. bstream->writeString(mFileNames[i]);
  66. }
  67. virtual void unpack(NetConnection *, BitStream *bstream)
  68. {
  69. nameCount = bstream->readRangedU32(0, MaxFileNames);
  70. for(U32 i = 0; i < nameCount; i++)
  71. bstream->readString(mFileNames[i]);
  72. }
  73. virtual void process(NetConnection *connection)
  74. {
  75. U32 i;
  76. for(i = 0; i < nameCount; i++)
  77. if(connection->startSendingFile(mFileNames[i]))
  78. break;
  79. if(i == nameCount)
  80. connection->startSendingFile(NULL); // none of the files were sent
  81. }
  82. DECLARE_CONOBJECT(FileDownloadRequestEvent);
  83. };
  84. IMPLEMENT_CO_NETEVENT_V1(FileDownloadRequestEvent);
  85. ConsoleDocClass( FileDownloadRequestEvent,
  86. "@brief Used by NetConnection for transmitting requests to obtain files from server during loading.\n\n"
  87. "Not intended for game development, for editors or internal use only.\n\n "
  88. "@internal");
  89. class FileChunkEvent : public NetEvent
  90. {
  91. public:
  92. typedef NetEvent Parent;
  93. enum
  94. {
  95. ChunkSize = 63,
  96. };
  97. U8 chunkData[ChunkSize];
  98. U32 chunkLen;
  99. FileChunkEvent(U8 *data = NULL, U32 len = 0)
  100. {
  101. if(data)
  102. dMemcpy(chunkData, data, len);
  103. chunkLen = len;
  104. }
  105. virtual void pack(NetConnection *, BitStream *bstream)
  106. {
  107. bstream->writeRangedU32(chunkLen, 0, ChunkSize);
  108. bstream->write(chunkLen, chunkData);
  109. }
  110. virtual void write(NetConnection *, BitStream *bstream)
  111. {
  112. bstream->writeRangedU32(chunkLen, 0, ChunkSize);
  113. bstream->write(chunkLen, chunkData);
  114. }
  115. virtual void unpack(NetConnection *, BitStream *bstream)
  116. {
  117. chunkLen = bstream->readRangedU32(0, ChunkSize);
  118. bstream->read(chunkLen, chunkData);
  119. }
  120. virtual void process(NetConnection *connection)
  121. {
  122. connection->chunkReceived(chunkData, chunkLen);
  123. }
  124. virtual void notifyDelivered(NetConnection *nc, bool madeIt)
  125. {
  126. if(!nc->isRemoved())
  127. nc->sendFileChunk();
  128. }
  129. DECLARE_CONOBJECT(FileChunkEvent);
  130. };
  131. IMPLEMENT_CO_NETEVENT_V1(FileChunkEvent);
  132. ConsoleDocClass( FileChunkEvent,
  133. "@brief Used by NetConnection for sending/receiving chunks of data.\n\n"
  134. "Not intended for game development, for editors or internal use only.\n\n "
  135. "@internal");
  136. void NetConnection::sendFileChunk()
  137. {
  138. U8 buffer[FileChunkEvent::ChunkSize];
  139. U32 len = FileChunkEvent::ChunkSize;
  140. if(len + mCurrentFileBufferOffset > mCurrentFileBufferSize)
  141. len = mCurrentFileBufferSize - mCurrentFileBufferOffset;
  142. if(!len)
  143. {
  144. delete mCurrentDownloadingFile;
  145. mCurrentDownloadingFile = NULL;
  146. return;
  147. }
  148. mCurrentFileBufferOffset += len;
  149. mCurrentDownloadingFile->read(len, buffer);
  150. postNetEvent(new FileChunkEvent(buffer, len));
  151. }
  152. bool NetConnection::startSendingFile(const char *fileName)
  153. {
  154. if(!fileName || Con::getBoolVariable("$NetConnection::neverUploadFiles"))
  155. {
  156. sendConnectionMessage(SendNextDownloadRequest);
  157. return false;
  158. }
  159. mCurrentDownloadingFile = FileStream::createAndOpen( fileName, Torque::FS::File::Read );
  160. if(!mCurrentDownloadingFile)
  161. {
  162. // the server didn't have the file, so send a 0 byte chunk:
  163. Con::printf("No such file '%s'.", fileName);
  164. postNetEvent(new FileChunkEvent(NULL, 0));
  165. return false;
  166. }
  167. Con::printf("Sending file '%s'.", fileName);
  168. mCurrentFileBufferSize = mCurrentDownloadingFile->getStreamSize();
  169. mCurrentFileBufferOffset = 0;
  170. // always have 32 file chunks (64 bytes each) in transit
  171. sendConnectionMessage(FileDownloadSizeMessage, mCurrentFileBufferSize);
  172. for(U32 i = 0; i < 32; i++)
  173. sendFileChunk();
  174. return true;
  175. }
  176. void NetConnection::sendNextFileDownloadRequest()
  177. {
  178. // see if we've already downloaded this file...
  179. while(mMissingFileList.size() && (Torque::FS::IsFile(mMissingFileList[0]) || Con::getBoolVariable("$NetConnection::neverDownloadFiles")))
  180. {
  181. dFree(mMissingFileList[0]);
  182. mMissingFileList.pop_front();
  183. }
  184. if(mMissingFileList.size())
  185. {
  186. postNetEvent(new FileDownloadRequestEvent(&mMissingFileList));
  187. }
  188. else
  189. {
  190. fileDownloadSegmentComplete();
  191. }
  192. }
  193. void NetConnection::chunkReceived(U8 *chunkData, U32 chunkLen)
  194. {
  195. if(chunkLen == 0)
  196. {
  197. // the server didn't have the file... apparently it's one we don't need...
  198. dFree(mCurrentFileBuffer);
  199. mCurrentFileBuffer = NULL;
  200. dFree(mMissingFileList[0]);
  201. mMissingFileList.pop_front();
  202. return;
  203. }
  204. if(chunkLen + mCurrentFileBufferOffset > mCurrentFileBufferSize)
  205. {
  206. setLastError("Invalid file chunk from server.");
  207. return;
  208. }
  209. dMemcpy(((U8 *) mCurrentFileBuffer) + mCurrentFileBufferOffset, chunkData, chunkLen);
  210. mCurrentFileBufferOffset += chunkLen;
  211. if(mCurrentFileBufferOffset == mCurrentFileBufferSize)
  212. {
  213. // this file's done...
  214. // save it to disk:
  215. FileStream *stream;
  216. Con::printf("Saving file %s.", mMissingFileList[0]);
  217. if((stream = FileStream::createAndOpen( mMissingFileList[0], Torque::FS::File::Write )) == NULL)
  218. {
  219. setLastError("Couldn't open file downloaded by server.");
  220. return;
  221. }
  222. dFree(mMissingFileList[0]);
  223. mMissingFileList.pop_front();
  224. stream->write(mCurrentFileBufferSize, mCurrentFileBuffer);
  225. delete stream;
  226. mNumDownloadedFiles++;
  227. dFree(mCurrentFileBuffer);
  228. mCurrentFileBuffer = NULL;
  229. sendNextFileDownloadRequest();
  230. }
  231. else
  232. {
  233. Con::executef("onFileChunkReceived", mMissingFileList[0], Con::getIntArg(mCurrentFileBufferOffset), Con::getIntArg(mCurrentFileBufferSize));
  234. }
  235. }