Skip to content

opencave/bencode

Repository files navigation

bencode

License Language version Go report Go Reference

bencode (pronounced like Bee-encode) is the encoding used by the peer-to-peer file sharing system BitTorrent for storing and transmitting loosely structured data.

About opencave

opencave is an open source organization focusing on peer-to-peer solutions, find more information here

Install

go get github.com/opencave/bencode

Usage

bencode.Marshal take a object and returns a byte slice. bencode.Unmarshal take a byte slice and bind data pointer.

import "github.com/opencave/bencode"

func main() {
	type FileInfo struct {
		Name       string  `bencode:"name"`
		Size       int     `bencode:"size"`
		FloatValue float64 // will be ignored, bencode not support float64 datatype
	}

	type Torrent struct {
		Announce string     `bencode:"announce"`
		Files    []FileInfo `bencode:"files"`
		Created  int64      `bencode:"-"`
	}

	tor := Torrent{
		Announce: "http://tracker",
		Files: []FileInfo{
			{Name: "file1.txt", Size: 1024, FloatValue: 1.0},
		},
	}
	data, err := Marshal(tor)
	if err != nil {
	  // do something
	}
	fmt.Println(string(data)) // d8:announce14:http://tracker5:filesld4:name9:file1.txt4:sizei1024eeee

	var decoded Torrent
	err = Unmarshal(data, &decoded)
	if err != nil {
	  // do something
	}
	fmt.Printf("announce: %s\n", tor.Announce) // announce: http://tracker
}

License

openHoles is licensed under the Apache License 2.0. Refer to LICENSE for more details.

About

bencode codec implement by Go

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages