2012-11-13 3 views
1

이것은 분명히 새내기 문제이므로 나와 함께하시기 바랍니다.보호 수준으로 인해 Monotorrent 부품을 액세스 할 수 없습니다.

모노 토런트와 함께 제공되는 예제 컴파일하는 중에 : 나는 다음과 같은 발생

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Net.Sockets; 
using System.IO; 
using MonoTorrent; 
using MonoTorrent.Common; 
using MonoTorrent.Client; 
using System.Net; 
using System.Diagnostics; 
using System.Threading; 
using MonoTorrent.BEncoding; 
using MonoTorrent.Client.Encryption; 
using MonoTorrent.Client.Tracker; 
using MonoTorrent.Dht; 
using MonoTorrent.Dht.Listeners; 

namespace MonoTorrent 
{ 
    class main 
    { 
     static string dhtNodeFile; 
     static string basePath; 
     static string downloadsPath; 
     static string fastResumeFile; 
     static string torrentsPath; 
     static ClientEngine engine;    // The engine used for downloading 
     static List<TorrentManager> torrents; // The list where all the torrentManagers will be stored that the engine gives us 
     static Top10Listener listener;   // This is a subclass of TraceListener which remembers the last 20 statements sent to it 

     static void Main(string[] args) 
     { 
      /* Generate the paths to the folder we will save .torrent files to and where we download files to */ 
      basePath = Environment.CurrentDirectory;      // This is the directory we are currently in 
      torrentsPath = Path.Combine(basePath, "Torrents");    // This is the directory we will save .torrents to 
      downloadsPath = Path.Combine(basePath, "Downloads");   // This is the directory we will save downloads to 
      fastResumeFile = Path.Combine(torrentsPath, "fastresume.data"); 
      dhtNodeFile = Path.Combine(basePath, "DhtNodes"); 
      torrents = new List<TorrentManager>();       // This is where we will store the torrentmanagers 
      listener = new Top10Listener(10); 

      // We need to cleanup correctly when the user closes the window by using ctrl-c 
      // or an unhandled exception happens 
      Console.CancelKeyPress += delegate { shutdown(); }; 
      AppDomain.CurrentDomain.ProcessExit += delegate { shutdown(); }; 
      AppDomain.CurrentDomain.UnhandledException += delegate(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine(e.ExceptionObject); shutdown(); }; 
      Thread.GetDomain().UnhandledException += delegate(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine(e.ExceptionObject); shutdown(); }; 

      StartEngine(); 
     } 

