public CFileBlockRequest(MemoryStream buffer,byte[] FileHash,ref CFileBlock block1,ref CFileBlock block2,ref CFileBlock block3) { BinaryWriter writer=new BinaryWriter(buffer); DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommand.RequestParts,writer); writer.Write(FileHash); writer.Write(block1.start); writer.Write(block2.start); writer.Write(block3.start); writer.Write(block1.end+1); writer.Write(block2.end+1); writer.Write(block3.end+1); header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1; writer.Seek(0,SeekOrigin.Begin); header.Serialize(writer); }
public void RequestBlocks() { #if VERBOSE CKernel.LogClient.AddLog("RequestBlocks,download blocks.count:"+Convert.ToString(m_DownloadBlocks.Count),m_ID); #endif if (m_DownloadElement==null) { if (DownFileHash!=null) { m_DownloadElement=(CElement)CKernel.FilesList[DownFileHash]; if ((m_DownloadElement!=null)&&(m_DownloadElement.SourcesList!=null)) { m_DownloadElement.SourcesList.AddSource(this); } else { // probablemente el fichero ya se ha completado return; } } else { return; } } m_DownloadState=Protocol.DownloadState.Downloading; //CElement Element=(CElement)CKernel.FilesList[CKernel.HashToString(DownFileHash)]; uint start=0; uint end=0; int chunk=0; CFileBlock[] blocks=new CFileBlock[3]; for (uint i=0;i!=3;i++) { blocks[i]=new CFileBlock(); //Debug.Write("LastChunk:"+m_LastChunkRequested.ToString()); chunk=m_DownloadElement.SourcesList.SelectChunk(m_DownFileChunks,m_LastChunkRequested); m_LastChunkRequested=chunk; //Debug.WriteLine("NewChunk:"+m_LastChunkRequested.ToString()); if (chunk>=0) { if (m_DownloadElement.File.GetNewBlockInChunk((uint)chunk,ref start,ref end)) { blocks[i].start=start; blocks[i].end=end; blocks[i].FileHash=DownFileHash; blocks[i].position=start; //blocks[i].buffer=new Byte[fin+1-inicio]; m_DownloadBlocks.Add(blocks[i]); if ((end==0)&&(start==0)) { Debug.WriteLine("no more blocks to request "+Convert.ToString(chunk)); } else { //Debug.WriteLine(m_UserName+", ChunksStatus = "+Convert.ToString(m_DownloadElement.File.ChunksStatus[0])/*+Convert.ToString(m_DownloadElement.File.GetChunksStatus()[1])*/); #if VERBOSE CKernel.LogClient.AddLog("RequestBlocks-chunk requested "+Convert.ToString(chunk)+" block: "+Convert.ToString(start)+"-"+Convert.ToString(end)+"\n",m_ID); #endif } } } } if ((blocks[0].start==0)&&(blocks[0].end==0)) { #if VERBOSE CKernel.LogClient.AddLog("RequestBlocks-first block is null,no request blocks",m_ID); #endif #if DEBUG Debug.WriteLine(m_UserName+" Parte: "+Convert.ToString(chunk)+" first block is null,no request blocks"); for (int j=0;j<m_DownloadElement.File.ChunksStatus.Length;j++) { if (((m_DownloadElement.File.ChunksStatus[j]==(byte)Protocol.ChunkState.Empty)|| (m_DownloadElement.File.ChunksStatus[j]==(byte)Protocol.ChunkState.InProgress))&& (m_DownFileChunks[j]==(byte)Protocol.ChunkState.Complete)) Debug.WriteLine("Chunk not selected when available"); } #endif if (m_DownloadBlocks.Count==0) { //no needed new blocks to download and nothing pending for receiving bool willBeChangedToOtherFile=((m_OtherDownFiles!=null)&&(m_OtherDownFiles.Count>0)); CancelDownloadTransfer(false); if (!willBeChangedToOtherFile) { m_DownloadState=Protocol.DownloadState.NoNeededParts; } } } else { MemoryStream packet=new MemoryStream(); CFileBlockRequest fileBlockRequest=new CFileBlockRequest(packet,DownFileHash,ref blocks[0],ref blocks[1],ref blocks[2]); if (connection!=null) { connection.SendPacket(packet); } } }
public CFileBlockRequest(MemoryStream buffer) { byte[] FileHash; if (buffer.Length!=40) return; CFileBlock block1=new CFileBlock(); CFileBlock block2=new CFileBlock(); CFileBlock block3=new CFileBlock(); RequestedBlocks=new ArrayList(); BinaryReader reader=new BinaryReader(buffer); //FileHash=new Byte[16]; FileHash=reader.ReadBytes(16); block1.start=reader.ReadUInt32(); block2.start=reader.ReadUInt32(); block3.start=reader.ReadUInt32(); block1.end=reader.ReadUInt32(); block2.end=reader.ReadUInt32(); block3.end=reader.ReadUInt32(); block1.FileHash=FileHash; block2.FileHash=FileHash; block3.FileHash=FileHash; if (block1.end>block1.start) RequestedBlocks.Add(block1); if (block2.end>block2.start) RequestedBlocks.Add(block2); if (block3.end>block3.start) RequestedBlocks.Add(block3); reader.Close(); buffer.Close(); reader=null; buffer=null; }