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); } } }
public void AddFileFound(byte[] Hash, string name, uint size, uint avaibility, string codec, string length, uint bitrate, bool complete, uint ip, ushort port) { try { string strHash = CKernel.HashToString(Hash); m_sources += avaibility; if (ContainsKey(strHash)) { CFileFound fileFound = (CFileFound)this[strHash]; fileFound.UpdateFile(avaibility, name, codec, length, bitrate, complete, ip, port); CKernel.SearchFileModified(fileFound, (int)CKernel.Searchs.GetKey(CKernel.Searchs.IndexOfValue(this))); } else { CFileFound fileFound = new CFileFound(strHash, name, size, avaibility, codec, length, bitrate, complete, ip, port); Add(strHash, fileFound); CKernel.NewFileFound(fileFound, (int)CKernel.Searchs.GetKey(CKernel.Searchs.IndexOfValue(this))); } } catch (Exception e) { Debug.WriteLine("Search error"); Debug.WriteLine(e.ToString()); CKernel.SearchEnded((int)CKernel.Searchs.GetKey(CKernel.Searchs.IndexOfValue(this))); } }
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 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); } }
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; } }
//Clear deleted or deleted incoming folders private void m_ClearDeletedFiles() { ArrayList toRemove = null; string IncomingFolder = String.Join(":", CKernel.Preferences.GetStringArray("SharedFolders")) + ":"; lock (m_FileList.SyncRoot) { foreach (CElement Element in m_FileList) { string m_CompleteName = Element.File.CompleteName; if ((Element.File.Completed) && (!File.Exists(m_CompleteName) || (IncomingFolder.IndexOf(Path.GetDirectoryName(m_CompleteName) + ":") == -1))) { if (toRemove == null) { toRemove = new ArrayList(); } toRemove.Add(Element); } } } if (toRemove != null) { foreach (CElement Element in toRemove) { //Debug.WriteLine("File deleted from FilesList: "+ Element.File.CompleteName); m_FileList.Remove(Element); CKernel.RemovedSharedFile(Element); } } }
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); } } } }
private int m_FindFriend(uint ID, byte[] ClientHash, ushort Port) { //First: i try to locate Friend with UserHash if (ClientHash != null) { for (int n = 0; n != List.Count; n++) { byte[] hashFriend = (byte[])((SFriend)List[n]).UserHash; if (hashFriend != null) { if (CKernel.SameHash(ref ClientHash, ref hashFriend)) { return(n); } } } } //If i did not locate Friend with UserHash, i try to locate Friend with IP&Port for (int n = 0; n != List.Count; n++) { if ((((SFriend)List[n]).ID == ID) && (((SFriend)List[n]).Port == Port)) { return(n); } } return(-1); }
public static void Log(Constants.Log importance, string message) { #if !DEBUG if (importance == Constants.Log.Verbose) { return; } #endif CKernel.NewLogMessage(importance, CKernel.Globalization[message]); }
private bool m_Contains(byte[] fileHash) { foreach (CElement element in m_FileList) { if (CKernel.SameHash(ref fileHash, ref element.File.FileHash)) { return(true); } } return(false); }
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(); }
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); } }
public CSourceOld GetSourceOld(byte[] UserHash) { lock (m_ArrayList.SyncRoot) { foreach (CSourceOld sourceOld in m_ArrayList) { if (CKernel.SameHash(ref UserHash, ref sourceOld.UserHash)) { return(sourceOld); } } } return(null); }
public bool Remove(uint ip, ushort port) { CServer server = this[ip, port]; if (server != null) { InnerList.Remove(server); CKernel.DeleteServer(ip, port); return(true); } else { return(false); } }
/// <summary> /// Request file several sources in one TCP frame from server. /// </summary> /// <param name="FileHashes">Array with the file hashes.</param> public void RequestSources(ArrayList FileHashes) { MemoryStream buffer = new MemoryStream(); CServerAskSources RequestSources; foreach (byte[] FileHash in FileHashes) { RequestSources = new CServerAskSources(FileHash, buffer); Debug.WriteLine("Asking file " + CKernel.HashToString(FileHash)); } 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 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 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); } } }
private void m_AcceptNewUpload() { if (!m_AllowNewUpload()) { return; } CClient client = SelectNextClient(); if (client != null) { client.StartUpload(); m_LastAcceptedClientTime = DateTime.Now; UploadList.Add(client); m_QueueList.Remove(client); CKernel.NewUpload(client); } }
public static void Log(Constants.Log importance, string message, Object arg1, Object arg2) { #if !DEBUG if (importance == Constants.Log.Verbose) { return; } #endif string translatedMsg = CKernel.Globalization[message]; try { CKernel.NewLogMessage(importance, String.Format(translatedMsg, arg1, arg2)); } catch { Debug.Write("Invalid message format:" + message + "\n"); } }
public void AddClientToFile(CClient client, byte[] FileHash) { if (client.DownFileHash == null) { client.DownFileHash = FileHash; CKernel.FilesList.AddSource(FileHash, ref client); } else { if (CKernel.SameHash(ref FileHash, ref client.DownFileHash)) { CKernel.FilesList.AddSource(FileHash, ref client); } else { client.AddOtherDownloadFile(CKernel.HashToString(FileHash)); } } }
/// <summary> /// Add a server to the list. /// </summary> /// <param name="ip">IP of the server.</param> /// <param name="port">Port of the server.</param> /// <returns>The new server.</returns> public CServer Add(uint ip, ushort port) { if ((ip < Protocol.LowIDLimit) || (port == 0) || (port >= IPEndPoint.MaxPort - 4)) { return(null); } if (this[ip, 0] == null) { CServer server = new CServer(ip, port); this.Add(server); CKernel.NewServer(server); return(server); } else { return(null); } }
public CServerProcessSources(MemoryStream buffer, uint ServerIP, ushort ServerPort) { byte[] FileHash; uint IP; ushort Port; byte nSources; bool moreFiles = true; BinaryReader reader = new BinaryReader(buffer); do { FileHash = reader.ReadBytes(16); nSources = reader.ReadByte(); Debug.WriteLine("Received " + nSources.ToString() + " for " + CKernel.HashToString(FileHash)); while (nSources > 0) { IP = reader.ReadUInt32(); Port = reader.ReadUInt16(); nSources--; CKernel.ClientsList.AddClientToFile(IP, Port, ServerIP, ServerPort, FileHash); } if ((reader.PeekChar() != 0) && (reader.PeekChar() != -1)) { if ((Protocol.ProtocolType)reader.ReadByte() != Protocol.ProtocolType.eDonkey) { moreFiles = false; } if ((reader.PeekChar() == -1) || (reader.ReadByte() != (byte)Protocol.ServerCommandUDP.GlobalFoundSources)) { moreFiles = false; } } else { moreFiles = false; } }while (moreFiles); reader.Close(); buffer.Close(); reader = null; buffer = null; }
/// <summary> /// Delete all servers where connection failed too often. /// </summary> private void m_DeleteFailedServers() { CServer server; int deleted = 0; for (int i = 0; i != this.Count; i++) { server = (CServer)this[i]; if (server.Fails >= CKernel.Preferences.GetShort("MaxServerFails")) { if (server != ActiveServer) { this.RemoveAt(i); CKernel.DeleteServer(server.IP, server.Port); deleted++; } } } CLog.Log(Constants.Log.Info, "SRV_DELETED", deleted); }
public void OnTCPSearchEnded() { if ((CKernel.Preferences.GetBool("AutoExtendSearch")) && (m_sources < Protocol.MaxSearchResults) && (!m_IsClientSearch) && (m_Searching)) { Debug.Write("Autoextending search\n"); m_ServerIndex = 0; for (uint i = 0; i < CKernel.ServersList.Count; i++) { if ((m_sources > Protocol.MaxSearchResults) || (m_SearchCanceled)) { break; } ExtendSearch(); Thread.Sleep(250); CKernel.NewSearchProgress((int)(((float)i / (float)CKernel.ServersList.Count) * 100.0F), (int)CKernel.Searchs.GetKey(CKernel.Searchs.IndexOfValue(this))); } //m_UDPPacket.Close(); //m_UDPPacket=null; if (m_ServerIndex >= CKernel.ServersList.Count) { m_ServerIndex = 0; } } // else // { // if ((!m_IsClientSearch)&&(m_Searching)&&(CKernel.ServersList.ActiveServer!=null)) // CKernel.ServersList.ActiveServer.QueryMoreResults(); // // } m_Searching = false; if (CKernel.Searchs.IndexOfValue(this) >= 0) { CKernel.SearchEnded((int)CKernel.Searchs.GetKey(CKernel.Searchs.IndexOfValue(this))); } }
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 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 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); } }