Add more tests for encoding
This commit is contained in:
18
pdu/pdu.go
18
pdu/pdu.go
@@ -1,6 +1,9 @@
|
||||
package pdu
|
||||
|
||||
import "encoding/binary"
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type (
|
||||
PDU interface {
|
||||
@@ -22,20 +25,27 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func (p *PDU_HEADER) Encode() []uint8 {
|
||||
func (p *PDU_HEADER) Encode() ([]uint8, error) {
|
||||
buf := make([]uint8, 16)
|
||||
binary.BigEndian.PutUint32(buf[0:4], p.command_length)
|
||||
binary.BigEndian.PutUint32(buf[4:8], p.command_id)
|
||||
binary.BigEndian.PutUint32(buf[8:12], p.command_status)
|
||||
binary.BigEndian.PutUint32(buf[12:16], p.sequence_number)
|
||||
return buf
|
||||
return buf, nil
|
||||
}
|
||||
func (p *PDU_HEADER) EncodeInto(buf *[]uint8) {
|
||||
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)
|
||||
return nil
|
||||
}
|
||||
func (p *PDU_HEADER) Decode(data []uint8) {
|
||||
if len(data) >= 4 {
|
||||
|
||||
Reference in New Issue
Block a user