本文整理汇总了C#中BEncodedDictionary.ContainsKey方法的典型用法代码示例。如果您正苦于以下问题:C# BEncodedDictionary.ContainsKey方法的具体用法?C# BEncodedDictionary.ContainsKey怎么用?C# BEncodedDictionary.ContainsKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BEncodedDictionary
的用法示例。
在下文中一共展示了BEncodedDictionary.ContainsKey方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decode
public override void Decode(byte[] buffer, int offset, int length)
{
peerDict = BEncodedValue.Decode<BEncodedDictionary>(buffer, offset, length, false);
if (!peerDict.ContainsKey(AddedKey))
peerDict.Add(AddedKey, (BEncodedString)"");
if (!peerDict.ContainsKey(AddedDotFKey))
peerDict.Add(AddedDotFKey, (BEncodedString)"");
if (!peerDict.ContainsKey(DroppedKey))
peerDict.Add(DroppedKey, (BEncodedString)"");
}
示例2: FastResume
public FastResume(BEncodedDictionary dict)
{
CheckContent(dict, VersionKey, (BEncodedNumber)1);
CheckContent(dict, InfoHashKey);
CheckContent(dict, BitfieldKey);
CheckContent(dict, BitfieldLengthKey);
infoHash = new InfoHash(((BEncodedString)dict[InfoHashKey]).TextBytes);
bitfield = new BitField((int)((BEncodedNumber)dict[BitfieldLengthKey]).Number);
byte[] data = ((BEncodedString)dict[BitfieldKey]).TextBytes;
bitfield.FromArray(data, 0, data.Length);
if (dict.ContainsKey(PrioritiesKey))
{
var list = (BEncodedList)dict[PrioritiesKey];
priorities = list.Select(v => (Priority)((BEncodedNumber)v).Number).ToArray();
}
}
示例3: 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)) { }
port = 4545;
// 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");
//.........这里部分代码省略.........
示例4: Set
static void Set(BEncodedDictionary dictionary, BEncodedString key, BEncodedValue value)
{
if (dictionary.ContainsKey(key)) dictionary[key] = value;
else dictionary.Add(key, value);
}
示例5: Get
static BEncodedValue Get(BEncodedDictionary dictionary, BEncodedString key)
{
return dictionary.ContainsKey(key) ? dictionary[key] : null;
}
示例6: CheckContent
private void CheckContent(BEncodedDictionary dict, BEncodedString key)
{
if (dict == null) throw new ArgumentNullException(nameof(dict));
if (!dict.ContainsKey(key))
throw new TorrentException(string.Format("Invalid FastResume data. Key '{0}' was not present", key));
}
示例7: DecodeFromDict
private static Peer DecodeFromDict(BEncodedDictionary dict)
{
string peerId;
if (dict.ContainsKey("peer id"))
peerId = dict["peer id"].ToString();
else if (dict.ContainsKey("peer_id")) // HACK: Some trackers return "peer_id" instead of "peer id"
peerId = dict["peer_id"].ToString();
else
peerId = string.Empty;
Uri connectionUri = new Uri("tcp://" + dict["ip"].ToString() + ":" + dict["port"].ToString());
return new Peer(peerId, connectionUri, EncryptionTypes.All);
}
示例8: InitializeTorrent
public void InitializeTorrent(string torrentDir)
{
BEncodedDictionary fastResume;
try
{
fastResume = BEncodedValue.Decode<BEncodedDictionary>(File.ReadAllBytes(this.fastResumeFile));
}
catch
{
fastResume = new BEncodedDictionary();
}
Torrent torrent = null;
// Load the file into the engine if it ends with .torrent.
if (torrentDir.EndsWith(".torrent"))
{
try
{
// Load the .torrent file from the file into a Torrent instance
// You can use this to do preprocessing should you need to.
torrent = Torrent.Load(torrentDir);
Console.WriteLine(torrent.InfoHash.ToString());
}
catch (Exception e)
{
Console.Write("Could not decode {0}: {1}", torrentDir, e.Message);
return;
}
// When any preprocessing has been completed, you create a TorrentManager which you then register with the engine.
TorrentManager postProcessManager = new TorrentManager(torrent, this.downloadDir, torrentDefaults);
if (fastResume.ContainsKey(torrent.InfoHash.ToHex()))
{
postProcessManager.LoadFastResume(new FastResume((BEncodedDictionary)fastResume[torrent.InfoHash.ToHex()]));
}
this.clientEngine.Register(postProcessManager);
// Store the torrent manager in our list so we can access it later.
this.torrentManagers.Add(postProcessManager);
postProcessManager.PeersFound += new EventHandler<PeersAddedEventArgs>(this.manager_PeersFound);
}
// 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 torrentManager in this.torrentManagers)
{
// Every time a piece is hashed, this is fired.
torrentManager.PieceHashed += delegate(object o, PieceHashedEventArgs e)
{
lock (this.topTrackers)
{
topTrackers.WriteLine(string.Format("Piece hashed: {0} - {1} ", e.PieceIndex, e.HashPassed ? "passed" : "failed"));
}
};
// Everytime the state changes (stopped -> seeding -> downloading -> hashing), this is fired.
foreach (TrackerTier tier in torrentManager.TrackerManager)
{
List<Tracker> trackers = tier.GetTrackers();
foreach (Tracker tracker in trackers)
{
tracker.AnnounceComplete += delegate(object sender, AnnounceResponseEventArgs e)
{
this.topTrackers.WriteLine(string.Format("{0}: {1}", e.Successful, e.Tracker.ToString()));
};
}
}
// Start the torrentManager. The file will then hash (if required) and begin loading.
torrentManager.Start();
}
Thread.Sleep(500);
}
示例9: CheckContent
private static void CheckContent(BEncodedDictionary dict, BEncodedString key)
{
if (dict.ContainsKey(key) == false)
throw new TorrentException(string.Format("Invalid FastResume data. Key '{0}' was not present", key));
}
示例10: Initiate
/// <summary>
/// Fully starts the torrent engine and dht engine and listener
/// Loads in any torrents from the directory and resumes them automaticallhy
/// </summary>
public void Initiate()
{
torrentEngine = new ClientEngine(engineSettings);
torrentEngine.ChangeListenEndpoint(new IPEndPoint(IPAddress.Any, Port));
//Load DHT nodes from a saved file
//if not there or any errors then continue
byte[] nodes = null;
try
{
nodes = File.ReadAllBytes(dhtNodeFile);
}
catch
{
//keep it null, what we use it for will instead create its own and then save them
nodes = null;
}
//construct our listener for our dht engine and then create the dht engine
//register it and then start it based on the nodes we attempted to load
//construct our listener for our dht engine and then create the dht engine
//register it and then start it based on the nodes we attempted to load
dhtListener = new DhtListener(new IPEndPoint(IPAddress.Any, Port));
dhtEngine = new DhtEngine(dhtListener);
torrentEngine.RegisterDht(dhtEngine);
torrentEngine.DhtEngine.Start(nodes);
//same as with the dht nodes try to load but if any errors then continue
try
{
fastResume = BEncodedValue.Decode<BEncodedDictionary>(File.ReadAllBytes(resumeFile));
}
catch
{
fastResume = new BEncodedDictionary();
}
//This is where we go into our save directory and look for any torrents
//user may of left off on a download and we can possibly resume it
//also a backend way to force a download from another provider source to download
foreach (string file in Directory.GetFiles(saveDirectory))
{
if (file.EndsWith(".torrent"))
{
try
{
Torrent torrent = null;
torrent = Torrent.Load(file);
TorrentManager manager = new TorrentManager(torrent, saveDirectory, torrentSettings);
if (fastResume.ContainsKey(torrent.InfoHash.ToHex()))
manager.LoadFastResume(new FastResume((BEncodedDictionary)fastResume[torrent.InfoHash.ToHex()]));
torrentEngine.Register(manager);
}
catch { }
}
}
}