#include "main.h" FsFile* LoadFile(unsigned long hash); int HandleOpcode_FS_REQUEST(NetMessage* pData, LocalData* pLir) { //Calculate the number of hashes asked for unsigned int num_hashes=(Read_uShort(pData->pData+2) - 4) / 8; //Start of our requested hash array is 4 bytes in unsigned char* pDataPtr=pData->pData + 4; /*gCoreLog.notice("%u - Requested hashes: ", pLir->networkId); gCoreLog.notice("---------------------");//*/ //Cycle through every hash in the packet. for (unsigned int i=0; i 0) { //Find the file with the hash FsFile* pFile=LoadFile(hash); //If a file with this hash has been registered if (pFile != NULL) { pLir->hashes.push_back(hash); } //No file found registered with requested hash else { CompiledPacket pck; pck.AddByte(pLir->wtfByte); pck.AddByte(FS_NOTFOUND); pck.AddShort(8); pck.AddLong(hash); Util::SendCompiledPacket(&pck, pData->networkId, gpFileServer); gCoreLog.error("No file found for hash 0x%X. Unable to fulfill request.", hash); } //gCoreLog.notice("0x%X", hash); } pDataPtr+=8; } //gCoreLog.notice("---------------------\n"); return Read_uShort(pData->pData + 2); } int HandleOpcode_FS_COUNTER_SETTING(NetMessage* pData, LocalData* pLir) { //The client is requesting more data and that we change our segment size pLir->segmentLength=EXPANDED_SEGMENT_SIZE; return 12; } int HandleOpcode_FS_REQUEST_MORE(NetMessage* pData, LocalData* pLir) { //The client is requesting more data so queue a segment send pLir->queuedSends++; return 8; } void SendDataHeader(LocalData* pLir, unsigned long segmentLength) { //Load the file data FsFile* pFile=LoadFile(pLir->hashes.front()); if(pFile == NULL){ return; } //Calculate the amount of data to send, limit to the segment length unsigned long data_size=pFile->compressedSize - pLir->filePos; data_size=Util::Limit(data_size, pLir->segmentLength); if(data_sizesegmentLength) { pLir->wtfByte++; } //If we still have data left to send if(data_size > 0) { //Build the data response packet header and send it (Data will be sent in chunks after) CompiledPacket pck; pck.AddByte(pLir->wtfByte); pck.AddByte(FS_RESPONSE); pck.AddShort((unsigned short)(data_size + 4)); pLir->bytesRemainingInSegment=data_size; //Calculate the size this packet should be, limit to PACKET_SIZE unsigned long data_size2=pLir->bytesRemainingInSegment; data_size2=Util::Limit(data_size2, CHUNK_SIZE - 4); //Prepare and send the file chunk pck.Add(pFile->pFileData + pLir->filePos, data_size2, false); //Increase our position in the file data pLir->filePos += data_size2; pLir->bytesRemainingInSegment -= data_size2; pLir->stupidSwitch=true; Util::SendCompiledPacket(&pck, pLir->networkId, gpFileServer); } } void SendData(LocalData* pLir) { //Load the file data FsFile* pFile=LoadFile(pLir->hashes.front()); if (pFile==NULL){ return; } //Calculate the size this packet should be, limit to PACKET_SIZE unsigned long data_size=pLir->bytesRemainingInSegment; if (!pLir->stupidSwitch) { data_size=Util::Limit(data_size, CHUNK_SIZE); pLir->stupidSwitch=true; } else { data_size=Util::Limit(data_size, CHUNK_SIZE*2); pLir->stupidSwitch=false; } //Prepare and send the file chunk CompiledPacket pck; pck.Add(pFile->pFileData + pLir->filePos, data_size, false); Util::SendCompiledPacket(&pck, pLir->networkId, gpFileServer); //Increase our position in the file data pLir->filePos += data_size; pLir->bytesRemainingInSegment -= data_size; }