     private static void StartEngine() 
     { 
      int port; 
      Torrent torrent = null; 
      // Ask the user what port they want to use for incoming connections 
      Console.Write(Environment.NewLine + "Choose a listen port: "); 
      while (!Int32.TryParse(Console.ReadLine(), out port)) { } 



      // Create the settings which the engine will use 
      // downloadsPath - this is the path where we will save all the files to 
      // port - this is the port we listen for connections on 
      EngineSettings engineSettings = new EngineSettings(downloadsPath, port); 
      engineSettings.PreferEncryption = false; 
      engineSettings.AllowedEncryption = EncryptionTypes.All; 

      //engineSettings.GlobalMaxUploadSpeed = 30 * 1024; 
      //engineSettings.GlobalMaxDownloadSpeed = 100 * 1024; 
      //engineSettings.MaxReadRate = 1 * 1024 * 1024; 


      // Create the default settings which a torrent will have. 
      // 4 Upload slots - a good ratio is one slot per 5kB of upload speed 
      // 50 open connections - should never really need to be changed 
      // Unlimited download speed - valid range from 0 -> int.Max 
      // Unlimited upload speed - valid range from 0 -> int.Max 
      TorrentSettings torrentDefaults = new TorrentSettings(4, 150, 0, 0); 

      // Create an instance of the engine. 
      engine = new ClientEngine(engineSettings); 
      engine.ChangeListenEndpoint(new IPEndPoint(IPAddress.Any, port)); 
      byte[] nodes = null; 
      try 
      { 
       nodes = File.ReadAllBytes(dhtNodeFile); 
      } 
      catch 
      { 
       Console.WriteLine("No existing dht nodes could be loaded"); 
      } 

      DhtListener dhtListner = new DhtListener(new IPEndPoint(IPAddress.Any, port)); 
      DhtEngine dht = new DhtEngine(dhtListner); 
      engine.RegisterDht(dht); 
      dhtListner.Start(); 
      engine.DhtEngine.Start(nodes); 

      // If the SavePath does not exist, we want to create it. 
      if (!Directory.Exists(engine.Settings.SavePath)) 
       Directory.CreateDirectory(engine.Settings.SavePath); 

      // If the torrentsPath does not exist, we want to create it 
      if (!Directory.Exists(torrentsPath)) 
       Directory.CreateDirectory(torrentsPath); 

      BEncodedDictionary fastResume; 
      try 
      { 
       fastResume = BEncodedValue.Decode<BEncodedDictionary>(File.ReadAllBytes(fastResumeFile)); 
      } 
      catch 
      { 
       fastResume = new BEncodedDictionary(); 
      } 

      // For each file in the torrents path that is a .torrent file, load it into the engine. 
      foreach (string file in Directory.GetFiles(torrentsPath)) 
      { 
       if (file.EndsWith(".torrent")) 
       { 
        try 
        { 
         // Load the .torrent from the file into a Torrent instance 
         // You can use this to do preprocessing should you need to 
         torrent = Torrent.Load(file); 
         Console.WriteLine(torrent.InfoHash.ToString()); 
        } 
        catch (Exception e) 
        { 
         Console.Write("Couldn't decode {0}: ", file); 
         Console.WriteLine(e.Message); 
         continue; 
        } 
        // When any preprocessing has been completed, you create a TorrentManager 
        // which you then register with the engine. 
        TorrentManager manager = new TorrentManager(torrent, downloadsPath, torrentDefaults); 
        if (fastResume.ContainsKey(torrent.InfoHash.ToHex())) 
         manager.LoadFastResume(new FastResume((BEncodedDictionary)fastResume[torrent.infoHash.ToHex()])); 
        engine.Register(manager); 

        // Store the torrent manager in our list so we can access it later 
        torrents.Add(manager); 
        manager.PeersFound += new EventHandler<PeersAddedEventArgs>(manager_PeersFound); 
       } 
      } 

      // If we loaded no torrents, just exist. The user can put files in the torrents directory and start 
      // the client again 
      if (torrents.Count == 0) 
      { 
       Console.WriteLine("No torrents found in the Torrents directory"); 
       Console.WriteLine("Exiting..."); 
       engine.Dispose(); 
       return; 
      } 

      // For each torrent manager we loaded and stored in our list, hook into the events 
      // in the torrent manager and start the engine. 
      foreach (TorrentManager manager in torrents) 
      { 
       // Every time a piece is hashed, this is fired. 
       manager.PieceHashed += delegate(object o, PieceHashedEventArgs e) 
       { 
        lock (listener) 
         listener.WriteLine(string.Format("Piece Hashed: {0} - {1}", e.PieceIndex, e.HashPassed ? "Pass" : "Fail")); 
       }; 

       // Every time the state changes (Stopped -> Seeding -> Downloading -> Hashing) this is fired 
       manager.TorrentStateChanged += delegate(object o, TorrentStateChangedEventArgs e) 
       { 
        lock (listener) 
         listener.WriteLine("OldState: " + e.OldState.ToString() + " NewState: " + e.NewState.ToString()); 
       }; 

       // Every time the tracker's state changes, this is fired 
       foreach (TrackerTier tier in manager.TrackerManager) 
       { 
        foreach (MonoTorrent.Client.Tracker.Tracker t in tier.Trackers) 
        { 
         t.AnnounceComplete += delegate(object sender, AnnounceResponseEventArgs e) 
         { 
          listener.WriteLine(string.Format("{0}: {1}", e.Successful, e.Tracker.ToString())); 
         }; 
        } 
       } 
       // Start the torrentmanager. The file will then hash (if required) and begin downloading/seeding 
       manager.Start(); 
      } 

      // While the torrents are still running, print out some stats to the screen. 
      // Details for all the loaded torrent managers are shown. 
      int i = 0; 
      bool running = true; 
      StringBuilder sb = new StringBuilder(1024); 
      while (running) 
      { 
       if ((i++) % 10 == 0) 
       { 
        sb.Remove(0, sb.Length); 
        running = torrents.Exists(delegate(TorrentManager m) { return m.State != TorrentState.Stopped; }); 

        AppendFormat(sb, "Total Download Rate: {0:0.00}kB/sec", engine.TotalDownloadSpeed/1024.0); 
        AppendFormat(sb, "Total Upload Rate: {0:0.00}kB/sec", engine.TotalUploadSpeed/1024.0); 
        AppendFormat(sb, "Disk Read Rate:  {0:0.00} kB/s", engine.DiskManager.ReadRate/1024.0); 
        AppendFormat(sb, "Disk Write Rate:  {0:0.00} kB/s", engine.DiskManager.WriteRate/1024.0); 
        AppendFormat(sb, "Total Read:   {0:0.00} kB", engine.DiskManager.TotalRead/1024.0); 
        AppendFormat(sb, "Total Written:  {0:0.00} kB", engine.DiskManager.TotalWritten/1024.0); 
        AppendFormat(sb, "Open Connections: {0}", engine.ConnectionManager.OpenConnections); 

        foreach (TorrentManager manager in torrents) 
        { 
         AppendSeperator(sb); 
         AppendFormat(sb, "State:   {0}", manager.State); 
         AppendFormat(sb, "Name:   {0}", manager.Torrent == null ? "MetaDataMode" : manager.Torrent.Name); 
         AppendFormat(sb, "Progress:   {0:0.00}", manager.Progress); 
         AppendFormat(sb, "Download Speed:  {0:0.00} kB/s", manager.Monitor.DownloadSpeed/1024.0); 
         AppendFormat(sb, "Upload Speed:  {0:0.00} kB/s", manager.Monitor.UploadSpeed/1024.0); 
         AppendFormat(sb, "Total Downloaded: {0:0.00} MB", manager.Monitor.DataBytesDownloaded/(1024.0 * 1024.0)); 
         AppendFormat(sb, "Total Uploaded:  {0:0.00} MB", manager.Monitor.DataBytesUploaded/(1024.0 * 1024.0)); 
         MonoTorrent.Client.Tracker.Tracker tracker = manager.TrackerManager.CurrentTracker; 
         //AppendFormat(sb, "Tracker Status:  {0}", tracker == null ? "<no tracker>" : tracker.State.ToString()); 
         AppendFormat(sb, "Warning Message: {0}", tracker == null ? "<no tracker>" : tracker.WarningMessage); 
         AppendFormat(sb, "Failure Message: {0}", tracker == null ? "<no tracker>" : tracker.FailureMessage); 
         if (manager.PieceManager != null) 
          AppendFormat(sb, "Current Requests: {0}", manager.PieceManager.CurrentRequestCount()); 

         foreach (PeerId p in manager.GetPeers()) 
          AppendFormat(sb, "\t{2} - {1:0.00}/{3:0.00}kB/sec - {0}", p.Peer.ConnectionUri, 
                         p.Monitor.DownloadSpeed/1024.0, 
                         p.AmRequestingPiecesCount, 
                         p.Monitor.UploadSpeed/1024.0); 

         AppendFormat(sb, "", null); 
         if (manager.Torrent != null) 
          foreach (TorrentFile file in manager.Torrent.Files) 
           AppendFormat(sb, "{1:0.00}% - {0}", file.Path, file.BitField.PercentComplete); 
        } 
        Console.Clear(); 
        Console.WriteLine(sb.ToString()); 
        listener.ExportTo(Console.Out); 
       } 

       System.Threading.Thread.Sleep(500); 
      } 
     } 

