本文整理汇总了C#中BEncodedList类的典型用法代码示例。如果您正苦于以下问题:C# BEncodedList类的具体用法?C# BEncodedList怎么用?C# BEncodedList使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BEncodedList类属于命名空间,在下文中一共展示了BEncodedList类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadTorrentFileList
/// <summary>
/// Reads the file list from a .torrent file.
/// </summary>
/// <param name="downloadPath"></param>
/// <returns></returns>
public static List<SimpleFileInfo> ReadTorrentFileList(string torrentFile)
{
if (!File.Exists(torrentFile))
throw new FileNotFoundException("Can't find file " + torrentFile);
var ret = new List<SimpleFileInfo>();
//read and decode the .torrent file
byte[] torrentBytes = File.ReadAllBytes(torrentFile);
var torrentData = BEncodedValue.Decode<BEncodedDictionary>(torrentBytes);
BEncodedList fileList = new BEncodedList();
try
{
fileList = torrentData.Item<BEncodedDictionary>("info").Item<BEncodedList>("files");
}
catch { };
foreach (var fileItem in fileList)
{
var fileData = fileItem as BEncodedDictionary;
string filePath = BuildPathFromDirectoryList(fileData.Item<BEncodedList>("path"));
long length = fileData.Item<BEncodedNumber>("length").Number;
ret.Add(new SimpleFileInfo(filePath, length));
}
ret.Sort( new Comparison<SimpleFileInfo>( FileNameComparison));
return ret;
}
示例2: CorruptDictionary
public void CorruptDictionary()
{
BEncodedList l = new BEncodedList();
BEncodedDictionary d = new BEncodedDictionary();
l.Add(d);
IList<Peer> decoded = Peer.Decode(l);
Assert.AreEqual(0, decoded.Count, "#1");
}
示例3: CorruptDictionary
public void CorruptDictionary()
{
var l = new BEncodedList();
var d = new BEncodedDictionary();
l.Add(d);
IList<Peer> decoded = Peer.Decode(l);
Assert.Equal(0, decoded.Count);
}
示例4: ErrorMessage
public ErrorMessage(ErrorCode error, string message)
: base(ErrorType)
{
var l = new BEncodedList();
l.Add(new BEncodedNumber((int) error));
l.Add(new BEncodedString(message));
properties.Add(ErrorListKey, l);
}
示例5: CorruptDictionary
public void CorruptDictionary()
{
var list = new BEncodedList();
var dictionary = new BEncodedDictionary();
list.Add(dictionary);
IList<Peer> decoded = Peer.Decode(list);
Assert.AreEqual(0, decoded.Count, "#1");
}
示例6: benDictionaryEncodingBuffered
public void benDictionaryEncodingBuffered()
{
var data = Encoding.UTF8.GetBytes("d4:spaml1:a1:bee");
var dict = new BEncodedDictionary();
var list = new BEncodedList();
list.Add(new BEncodedString("a"));
list.Add(new BEncodedString("b"));
dict.Add("spam", list);
var result = new byte[dict.LengthInBytes()];
dict.Encode(result, 0);
Assert.True(Toolbox.ByteMatch(data, result));
}
示例7: benDictionaryEncoding
public void benDictionaryEncoding()
{
var data = Encoding.UTF8.GetBytes("d4:spaml1:a1:bee");
var dict = new BEncodedDictionary();
var list = new BEncodedList();
list.Add(new BEncodedString("a"));
list.Add(new BEncodedString("b"));
dict.Add("spam", list);
Assert.Equal(Encoding.UTF8.GetString(data), Encoding.UTF8.GetString(dict.Encode()));
Assert.True(Toolbox.ByteMatch(data, dict.Encode()));
}
示例8: benDictionaryEncoding
public void benDictionaryEncoding()
{
byte[] data = System.Text.Encoding.UTF8.GetBytes("d4:spaml1:a1:bee");
BEncodedDictionary dict = new BEncodedDictionary();
BEncodedList list = new BEncodedList();
list.Add(new BEncodedString("a"));
list.Add(new BEncodedString("b"));
dict.Add("spam", list);
Assert.AreEqual(System.Text.Encoding.UTF8.GetString(data), System.Text.Encoding.UTF8.GetString(dict.Encode()));
Assert.IsTrue(Toolbox.ByteMatch(data, dict.Encode()));
}
示例9: CorruptList
public void CorruptList()
{
var list = new BEncodedList();
for (var i = 0; i < peers.Count; i++)
list.Add((BEncodedString) peers[i].CompactPeer());
list.Insert(2, new BEncodedNumber(5));
VerifyDecodedPeers(Peer.Decode(list));
list.Clear();
list.Add(new BEncodedString(new byte[3]));
IList<Peer> decoded = Peer.Decode(list);
Assert.Equal(0, decoded.Count);
}
示例10: AddCommonStuff
private void AddCommonStuff(BEncodedDictionary torrent)
{
if (Announces.Count > 0 && Announces[0].Count > 0)
Announce = Announces[0][0];
if (GetrightHttpSeeds.Count > 0)
{
var seedlist = new BEncodedList();
seedlist.AddRange(
GetrightHttpSeeds.ConvertAll<BEncodedValue>(delegate(string s) { return (BEncodedString) s; }));
torrent["url-list"] = seedlist;
}
var span = DateTime.Now - new DateTime(1970, 1, 1);
torrent["creation date"] = new BEncodedNumber((long) span.TotalSeconds);
}
示例11: CreateTorrent
private static BEncodedDictionary CreateTorrent(int pieceLength)
{
var infoDict = new BEncodedDictionary();
infoDict[new BEncodedString("piece length")] = new BEncodedNumber(pieceLength);
infoDict[new BEncodedString("pieces")] = new BEncodedString(new byte[20*15]);
infoDict[new BEncodedString("length")] = new BEncodedNumber(15*256*1024 - 1);
infoDict[new BEncodedString("name")] = new BEncodedString("test.files");
var dict = new BEncodedDictionary();
dict[new BEncodedString("info")] = infoDict;
var announceTier = new BEncodedList();
announceTier.Add(new BEncodedString("custom://transfers1/announce"));
announceTier.Add(new BEncodedString("custom://transfers2/announce"));
announceTier.Add(new BEncodedString("http://transfers3/announce"));
var announceList = new BEncodedList();
announceList.Add(announceTier);
dict[new BEncodedString("announce-list")] = announceList;
return dict;
}
示例12: Handle
public override void Handle(DhtEngine engine, Node node)
{
base.Handle(engine, node);
BEncodedString token = engine.TokenManager.GenerateToken(node);
var response = new GetPeersResponse(engine.RoutingTable.LocalNode.Id, TransactionId, token);
if (engine.Torrents.ContainsKey(InfoHash))
{
var list = new BEncodedList();
foreach (Node n in engine.Torrents[InfoHash])
list.Add(n.CompactPort());
response.Values = list;
}
else
{
// Is this right?
response.Nodes = Node.CompactNode(engine.RoutingTable.GetClosest(InfoHash));
}
engine.MessageLoop.EnqueueSend(response, node.EndPoint);
}
示例13: Decode
public static MonoTorrentCollection<Peer> Decode(BEncodedList peers)
{
var list = new MonoTorrentCollection<Peer>(peers.Count);
foreach (var value in peers)
{
try
{
if (value is BEncodedDictionary)
list.Add(DecodeFromDict((BEncodedDictionary) value));
else if (value is BEncodedString)
foreach (var p in Decode((BEncodedString) value))
list.Add(p);
}
catch
{
// If something is invalid and throws an exception, ignore it
// and continue decoding the rest of the peers
}
}
return list;
}
示例14: AddCommonStuff
///<summary>
///this adds stuff common to single and multi file torrents
///</summary>
void AddCommonStuff(BEncodedDictionary torrent)
{
if (announces.Count > 0 && announces[0].Count > 0) torrent.Add("announce", new BEncodedString(announces[0][0]));
// If there is more than one tier or the first tier has more than 1 tracker
if (announces.Count > 1 || (announces.Count > 0 && announces[0].Count > 1))
{
var announceList = new BEncodedList();
for (var i = 0; i < announces.Count; i++)
{
var tier = new BEncodedList();
for (var j = 0; j < announces[i].Count; j++) tier.Add(new BEncodedString(announces[i][j]));
announceList.Add(tier);
}
torrent.Add("announce-list", announceList);
}
var epocheStart = new DateTime(1970, 1, 1);
var span = DateTime.Now - epocheStart;
Logger.Log(null, "creation date: {0} - {1} = {2}:{3}", DateTime.Now, epocheStart, span, span.TotalSeconds);
torrent.Add("creation date", new BEncodedNumber((long)span.TotalSeconds));
}
示例15: SaveNodes
public byte[] SaveNodes()
{
BEncodedList details = new BEncodedList();
MainLoop.QueueWait((MainLoopTask)delegate {
foreach (Bucket b in RoutingTable.Buckets)
{
foreach (Node n in b.Nodes)
if (n.State != NodeState.Bad)
details.Add(n.CompactNode());
if (b.Replacement != null)
if (b.Replacement.State != NodeState.Bad)
details.Add(b.Replacement.CompactNode());
}
});
return details.Encode();
}