/// <summary> /// Adds a file object in the list. /// </summary> /// <param name="File">The file object.</param> public static void AddFile(Objects.SharedFile File) { for (int i = 0; i < m_filesList.Count; i++) { if (m_filesList[i].SHA1 == File.SHA1) { return; } } m_filesList.Add(File); }
/// <summary> /// Save the list in a file. /// </summary> private static void SaveList() { // create the file XmlDocument XmlDoc = new XmlDocument(); XmlDoc.LoadXml("<Files></Files>"); Objects.SharedFile file = null; // write in the file for (int i = 0; i < m_filesList.Count; i++) { try { file = m_filesList[i]; XmlElement xmlFile = XmlDoc.CreateElement("_" + file.SHA1); XmlElement fileName = XmlDoc.CreateElement("Name"); fileName.InnerText = file.Name; xmlFile.AppendChild(fileName); XmlElement filePath = XmlDoc.CreateElement("Path"); filePath.InnerText = file.Path; xmlFile.AppendChild(filePath); XmlElement fileSize = XmlDoc.CreateElement("Size"); fileSize.InnerText = file.Size.ToString(); xmlFile.AppendChild(fileSize); XmlElement fileModifiedDate = XmlDoc.CreateElement("ModifiedDate"); fileModifiedDate.InnerText = file.ModifiedDate.ToString(); xmlFile.AppendChild(fileModifiedDate); XmlDoc.DocumentElement.AppendChild(xmlFile); } catch { } } // save the file XmlTextWriter writer = new XmlTextWriter(Global.iKiwiPath + "FilesList.xml", null); writer.Formatting = Formatting.Indented; XmlDoc.Save(writer); writer.Close(); Utilities.Log.Write("Saved the list of the shared files", Utilities.Log.LogCategory.Info); }
/// <summary> /// Automatic creates a File object and add it to this list. /// </summary> /// <param name="Path">The path of file.</param> public static void AddFile(string Path) { try { Objects.SharedFile file = new Objects.SharedFile(Path); for (int i = 0; i < m_filesList.Count; i++) { if (m_filesList[i].SHA1 == file.SHA1) { return; } } m_filesList.Add(file); } catch { return; } }
public override void Process() { Objects.SharedFile file = Lists.FilesList.GetFile(m_strFileName, m_strFileID); // control if the file exists if (file != null) { while (true) { try { FileStream fs = File.OpenRead(file.Path); // lock the access to file fs.Lock(0, 16384); byte[] buffer = new byte[16384]; fs.Seek(m_nStartPoint, SeekOrigin.Begin); // control that the FilePack_Length isn't more big than the rest of file if (fs.Length > (m_nStartPoint + 16384)) { fs.Read(buffer, 0, buffer.Length); } else { fs.Read(buffer, 0, (int)(fs.Length - m_nStartPoint)); } // unlock the access to file fs.Unlock(0, 16384); fs.Close(); // send the FP-message SHA1 sha1 = new SHA1CryptoServiceProvider(); string[] Params = new string[4]; Params[0] = m_strFileName; Params[1] = m_strFileID; Params[2] = BitConverter.ToString(sha1.ComputeHash(buffer)).Replace("-", ""); Params[3] = m_nStartPoint.ToString(); // build the message IMessage iMessage = MessagesFactory.Instance.CreateMessage(Commands.FP, Params, buffer); // send the message this.SenderPeer.Send(iMessage); // indicate the uploading of a file pack this.SenderPeer.FilePackUploaded = true; break; } catch (IOException) // the file is already locked { Thread.Sleep(1); } catch (Exception ex) // other problem { Utilities.Log.Write("Error to read from a file during a upload process: \n" + ex.Message, Utilities.Log.LogCategory.Error); break; } } } }