Rework every uint8 to byte and rework every []byte to bytes.Buffer
I just learnt that uint8 and byte are the fucking same And that bytes.Buffer is the thing to use Idiot idiot idiot... Live and learn
This commit is contained in:
52
pdu/pdu.go
52
pdu/pdu.go
@@ -1,17 +1,16 @@
|
||||
package pdu
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type (
|
||||
PDU interface {
|
||||
EncodeInto(*[]uint8)
|
||||
Encode() []uint8
|
||||
Decode([]uint8)
|
||||
Encode(*bytes.Buffer) error
|
||||
Decode(*bytes.Buffer) error
|
||||
// Size in bytes
|
||||
Size() uint32
|
||||
Size() int
|
||||
}
|
||||
|
||||
PDU_HEADER struct {
|
||||
@@ -57,41 +56,20 @@ type (
|
||||
// What are the other 0s?
|
||||
// Don't know
|
||||
|
||||
func (p *PDU_HEADER) Encode() (*[]uint8, error) {
|
||||
buf := ByteBufferPool.Get(uint(p.Size()))
|
||||
err := p.EncodeInto(buf)
|
||||
return buf, err
|
||||
}
|
||||
func (p *PDU_HEADER) EncodeInto(buf *[]uint8) error {
|
||||
if buf == nil {
|
||||
return fmt.Errorf("cannot encode PDU_HEADER, buffer is nil")
|
||||
}
|
||||
if len(*buf) < 16 {
|
||||
return fmt.Errorf("cannot encode PDU_HEADER, buffer too small (%d, required 16)", len(*buf))
|
||||
}
|
||||
bufVal := *buf
|
||||
binary.BigEndian.PutUint32(bufVal[0:4], p.command_length)
|
||||
binary.BigEndian.PutUint32(bufVal[4:8], p.command_id)
|
||||
binary.BigEndian.PutUint32(bufVal[8:12], p.command_status)
|
||||
binary.BigEndian.PutUint32(bufVal[12:16], p.sequence_number)
|
||||
func (p *PDU_HEADER) Encode(buf *bytes.Buffer) error {
|
||||
binary.Write(buf, binary.BigEndian, p.command_length)
|
||||
binary.Write(buf, binary.BigEndian, p.command_id)
|
||||
binary.Write(buf, binary.BigEndian, p.command_status)
|
||||
binary.Write(buf, binary.BigEndian, p.sequence_number)
|
||||
return nil
|
||||
}
|
||||
func (p *PDU_HEADER) Decode(data []uint8) error {
|
||||
if len(data) >= 4 {
|
||||
p.command_length = binary.BigEndian.Uint32(data[0:4])
|
||||
}
|
||||
if len(data) >= 8 {
|
||||
p.command_id = binary.BigEndian.Uint32(data[4:8])
|
||||
}
|
||||
if len(data) >= 12 {
|
||||
p.command_status = binary.BigEndian.Uint32(data[8:12])
|
||||
|
||||
}
|
||||
if len(data) >= 16 {
|
||||
p.sequence_number = binary.BigEndian.Uint32(data[12:16])
|
||||
}
|
||||
func (p *PDU_HEADER) Decode(buf *bytes.Buffer) error {
|
||||
binary.Read(buf, binary.BigEndian, &p.command_length)
|
||||
binary.Read(buf, binary.BigEndian, &p.command_id)
|
||||
binary.Read(buf, binary.BigEndian, &p.command_status)
|
||||
binary.Read(buf, binary.BigEndian, &p.sequence_number)
|
||||
return nil
|
||||
}
|
||||
func (p *PDU_HEADER) Size() uint32 {
|
||||
func (p *PDU_HEADER) Size() int {
|
||||
return 16
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user