neo-go/pkg/wire/payload/maddr.go

65 lines
1.3 KiB
Go
Raw Normal View History

2019-02-25 22:44:14 +00:00
package payload
import (
"io"
"github.com/CityOfZion/neo-go/pkg/wire/command"
"github.com/CityOfZion/neo-go/pkg/wire/util"
)
// AddrMessage represents an address message on the neo network
2019-02-25 22:44:14 +00:00
type AddrMessage struct {
AddrList []*NetAddr
2019-02-25 22:44:14 +00:00
}
// NewAddrMessage instantiates a new AddrMessage
2019-02-25 22:44:14 +00:00
func NewAddrMessage() (*AddrMessage, error) {
addrMess := &AddrMessage{
nil,
}
return addrMess, nil
}
// AddNetAddr will add a net address into the Address message
func (a *AddrMessage) AddNetAddr(n *NetAddr) error {
2019-02-25 22:44:14 +00:00
a.AddrList = append(a.AddrList, n)
// TODO:check if max reached, if so return err. What is max?
return nil
}
// DecodePayload Implements Messager interface
2019-02-25 22:44:14 +00:00
func (a *AddrMessage) DecodePayload(r io.Reader) error {
br := &util.BinReader{R: r}
listLen := br.VarUint()
a.AddrList = make([]*NetAddr, listLen)
2019-02-25 22:44:14 +00:00
for i := 0; i < int(listLen); i++ {
a.AddrList[i] = &NetAddr{}
2019-02-25 22:44:14 +00:00
a.AddrList[i].DecodePayload(br)
if br.Err != nil {
return br.Err
}
}
return br.Err
}
// EncodePayload Implements messager interface
func (a *AddrMessage) EncodePayload(w io.Writer) error {
2019-02-25 22:44:14 +00:00
bw := &util.BinWriter{W: w}
listLen := uint64(len(a.AddrList))
2019-02-25 22:44:14 +00:00
bw.VarUint(listLen)
for _, addr := range a.AddrList {
2019-02-25 22:44:14 +00:00
addr.EncodePayload(bw)
}
return bw.Err
}
// Command Implements messager interface
func (a *AddrMessage) Command() command.Type {
2019-02-25 22:44:14 +00:00
return command.Addr
}