public void DeleteFile(string strFileHash) { CElement Element = (CElement)this[CKernel.StringToHash(strFileHash)]; if ((Element != null) && (Element.File.FileStatus == Protocol.FileState.Complete)) { if (Element.SourcesList != null) { m_StopFile(Element); } if (Element.Comments != null) { Element.Comments.Element = null; } Element.Comments = null; try { File.Delete(Element.File.CompleteName); } catch {} Element.File = null; m_FileList.Remove(Element); Element = null; } }
public CElement this[byte[] key] { get { try { CElement result = null; lock (m_FileList.SyncRoot) { foreach (CElement element in m_FileList) { if (CKernel.SameHash(ref key, ref element.File.FileHash)) { result = element; break; } } } return(result); } catch { return(null); } } }
private void AddFile(CFile file) { if (!m_Contains(file.FileHash)) { CElement Element = new CElement(); Element.File = file; Element.Statistics = new CFileStatistics(); m_FileList.Add(Element); if (((file.FileStatus == Protocol.FileState.Ready) || (Element.File.FileStatus == Protocol.FileState.Completing)) && (!file.Completed)) { Element.SourcesList = new CSourcesList(Element); Element.Comments = new CedonkeyComments(Element); } if (!file.Completed) { CKernel.NewFile(Element); } else { CKernel.NewSharedFile(Element); } } else { CLog.Log(Constants.Log.Notify, "FIL_DUP", CKernel.HashToString(file.FileHash)); file = null; } }
public void AddFile(string name, uint size, byte[] Hash, stDatosFuente[] sources) { CElement Element; if (this.m_Contains(Hash)) { CLog.Log(Constants.Log.Notify, "FIL_DUP", name); Element = (CElement)this[Hash]; } else { CFile file = new CFile(Hash, name, size); Element = new CElement(); Element.File = file; Element.Statistics = new CFileStatistics(); m_FileList.Add(Element); CKernel.NewFile(Element); CLog.Log(Constants.Log.Notify, "FIL_ADDED", name); } if ((Element.SourcesList == null) && ((Element.File.FileStatus == Protocol.FileState.Ready) || (Element.File.FileStatus == Protocol.FileState.Completing))) { CSourcesList sourcesList = new CSourcesList(Element); Element.SourcesList = sourcesList; if (CKernel.ServersList.ActiveServer != null) { CKernel.ServersList.ActiveServer.RequestSources(Element.File.FileHash); } } if ((Element.SourcesList != null) && ((Element.File.FileStatus == Protocol.FileState.Ready) || (Element.File.FileStatus == Protocol.FileState.Completing)) && (sources != null)) { CKernel.ClientsList.AddClientsToFile(sources, Hash); } }
public void ResumeFile(string strFileHash) { CElement Elemento = (CElement)this[CKernel.StringToHash(strFileHash)]; if ((Elemento != null) && (Elemento.SourcesList == null) && ((Elemento.File.FileStatus == Protocol.FileState.Stopped) || (Elemento.File.FileStatus == Protocol.FileState.Pause))) { Elemento.SourcesList = new CSourcesList(Elemento); Elemento.File.FileStatus = Protocol.FileState.Ready; if (Elemento.Comments == null) { Elemento.Comments = new CedonkeyComments(Elemento); } Elemento.Statistics.ResetStartTime(); lock (CKernel.Queue.List.SyncRoot) { foreach (CClient Client in CKernel.Queue.List) { if (Client.UploadElement == Elemento) { CKernel.ClientsList.AddClientToFile(Client, Elemento.File.FileHash); } } if (CKernel.ServersList.ActiveServer != null) { CKernel.ServersList.ActiveServer.RequestSources(Elemento.File.FileHash); } } } }
public void RemoveSharedFileIfNotExists(byte[] fileHash) { CElement Element = this[fileHash]; if ((Element != null) && (Element.File != null) && (Element.File.Completed) && (!File.Exists(Element.File.CompleteName))) { m_FileList.Remove(Element); ArrayList toRemove = null; foreach (CClient Client in CKernel.Queue.List) { if (Client.UploadElement == Element) { if (toRemove == null) { toRemove = new ArrayList(); } toRemove.Add(Client); } } if (toRemove != null) { foreach (CClient Client in toRemove) { CKernel.Queue.List.Remove(Client); //Debug.WriteLine(Client.UserName+" cleaned from queue due to deleted file"); CKernel.ClientsList.IsClientNeeded(Client); } } } }
private bool m_AddFileToPacket(BinaryWriter writer, CElement element) { try { if (!element.File.Empty) { writer.Write(element.File.FileHash); if (element.File.Completed) { writer.Write((uint)0xfbfbfbfb); writer.Write((ushort)0xfbfb); } else { writer.Write((uint)0xfcfcfcfc); writer.Write((ushort)0xfcfc); } uint nParameters = 2; if (element.File.Details.ListDetails.ContainsKey(Constants.Avi.Length)) { nParameters++; } if (element.File.Details.ListDetails.ContainsKey(Constants.Avi.VBitrate)) { nParameters++; } if (element.File.Details.ListDetails.ContainsKey(Constants.Avi.VCodec)) { nParameters++; } writer.Write(nParameters); // name new ParameterWriter((byte)Protocol.FileTag.Name, element.File.FileName, writer); // size new ParameterWriter((byte)Protocol.FileTag.Size, element.File.FileSize, writer); if (element.File.Details.Type == (byte)Constants.FileType.Avi) { if (element.File.Details.ListDetails.ContainsKey(Constants.Avi.Length)) { new ParameterWriter(Protocol.FileExtraTags.length.ToString(), (string)element.File.Details.ListDetails[Constants.Avi.Length], writer); } if (element.File.Details.ListDetails.ContainsKey(Constants.Avi.VBitrate)) { new ParameterWriter(Protocol.FileExtraTags.bitrate.ToString(), Convert.ToUInt32(((string)element.File.Details.ListDetails[Constants.Avi.VBitrate]).Replace(" Kbps", "")), writer); } if (element.File.Details.ListDetails.ContainsKey(Constants.Avi.VCodec)) { new ParameterWriter(Protocol.FileExtraTags.codec.ToString(), (string)element.File.Details.ListDetails[Constants.Avi.VCodec], writer); } } return(true); } } catch (Exception e) { Debug.WriteLine(e.ToString()); } return(false); }
private void m_StopFile(CElement Element) { if ((Element != null) && (Element.SourcesList != null)) { Element.SourcesList.Clean(); Element.SourcesList = null; } }
public void AddSource(byte[] FileHash, ref CClient client) { CElement Element = (CElement)this[FileHash]; if ((Element != null) && (Element != null) && (!Element.File.Completed)) { Element.SourcesList.AddSource(client); } }
public void Clean() { m_Element = null; while (m_ArrayList.Count > 0) { CClient Client = (CClient)m_ArrayList[0]; RemoveSource(Client); } }
public void StopFile(byte[] FileHash) { CElement Element = (CElement)this[FileHash]; if ((Element != null) && (Element.File.FileStatus == Protocol.FileState.Ready)) { m_StopFile(Element); Element.File.FileStatus = Protocol.FileState.Stopped; } }
public void SetDownloadPriority(string strFileHash, Constants.Priority priority) { CElement ChangedElement = (CElement)this[CKernel.StringToHash(strFileHash)]; if ((ChangedElement == null) || (ChangedElement.File.DownPriority == priority) || (ChangedElement.File.FileStatus == Protocol.FileState.Complete)) { return; } ChangedElement.File.DownPriority = (Constants.Priority)priority; CKernel.ClientsList.SwapClientsToHigherPriorityFile(); }
internal static void RemovedSharedFile(CElement element) { InterfaceFile file = InterfaceGateway[0].FileToInterfaceFile(element); for (int i = 0; i <= InterfaceGateway.Length - 1; i++) { if (CKernel.InterfaceGateway[i] != null) { InterfaceGateway[i].RemovedSharedFile(file); } } }
public CSourcesList(CElement in_Element) { m_ArrayList = ArrayList.Synchronized(new ArrayList()); m_Element = in_Element; m_Avaibility = null; Random rnd = new Random(); m_LastTCPSearch = DateTime.MinValue; //-TimeSpan.FromSeconds(rnd.Next(60)); m_LastSourceExchangeRequest = DateTime.MinValue; m_DownloadSpeed = 0; }
public void EmptyFileToSharedFile(CFile file) { CElement Element = this[file.FileHash]; if (Element != null) { CKernel.NewSharedFile(Element); } //TODO publish the file on the conected server if (CKernel.ServersList.ActiveServer != null) { CKernel.ServersList.ActiveServer.PublishSharedFile(Element); } }
//constructor to publish one file public CServerPublishFiles(MemoryStream buffer, CElement element) { DonkeyHeader header; BinaryWriter writer = new BinaryWriter(buffer); header = new DonkeyHeader((byte)Protocol.ServerCommand.OfferFiles, writer); writer.Write(1); m_AddFileToPacket(writer, element); header.Packetlength = (uint)writer.BaseStream.Length - header.Packetlength + 1; writer.Seek(0, SeekOrigin.Begin); header.Serialize(writer); writer.Write(1); CLog.Log(Types.Constants.Log.Info, "FIL_PUBLISHED", 1); }
/// <summary> /// Publish one new shared file (only one) /// </summary> public void PublishSharedFile(CElement Element) { MemoryStream buffer = new MemoryStream(); CServerPublishFiles ServerPublishFiles = new CServerPublishFiles(buffer, Element); //if server supports compression we send it compressed if (SupportCompression) { CCompressedPacket CompressedPacket = new CCompressedPacket(ref buffer); } if (m_ServerConnection != null) { m_ServerConnection.SendPacket(buffer); } }
private bool m_LoadFilesFromFolder(string folder /*,bool refreshing*/) { if (!Directory.Exists(folder)) { return(false); } string [] ficheros = Directory.GetFiles(folder); DateTime date; string name; if (m_EdonkeyHash == null) { m_EdonkeyHash = new CHash(); } foreach (string file in ficheros) { // 0 Kb file are ignored, avoid a crash FileInfo filInfo = new FileInfo(file); if ((filInfo.Length == 0) || ((filInfo.Attributes & (FileAttributes.Hidden | FileAttributes.System)) != 0)) { continue; } date = File.GetLastWriteTime(file).ToUniversalTime(); name = Path.GetFileName(file); CKnownFileAttributes Atributos; if ((Atributos = m_IsKnown(name, date)) != null) { //if (refreshing) continue; if (!m_Contains(Atributos.FileHash)) { CElement Element = new CElement(); Element.File = new CFile(Atributos.FileHash, Atributos.Date, folder, name, Atributos.Size, Atributos.Priority, Atributos.Permisions, Atributos.HashSet, Atributos.Comment, Atributos.Rating); Element.SourcesList = null; Element.Statistics = new CFileStatistics(); this.m_FileList.Add(Element); CKernel.NewSharedFile(Element); } //if (!m_Contains(Atributos.FileHash))this.m_FileList.Add(Element); } else //new file { m_EdonkeyHash.AddFile(file); } } return(true); }
public void CompleteFile(string strFileHash) { CElement Element = (CElement)this[CKernel.StringToHash(strFileHash)]; if (Element != null) { m_StopFile(Element); Element.File.FileStatus = Protocol.FileState.Complete; Element.File.UpPriority = Constants.Priority.Normal; CLog.Log(Constants.Log.Notify, "FIL_COMPLETED", Element.File.FileName); Element.Statistics.TimeCompleted = DateTime.Now; if (CKernel.Preferences.GetBool("StartNextStoppedFile")) { m_StartNextPausedFile(Element.File.CategoryID); } } }
public void SetFileCategory(string strFileHash, string category) { CElement Element = (CElement)this[CKernel.StringToHash(strFileHash)]; if (Element != null) { CCategory cat = CKernel.CategoriesList.GetCategoryByName(category); if (cat != null) { Element.File.CategoryID = cat.ID; } else { Element.File.CategoryID = 0; } } }
public void CancelFile(string strFileHash) { CElement Element = (CElement)this[CKernel.StringToHash(strFileHash)]; if ((Element != null) && (Element.File.FileStatus != Protocol.FileState.Complete)) { Element.File.FileStatus = Protocol.FileState.Stopped; if (Element.SourcesList != null) { m_StopFile(Element); } if (Element.Comments != null) { Element.Comments.Element = null; } Element.Comments = null; Element.File.CancelFile(); Element.File = null; m_FileList.Remove(Element); Element = null; } }
public void AddSourcesToFile() { if ((Sources == null) || (Sources.Count == 0)) { return; } CElement element = CKernel.FilesList[CKernel.StringToHash(this.Hash)]; int i; if ((element != null) && (element.File.FileStatus == Protocol.FileState.Ready)) { stDatosFuente[] sourcesList = new stDatosFuente[Sources.Count]; i = 0; foreach (uint ip in Sources.Keys) { sourcesList[i].IP = ip; sourcesList[i].Port = (ushort)Sources[ip]; i++; } CKernel.ClientsList.AddClientsToFile(sourcesList, CKernel.StringToHash(this.Hash)); } }
public void UpdateFile(uint in_Avaibility, string in_Name, string in_codec, string in_length, uint in_bitrate, bool in_complete, uint in_ip, ushort in_port) { this.Avaibility += in_Avaibility; Complete = Complete || in_complete; if (!this.OtherNames.Contains(in_Name)) { this.OtherNames.Add(in_Name); } if (((Length == null) || (Length.Length == 0)) && (in_length.Length > 0)) { Length = in_length; } if ((Codec.Length == 0) && (in_codec.Length > 0)) { Codec = in_codec; } if ((BitRate == 0) && (in_bitrate > 0)) { BitRate = in_bitrate; } if ((in_ip > Protocol.LowIDLimit) && (in_port > 0) && (in_port < ushort.MaxValue)) { if (Sources == null) { Sources = new Hashtable(); } if (!Sources.Contains(in_ip)) { Sources.Add(in_ip, in_port); } //Debug.WriteLine(in_ip.ToString()+":"+in_port.ToString()); CElement element = CKernel.FilesList[CKernel.StringToHash(Hash)]; if ((element != null) && (element.File.FileStatus == Protocol.FileState.Ready)) { CKernel.ClientsList.AddClientToFile(in_ip, in_port, 0, 0, element.File.FileHash); } } }
private void m_StartNextPausedFile(uint category) { CElement elementCandidate = null; lock (m_FileList.SyncRoot) { foreach (CElement element in m_FileList) { if ((element.File.FileStatus == Protocol.FileState.Stopped) || (element.File.FileStatus == Protocol.FileState.Pause)) { if (elementCandidate == null) //no one selected, select first { elementCandidate = element; } else if (element.File.CategoryID == category) //same category { if (elementCandidate.File.CategoryID != category) //selected from other category { elementCandidate = element; } else if (elementCandidate.File.DownPriority < element.File.DownPriority) //selected from same category lower priority { elementCandidate = element; } } else if ((elementCandidate.File.CategoryID != category) && (elementCandidate.File.DownPriority < element.File.DownPriority)) { elementCandidate = element; } } } } if (elementCandidate != null) { this.ResumeFile(CKernel.HashToString(elementCandidate.File.FileHash)); } }
public void CloseDownloads(string strFileHash, bool stop) { if (strFileHash.Length == 0) //stop all downloads { foreach (CElement Elemento in this.Values) { if (Elemento.SourcesList != null) { Elemento.SourcesList.Clean(); } Elemento.SourcesList = null; } if (stop) { m_FileListThread.Abort(); } } else //stop specified download { CElement Elemento = (CElement)this[CKernel.StringToHash(strFileHash)]; m_StopFile(Elemento); } }
public CFileFound(string in_Hash, string in_Name, uint in_Size, uint in_Avaibility, string in_codec, string in_length, uint in_bitrate, bool in_complete, uint in_ip, ushort in_port) { this.Hash = in_Hash; this.Name = in_Name; this.Size = in_Size; this.Avaibility = in_Avaibility; Codec = in_codec; BitRate = in_bitrate; Length = in_length; Complete = in_complete; this.OtherNames = new StringCollection(); this.OtherNames.Add(Name); CElement element = CKernel.FilesList[CKernel.StringToHash(in_Hash)]; if (element == null) { ResultState = Types.Constants.SearchResultState.New; } else if (element.File.FileStatus == Protocol.FileState.Complete) { ResultState = Types.Constants.SearchResultState.AlreadyDownloaded; } else { ResultState = Types.Constants.SearchResultState.AlreadyDownloading; } if ((in_ip > Protocol.LowIDLimit) && (in_port > 0) && (in_port < ushort.MaxValue)) { Sources = new Hashtable(); Sources.Add(in_ip, in_port); //Debug.WriteLine(in_ip.ToString()+":"+in_port.ToString()); if ((element != null) && (element.File.FileStatus == Protocol.FileState.Ready)) { CKernel.ClientsList.AddClientToFile(in_ip, in_port, 0, 0, element.File.FileHash); } } }
public CedonkeyComments(CElement in_Element) { Element = in_Element; }
private void m_ProcessUDPPacket(byte command, ref MemoryStream packet) { try { switch ((Protocol.ClientCommandExtUDP)command) { case Protocol.ClientCommandExtUDP.ReaskFilePing: if (!m_PortIsAccesible) { CLog.Log(Types.Constants.Log.Info, "UDP_PORT_IS_OPEN"); } m_PortIsAccesible = true; MemoryStream responseBuffer; // if (packet.Length!=16) break; // byte[] FilaHash=new byte[16]; // packet.Read(FilaHash,0,16); CFileReaskUDP reaskMessage = new CFileReaskUDP(packet); byte[] FilaHash = reaskMessage.FileHash; //byte[] chunksAvaibility=reaskMessage.partes; //TODO update chunksAvaibility in shared file CElement requestedElement = CKernel.FilesList[FilaHash]; if (requestedElement == null) { responseBuffer = new MemoryStream(2); CFileNotFoundUDP FicheroNoEncontradoUDP = new CFileNotFoundUDP(responseBuffer); } else { m_RemoteIPEndPoint = (IPEndPoint)m_RemoteEndPoint; CClient foundClient = null; ushort queuePosition = CKernel.Queue.RefreshClientUDP(BitConverter.ToUInt32(m_RemoteIPEndPoint.Address.GetAddressBytes(), 0), (ushort)m_RemoteIPEndPoint.Port, ref foundClient); if (queuePosition <= 0) { responseBuffer = new MemoryStream(2); CQueueFullUDP PaqueteColaLlenaUDP = new CQueueFullUDP(responseBuffer); } else { responseBuffer = new MemoryStream(4); CQueuePositionUDP PaquetePosicionColaUDP = new CQueuePositionUDP(responseBuffer, queuePosition, requestedElement.File.ChunksStatus, foundClient.VersionUDP); } } SendPacketUDP(responseBuffer.GetBuffer(), m_RemoteIPEndPoint); break; case Protocol.ClientCommandExtUDP.ReaskAck: m_RemoteIPEndPoint = (IPEndPoint)m_RemoteEndPoint; CClient client = CKernel.ClientsList.GetClient(BitConverter.ToUInt32(m_RemoteIPEndPoint.Address.GetAddressBytes(), 0), 0, 0, null); if (client != null) { ushort queuePosition; if ((packet.Length > 2) && (client.VersionUDP > 3)) { packet.Seek(0, SeekOrigin.Begin); client.ProcessChunksStatus(packet, true); } BinaryReader reader = new BinaryReader(packet); queuePosition = reader.ReadUInt16(); client.ProcessUDPReask(queuePosition); packet.Close(); packet = null; } break; case Protocol.ClientCommandExtUDP.QueueFull: client = CKernel.ClientsList.GetClient(BitConverter.ToUInt32(m_RemoteIPEndPoint.Address.GetAddressBytes(), 0), 0, 0, null); if (client != null) { client.ProcessUDPReask(0); } break; case Protocol.ClientCommandExtUDP.FileNotFound: client = CKernel.ClientsList.GetClient(BitConverter.ToUInt32(m_RemoteIPEndPoint.Address.GetAddressBytes(), 0), 0, 0, null); if ((client != null) && (client.DownloadElement != null) && (client.DownloadElement.SourcesList != null)) { client.DownloadElement.SourcesList.RemoveSource(client); } break; default: CLog.Log(Constants.Log.Verbose, "CLI_UNK_UDP_PACKET", command); break; } } catch (Exception e) { Debug.WriteLine(e.ToString()); CLog.Log(Constants.Log.Verbose, "CLI_PAQ_UDP_ERROR", command); } }