Skip to content

Commit

Permalink
remove obsolete temp layer
Browse files Browse the repository at this point in the history
  • Loading branch information
BugFreeSoftware committed Jun 23, 2023
1 parent 66680cd commit b67e0b3
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 342 deletions.
10 changes: 6 additions & 4 deletions packages/isc/hname.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ func (hn Hname) String() string {
}

func (hn *Hname) Read(r io.Reader) error {
u32, err := rwutil.ReadUint32(r)
*hn = Hname(u32)
return err
rr := rwutil.NewReader(r)
*hn = Hname(rr.ReadUint32())
return rr.Err
}

func (hn *Hname) Write(w io.Writer) error {
return rwutil.WriteUint32(w, uint32(*hn))
ww := rwutil.NewWriter(w)
ww.WriteUint32(uint32(*hn))
return ww.Err
}
12 changes: 5 additions & 7 deletions packages/peering/lpp/lpp_net_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ func (n *netImpl) maintenanceLoop(stopCh chan bool) {
}
}

// readFrame differs from rwutil.ReadBytes because it uses ReadFull instead of Read to read the data.
// readFrame differs from ReadBytes because it uses ReadFull instead of Read to read the data.
func readFrame(stream network.Stream) ([]byte, error) {
var msgLenB [4]byte
if msgLenN, err := io.ReadFull(stream, msgLenB[:]); err != nil || msgLenN != len(msgLenB) {
Expand All @@ -560,12 +560,10 @@ func readFrame(stream network.Stream) ([]byte, error) {
}

func writeFrame(stream network.Stream, payload []byte) error {
err := rwutil.WriteUint32(stream, uint32(len(payload)))
if err != nil {
return err
}
ww := rwutil.NewWriter(stream)
ww.WriteUint32(uint32(len(payload)))
if len(payload) != 0 {
_, err = stream.Write(payload)
ww.WriteN(payload)
}
return err
return ww.Err
}
274 changes: 34 additions & 240 deletions packages/util/rwutil/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
package rwutil

import (
"encoding/binary"
"errors"
"io"
"math"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -52,159 +50,9 @@ func WriteN(w io.Writer, data []byte) error {
return nil
}

//////////////////// bool \\\\\\\\\\\\\\\\\\\\
//////////////////// size32 encoding/decoding \\\\\\\\\\\\\\\\\\\\

func ReadBool(r io.Reader) (bool, error) {
var b [1]byte
err := ReadN(r, b[:])
if err != nil {
return false, err
}
if (b[0] & 0xfe) != 0x00 {
return false, errors.New("unexpected bool value")
}
return b[0] != 0, nil
}

func WriteBool(w io.Writer, cond bool) error {
var b [1]byte
if cond {
b[0] = 1
}
err := WriteN(w, b[:])
return err
}

//////////////////// byte \\\\\\\\\\\\\\\\\\\\

func ReadByte(r io.Reader) (byte, error) {
var b [1]byte
err := ReadN(r, b[:])
return b[0], err
}

func WriteByte(w io.Writer, val byte) error {
return WriteN(w, []byte{val})
}

//////////////////// bytes \\\\\\\\\\\\\\\\\\\\

func ReadBytes(r io.Reader) ([]byte, error) {
length, err := ReadSize32(r)
if err != nil {
return nil, err
}
if length == 0 {
return []byte{}, nil
}
ret := make([]byte, length)
err = ReadN(r, ret)
if err != nil {
return nil, err
}
return ret, nil
}

func WriteBytes(w io.Writer, data []byte) error {
size := len(data)
if size > math.MaxUint32 {
panic("data size overflow")
}
err := WriteSize32(w, uint32(size))
if err != nil {
return err
}
if size != 0 {
return WriteN(w, data)
}
return nil
}

//////////////////// int8 \\\\\\\\\\\\\\\\\\\\

func ReadInt8(r io.Reader) (int8, error) {
val, err := ReadUint8(r)
return int8(val), err
}

func WriteInt8(w io.Writer, val int8) error {
return WriteUint8(w, uint8(val))
}

//////////////////// int16 \\\\\\\\\\\\\\\\\\\\

func ReadInt16(r io.Reader) (int16, error) {
val, err := ReadUint16(r)
return int16(val), err
}

func WriteInt16(w io.Writer, val int16) error {
return WriteUint16(w, uint16(val))
}

//////////////////// int32 \\\\\\\\\\\\\\\\\\\\

func ReadInt32(r io.Reader) (int32, error) {
val, err := ReadUint32(r)
return int32(val), err
}

func WriteInt32(w io.Writer, val int32) error {
return WriteUint32(w, uint32(val))
}

//////////////////// int64 \\\\\\\\\\\\\\\\\\\\

func ReadInt64(r io.Reader) (int64, error) {
val, err := ReadUint64(r)
return int64(val), err
}

func WriteInt64(w io.Writer, val int64) error {
return WriteUint64(w, uint64(val))
}

//////////////////// size32 \\\\\\\\\\\\\\\\\\\\

func Size32FromBytes(data []byte) (uint32, error) {
buf := Buffer(data)
return ReadSize32(&buf)
}

func Size32ToBytes(s uint32) []byte {
switch {
case s < 0x80:
return []byte{byte(s)}
case s < 0x4000:
return []byte{byte(s | 0x80), byte(s >> 7)}
case s < 0x200000:
return []byte{byte(s | 0x80), byte((s >> 7) | 0x80), byte(s >> 14)}
case s < 0x10000000:
return []byte{byte(s | 0x80), byte((s >> 7) | 0x80), byte((s >> 14) | 0x80), byte(s >> 21)}
default:
return []byte{byte(s | 0x80), byte((s >> 7) | 0x80), byte((s >> 14) | 0x80), byte((s >> 21) | 0x80), byte(s >> 28)}
}
}

func MustSize32FromBytes(b []byte) uint32 {
size, err := Size32FromBytes(b)
if err != nil {
panic(err)
}
return size
}

func ReadSize32(r io.Reader) (uint32, error) {
return decodeSize32(func() (byte, error) {
return ReadByte(r)
})
}

func WriteSize32(w io.Writer, value uint32) error {
return WriteN(w, Size32ToBytes(value))
}

func decodeSize32(readByte func() (byte, error)) (uint32, error) {
func size32Decode(readByte func() (byte, error)) (uint32, error) {
b, err := readByte()
if err != nil {
return 0, err
Expand Down Expand Up @@ -251,95 +99,22 @@ func decodeSize32(readByte func() (byte, error)) (uint32, error) {
return 0, errors.New("size32 overflow")
}

//////////////////// string \\\\\\\\\\\\\\\\\\\\

func ReadString(r io.Reader) (string, error) {
ret, err := ReadBytes(r)
if err != nil {
return "", err
}
return string(ret), err
}

func WriteString(w io.Writer, str string) error {
return WriteBytes(w, []byte(str))
}

//////////////////// uint8 \\\\\\\\\\\\\\\\\\\\

func ReadUint8(r io.Reader) (uint8, error) {
var b [1]byte
err := ReadN(r, b[:])
return b[0], err
}

func WriteUint8(w io.Writer, val uint8) error {
return WriteN(w, []byte{val})
}

//////////////////// uint16 \\\\\\\\\\\\\\\\\\\\

func ReadUint16(r io.Reader) (uint16, error) {
var b [2]byte
err := ReadN(r, b[:])
if err != nil {
return 0, err
}
return binary.LittleEndian.Uint16(b[:]), nil
}

func WriteUint16(w io.Writer, val uint16) error {
var b [2]byte
binary.LittleEndian.PutUint16(b[:], val)
return WriteN(w, b[:])
}

//////////////////// uint32 \\\\\\\\\\\\\\\\\\\\

func ReadUint32(r io.Reader) (uint32, error) {
var b [4]byte
err := ReadN(r, b[:])
if err != nil {
return 0, err
}
return binary.LittleEndian.Uint32(b[:]), nil
}

func WriteUint32(w io.Writer, val uint32) error {
var b [4]byte
binary.LittleEndian.PutUint32(b[:], val)
return WriteN(w, b[:])
}

//////////////////// uint64 \\\\\\\\\\\\\\\\\\\\

func ReadUint64(r io.Reader) (uint64, error) {
var b [8]byte
err := ReadN(r, b[:])
if err != nil {
return 0, err
func size32Encode(s uint32) []byte {
switch {
case s < 0x80:
return []byte{byte(s)}
case s < 0x4000:
return []byte{byte(s | 0x80), byte(s >> 7)}
case s < 0x200000:
return []byte{byte(s | 0x80), byte((s >> 7) | 0x80), byte(s >> 14)}
case s < 0x10000000:
return []byte{byte(s | 0x80), byte((s >> 7) | 0x80), byte((s >> 14) | 0x80), byte(s >> 21)}
default:
return []byte{byte(s | 0x80), byte((s >> 7) | 0x80), byte((s >> 14) | 0x80), byte((s >> 21) | 0x80), byte(s >> 28)}
}
return binary.LittleEndian.Uint64(b[:]), nil
}

func WriteUint64(w io.Writer, val uint64) error {
var b [8]byte
binary.LittleEndian.PutUint64(b[:], val)
return WriteN(w, b[:])
}

//////////////////// bytes \\\\\\\\\\\\\\\\\\\\

// ReadFromFunc allows a reader to use any <Type>FromBytes()-like function as a source.
// It will read the next group of bytes and pass it to the specified function and
// returns the correct type of object
func ReadFromFunc[T any](rr *Reader, fromBytes func([]byte) (T, error)) (ret T) {
data := rr.ReadBytes()
if rr.Err == nil {
ret, rr.Err = fromBytes(data)
}
return ret
}
//////////////////// one-line implementation wrapper functions \\\\\\\\\\\\\\\\\\\\

// ReadFromBytes is a wrapper that uses an object's Read() function to marshal
// the object from data bytes. It's typically used to implement a one-line
Expand Down Expand Up @@ -371,6 +146,25 @@ func WriteToBytes(obj IoWriter) []byte {
return ww.Bytes()
}

//////////////////// misc generic wrapper functions \\\\\\\\\\\\\\\\\\\\

// ReadFromFunc allows a reader to use any <Type>FromBytes()-like function as a source.
// It will read the next group of bytes and pass it to the specified function and
// returns the correct type of object
func ReadFromFunc[T any](rr *Reader, fromBytes func([]byte) (T, error)) (ret T) {
data := rr.ReadBytes()
if rr.Err == nil {
ret, rr.Err = fromBytes(data)
}
return ret
}

// SerializationTest can be used with any object that implements IoReader and IoWriter
// to test whether the Read() and Write() functions complement each other correctly.
// You pass in an object that has all fields that need serialization set, plus a new,
// empty object that will receive the deserialized data. The function will Write, Read,
// and Write again and compare the objects for equality and both serialized versions
// as well.
func SerializationTest[T IoReadWriter](t *testing.T, obj1 T, newObj T) {
data1 := WriteToBytes(obj1)
obj2, err := ReadFromBytes(data1, newObj)
Expand Down
Loading

0 comments on commit b67e0b3

Please sign in to comment.