Skip to content

An implementation in C# of B-encode format that is used by the peer-to-peer file sharing system BitTorrent.

License

Notifications You must be signed in to change notification settings

mtanksl/Bencode.net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

B-encode.net

An implementation in C# of B-encode format that is used by the peer-to-peer file sharing system BitTorrent.

Install using NuGet

Nuget

dotnet add package mtanksl.Bencode --version 1.1.0

Specification

From Wikipedia:

  • Byte Strings are encoded as <length>:<bytes>
  • Integers are encoded as i<integer>e
  • Lists are encoded as l<elements>e
  • Dictionaries are encoded as d<pairs>e

Creating with LINQ

BElement value = 
    new BDictionary(
        new BPair("key 1", ""),
        new BPair("key 3", 9223372036854775807L),
        new BPair("key 2", "Hello World"),
        new BPair("key 4", new BList("", "Hello World", 9223372036854775807L) ) );

How to serialize an object

string bencode = BencodeConvert.SerializeObject(value);

// "d5:key 10:5:key 211:Hello World5:key 3i9223372036854775807e5:key 4l0:11:Hello Worldi9223372036854775807eee"

or

using (var stream = File.OpenWrite("...") )
{
    using (var writer = new BencodeWriter(stream) )
    {
        var serializer = new BencodeSerializer();

        serializer.Serialize(writer, value);
    }
}

How to deserialize an object

BElement value = (BElement)BencodeConvert.DeserializeObject(bencode);

or

using (var stream = File.OpenRead("...") )
{
    using (var reader = new BencodeReader(stream) )
    {
        var serializer = new BencodeSerializer();

        var value = (BElement)serializer.Deserialize(reader);

        //...
    }
}

Querying with LINQ

string helloWorld = (string)value["key 4"][1];

// "Hello World"

(See the Tests project for more examples)

Custom Converter

Create a custom converter by implementing an interface

public class MyDto : IBencodeSerializable
{
    public void Read(BencodeReader reader, Type type, BencodeSerializer serializer) { ... }
        
    public void Write(BencodeWriter writer, Type type, BencodeSerializer serializer) { ... }
}

or by implementing a base class

public class MyConverter : BencodeConverter
{
    public override bool CanConvert(Type type) { ... }

    public override object Read(BencodeReader reader, Type type, BencodeSerializer serializer) { ... }

    public override void Write(BencodeWriter writer, object value, Type type, BencodeSerializer serializer) { ... }
}

then using in the settings

new BencodeSerializerSettings() { Converters = new List<BencodeConverter>() { new DateTimeConverter() } }

or in the class attribute

[BencodeObject(ItemConverterType = typeof(TorrentConverter))]

or in the property attribute

[BencodeProperty(ItemConverterType = typeof(DateTimeConverter))]

About

An implementation in C# of B-encode format that is used by the peer-to-peer file sharing system BitTorrent.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages