netDownload.cc 7.7 KB

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