neoneo-go/pkg/network/payload/addr.go

92 lines
2.1 KiB
Go
Raw Normal View History

2018-01-27 15:00:28 +00:00
package payload
import (
2018-01-28 10:12:05 +00:00
"encoding/binary"
2018-01-28 15:18:48 +00:00
"io"
2018-02-01 18:54:23 +00:00
"github.com/CityOfZion/neo-go/pkg/util"
2018-01-29 18:17:49 +00:00
)
2018-01-28 13:59:32 +00:00
2018-01-27 15:00:28 +00:00
// AddrWithTime payload
type AddrWithTime struct {
2018-01-28 13:59:32 +00:00
// Timestamp the node connected to the network.
2018-01-27 15:00:28 +00:00
Timestamp uint32
Services uint64
2018-01-29 18:17:49 +00:00
Addr util.Endpoint
2018-01-27 15:00:28 +00:00
}
2018-01-29 18:17:49 +00:00
// NewAddrWithTime return a pointer to AddrWithTime.
func NewAddrWithTime(addr util.Endpoint) *AddrWithTime {
2018-01-28 15:18:48 +00:00
return &AddrWithTime{
Timestamp: 1337,
Services: 1,
Addr: addr,
}
}
// Size implements the payload interface.
2018-01-27 15:00:28 +00:00
func (p *AddrWithTime) Size() uint32 {
2018-01-28 10:12:05 +00:00
return 30
2018-01-27 15:00:28 +00:00
}
2018-01-28 15:18:48 +00:00
// DecodeBinary implements the Payload interface.
func (p *AddrWithTime) DecodeBinary(r io.Reader) error {
err := binary.Read(r, binary.LittleEndian, &p.Timestamp)
err = binary.Read(r, binary.LittleEndian, &p.Services)
err = binary.Read(r, binary.BigEndian, &p.Addr.IP)
err = binary.Read(r, binary.BigEndian, &p.Addr.Port)
return err
2018-01-27 15:00:28 +00:00
}
2018-01-28 15:18:48 +00:00
// EncodeBinary implements the Payload interface.
func (p *AddrWithTime) EncodeBinary(w io.Writer) error {
err := binary.Write(w, binary.LittleEndian, p.Timestamp)
err = binary.Write(w, binary.LittleEndian, p.Services)
err = binary.Write(w, binary.BigEndian, p.Addr.IP)
err = binary.Write(w, binary.BigEndian, p.Addr.Port)
return err
2018-01-28 10:12:05 +00:00
}
2018-01-28 15:18:48 +00:00
// AddressList holds a slice of AddrWithTime.
2018-01-28 10:12:05 +00:00
type AddressList struct {
Addrs []*AddrWithTime
2018-01-27 15:00:28 +00:00
}
2018-01-28 15:18:48 +00:00
// DecodeBinary implements the Payload interface.
func (p *AddressList) DecodeBinary(r io.Reader) error {
2018-01-28 10:12:05 +00:00
var lenList uint8
2018-01-28 15:18:48 +00:00
binary.Read(r, binary.LittleEndian, &lenList)
2018-01-28 10:12:05 +00:00
p.Addrs = make([]*AddrWithTime, lenList)
2018-01-28 15:18:48 +00:00
for i := 0; i < int(4); i++ {
addr := &AddrWithTime{}
if err := addr.DecodeBinary(r); err != nil {
2018-01-28 15:18:48 +00:00
return err
}
p.Addrs[i] = addr
2018-01-28 10:12:05 +00:00
}
2018-01-27 15:00:28 +00:00
return nil
}
2018-01-28 15:18:48 +00:00
// EncodeBinary implements the Payload interface.
func (p *AddressList) EncodeBinary(w io.Writer) error {
// Write the length of the slice
binary.Write(w, binary.LittleEndian, uint8(len(p.Addrs)))
for _, addr := range p.Addrs {
if err := addr.EncodeBinary(w); err != nil {
return err
}
}
return nil
2018-01-28 10:12:05 +00:00
}
// Size implements the Payloader interface.
func (p *AddressList) Size() uint32 {
return uint32(len(p.Addrs) * 30)
2018-01-27 15:00:28 +00:00
}