예제 #1
0
        public void ProcessChunksStatus(MemoryStream packet, bool udpConnection)
        {
            #if VERBOSE
            CKernel.LogClient.AddLog("ProcessChunksStatus",m_ID);
            #endif
            if ((DownFileHash==null)||(m_DownloadElement==null)) return;

            CFileStatus fileStatus=new CFileStatus(packet,!udpConnection);

            if ((!udpConnection)&&(!CKernel.SameHash(ref DownFileHash,ref fileStatus.FileHash)))
            {
                Exception e=new Exception("Chunks status: invalid fileHash");
                throw(e);
            }

            if (!CKernel.SameHash(ref DownFileHash,ref m_DownloadElement.File.FileHash))
            {
                m_DownloadElement=CKernel.FilesList[DownFileHash];
            }

            if ((CHash.GetChunksCount(m_DownloadElement.File.FileSize)!=fileStatus.nChunks)
                && (fileStatus.nChunks!=0))
            {
                m_DownFileChunks=null;
                Exception e=new Exception("Received chunks numer does not match with file chunks number\n");
                throw(e);
            }
            if (m_DownloadElement.SourcesList!=null) m_DownloadElement.SourcesList.RemoveChunksAvaibility(m_DownFileChunks);
            uint nChunks=CHash.GetChunksCount(m_DownloadElement.File.FileSize);
            if (fileStatus.Chunks==null)
            {
                if ((m_DownFileChunks==null)||(m_DownFileChunks.Length!=nChunks))
                {
                    m_DownFileChunks=new byte[nChunks];
                }

                for(int i=0; i!=m_DownFileChunks.Length; i++)
                {
                    m_DownFileChunks[i]=(byte)Protocol.ChunkState.Complete;
                }
                //TODO hay alguna función para hacer esto en .NET equivalente a memset?
            }
            else
            {
                if ((m_DownFileChunks==null)||(m_DownFileChunks.Length!=nChunks))
                {
                    m_DownFileChunks=new byte[nChunks];
                }
                Buffer.BlockCopy(fileStatus.Chunks,0,m_DownFileChunks,0,fileStatus.Chunks.Length);
            }

            if (fileStatus.nChunks>0)
            {
                m_DownloadState=Protocol.DownloadState.NoNeededParts;

                for (int i=0; i!=m_DownFileChunks.Length; i++)
                {
                    if (((Protocol.ChunkState)m_DownFileChunks[i]==Protocol.ChunkState.Complete)
                        &&((m_DownloadElement.File.ChunksStatus.Length==0)
                        ||((Protocol.ChunkState)m_DownloadElement.File.ChunksStatus[i]!=Protocol.ChunkState.Complete)))
                    {
                        m_DownloadState=Protocol.DownloadState.OnQueue;
                        break;
                    }
                }
            }
            else
            {
                m_DownloadState=Protocol.DownloadState.OnQueue;
            }

            //m_DownloadElement.SourcesList.UpdateChunksAvaibility();
            if (m_DownloadElement.SourcesList!=null) m_DownloadElement.SourcesList.AddChunksAvaibility(m_DownFileChunks);

            if (udpConnection) return;

            packet.Close();
            packet=null;
            // ver si necesitamos el hashset del fichero(si no lo tenemos aun lo pedimos)
            // CElement Element=(CElement)CKernel.FilesList[CKernel.HashToString(DownFileHash)];
            // TODO no es necesario pedir hashSets de ficheros de una única parte
            if ((m_DownloadElement.File.HashSetNeeded())&&(m_DownloadState!=Protocol.DownloadState.NoNeededParts))
            {
            #if VERBOSE
                CKernel.LogClient.AddLog("ProcesarInformacionPartesFichero-Pide HashSet",m_ID);
            #endif
                m_DownloadState=Protocol.DownloadState.ReqestHashSet;

                MemoryStream responsePacket=new MemoryStream();
                CHashSetRequest hashSet=new CHashSetRequest(DownFileHash,responsePacket);

                if (connection!=null)
                {
                    connection.SendPacket(responsePacket);
                }
            }
            else
            {
                if (m_DownloadState!=Protocol.DownloadState.NoNeededParts)
                {
            #if VERBOSE
                    CKernel.LogClient.AddLog("ProcesarInformacionPartesFichero-Envía IniciarDescarga",m_ID);
            #endif
                    MemoryStream responsePacket=new MemoryStream();
                    CStartDownload startDonwload=new CStartDownload(responsePacket,DownFileHash);

                    if (connection!=null)
                    {
                        connection.SendPacket(responsePacket);
                    }
                }
            }
        }
예제 #2
0
        public void ProcessRequestFileID(byte[] packet)
        {
            #if VERBOSE
            CKernel.LogClient.AddLog("ProcessRequestFileID",m_ID);
            #endif
            CIDFileChange IDFileChange=new CIDFileChange(new MemoryStream(packet));

            if (!CKernel.SameHash(ref m_UpFileHash,ref IDFileChange.FileHash))
            {
                m_UpFileHash=IDFileChange.FileHash;
                m_UploadElement=(CElement)CKernel.FilesList[m_UpFileHash];
            }

            if ((m_UploadElement==null)||(m_UploadElement.File.Empty))
            {
                //Debug.Write("No se encontro fichero (Cambio ID) \n");
                MemoryStream buffer=new MemoryStream();
                CNoFile noFile=new CNoFile(buffer,m_UpFileHash);
                m_UpFileHash=null;
                if (connection!=null)
                {
                    connection.SendPacket(buffer);
                }
            #if VERBOSE
                CKernel.LogClient.AddLog("ProcessRequestFileID-No found",m_ID);
            #endif
            }
            else
            {
            #if VERBOSE
                CKernel.LogClient.AddLog("ProcessRequestFileID-send file status",m_ID);
            #endif
                MemoryStream packetState=new MemoryStream();
                CFileStatus fileState;

                if (m_UploadElement.File.Completed)
                {
                    fileState=new CFileStatus(m_UpFileHash,null,packetState);
                }
                else
                {
                    fileState=new CFileStatus(m_UpFileHash,m_UploadElement.File.ChunksStatus,packetState);
                }

                if (connection!=null)
                {
                    connection.SendPacket(packetState);
                }
            }
        }