2018-02-02 18:29:57 +08:00
|
|
|
package peer_protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
2021-09-30 08:24:03 +08:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2018-02-02 18:29:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Integer uint32
|
|
|
|
|
2021-09-30 07:01:10 +08:00
|
|
|
func (i *Integer) UnmarshalBinary(b []byte) error {
|
2021-09-30 08:24:03 +08:00
|
|
|
if len(b) != 4 {
|
|
|
|
return errors.New("expected 4 bytes")
|
|
|
|
}
|
|
|
|
*i = Integer(binary.BigEndian.Uint32(b))
|
|
|
|
return nil
|
2021-09-30 07:01:10 +08:00
|
|
|
}
|
|
|
|
|
2021-09-30 08:24:03 +08:00
|
|
|
func (i *Integer) Read(r io.Reader) error {
|
2021-09-30 07:59:18 +08:00
|
|
|
var b [4]byte
|
2021-10-19 10:13:23 +08:00
|
|
|
n, err := io.ReadFull(r, b[:])
|
2021-09-30 07:59:18 +08:00
|
|
|
if err == nil {
|
2021-10-19 10:13:23 +08:00
|
|
|
if n != 4 {
|
|
|
|
panic(n)
|
|
|
|
}
|
|
|
|
return i.UnmarshalBinary(b[:])
|
2021-09-30 07:59:18 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-02 18:29:57 +08:00
|
|
|
// It's perfectly fine to cast these to an int. TODO: Or is it?
|
|
|
|
func (i Integer) Int() int {
|
|
|
|
return int(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i Integer) Uint64() uint64 {
|
|
|
|
return uint64(i)
|
|
|
|
}
|
2018-07-12 07:16:40 +08:00
|
|
|
|
|
|
|
func (i Integer) Uint32() uint32 {
|
|
|
|
return uint32(i)
|
|
|
|
}
|