     static void manager_PeersFound(object sender, PeersAddedEventArgs e) 
     { 
      lock (listener) 
       listener.WriteLine(string.Format("Found {0} new peers and {1} existing peers", e.NewPeers, e.ExistingPeers));//throw new Exception("The method or operation is not implemented."); 
     } 

     private static void AppendSeperator(StringBuilder sb) 
     { 
      AppendFormat(sb, "", null); 
      AppendFormat(sb, "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -", null); 
      AppendFormat(sb, "", null); 
     } 
     private static void AppendFormat(StringBuilder sb, string str, params object[] formatting) 
     { 
      if (formatting != null) 
       sb.AppendFormat(str, formatting); 
      else 
       sb.Append(str); 
      sb.AppendLine(); 
     } 

     private static void shutdown() 
     { 
      BEncodedDictionary fastResume = new BEncodedDictionary(); 
      for (int i = 0; i < torrents.Count; i++) 
      { 
       torrents[i].Stop(); ; 
       while (torrents[i].State != TorrentState.Stopped) 
       { 
        Console.WriteLine("{0} is {1}", torrents[i].Torrent.Name, torrents[i].State); 
        Thread.Sleep(250); 
       } 

       fastResume.Add(torrents[i].Torrent.InfoHash.ToHex(), torrents[i].SaveFastResume().Encode()); 
      } 

      #if !DISABLE_DHT 
      File.WriteAllBytes(dhtNodeFile, engine.DhtEngine.SaveNodes()); 
      #endif 
      File.WriteAllBytes(fastResumeFile, fastResume.Encode()); 
      engine.Dispose(); 

      foreach (TraceListener lst in Debug.Listeners) 
      { 
       lst.Flush(); 
       lst.Close(); 
      } 

      System.Threading.Thread.Sleep(2000); 
     } 
    } 
} 

