本文整理汇总了C#中BEncodedDictionary.Encode方法的典型用法代码示例。如果您正苦于以下问题:C# BEncodedDictionary.Encode方法的具体用法?C# BEncodedDictionary.Encode怎么用?C# BEncodedDictionary.Encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BEncodedDictionary
的用法示例。
在下文中一共展示了BEncodedDictionary.Encode方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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()));
}
示例2: 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()));
}
示例3: 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));
}
示例4: shutdown
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);
}
示例5: HandleLtMetadataMessage
protected override void HandleLtMetadataMessage(PeerId id, LTMetadata message)
{
base.HandleLtMetadataMessage(id, message);
switch (message.MetadataMessageType)
{
case LTMetadata.eMessageType.Data:
if (stream == null)
throw new Exception("Need extention handshake before ut_metadata message.");
stream.Seek(message.Piece * LTMetadata.BlockSize, SeekOrigin.Begin);
stream.Write(message.MetadataPiece, 0, message.MetadataPiece.Length);
bitField[message.Piece] = true;
if (bitField.AllTrue)
{
byte[] hash;
stream.Position = 0;
using (SHA1 hasher = HashAlgoFactory.Create<SHA1>())
hash = hasher.ComputeHash(stream);
if (!Manager.InfoHash.Equals (hash))
{
bitField.SetAll(false);
}
else
{
Torrent t;
stream.Position = 0;
BEncodedDictionary dict = new BEncodedDictionary();
dict.Add ("info", BEncodedValue.Decode(stream));
// FIXME: Add the trackers too
if (Torrent.TryLoad(dict.Encode (), out t))
{
try
{
if (Directory.Exists(savePath))
savePath = Path.Combine (savePath, Manager.InfoHash.ToHex() + ".torrent");
File.WriteAllBytes(savePath, dict.Encode ());
}
catch (Exception ex)
{
Logger.Log(null, "*METADATA EXCEPTION* - Can not write in {0} : {1}", savePath, ex);
Manager.Error = new Error (Reason.WriteFailure, ex);
Manager.Mode = new ErrorMode(Manager);
return;
}
t.TorrentPath = savePath;
Manager.Torrent = t;
SwitchToRegular();
}
else
{
bitField.SetAll(false);
}
}
}
//Double test because we can change the bitfield in the other block
if (!bitField.AllTrue)
{
RequestNextNeededPiece(id);
}
break;
case LTMetadata.eMessageType.Reject:
//TODO
//Think to what we do in this situation
//for moment nothing ;)
//reject or flood?
break;
case LTMetadata.eMessageType.Request://ever done in base class but needed to avoid default
break;
default:
throw new MessageException(string.Format("Invalid messagetype in LTMetadata: {0}", message.MetadataMessageType));
}
}
示例6: shutdown
private static void shutdown()
{
BEncodedDictionary fastResume = new BEncodedDictionary();
for (int i = 0; i < torrents.Count; i++)
{
WaitHandle handle = torrents[i].Stop(); ;
while (!handle.WaitOne(10, true))
Console.WriteLine(handle.ToString());
Console.WriteLine(handle.ToString());
fastResume.Add(torrents[i].Torrent.InfoHash, torrents[i].SaveFastResume().Encode());
}
File.WriteAllBytes(dhtNodeFile, engine.DhtEngine.SaveNodes());
File.WriteAllBytes(fastResumeFile, fastResume.Encode());
engine.Dispose();
foreach (TraceListener lst in Debug.Listeners)
{
lst.Flush();
lst.Close();
}
System.Threading.Thread.Sleep(2000);
}
示例7: WindowClosed
private void WindowClosed(object sender, EventArgs e)
{
if (SettingsManager.SaveSession)
{
var resume = new BEncodedDictionary();
var serializer = new JsonSerializer();
foreach (var torrent in Client.Torrents)
{
torrent.Torrent.Stop();
var start = DateTime.Now;
while (torrent.Torrent.State != TorrentState.Stopped && torrent.Torrent.State != TorrentState.Error &&
(DateTime.Now - start).TotalSeconds < 2) // Time limit for trying to let it stop on its own
Thread.Sleep(100);
torrent.UpdateInfo();
using (var writer = new StreamWriter(Path.Combine(SettingsManager.TorrentCachePath,
Path.GetFileNameWithoutExtension(torrent.CacheFilePath) + ".info")))
serializer.Serialize(new JsonTextWriter(writer), torrent.TorrentInfo);
// TODO: Notify users on error? The application is shutting down here, it wouldn't be particualry
// easy to get information to the user
resume.Add(torrent.Torrent.InfoHash.ToHex(), torrent.Torrent.SaveFastResume().Encode());
}
File.WriteAllBytes(SettingsManager.FastResumePath, resume.Encode());
}
SettingsManager.WindowWidth = (int)Width;
SettingsManager.WindowHeight = (int)Height;
SaveSettings();
Client.Shutdown();
}
示例8: SaveFastResume
public void SaveFastResume ()
{
BEncodedDictionary dict = new BEncodedDictionary();
foreach (TorrentManager manager in items)
dict.Add (manager.Torrent.InfoHash, manager.SaveFastResume ().Encode ());
try
{
File.WriteAllBytes (TorrentCurses.fast_resume, dict.Encode ());
}
catch
{
}
}
示例9: HandleLtMetadataMessage
protected override async void HandleLtMetadataMessage(PeerId id, LTMetadata message)
{
base.HandleLtMetadataMessage(id, message);
switch (message.MetadataMessageType)
{
case LTMetadata.eMessageType.Data:
if (Stream == null)
throw new Exception("Need extention handshake before ut_metadata message.");
Stream.Seek(message.Piece*LTMetadata.BlockSize, SeekOrigin.Begin);
Stream.Write(message.MetadataPiece, 0, message.MetadataPiece.Length);
_bitField[message.Piece] = true;
if (_bitField.AllTrue)
{
Stream.Position = 0;
var hash = SHA1Helper.ComputeHash(Stream.ToArray());
if (!Manager.InfoHash.Equals(hash))
{
_bitField.SetAll(false);
}
else
{
Common.Torrent t;
Stream.Position = 0;
var dict = new BEncodedDictionary {{"info", BEncodedValue.Decode(Stream)}};
// FIXME: Add the trackers too
if (Common.Torrent.TryLoad(dict.Encode(), out t))
{
try
{
var file = await _saveFolder.CreateFileAsync(Manager.InfoHash.ToHex() + ".torrent",
CreationCollisionOption.ReplaceExisting);
File.WriteAllBytes(file.Path, dict.Encode());
}
catch (Exception ex)
{
Debug.WriteLine("*METADATA EXCEPTION* - Can not write in {0} : {1}", _saveFolder,
ex);
Manager.Error = new Error(Reason.WriteFailure, ex);
Manager.Mode = new ErrorMode(Manager);
return;
}
t.TorrentPath = _saveFolder.Path;
Manager.Torrent = t;
SwitchToRegular();
}
else
{
_bitField.SetAll(false);
}
}
}
//Double test because we can change the bitfield in the other block
if (!_bitField.AllTrue)
{
RequestNextNeededPiece(id);
}
break;
case LTMetadata.eMessageType.Reject:
//TODO
//Think to what we do in this situation
//for moment nothing ;)
//reject or flood?
break;
case LTMetadata.eMessageType.Request: //ever done in base class but needed to avoid default
break;
default:
throw new MessageException(string.Format("Invalid messagetype in LTMetadata: {0}",
message.MetadataMessageType));
}
}
示例10: Shutdown
private static void Shutdown()
{
var fastResume = new BEncodedDictionary();
foreach (var torrentManager in torrents)
{
torrentManager.Stop();
while (torrentManager.State != TorrentState.Stopped)
{
Console.WriteLine("{0} is {1}", torrentManager.Torrent.Name, torrentManager.State);
Thread.Sleep(250);
}
fastResume.Add(torrentManager.Torrent.InfoHash.ToHex (), torrentManager.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();
}
Thread.Sleep(2000);
}
示例11: Shutdown
/// <summary>
/// Shutdowns torrent client and saves unfinished torrents to resume file
/// This method must be called when Exit button is pressed!
/// </summary>
public void Shutdown()
{
var fastResume = new BEncodedDictionary();
foreach (var torrent in _torrents)
{
torrent.Stop();
while (torrent.State != TorrentState.Stopped)
{
Thread.Sleep(250);
}
fastResume.Add(torrent.Torrent.InfoHash.ToHex(), torrent.SaveFastResume().Encode());
}
File.WriteAllBytes(FastResumeFile, fastResume.Encode());
_engine.Dispose();
Thread.Sleep(2000);
}
示例12: WindowClosed
private void WindowClosed(object sender, EventArgs e)
{
var resume = new BEncodedDictionary();
foreach (var torrent in Client.Torrents)
{
torrent.Torrent.Stop();
while (torrent.Torrent.State != TorrentState.Stopped && torrent.Torrent.State != TorrentState.Error)
Thread.Sleep(100);
// TODO: Notify users on error? The application is shutting down here, it wouldn't be particualry
// easy to get information to the user
resume.Add(torrent.Torrent.InfoHash.ToHex(), torrent.Torrent.SaveFastResume().Encode());
}
File.WriteAllBytes(SettingsManager.FastResumePath, resume.Encode());
Client.Shutdown();
}
示例13: Stop
void Stop()
{
File.WriteAllBytes(dhtNodeFile, engine.DhtEngine.SaveNodes());
BEncodedDictionary fastResume = new BEncodedDictionary();
for (int i = 0; i < torrents.Count; i++)
{
torrents[i].Stop();
try
{
long count = 0;
while (torrents[i].State != TorrentState.Stopped && count < 5000)
{
Console.WriteLine("{0} is {1}", torrents[i].Torrent.Name, torrents[i].State);
count += 250;
Thread.Sleep(250);
}
}
catch
{
}
fastResume.Add(torrents[i].Torrent.InfoHash.ToHex(), torrents[i].SaveFastResume().Encode());
}
File.WriteAllBytes(fastResumeFile, fastResume.Encode());
engine.Dispose();
System.Threading.Thread.Sleep(2000);
}