2
0
mirror of synced 2025-02-24 14:48:27 +00:00

44 lines
719 B
Go
Raw Normal View History

package peer_protocol
import (
2021-09-30 09:01:10 +10:00
"bytes"
"encoding/binary"
"io"
)
type Integer uint32
func (i *Integer) Read(r io.Reader) error {
return binary.Read(r, binary.BigEndian, i)
}
2021-09-30 09:01:10 +10:00
func (i *Integer) UnmarshalBinary(b []byte) error {
return i.Read(bytes.NewReader(b))
}
func (i *Integer) ReadFrom(r io.Reader) error {
var b [4]byte
n, err := r.Read(b[:])
if n == 4 {
*i = Integer(binary.BigEndian.Uint32(b[:]))
return nil
}
if err == nil {
return io.ErrUnexpectedEOF
}
return err
}
// 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 09:16:40 +10:00
func (i Integer) Uint32() uint32 {
return uint32(i)
}