llxfer.cpp

Go to the documentation of this file.
00001 
00032 #include "linden_common.h"
00033 
00034 #include "llxfer.h"
00035 #include "lluuid.h"
00036 #include "llerror.h"
00037 #include "llmath.h"
00038 #include "u64.h"
00039 
00040 //number of bytes sent in each message
00041 const U32 LL_XFER_CHUNK_SIZE = 1000;
00042 
00043 const U32 LLXfer::XFER_FILE = 1;
00044 const U32 LLXfer::XFER_VFILE = 2;
00045 const U32 LLXfer::XFER_MEM = 3;
00046 
00048 
00049 LLXfer::LLXfer (S32 chunk_size)
00050 {
00051         init(chunk_size);
00052 }
00053 
00055 
00056 LLXfer::~LLXfer ()
00057 {
00058         free();
00059 }
00060 
00062 
00063 void LLXfer::init (S32 chunk_size)
00064 {
00065         mID = 0;
00066 
00067         mPacketNum = -1; // there's a preincrement before sending the zeroth packet
00068         mXferSize = 0;
00069 
00070         mStatus = e_LL_XFER_UNINITIALIZED;
00071         mNext = NULL;
00072         mWaitingForACK = FALSE;
00073         
00074         mCallback = NULL;
00075         mCallbackDataHandle = NULL;
00076 
00077         mBufferContainsEOF = FALSE;
00078         mBuffer = NULL;
00079         mBufferLength = 0;
00080         mBufferStartOffset = 0;
00081 
00082         mRetries = 0;
00083 
00084         if (chunk_size < 1)
00085         {
00086                 chunk_size = LL_XFER_CHUNK_SIZE;
00087         }
00088         mChunkSize = chunk_size;
00089 }
00090         
00092 
00093 void LLXfer::free ()
00094 {
00095         if (mBuffer)
00096         {
00097                 delete[] mBuffer;
00098                 mBuffer = NULL;
00099         }
00100 }
00101 
00103 
00104 S32 LLXfer::startSend (U64 xfer_id, const LLHost &remote_host)
00105 {
00106         llwarns << "undifferentiated LLXfer::startSend for " << getName() << llendl;
00107         return (-1);
00108 }
00109 
00111 
00112 void LLXfer::setXferSize (S32 xfer_size)
00113 {       
00114         mXferSize = xfer_size;
00115 //      cout << "starting transfer of size: " << xfer_size << endl;
00116 }
00117 
00119 
00120 S32 LLXfer::startDownload()
00121 {
00122         llwarns << "undifferentiated LLXfer::startDownload for " << getName()
00123                         << llendl;
00124         return (-1);
00125 }
00126 
00128 
00129 S32 LLXfer::receiveData (char *datap, S32 data_size)
00130 {
00131         S32 retval = 0;
00132 
00133         if (((S32) mBufferLength + data_size) > getMaxBufferSize())
00134         {
00135                 retval = flush();
00136         }
00137 
00138         if (!retval)
00139         {
00140                 if (datap != NULL)
00141                 {
00142                         memcpy(&mBuffer[mBufferLength],datap,data_size);        /*Flawfinder: ignore*/
00143                         mBufferLength += data_size;
00144                 }
00145                 else
00146                 {
00147                         llerrs << "NULL data passed in receiveData" << llendl;
00148                 }
00149         }
00150 
00151         return (retval);
00152 }
00153 
00155 
00156 S32 LLXfer::flush()
00157 {
00158         // only files have somewhere to flush to
00159         // if we get called with a flush it means we've blown past our
00160         // allocated buffer size
00161 
00162         return (-1);
00163 }
00164 
00165 
00167 
00168 S32 LLXfer::suck(S32 start_position)
00169 {
00170         llwarns << "Attempted to send a packet outside the buffer bounds in LLXfer::suck()" << llendl;
00171         return (-1);
00172 }
00173 
00175 
00176 void LLXfer::sendPacket(S32 packet_num)
00177 {
00178         char fdata_buf[LL_XFER_LARGE_PAYLOAD+4];                /* Flawfinder: ignore */
00179         S32 fdata_size = mChunkSize;
00180         BOOL last_packet = FALSE;
00181         S32 num_copy = 0;
00182 
00183         // if the desired packet is not in our current buffered excerpt from the file. . . 
00184         if (((U32)packet_num*fdata_size < mBufferStartOffset) 
00185                 || ((U32)llmin((U32)mXferSize,(U32)((U32)(packet_num+1)*fdata_size)) > mBufferStartOffset + mBufferLength))
00186         
00187         {
00188                 if (suck(packet_num*fdata_size))  // returns non-zero on failure
00189                 {
00190                         abort(LL_ERR_EOF);
00191                         return;
00192                 }       
00193         }
00194                 
00195         S32 desired_read_position = 0;
00196                 
00197         desired_read_position = packet_num * fdata_size - mBufferStartOffset;
00198         
00199         fdata_size = llmin((S32)mBufferLength-desired_read_position, mChunkSize);
00200 
00201         if (fdata_size < 0)
00202         {
00203                 llwarns << "negative data size in xfer send, aborting" << llendl;
00204                 abort(LL_ERR_EOF);
00205                 return;
00206         }
00207 
00208         if (((U32)(desired_read_position + fdata_size) >= (U32)mBufferLength) && (mBufferContainsEOF))
00209         {
00210                 last_packet = TRUE;
00211         }
00212                 
00213         if (packet_num)
00214         { 
00215                 num_copy = llmin(fdata_size, (S32)sizeof(fdata_buf));
00216                 num_copy = llmin(num_copy, (S32)(mBufferLength - desired_read_position));
00217                 if (num_copy > 0)
00218                 {
00219                         memcpy(fdata_buf,&mBuffer[desired_read_position],num_copy);     /*Flawfinder: ignore*/
00220                 }
00221         }
00222         else  
00223         {
00224                 // if we're the first packet, encode size as an additional S32
00225                 // at start of data.
00226                 num_copy = llmin(fdata_size, (S32)(sizeof(fdata_buf)-sizeof(S32)));
00227                 num_copy = llmin(
00228                         num_copy,
00229                         (S32)(mBufferLength - desired_read_position));
00230                 if (num_copy > 0)
00231                 {
00232                         memcpy( /*Flawfinder: ignore*/
00233                                 fdata_buf + sizeof(S32),
00234                                 &mBuffer[desired_read_position],
00235                                 num_copy);
00236                 }
00237                 fdata_size += sizeof(S32);
00238                 htonmemcpy(fdata_buf,&mXferSize, MVT_S32, sizeof(S32));
00239         }
00240 
00241         S32 encoded_packetnum = encodePacketNum(packet_num,last_packet);
00242                 
00243         if (fdata_size)
00244         {
00245                 // send the packet 
00246                 gMessageSystem->newMessageFast(_PREHASH_SendXferPacket);
00247                 gMessageSystem->nextBlockFast(_PREHASH_XferID);
00248                 
00249                 gMessageSystem->addU64Fast(_PREHASH_ID, mID);
00250                 gMessageSystem->addU32Fast(_PREHASH_Packet, encoded_packetnum);
00251                 
00252                 gMessageSystem->nextBlockFast(_PREHASH_DataPacket);
00253                 gMessageSystem->addBinaryDataFast(_PREHASH_Data, &fdata_buf,fdata_size);
00254                         
00255                 gMessageSystem->sendMessage(mRemoteHost);
00256 
00257                 ACKTimer.reset();
00258                 mWaitingForACK = TRUE;
00259         }
00260         if (last_packet)
00261         {
00262                 mStatus = e_LL_XFER_COMPLETE;   
00263         }
00264         else
00265         {
00266                 mStatus = e_LL_XFER_IN_PROGRESS;
00267         }
00268 }
00269 
00271 
00272 void LLXfer::sendNextPacket()
00273 {
00274         mRetries = 0;
00275         sendPacket(++mPacketNum);
00276 }
00277 
00279 
00280 void LLXfer::resendLastPacket()
00281 {
00282         mRetries++;
00283         sendPacket(mPacketNum);
00284 }
00285 
00287 
00288 S32 LLXfer::processEOF()
00289 {
00290         S32 retval = 0;
00291 
00292         mStatus = e_LL_XFER_COMPLETE;
00293 
00294         if (LL_ERR_NOERR == mCallbackResult)
00295         {
00296                 llinfos << "xfer from " << mRemoteHost << " complete: " << getName()
00297                                 << llendl;
00298         }
00299         else
00300         {
00301                 llinfos << "xfer from " << mRemoteHost << " failed, code "
00302                                 << mCallbackResult << ": " << getName() << llendl;
00303         }
00304 
00305         if (mCallback)
00306         {
00307                 mCallback(mCallbackDataHandle,mCallbackResult,LL_EXSTAT_NONE);
00308         }
00309 
00310         return(retval);
00311 }
00312 
00314 
00315 S32 LLXfer::encodePacketNum(S32 packet_num, BOOL is_EOF)
00316 {
00317         if (is_EOF)
00318         {
00319                 packet_num |= 0x80000000;
00320         }
00321         return packet_num;
00322 }
00323 
00325 
00326 void LLXfer::abort (S32 result_code)
00327 {
00328         mCallbackResult = result_code;
00329 
00330         llinfos << "Aborting xfer from " << mRemoteHost << " named " << getName()
00331                         << " - error: " << result_code << llendl;
00332 
00333         gMessageSystem->newMessageFast(_PREHASH_AbortXfer);
00334         gMessageSystem->nextBlockFast(_PREHASH_XferID);
00335         gMessageSystem->addU64Fast(_PREHASH_ID, mID);
00336         gMessageSystem->addS32Fast(_PREHASH_Result, result_code);
00337         
00338         gMessageSystem->sendMessage(mRemoteHost);
00339 
00340         mStatus = e_LL_XFER_ABORTED;
00341 }
00342 
00343 
00345 
00346 const char * LLXfer::getName() 
00347 {
00348         static char tmp_str[256];               /* Flawfinder: ignore */
00349 
00350         return (U64_to_str(mID, tmp_str, sizeof(tmp_str)));
00351 }
00352 
00354 
00355 U32 LLXfer::getXferTypeTag()
00356 {
00357         return 0;
00358 }
00359 
00361 
00362 S32 LLXfer::getMaxBufferSize ()
00363 {
00364         return(mXferSize);
00365 }
00366 
00367 
00368 std::ostream& operator<< (std::ostream& os, LLXfer &hh)
00369 {
00370         os << hh.getName() ;
00371         return os;
00372 }
00373 
00374 
00375 
00376 
00377 
00378 
00379 
00380 
00381 

Generated on Fri May 16 08:32:32 2008 for SecondLife by  doxygen 1.5.5