netDownload.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 "sim/netConnection.h"
  26. #include "core/stream/bitStream.h"
  27. #include "core/stream/fileStream.h"
  28. #include "sim/netObject.h"
  29. class FileDownloadRequestEvent : public NetEvent
  30. {
  31. public:
  32. typedef NetEvent Parent;
  33. enum
  34. {
  35. MaxFileNames = 31,
  36. };
  37. U32 nameCount;
  38. char mFileNames[MaxFileNames][256];
  39. FileDownloadRequestEvent(Vector<char *> *nameList = NULL)
  40. {
  41. nameCount = 0;
  42. if(nameList)
  43. {
  44. nameCount = nameList->size();
  45. if(nameCount > MaxFileNames)
  46. nameCount = MaxFileNames;
  47. for(U32 i = 0; i < nameCount; i++)
  48. {
  49. dStrcpy(mFileNames[i], (*nameList)[i]);
  50. //Con::printf("Sending request for file %s", mFileNames[i]);
  51. }
  52. }
  53. }
  54. virtual void pack(NetConnection *, BitStream *bstream)
  55. {
  56. bstream->writeRangedU32(nameCount, 0, MaxFileNames);
  57. for(U32 i = 0; i < nameCount; i++)
  58. bstream->writeString(mFileNames[i]);
  59. }
  60. virtual void write(NetConnection *, BitStream *bstream)
  61. {
  62. bstream->writeRangedU32(nameCount, 0, MaxFileNames);
  63. for(U32 i = 0; i < nameCount; i++)
  64. bstream->writeString(mFileNames[i]);
  65. }
  66. virtual void unpack(NetConnection *, BitStream *bstream)
  67. {
  68. nameCount = bstream->readRangedU32(0, MaxFileNames);
  69. for(U32 i = 0; i < nameCount; i++)
  70. bstream->readString(mFileNames[i]);
  71. }
  72. virtual void process(NetConnection *connection)
  73. {
  74. U32 i;
  75. for(i = 0; i < nameCount; i++)
  76. if(connection->startSendingFile(mFileNames[i]))
  77. break;
  78. if(i == nameCount)
  79. connection->startSendingFile(NULL); // none of the files were sent
  80. }
  81. DECLARE_CONOBJECT(FileDownloadRequestEvent);
  82. };
  83. IMPLEMENT_CO_NETEVENT_V1(FileDownloadRequestEvent);
  84. ConsoleDocClass( FileDownloadRequestEvent,
  85. "@brief Used by NetConnection for transmitting requests to obtain files from server during loading.\n\n"
  86. "Not intended for game development, for editors or internal use only.\n\n "
  87. "@internal");
  88. class FileChunkEvent : public NetEvent
  89. {
  90. public:
  91. typedef NetEvent Parent;
  92. enum
  93. {
  94. ChunkSize = 63,
  95. };
  96. U8 chunkData[ChunkSize];
  97. U32 chunkLen;
  98. FileChunkEvent(U8 *data = NULL, U32 len = 0)
  99. {
  100. if(data)
  101. dMemcpy(chunkData, data, len);
  102. chunkLen = len;
  103. }
  104. virtual void pack(NetConnection *, BitStream *bstream)
  105. {
  106. bstream->writeRangedU32(chunkLen, 0, ChunkSize);
  107. bstream->write(chunkLen, chunkData);
  108. }
  109. virtual void write(NetConnection *, BitStream *bstream)
  110. {
  111. bstream->writeRangedU32(chunkLen, 0, ChunkSize);
  112. bstream->write(chunkLen, chunkData);
  113. }
  114. virtual void unpack(NetConnection *, BitStream *bstream)
  115. {
  116. chunkLen = bstream->readRangedU32(0, ChunkSize);
  117. bstream->read(chunkLen, chunkData);
  118. }
  119. virtual void process(NetConnection *connection)
  120. {
  121. connection->chunkReceived(chunkData, chunkLen);
  122. }
  123. virtual void notifyDelivered(NetConnection *nc, bool madeIt)
  124. {
  125. if(!nc->isRemoved())
  126. nc->sendFileChunk();
  127. }
  128. DECLARE_CONOBJECT(FileChunkEvent);
  129. };
  130. IMPLEMENT_CO_NETEVENT_V1(FileChunkEvent);
  131. ConsoleDocClass( FileChunkEvent,
  132. "@brief Used by NetConnection for sending/receiving chunks of data.\n\n"
  133. "Not intended for game development, for editors or internal use only.\n\n "
  134. "@internal");
  135. void NetConnection::sendFileChunk()
  136. {
  137. U8 buffer[FileChunkEvent::ChunkSize];
  138. U32 len = FileChunkEvent::ChunkSize;
  139. if(len + mCurrentFileBufferOffset > mCurrentFileBufferSize)
  140. len = mCurrentFileBufferSize - mCurrentFileBufferOffset;
  141. if(!len)
  142. {
  143. delete mCurrentDownloadingFile;
  144. mCurrentDownloadingFile = NULL;
  145. return;
  146. }
  147. mCurrentFileBufferOffset += len;
  148. mCurrentDownloadingFile->read(len, buffer);
  149. postNetEvent(new FileChunkEvent(buffer, len));
  150. }
  151. bool NetConnection::startSendingFile(const char *fileName)
  152. {
  153. if(!fileName || Con::getBoolVariable("$NetConnection::neverUploadFiles"))
  154. {
  155. sendConnectionMessage(SendNextDownloadRequest);
  156. return false;
  157. }
  158. mCurrentDownloadingFile = FileStream::createAndOpen( fileName, Torque::FS::File::Read );
  159. if(!mCurrentDownloadingFile)
  160. {
  161. // the server didn't have the file, so send a 0 byte chunk:
  162. Con::printf("No such file '%s'.", fileName);
  163. postNetEvent(new FileChunkEvent(NULL, 0));
  164. return false;
  165. }
  166. Con::printf("Sending file '%s'.", fileName);
  167. mCurrentFileBufferSize = mCurrentDownloadingFile->getStreamSize();
  168. mCurrentFileBufferOffset = 0;
  169. // always have 32 file chunks (64 bytes each) in transit
  170. sendConnectionMessage(FileDownloadSizeMessage, mCurrentFileBufferSize);
  171. for(U32 i = 0; i < 32; i++)
  172. sendFileChunk();
  173. return true;
  174. }
  175. void NetConnection::sendNextFileDownloadRequest()
  176. {
  177. // see if we've already downloaded this file...
  178. while(mMissingFileList.size() && (Torque::FS::IsFile(mMissingFileList[0]) || Con::getBoolVariable("$NetConnection::neverDownloadFiles")))
  179. {
  180. dFree(mMissingFileList[0]);
  181. mMissingFileList.pop_front();
  182. }
  183. if(mMissingFileList.size())
  184. {
  185. postNetEvent(new FileDownloadRequestEvent(&mMissingFileList));
  186. }
  187. else
  188. {
  189. fileDownloadSegmentComplete();
  190. }
  191. }
  192. void NetConnection::chunkReceived(U8 *chunkData, U32 chunkLen)
  193. {
  194. if(chunkLen == 0)
  195. {
  196. // the server didn't have the file... apparently it's one we don't need...
  197. dFree(mCurrentFileBuffer);
  198. mCurrentFileBuffer = NULL;
  199. dFree(mMissingFileList[0]);
  200. mMissingFileList.pop_front();
  201. return;
  202. }
  203. if(chunkLen + mCurrentFileBufferOffset > mCurrentFileBufferSize)
  204. {
  205. setLastError("Invalid file chunk from server.");
  206. return;
  207. }
  208. dMemcpy(((U8 *) mCurrentFileBuffer) + mCurrentFileBufferOffset, chunkData, chunkLen);
  209. mCurrentFileBufferOffset += chunkLen;
  210. if(mCurrentFileBufferOffset == mCurrentFileBufferSize)
  211. {
  212. // this file's done...
  213. // save it to disk:
  214. FileStream *stream;
  215. Con::printf("Saving file %s.", mMissingFileList[0]);
  216. if((stream = FileStream::createAndOpen( mMissingFileList[0], Torque::FS::File::Write )) == NULL)
  217. {
  218. setLastError("Couldn't open file downloaded by server.");
  219. return;
  220. }
  221. dFree(mMissingFileList[0]);
  222. mMissingFileList.pop_front();
  223. stream->write(mCurrentFileBufferSize, mCurrentFileBuffer);
  224. delete stream;
  225. mNumDownloadedFiles++;
  226. dFree(mCurrentFileBuffer);
  227. mCurrentFileBuffer = NULL;
  228. sendNextFileDownloadRequest();
  229. }
  230. else
  231. {
  232. Con::executef("onFileChunkReceived", mMissingFileList[0], Con::getIntArg(mCurrentFileBufferOffset), Con::getIntArg(mCurrentFileBufferSize));
  233. }
  234. }