本文整理汇总了Golang中github.com/anacrolix/torrent/bencode.Marshal函数的典型用法代码示例。如果您正苦于以下问题:Golang Marshal函数的具体用法?Golang Marshal怎么用?Golang Marshal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Marshal函数的17个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: test_file
func test_file(t *testing.T, filename string) {
mi, err := LoadFromFile(filename)
if err != nil {
t.Fatal(err)
}
if len(mi.Info.Files) == 1 {
t.Logf("Single file: %s (length: %d)\n", mi.Info.Name, mi.Info.Files[0].Length)
} else {
t.Logf("Multiple files: %s\n", mi.Info.Name)
for _, f := range mi.Info.Files {
t.Logf(" - %s (length: %d)\n", path.Join(f.Path...), f.Length)
}
}
for _, group := range mi.AnnounceList {
for _, tracker := range group {
t.Logf("Tracker: %s\n", tracker)
}
}
// for _, url := range mi.WebSeedURLs {
// t.Logf("URL: %s\n", url)
// }
b, err := bencode.Marshal(mi.Info)
if !bytes.Equal(b, mi.Info.Bytes) {
t.Logf("\n%q\n%q", b[len(b)-20:], mi.Info.Bytes[len(mi.Info.Bytes)-20:])
t.Fatal("encoded and decoded bytes don't match")
}
}
开发者ID:pmwoodward3,项目名称:torrentsaga,代码行数:30,代码来源:metainfo_test.go
示例2: testFile
func testFile(t *testing.T, filename string) {
mi, err := LoadFromFile(filename)
if err != nil {
t.Fatal(err)
}
if len(mi.Info.Files) == 1 {
t.Logf("Single file: %s (length: %d)\n", mi.Info.Name, mi.Info.Files[0].Length)
} else {
t.Logf("Multiple files: %s\n", mi.Info.Name)
for _, f := range mi.Info.Files {
t.Logf(" - %s (length: %d)\n", path.Join(f.Path...), f.Length)
}
}
for _, group := range mi.AnnounceList {
for _, tracker := range group {
t.Logf("Tracker: %s\n", tracker)
}
}
// for _, url := range mi.WebSeedURLs {
// t.Logf("URL: %s\n", url)
// }
b, err := bencode.Marshal(mi.Info)
require.NoError(t, err)
assert.EqualValues(t, b, mi.Info.Bytes)
}
开发者ID:CaptainIlu,项目名称:cloud-torrent,代码行数:28,代码来源:metainfo_test.go
示例3: requestMetadataPiece
func (c *connection) requestMetadataPiece(index int) {
eID := c.PeerExtensionIDs["ut_metadata"]
if eID == 0 {
return
}
if index < len(c.metadataRequests) && c.metadataRequests[index] {
return
}
c.Post(pp.Message{
Type: pp.Extended,
ExtendedID: eID,
ExtendedPayload: func() []byte {
b, err := bencode.Marshal(map[string]int{
"msg_type": pp.RequestMetadataExtensionMsgType,
"piece": index,
})
if err != nil {
panic(err)
}
return b
}(),
})
for index >= len(c.metadataRequests) {
c.metadataRequests = append(c.metadataRequests, false)
}
c.metadataRequests[index] = true
}
开发者ID:jaswinder-singh,项目名称:torrentsaga,代码行数:27,代码来源:connection.go
示例4: UpdateBytes
// Marshals .Info, and sets .Bytes with the result.
func (ie *InfoEx) UpdateBytes() {
var err error
ie.Bytes, err = bencode.Marshal(&ie.Info)
if err != nil {
panic(err)
}
}
开发者ID:ymonk,项目名称:torrent,代码行数:8,代码来源:infoex.go
示例5: query
func (s *Server) query(node dHTAddr, q string, a map[string]interface{}, onResponse func(Msg)) (t *Transaction, err error) {
tid := s.nextTransactionID()
if a == nil {
a = make(map[string]interface{}, 1)
}
a["id"] = s.ID()
d := map[string]interface{}{
"t": tid,
"y": "q",
"q": q,
"a": a,
}
b, err := bencode.Marshal(d)
if err != nil {
return
}
t = &Transaction{
remoteAddr: node,
t: tid,
response: make(chan Msg, 1),
done: make(chan struct{}),
queryPacket: b,
s: s,
onResponse: onResponse,
}
err = t.sendQuery()
if err != nil {
return
}
s.getNode(node, "").lastSentQuery = time.Now()
t.startTimer()
s.addTransaction(t)
return
}
开发者ID:gbjk,项目名称:torrent,代码行数:34,代码来源:dht.go
示例6: sendInitialMessages
func (cl *Client) sendInitialMessages(conn *connection, torrent *Torrent) {
if conn.PeerExtensionBytes.SupportsExtended() && cl.extensionBytes.SupportsExtended() {
conn.Post(pp.Message{
Type: pp.Extended,
ExtendedID: pp.HandshakeExtendedID,
ExtendedPayload: func() []byte {
d := map[string]interface{}{
"m": func() (ret map[string]int) {
ret = make(map[string]int, 2)
ret["ut_metadata"] = metadataExtendedId
if !cl.config.DisablePEX {
ret["ut_pex"] = pexExtendedId
}
return
}(),
"v": extendedHandshakeClientVersion,
// No upload queue is implemented yet.
"reqq": 64,
}
if !cl.config.DisableEncryption {
d["e"] = 1
}
if torrent.metadataSizeKnown() {
d["metadata_size"] = torrent.metadataSize()
}
if p := cl.incomingPeerPort(); p != 0 {
d["p"] = p
}
yourip, err := addrCompactIP(conn.remoteAddr())
if err != nil {
log.Printf("error calculating yourip field value in extension handshake: %s", err)
} else {
d["yourip"] = yourip
}
// log.Printf("sending %v", d)
b, err := bencode.Marshal(d)
if err != nil {
panic(err)
}
return b
}(),
})
}
if torrent.haveAnyPieces() {
conn.Bitfield(torrent.bitfield())
} else if cl.extensionBytes.SupportsFast() && conn.PeerExtensionBytes.SupportsFast() {
conn.Post(pp.Message{
Type: pp.HaveNone,
})
}
if conn.PeerExtensionBytes.SupportsDHT() && cl.extensionBytes.SupportsDHT() && cl.dHT != nil {
conn.Post(pp.Message{
Type: pp.Port,
Port: uint16(missinggo.AddrPort(cl.dHT.Addr())),
})
}
}
开发者ID:jakop345,项目名称:torrent,代码行数:57,代码来源:client.go
示例7: MarshalBencode
func (me CompactPeer) MarshalBencode() (ret []byte, err error) {
ip := me.IP
if ip4 := ip.To4(); ip4 != nil {
ip = ip4
}
ret = make([]byte, len(ip)+2)
copy(ret, ip)
binary.BigEndian.PutUint16(ret[len(ip):], uint16(me.Port))
return bencode.Marshal(ret)
}
开发者ID:CaptainIlu,项目名称:cloud-torrent,代码行数:10,代码来源:types.go
示例8: testMarshalUnmarshalMsg
func testMarshalUnmarshalMsg(t *testing.T, m Msg, expected string) {
b, err := bencode.Marshal(m)
require.NoError(t, err)
assert.Equal(t, expected, string(b))
var _m Msg
err = bencode.Unmarshal([]byte(expected), &_m)
assert.NoError(t, err)
assert.EqualValues(t, m, _m)
assert.EqualValues(t, m.R, _m.R)
}
开发者ID:soul9,项目名称:torrent,代码行数:10,代码来源:dht_test.go
示例9: MarshalBencode
func (me CompactIPv4NodeInfo) MarshalBencode() (ret []byte, err error) {
var buf bytes.Buffer
for _, ni := range me {
buf.Write(ni.ID[:])
if ni.Addr == nil {
err = errors.New("nil addr in node info")
return
}
buf.Write(ni.Addr.IP().To4())
binary.Write(&buf, binary.BigEndian, uint16(ni.Addr.UDPAddr().Port))
}
return bencode.Marshal(buf.Bytes())
}
开发者ID:jaswinder-singh,项目名称:torrentsaga,代码行数:13,代码来源:compactNodeInfo.go
示例10: reply
func (s *Server) reply(addr dHTAddr, t string, r Return) {
r.ID = s.ID()
m := Msg{
T: t,
Y: "r",
R: &r,
}
b, err := bencode.Marshal(m)
if err != nil {
panic(err)
}
err = s.writeToNode(b, addr)
if err != nil {
log.Printf("error replying to %s: %s", addr, err)
}
}
开发者ID:ssi379,项目名称:torrent,代码行数:16,代码来源:dht.go
示例11: newMetadataExtensionMessage
func (t *torrent) newMetadataExtensionMessage(c *connection, msgType int, piece int, data []byte) pp.Message {
d := map[string]int{
"msg_type": msgType,
"piece": piece,
}
if data != nil {
d["total_size"] = len(t.MetaData)
}
p, err := bencode.Marshal(d)
if err != nil {
panic(err)
}
return pp.Message{
Type: pp.Extended,
ExtendedID: byte(c.PeerExtensionIDs["ut_metadata"]),
ExtendedPayload: append(p, data...),
}
}
开发者ID:soul9,项目名称:torrent,代码行数:18,代码来源:torrent.go
示例12: reply
func (s *Server) reply(addr dHTAddr, t string, r map[string]interface{}) {
if r == nil {
r = make(map[string]interface{}, 1)
}
r["id"] = s.ID()
m := map[string]interface{}{
"t": t,
"y": "r",
"r": r,
}
b, err := bencode.Marshal(m)
if err != nil {
panic(err)
}
err = s.writeToNode(b, addr)
if err != nil {
log.Printf("error replying to %s: %s", addr, err)
}
}
开发者ID:pmwoodward3,项目名称:torrentsaga,代码行数:19,代码来源:dht.go
示例13: query
func (s *Server) query(node Addr, q string, a map[string]interface{}, onResponse func(krpc.Msg)) (t *Transaction, err error) {
tid := s.nextTransactionID()
if a == nil {
a = make(map[string]interface{}, 1)
}
a["id"] = s.ID()
d := map[string]interface{}{
"t": tid,
"y": "q",
"q": q,
"a": a,
}
// BEP 43. Outgoing queries from uncontactiable nodes should contain
// "ro":1 in the top level dictionary.
if s.config.Passive {
d["ro"] = 1
}
b, err := bencode.Marshal(d)
if err != nil {
return
}
_t := &Transaction{
remoteAddr: node,
t: tid,
response: make(chan krpc.Msg, 1),
done: make(chan struct{}),
queryPacket: b,
s: s,
onResponse: onResponse,
}
err = _t.sendQuery()
if err != nil {
return
}
s.getNode(node, "").lastSentQuery = time.Now()
_t.mu.Lock()
_t.startTimer()
_t.mu.Unlock()
s.addTransaction(_t)
t = _t
return
}
开发者ID:jakop345,项目名称:torrent,代码行数:42,代码来源:server.go
示例14: testFile
func testFile(t *testing.T, filename string) {
mi, err := LoadFromFile(filename)
require.NoError(t, err)
if len(mi.Info.Files) == 1 {
t.Logf("Single file: %s (length: %d)\n", mi.Info.Name, mi.Info.Files[0].Length)
} else {
t.Logf("Multiple files: %s\n", mi.Info.Name)
for _, f := range mi.Info.Files {
t.Logf(" - %s (length: %d)\n", path.Join(f.Path...), f.Length)
}
}
for _, group := range mi.AnnounceList {
for _, tracker := range group {
t.Logf("Tracker: %s\n", tracker)
}
}
b, err := bencode.Marshal(&mi.Info.Info)
require.NoError(t, err)
assert.EqualValues(t, string(b), string(mi.Info.Bytes))
}
开发者ID:jakop345,项目名称:torrent,代码行数:23,代码来源:metainfo_test.go
示例15: MarshalBencode
func (this InfoEx) MarshalBencode() ([]byte, error) {
if this.Bytes != nil {
return this.Bytes, nil
}
return bencode.Marshal(&this.Info)
}
开发者ID:robaman,项目名称:torrent,代码行数:6,代码来源:metainfo.go
示例16: MarshalBencode
func (e KRPCError) MarshalBencode() (ret []byte, err error) {
return bencode.Marshal([]interface{}{e.Code, e.Msg})
}
开发者ID:jakop345,项目名称:torrent,代码行数:3,代码来源:error.go
示例17: testMarshalMetainfo
func testMarshalMetainfo(t *testing.T, expected string, mi MetaInfo) {
b, err := bencode.Marshal(mi)
assert.NoError(t, err)
assert.EqualValues(t, expected, string(b))
}
开发者ID:CaptainIlu,项目名称:cloud-torrent,代码行数:5,代码来源:nodes_test.go
注:本文中的github.com/anacrolix/torrent/bencode.Marshal函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论