2018-03-09 15:55:25 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2019-09-09 14:54:38 +00:00
|
|
|
"net"
|
2018-03-09 15:55:25 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AddressAndTime payload.
|
|
|
|
type AddressAndTime struct {
|
|
|
|
Timestamp uint32
|
|
|
|
Services uint64
|
2019-09-09 14:54:38 +00:00
|
|
|
IP [16]byte
|
|
|
|
Port uint16
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAddressAndTime creates a new AddressAndTime object.
|
2019-09-09 14:54:38 +00:00
|
|
|
func NewAddressAndTime(e *net.TCPAddr, t time.Time) *AddressAndTime {
|
|
|
|
aat := AddressAndTime{
|
2018-03-09 15:55:25 +00:00
|
|
|
Timestamp: uint32(t.UTC().Unix()),
|
|
|
|
Services: 1,
|
2019-09-09 14:54:38 +00:00
|
|
|
Port: uint16(e.Port),
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
2019-09-09 14:54:38 +00:00
|
|
|
copy(aat.IP[:], e.IP)
|
|
|
|
return &aat
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements the Payload interface.
|
|
|
|
func (p *AddressAndTime) DecodeBinary(r io.Reader) error {
|
2019-08-28 12:43:56 +00:00
|
|
|
br := util.BinReader{R: r}
|
|
|
|
br.ReadLE(&p.Timestamp)
|
|
|
|
br.ReadLE(&p.Services)
|
2019-09-09 14:54:38 +00:00
|
|
|
br.ReadBE(&p.IP)
|
|
|
|
br.ReadBE(&p.Port)
|
2019-08-28 12:43:56 +00:00
|
|
|
return br.Err
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements the Payload interface.
|
|
|
|
func (p *AddressAndTime) EncodeBinary(w io.Writer) error {
|
2019-08-28 12:43:56 +00:00
|
|
|
bw := util.BinWriter{W: w}
|
|
|
|
bw.WriteLE(p.Timestamp)
|
|
|
|
bw.WriteLE(p.Services)
|
2019-09-09 14:54:38 +00:00
|
|
|
bw.WriteBE(p.IP)
|
|
|
|
bw.WriteBE(p.Port)
|
2019-08-28 12:43:56 +00:00
|
|
|
return bw.Err
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddressList is a list with AddrAndTime.
|
|
|
|
type AddressList struct {
|
|
|
|
Addrs []*AddressAndTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements the Payload interface.
|
|
|
|
func (p *AddressList) DecodeBinary(r io.Reader) error {
|
2019-08-28 12:43:56 +00:00
|
|
|
br := util.BinReader{R: r}
|
|
|
|
listLen := br.ReadVarUint()
|
|
|
|
if br.Err != nil {
|
|
|
|
return br.Err
|
|
|
|
}
|
2018-03-09 15:55:25 +00:00
|
|
|
|
|
|
|
p.Addrs = make([]*AddressAndTime, listLen)
|
|
|
|
for i := 0; i < int(listLen); i++ {
|
|
|
|
p.Addrs[i] = &AddressAndTime{}
|
|
|
|
if err := p.Addrs[i].DecodeBinary(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements the Payload interface.
|
|
|
|
func (p *AddressList) EncodeBinary(w io.Writer) error {
|
2019-08-28 12:43:56 +00:00
|
|
|
bw := util.BinWriter{W: w}
|
|
|
|
bw.WriteVarUint(uint64(len(p.Addrs)))
|
|
|
|
if bw.Err != nil {
|
|
|
|
return bw.Err
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
for _, addr := range p.Addrs {
|
|
|
|
if err := addr.EncodeBinary(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|