コード例 #1
0
ファイル: Client.cs プロジェクト: sonicwang1989/lphant
        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);
                }
            }
        }
コード例 #2
0
ファイル: Client.cs プロジェクト: sonicwang1989/lphant
        public void ProcessRequestParts(byte[] packet)
        {
            #if VERBOSE
            CKernel.LogClient.AddLog("ProcessRequestParts",m_ID);
            #endif
            if (m_UploadState!=Protocol.UploadState.Uploading) return;

            CFileBlockRequest BlockRequest=new CFileBlockRequest(new MemoryStream(packet));

            foreach (CFileBlock block in BlockRequest.RequestedBlocks)
            {
                m_UploadBlocks.Add(block);
            }
        }