Exemplo n.º 1
0
        internal void HashFile(LocalFile file, AsyncCallback callback)
        {
            if (file.LocalPath == null)
            {
                throw new ArgumentNullException("file");
            }

            if (!File.Exists(file.LocalPath))
            {
                throw new ArgumentException("File does not exist");
            }

            lock (queue) {
                // Check to see if this file is already queued.
                foreach (var existingTask in queue)
                {
                    if (existingTask.File.LocalPath == file.LocalPath)
                    {
                        return;
                    }
                }
                // If not, add it!
                var task = new ShareHasherTask(file, callback);
                queue.Add(task);
                queue.Sort(comparer);

                if (QueueChanged != null)
                {
                    QueueChanged(this, EventArgs.Empty);
                }
            }
            mutex.Set();
        }
Exemplo n.º 2
0
        private void Hash(ShareHasherTask task)
        {
            if (StartedHashingFile != null)
            {
                StartedHashingFile(task);
            }

            /* Create the torrent */
            var creator = new TorrentCreator();

            // Have to put something bogus here, otherwise MonoTorrent crashes!
            creator.Announces.Add(new MonoTorrentCollection <string>());
            creator.Announces[0].Add(string.Empty);

            //creator.Path = task.File.LocalPath;
            //Torrent torrent = Torrent.Load(creator.Create());
            Torrent torrent = null;              // FIXME

            /* Update the database */
            var pieces = new string[torrent.Pieces.Count];

            for (var x = 0; x < torrent.Pieces.Count; x++)
            {
                var hash = torrent.Pieces.ReadHash(x);
                pieces[x] = Common.Utils.BytesToString(hash);
            }

            task.File.Update(Common.Utils.BytesToString(torrent.InfoHash.ToArray()),
                             Common.Utils.BytesToString(torrent.Files[0].SHA1),
                             torrent.PieceLength, pieces);

            if (FinishedHashingFile != null)
            {
                FinishedHashingFile(task);
            }

            if (task.Callback != null)
            {
                task.Callback(null);
            }
        }