:

`MonoTorrent.Common.Torrent.infoHash을 '인해 보호 수준 (CS0122에 액세스 할 수 없습니다) (테스트)

과 같은 오류가 더 있습니다. 어떻게해야합니까? monotorrent를 수정하여 몇 가지 속성을 공개해야합니까 (검색 결과가 제안하는 것처럼) 또는 다른 방법이 있습니까? 모노 토런트의 내부를 망칠만큼 자신감이 부족합니다.

내가 모노 - 개발 대상이 모노 /.NET 4 '

사실 내가있어 다섯 가지 이상의 오류

설정되어 사용하고 있습니다 :

/홈/GE가/실험실/시험/검사/test/Main.cs (75,75) : 오류 CS0122 : 'MonoTorrent.Client.Tracker.TrackerTier.Trackers'의 보호 수준 (CS0122) (테스트)으로 인해 액세스 할 수 없음

/home/ge/lab/test /test/test/Main.cs(75,75) : 오류 CS0122 : 'MonoTorrent.Client.Tracker.TrackerTier.Trackers.get'의 보호 수준 (CS0122)으로 인해 액세스 할 수 없습니다 (테스트)

/home/ge/lab/test/test/test/Main.cs(94,94) : 오류 CS0122 : 'MonoTorrent.Client.PieceManager.CurrentRequestCount()'의 보호 수준 (CS0122)으로 인해 액세스 할 수 없습니다 (테스트)

/home/ge/lab/test/test/test/Main.cs(89,89) : 오류 CS0122 : 보호 수준 (CS0122)으로 인해 'MonoTorrent.Client.PeerId.Peer'에 액세스 할 수 없습니다 (테스트)

/home/ge/lab/test/test/test/Main.cs(89,89) : 오류 CS0122 : 'MonoTorrent.Client.PeerId.Peer.get'의 보호 수준으로 인해 액세스 할 수 없습니다 (CS0122) (테스트)

+0

당신은 철자가 공개 속성 "InfoHash"를 사용하고 실수로 내부/개인 필드 인 "infoHash"를 참조했습니다. – Tormod

+0

@Tormod 그것은 실제로 샘플 코드가 더 좋든 나쁘 든입니다. 라이브러리가 내부 프로젝트를 샘플 프로젝트에 노출하기 때문에 샘플에서 작동합니다 ... – TheNextman

+0

글쎄, 누군가가 사적인 유형을 사용하지 않기 위해 샘플을 변경하도록 요청해야합니다. 이것은 꽤 많이 샘플 tbh의 오타처럼 보입니다. –

답변

3

MonoTorrent 프로젝트에서 AssemblyInfo.cs를 보면 다음과 같이 표시됩니다.

[assembly: InternalsVisibleTo("SampleClient")] 
[assembly: InternalsVisibleTo("MonoTorrent.Tests")] 

'SampleClient'프로젝트에서 가시성이 'internal'로 설정된 멤버에 액세스 할 수 있습니다.

프로젝트에서 내부/개인 필드가 아닌 공용 속성에만 액세스해야합니다.

manager.LoadFastResume(new FastResume((BEncodedDictionary)fastResume[torrent.infoHash.ToHex()])); 

것은이 같은 변화 할 수 있어야한다 : 예를 들어,이 라인이

manager.LoadFastResume(new FastResume((BEncodedDictionary)fastResume[torrent.InfoHash.ToHex()])); 

합니다 ('InfoHash'속성에 케이스를 참고)

+0

죄송 합니다만이 오류를 수정하는 방법 뒤에 원칙을 이해하지 못합니다. 모노 토런트 소스 코드를 파헤쳐 어떤 방법으로 getter/setter 또는 그런 것을 사용하는지 확인해야합니까? – Moonwalker

+0

내 대답에 표시된대로 행을 변경하십시오 ('torrent.infoHash'를 'torrent.infoHash'로 변경하십시오). 그러면 작동합니다. 코드에서 오류입니다. 공개 속성 'InfoHash'가 아닌 내부 백킹 필드 'infoHash'를 호출하고 있습니다. – TheNextman

+0

예,하지만 이처럼 5 개의 오류가 더 있습니다. 어떻게 수정해야합니까? – Moonwalker

관련 문제