Compare commits

..

1 Commits
v1.5 ... master

Author SHA1 Message Date
wangjian afc76d7d15 bug fix 2023-03-10 23:58:36 +08:00
6 changed files with 31 additions and 31 deletions

View File

@ -9,7 +9,7 @@ import (
// Encoder will encode object to encoding // Encoder will encode object to encoding
type encoder struct { type encoder struct {
seqID byte seqID byte
valbuf []byte valBuf []byte
isNode bool isNode bool
isArray bool isArray bool
buf *bytes.Buffer buf *bytes.Buffer
@ -21,19 +21,19 @@ type iEncoder interface {
} }
func (enc *encoder) GetValBuf() []byte { func (enc *encoder) GetValBuf() []byte {
return enc.valbuf return enc.valBuf
} }
func (enc *encoder) IsEmpty() bool { func (enc *encoder) IsEmpty() bool {
return len(enc.valbuf) == 0 return len(enc.valBuf) == 0
} }
func (enc *encoder) AddBytes(buf []byte) { func (enc *encoder) AddBytes(buf []byte) {
enc.valbuf = append(enc.valbuf, buf...) enc.valBuf = append(enc.valBuf, buf...)
} }
func (enc *encoder) addRawPacket(en iEncoder) { func (enc *encoder) addRawPacket(en iEncoder) {
enc.valbuf = append(enc.valbuf, en.Encode()...) enc.valBuf = append(enc.valBuf, en.Encode()...)
} }
// setTag write tag as seqID // setTag write tag as seqID
@ -51,11 +51,11 @@ func (enc *encoder) writeTag() {
} }
func (enc *encoder) writeLengthBuf() { func (enc *encoder) writeLengthBuf() {
vallen := len(enc.valbuf) valLen := len(enc.valBuf)
size := encoding.SizeOfPVarInt32(int32(vallen)) size := encoding.SizeOfPVarInt32(int32(valLen))
codec := encoding.VarCodec{Size: size} codec := encoding.VarCodec{Size: size}
tmp := make([]byte, size) tmp := make([]byte, size)
err := codec.EncodePVarInt32(tmp, int32(vallen)) err := codec.EncodePVarInt32(tmp, int32(valLen))
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -70,7 +70,7 @@ func (enc *encoder) Encode() []byte {
// Len // Len
enc.writeLengthBuf() enc.writeLengthBuf()
// Val // Val
enc.buf.Write(enc.valbuf) enc.buf.Write(enc.valBuf)
enc.complete = true enc.complete = true
} }
return enc.buf.Bytes() return enc.buf.Bytes()

View File

@ -60,7 +60,7 @@ func (codec *VarCodec) encodeVarFloat(buffer []byte, bits uint64, width int) err
return errors.New("nothing to encode") return errors.New("nothing to encode")
} }
const unit = 8 // bit width of encoding unit const unit = 8 // the bit width of encoding unit
var gap, mask = codec.sizeOfGap(width) var gap, mask = codec.sizeOfGap(width)
for (codec.Size & mask) > 0 { for (codec.Size & mask) > 0 {
@ -81,7 +81,7 @@ func (codec *VarCodec) decodeVarFloat(buffer []byte, bits *uint64, width int) er
return errors.New("nothing to decode") return errors.New("nothing to decode")
} }
const unit = 8 // bit width of encoding unit const unit = 8 // the bit width of encoding unit
var gap, mask = codec.sizeOfGap(width) var gap, mask = codec.sizeOfGap(width)
for (codec.Size & mask) > 0 { for (codec.Size & mask) > 0 {

2
go.mod
View File

@ -1,6 +1,6 @@
module git.hpds.cc/Component/mq_coder module git.hpds.cc/Component/mq_coder
go 1.18 go 1.19
require github.com/stretchr/testify v1.8.0 require github.com/stretchr/testify v1.8.0

View File

@ -48,7 +48,7 @@ func DecodeToNodePacket(buf []byte, pct *NodePacket) (consumedBytes int, err err
pct.buf.WriteByte(buf[pos]) pct.buf.WriteByte(buf[pos])
pos++ pos++
// `Length`: the type is `varint` // `Length`: the type is `varInt`
tmpBuf := buf[pos:] tmpBuf := buf[pos:]
var valLen int32 var valLen int32
codec := encoding.VarCodec{} codec := encoding.VarCodec{}
@ -87,7 +87,7 @@ func DecodeToNodePacket(buf []byte, pct *NodePacket) (consumedBytes int, err err
if isNode { if isNode {
pct.NodePackets[np.packet.tag.SeqId()] = *np pct.NodePackets[np.packet.tag.SeqId()] = *np
} else { } else {
pct.PrimitivePackets[byte(pp.SeqId())] = *pp pct.PrimitivePackets[pp.SeqId()] = *pp
} }
} }

View File

@ -43,7 +43,7 @@ func DecodeToPrimitivePacket(buf []byte, p *PrimitivePacket) (*DecodeState, erro
pos++ pos++
decoder.ConsumedBytes = pos decoder.ConsumedBytes = pos
// read `Varint` from buf for `Length of value` // read `Variant` from buf for `Length of value`
tmpBuf := buf[pos:] tmpBuf := buf[pos:]
var bufLen int32 var bufLen int32
codec := encoding.VarCodec{} codec := encoding.VarCodec{}

View File

@ -28,8 +28,8 @@ func NewPrimitivePacketEncoder(sid byte) *PrimitivePacketEncoder {
func (enc *PrimitivePacketEncoder) SetInt32Value(v int32) { func (enc *PrimitivePacketEncoder) SetInt32Value(v int32) {
size := encoding.SizeOfNVarInt32(v) size := encoding.SizeOfNVarInt32(v)
codec := encoding.VarCodec{Size: size} codec := encoding.VarCodec{Size: size}
enc.valbuf = make([]byte, size) enc.valBuf = make([]byte, size)
err := codec.EncodeNVarInt32(enc.valbuf, v) err := codec.EncodeNVarInt32(enc.valBuf, v)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -39,8 +39,8 @@ func (enc *PrimitivePacketEncoder) SetInt32Value(v int32) {
func (enc *PrimitivePacketEncoder) SetUInt32Value(v uint32) { func (enc *PrimitivePacketEncoder) SetUInt32Value(v uint32) {
size := encoding.SizeOfNVarUInt32(v) size := encoding.SizeOfNVarUInt32(v)
codec := encoding.VarCodec{Size: size} codec := encoding.VarCodec{Size: size}
enc.valbuf = make([]byte, size) enc.valBuf = make([]byte, size)
err := codec.EncodeNVarUInt32(enc.valbuf, v) err := codec.EncodeNVarUInt32(enc.valBuf, v)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -50,8 +50,8 @@ func (enc *PrimitivePacketEncoder) SetUInt32Value(v uint32) {
func (enc *PrimitivePacketEncoder) SetInt64Value(v int64) { func (enc *PrimitivePacketEncoder) SetInt64Value(v int64) {
size := encoding.SizeOfNVarInt64(v) size := encoding.SizeOfNVarInt64(v)
codec := encoding.VarCodec{Size: size} codec := encoding.VarCodec{Size: size}
enc.valbuf = make([]byte, size) enc.valBuf = make([]byte, size)
err := codec.EncodeNVarInt64(enc.valbuf, v) err := codec.EncodeNVarInt64(enc.valBuf, v)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -61,8 +61,8 @@ func (enc *PrimitivePacketEncoder) SetInt64Value(v int64) {
func (enc *PrimitivePacketEncoder) SetUInt64Value(v uint64) { func (enc *PrimitivePacketEncoder) SetUInt64Value(v uint64) {
size := encoding.SizeOfNVarUInt64(v) size := encoding.SizeOfNVarUInt64(v)
codec := encoding.VarCodec{Size: size} codec := encoding.VarCodec{Size: size}
enc.valbuf = make([]byte, size) enc.valBuf = make([]byte, size)
err := codec.EncodeNVarUInt64(enc.valbuf, v) err := codec.EncodeNVarUInt64(enc.valBuf, v)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -72,8 +72,8 @@ func (enc *PrimitivePacketEncoder) SetUInt64Value(v uint64) {
func (enc *PrimitivePacketEncoder) SetFloat32Value(v float32) { func (enc *PrimitivePacketEncoder) SetFloat32Value(v float32) {
var size = encoding.SizeOfVarFloat32(v) var size = encoding.SizeOfVarFloat32(v)
codec := encoding.VarCodec{Size: size} codec := encoding.VarCodec{Size: size}
enc.valbuf = make([]byte, size) enc.valBuf = make([]byte, size)
err := codec.EncodeVarFloat32(enc.valbuf, v) err := codec.EncodeVarFloat32(enc.valBuf, v)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -83,8 +83,8 @@ func (enc *PrimitivePacketEncoder) SetFloat32Value(v float32) {
func (enc *PrimitivePacketEncoder) SetFloat64Value(v float64) { func (enc *PrimitivePacketEncoder) SetFloat64Value(v float64) {
var size = encoding.SizeOfVarFloat64(v) var size = encoding.SizeOfVarFloat64(v)
codec := encoding.VarCodec{Size: size} codec := encoding.VarCodec{Size: size}
enc.valbuf = make([]byte, size) enc.valBuf = make([]byte, size)
err := codec.EncodeVarFloat64(enc.valbuf, v) err := codec.EncodeVarFloat64(enc.valBuf, v)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -94,8 +94,8 @@ func (enc *PrimitivePacketEncoder) SetFloat64Value(v float64) {
func (enc *PrimitivePacketEncoder) SetBoolValue(v bool) { func (enc *PrimitivePacketEncoder) SetBoolValue(v bool) {
var size = encoding.SizeOfPVarUInt32(uint32(1)) var size = encoding.SizeOfPVarUInt32(uint32(1))
codec := encoding.VarCodec{Size: size} codec := encoding.VarCodec{Size: size}
enc.valbuf = make([]byte, size) enc.valBuf = make([]byte, size)
err := codec.EncodePVarBool(enc.valbuf, v) err := codec.EncodePVarBool(enc.valBuf, v)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -103,10 +103,10 @@ func (enc *PrimitivePacketEncoder) SetBoolValue(v bool) {
// SetStringValue encode string // SetStringValue encode string
func (enc *PrimitivePacketEncoder) SetStringValue(v string) { func (enc *PrimitivePacketEncoder) SetStringValue(v string) {
enc.valbuf = []byte(v) enc.valBuf = []byte(v)
} }
// SetBytesValue encode []byte // SetBytesValue encode []byte
func (enc *PrimitivePacketEncoder) SetBytesValue(v []byte) { func (enc *PrimitivePacketEncoder) SetBytesValue(v []byte) {
enc.valbuf = v enc.valBuf = v
} }