github.com/Mrs4s/MiraiGo@v0.0.0-20240226124653-54bdd873e3fe/binary/reader.go (about)

     1  package binary
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"io"
     7  	"net"
     8  
     9  	"github.com/Mrs4s/MiraiGo/utils"
    10  )
    11  
    12  type Reader struct {
    13  	buf *bytes.Reader
    14  }
    15  
    16  type NetworkReader struct {
    17  	conn net.Conn
    18  }
    19  
    20  // --- ByteStream reader ---
    21  
    22  func NewReader(data []byte) *Reader {
    23  	buf := bytes.NewReader(data)
    24  	return &Reader{
    25  		buf: buf,
    26  	}
    27  }
    28  
    29  func (r *Reader) ReadByte() byte {
    30  	b, err := r.buf.ReadByte()
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  	return b
    35  }
    36  
    37  func (r *Reader) ReadBytes(len int) []byte {
    38  	b := make([]byte, len)
    39  	if len > 0 {
    40  		_, err := r.buf.Read(b)
    41  		if err != nil {
    42  			panic(err)
    43  		}
    44  	}
    45  	return b
    46  }
    47  
    48  func (r *Reader) ReadBytesShort() []byte {
    49  	return r.ReadBytes(int(r.ReadUInt16()))
    50  }
    51  
    52  func (r *Reader) ReadUInt16() uint16 {
    53  	b := make([]byte, 2)
    54  	_, _ = r.buf.Read(b)
    55  	return binary.BigEndian.Uint16(b)
    56  }
    57  
    58  func (r *Reader) ReadInt32() int32 {
    59  	b := make([]byte, 4)
    60  	_, _ = r.buf.Read(b)
    61  	return int32(binary.BigEndian.Uint32(b))
    62  }
    63  
    64  func (r *Reader) ReadInt64() int64 {
    65  	b := make([]byte, 8)
    66  	_, _ = r.buf.Read(b)
    67  	return int64(binary.BigEndian.Uint64(b))
    68  }
    69  
    70  func (r *Reader) ReadString() string {
    71  	data := r.ReadBytes(int(r.ReadInt32() - 4))
    72  	return utils.B2S(data)
    73  }
    74  
    75  func (r *Reader) ReadInt32Bytes() []byte {
    76  	return r.ReadBytes(int(r.ReadInt32() - 4))
    77  }
    78  
    79  func (r *Reader) ReadStringShort() string {
    80  	data := r.ReadBytes(int(r.ReadUInt16()))
    81  	return utils.B2S(data)
    82  }
    83  
    84  func (r *Reader) ReadStringLimit(limit int) string {
    85  	data := r.ReadBytes(limit)
    86  	return utils.B2S(data)
    87  }
    88  
    89  func (r *Reader) ReadAvailable() []byte {
    90  	return r.ReadBytes(r.buf.Len())
    91  }
    92  
    93  func (r *Reader) Len() int {
    94  	return r.buf.Len()
    95  }
    96  
    97  func (r *Reader) Index() int64 {
    98  	return r.buf.Size()
    99  }
   100  
   101  // --- Network reader ---
   102  
   103  func NewNetworkReader(conn net.Conn) *NetworkReader {
   104  	return &NetworkReader{conn: conn}
   105  }
   106  
   107  func (r *NetworkReader) ReadByte() (byte, error) {
   108  	buf := make([]byte, 1)
   109  	n, err := r.conn.Read(buf)
   110  	if err != nil {
   111  		return 0, err
   112  	}
   113  	if n != 1 {
   114  		return r.ReadByte()
   115  	}
   116  	return buf[0], nil
   117  }
   118  
   119  func (r *NetworkReader) ReadBytes(len int) ([]byte, error) {
   120  	buf := make([]byte, len)
   121  	_, err := io.ReadFull(r.conn, buf)
   122  	return buf, err
   123  }
   124  
   125  func (r *NetworkReader) ReadInt32() (int32, error) {
   126  	b := make([]byte, 4)
   127  	_, err := r.conn.Read(b)
   128  	if err != nil {
   129  		return 0, err
   130  	}
   131  	return int32(binary.BigEndian.Uint32(b)), nil
   132  }